예제 #1
0
파일: parser_test.go 프로젝트: kego/ke
func TestScan_errors(t *testing.T) {

	cb := tests.Context("").Cmd().Sempty().Jsystem().TempGopath(true)
	defer cb.Cleanup()

	_, err := Parse(cb.Ctx(), "does-not-exist")
	require.IsError(t, err, "SBALWXUPKN") // Error from ScanForEnv
	assert.HasError(t, err, "NIKCKQAKUI") // GoGet: exit status 1: package does-not-exist

	path, _ := cb.TempPackage("a", map[string]string{
		"a.json": `{
			"type": "system:package",
			"recursive": false
		}`,
		"b.json": "foo",
	})

	cb.Path(path)

	_, err = Parse(cb.Ctx(), path)
	assert.IsError(t, err, "VFUNPHUFHD")  // Error from scanForTypes
	assert.HasError(t, err, "HCYGNBDFFA") // Error trying to unmarshal a type

	path, _ = cb.TempPackage("a", map[string]string{
		"a.json": "foo",
	})

	cb.Path(path)

	_, err = Parse(cb.Ctx(), path)
	assert.IsError(t, err, "GJRHNGGWFD")  // Error from ScanForEnv
	assert.HasError(t, err, "MTDCXBYBEJ") // Error trying to scan for packages

}
예제 #2
0
파일: package_test.go 프로젝트: kego/ke
func TestCheck(t *testing.T) {
	var full string
	var err error

	full, err = Check("/a", "/b", false)
	require.NoError(t, err)
	assert.Equal(t, "/a/b", full)

	full, err = Check("/a", "../b", false)
	require.IsError(t, err, "CNMFTLYQFG")

	full, err = Check("/a/b", "../b/c", false)
	require.NoError(t, err)
	assert.Equal(t, "/a/b/c", full)

	full, err = Check("/a/", "/b", false)
	require.NoError(t, err)
	assert.Equal(t, "/a/b", full)

	full, err = Check("/a/", "../b", false)
	require.IsError(t, err, "CNMFTLYQFG")

	full, err = Check("/a/b/", "../b/c", false)
	require.NoError(t, err)
	assert.Equal(t, "/a/b/c", full)

	full, err = Check("/a/b/", "../../a/b/c", false)
	require.NoError(t, err)
	assert.Equal(t, "/a/b/c", full)

	full, err = Check("/a/b/", "../../a/c/d", false)
	require.IsError(t, err, "CNMFTLYQFG")

	full, err = Check("/a/b/", "c/d", false)
	require.IsError(t, err, "OABDMRQSJO")

	full, err = Check("/a/b/", "c/d", true)
	require.NoError(t, err)
	assert.Equal(t, "/a/b/c/d", full)
}
예제 #3
0
func TestAlmjsiError(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	// Note: map alias types in interfaces are always marshalled to explicit
	// object notation, so this throws an error.
	Run(t, ctx, `{
			"type": "multi",
			"almjsi": {"a": "b", "c": "d"}
		}`,
		UnmarshalError(func(t *testing.T, err error) {
			require.IsError(t, err, "SVXYHJWMOC")
			require.HasError(t, err, "RXEPCCGFKV")
		}),
	)
}
예제 #4
0
파일: marshal_ext_test.go 프로젝트: kego/ke
func TestAlsError(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	// Type must be als when unpacking into als, even if it's an alias
	// of simple
	Run(t, ctx, `{
			"type": "multi",
			"als": {"type": "simple", "js": "a"}
		}`,
		UnmarshalError(func(t *testing.T, err error) {
			require.IsError(t, err, "SVXYHJWMOC")
			require.HasError(t, err, "BNHVLQPFKC")
		}),
	)
}
예제 #5
0
파일: reference_test.go 프로젝트: kego/ke
func TestReferenceRule_Enforce(t *testing.T) {

	r := ReferenceRule{Rule: &Rule{Optional: false}, PatternNot: NewString(`[`)}
	fail, messages, err := r.Enforce(envctx.Empty, NewReference("", ""))
	require.NoError(t, err)
	assert.Equal(t, "PatternNot: regex does not compile: [", messages[0])
	assert.True(t, fail)

	r = ReferenceRule{Rule: &Rule{Optional: false}, Pattern: NewString(`[`)}
	fail, messages, err = r.Enforce(envctx.Empty, NewReference("", ""))
	require.NoError(t, err)
	assert.Equal(t, "Pattern: regex does not compile: [", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, "")
	require.IsError(t, err, "BYDVGGETWW")

	r = ReferenceRule{Rule: &Rule{Optional: false}, Pattern: NewString(`^foo\d`)}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Pattern: value must exist", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewReference("", "a"))
	require.NoError(t, err)
	assert.Equal(t, "Pattern: value \"a\" must match ^foo\\d", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewReference("", "foo1"))
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	r = ReferenceRule{Rule: &Rule{Optional: false}, PatternNot: NewString(`^foo\d`)}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "PatternNot: value must exist", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewReference("", "foo1"))
	require.NoError(t, err)
	assert.Equal(t, "PatternNot: value \"foo1\" must not match ^foo\\d", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewReference("", "a"))
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

}
예제 #6
0
파일: map_test.go 프로젝트: kego/ke
func TestMapRule_Enforce(t *testing.T) {

	r := MapRule{Keys: &IntRule{Rule: &Rule{}}}
	fail, messages, err := r.Enforce(envctx.Empty, map[string]int{"foo": 1})
	require.IsError(t, err, "WDKAXPCRJB")

	r = MapRule{Keys: &StringRule{Rule: &Rule{}, MaxLength: NewInt(1)}}
	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1})
	require.NoError(t, err)
	assert.Equal(t, 1, len(messages))
	assert.True(t, fail)
	assert.Equal(t, "MaxLength: length of \"foo\" must not be greater than 1", messages[0])

	r = MapRule{MaxItems: NewInt(2)}
	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1, "bar": 2})
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1, "bar": 2, "baz": 3})
	require.NoError(t, err)
	assert.Equal(t, "MaxItems: length 3 should not be greater than 2", messages[0])
	assert.True(t, fail)

	r = MapRule{MinItems: NewInt(2)}
	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1, "bar": 2})
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1})
	require.NoError(t, err)
	assert.Equal(t, "MinItems: length 1 should not be less than 2", messages[0])
	assert.True(t, fail)

	_, _, err = r.Enforce(envctx.Empty, "a")
	assert.IsError(t, err, "NFWPLTOJLP")

	r = MapRule{}
	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1})
	require.NoError(t, err)
	assert.False(t, fail)
	assert.Equal(t, 0, len(messages))

	var cr CollectionRule = &MapRule{Items: &StringRule{Equal: NewString("a")}}
	assert.Equal(t, "a", cr.GetItemsRule().(*StringRule).Equal.Value())

}
예제 #7
0
파일: data_test.go 프로젝트: kego/ke
func TestUnpackInterface(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)

	Run(t, ctx, `{
			"type": "aljb",
			"value": true
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			assert.Equal(t, true, bool(*v.(*data.Aljb)))
		}),
	)

	Run(t, ctx, `meh`,
		UnmarshalError(func(t *testing.T, err error) {
			require.IsError(t, err, "SVXYHJWMOC")
			require.HasError(t, err, "PDTPGAYXRX")
		}),
	)

	Run(t, ctx, `{
			"type": "multi",
			"sri": "a"
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			assert.Equal(t, "a", v.(*data.Multi).Sri.GetString(ctx).String())
		}),
	)

	Run(t, ctx, `{
			"type": "multi",
			"sri": {"type": "system:string", "value": "a"}
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			assert.Equal(t, "a", v.(*data.Multi).Sri.GetString(ctx).String())
		}),
		MarshalledString(`{
			"type": "multi",
			"sri": "a"
		}`),
	)
}