Esempio n. 1
0
func TestNumberUnmarshalJSON(t *testing.T) {

	var n *Number

	err := n.Unpack(envctx.Empty, Pack(nil), false)
	require.NoError(t, err)
	assert.Nil(t, n)

	n = NewNumber(0.0)
	err = n.Unpack(envctx.Empty, Pack(1.2), false)
	require.NoError(t, err)
	assert.NotNil(t, n)
	assert.Equal(t, 1.2, n.Value())

	n = NewNumber(0.0)
	err = n.Unpack(envctx.Empty, Pack(map[string]interface{}{
		"type":  "system:number",
		"value": 1.2,
	}), false)
	require.NoError(t, err)
	assert.NotNil(t, n)
	assert.Equal(t, 1.2, n.Value())

	n = NewNumber(0.0)
	err = n.Unpack(envctx.Empty, Pack("foo"), false)
	assert.Error(t, err)

}
Esempio n. 2
0
func TestStringUnmarshalJSON(t *testing.T) {

	var s *String

	err := s.Unpack(envctx.Empty, Pack(nil), false)
	require.NoError(t, err)
	assert.Nil(t, s)

	s = NewString("")
	err = s.Unpack(envctx.Empty, Pack(`foo "bar"`), false)
	require.NoError(t, err)
	assert.NotNil(t, s)
	assert.Equal(t, `foo "bar"`, s.Value())

	s = NewString("")
	err = s.Unpack(envctx.Empty, Pack(map[string]interface{}{
		"type":  "system:string",
		"value": `foo "bar"`,
	}), false)
	require.NoError(t, err)
	assert.NotNil(t, s)
	assert.Equal(t, `foo "bar"`, s.Value())

	s = NewString("")
	err = s.Unpack(envctx.Empty, Pack(1.0), false)
	assert.Error(t, err)

}
Esempio n. 3
0
File: bool_test.go Progetto: kego/ke
func TestBoolUnmarshalJSON(t *testing.T) {

	var b *Bool

	err := b.Unpack(envctx.Empty, Pack(nil), false)
	require.NoError(t, err)
	assert.Nil(t, b)

	b = NewBool(false)
	err = b.Unpack(envctx.Empty, Pack(true), false)
	require.NoError(t, err)
	assert.NotNil(t, b)
	assert.True(t, b.Value())

	b = NewBool(false)
	err = b.Unpack(envctx.Empty, Pack(map[string]interface{}{
		"type":  "system:bool",
		"value": true,
	}), false)
	require.NoError(t, err)
	assert.NotNil(t, b)
	assert.True(t, b.Value())

	b = NewBool(true)
	err = b.Unpack(envctx.Empty, Pack(false), false)
	require.NoError(t, err)
	assert.NotNil(t, b)
	assert.False(t, b.Value())

	b = NewBool(false)
	err = b.Unpack(envctx.Empty, Pack("foo"), false)
	assert.Error(t, err)

}
Esempio n. 4
0
func TestSave(t *testing.T) {
	d, err := ioutil.TempDir("", "")
	require.NoError(t, err)
	defer os.RemoveAll(d)
	err = save(d, []byte{}, "b", false)
	require.NoError(t, err)
	if _, err := os.Stat(filepath.Join(d, "b")); err == nil {
		assert.Fail(t, "File should not exist")
	}

	// backup is on, but the file doesn't exist, so nothing to backup
	err = save(d, []byte("a"), "c", true)
	require.NoError(t, err)
	val, err := ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "a", string(val))
	if _, err := os.Stat(filepath.Join(d, "c.backup")); err == nil {
		assert.Fail(t, "Backup file should not exist")
	}

	// backup is off, so the file is overwritten
	err = save(d, []byte("b"), "c", false)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "b", string(val))
	_, err = os.Stat(filepath.Join(d, "c.backup"))
	assert.Error(t, err)

	// backup is on and the file exists, so we should backup
	err = save(d, []byte("c"), "c", true)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "c", string(val))
	back, err := ioutil.ReadFile(filepath.Join(d, "c.backup"))
	require.NoError(t, err)
	assert.Equal(t, "b", string(back))

	// backup is on and the file exists, so we should backup and update the backup
	err = save(d, []byte("d"), "c", true)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "c"))
	require.NoError(t, err)
	assert.Equal(t, "d", string(val))
	back, err = ioutil.ReadFile(filepath.Join(d, "c.backup"))
	require.NoError(t, err)
	assert.Equal(t, "c", string(back))

	// dir doesn't exist
	err = save(filepath.Join(d, "e"), []byte("f"), "g", true)
	require.NoError(t, err)
	val, err = ioutil.ReadFile(filepath.Join(d, "e", "g"))
	require.NoError(t, err)
	assert.Equal(t, "f", string(val))
}
Esempio n. 5
0
func TestInt(t *testing.T) {

	cb := tests.New()
	defer cb.Cleanup()

	_, err := runKe(cb, "a", map[string]string{
		"gallery.yaml": `
			type: system:type
			id: gallery
			fields:
				images:
					type: system:@map
					items:
						type: "@photo"
			rules:
				-
					selector: "{photo} .width"
					type: system:@int
					minimum: 800`,
		"rectangle.yaml": `
			type: system:type
			id: rectangle
			fields:
				width:
					type: system:@int
				height:
					type: system:@int`,
		"photo.yaml": `
			type: system:type
			id: photo
			fields:
				size:
					type: "@rectangle"`,
		"faces.yaml": `
			type: gallery
			id: faces
			images:
				foo:
					type: photo
					size:
						type: rectangle
						width: 500
						height: 500`,
	})
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "Minimum: value 500 must not be less than 800")

}
Esempio n. 6
0
func TestRules(t *testing.T) {

	cb := tests.New()
	defer cb.Cleanup()

	_, err := runKe(cb, "a", map[string]string{
		"gallery.yaml": `
			type: system:type
			id: gallery
			fields:
				str:
					type: system:@string
					rules:
						-
							selector: ":root"
							type: system:@string
							equal: foo`,
		"faces.yaml": `
			type: gallery
			id: faces
			str: foo`,
	})
	require.NoError(t, err)

	_, err = runKe(cb, "b", map[string]string{
		"gallery.yaml": `
			type: system:type
			id: gallery
			fields:
				str:
					type: system:@string
					rules:
						-
							selector: ":root"
							type: system:@string
							equal: foo`,
		"faces.yaml": `
			type: gallery
			id: faces
			str: bar`,
	})
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "Equal: value \"bar\" must equal 'foo'")

}
Esempio n. 7
0
func TestSelector(t *testing.T) {

	cb := tests.New()
	defer cb.Cleanup()

	_, err := runKe(cb, "a", map[string]string{
		"gallery.yaml": `
			type: system:type
			id: gallery
			fields:
				images:
					type: system:@map
					items:
						type: "@photo"
						rules:
							-
								selector: ".protocol"
								type: system:@string
								equal: https`,
		"photo.yaml": `
			type: system:type
			id: photo
			fields:
				protocol:
					type: system:@string
					default: http
					optional: true`,
		"faces.yaml": `
			type: gallery
			id: faces
			images:
				foo:
					type: photo
					protocol: http`,
	})
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "Equal: value \"http\" must equal 'https'")

}