Beispiel #1
0
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

}
Beispiel #2
0
func TestWrapRule(t *testing.T) {

	type nonObjectStruct struct {
		*Rule
	}
	type ruleStruct struct {
		*Object
		*Rule
	}
	parentType := &Type{
		Object: &Object{Id: NewReference("a.b/c", "a"), Type: NewReference("kego.io/system", "type")},
	}
	ruleType := &Type{
		Object: &Object{Id: NewReference("a.b/c", "@a"), Type: NewReference("kego.io/system", "type")},
	}

	ctx := tests.Context("a.b/c").Stype("a", parentType).Stype("@a", ruleType).Ctx()

	r := &ruleStruct{
		Object: &Object{Type: NewReference("a.b/c", "@a")},
	}
	w := WrapRule(ctx, r)
	assert.Equal(t, "a", w.Parent.Id.Name)

}
Beispiel #3
0
func TestNode_Unpack5(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"sri": {"type":"system:string", "value": "b"},
	"nri": {"type":"system:number", "value": 2},
	"bri": {"type":"system:bool", "value": true},
	"aljbi": {"type":"aljb", "value": true},
	"aljni": {"type":"aljn", "value": 3.0},
	"aljsi": {"type":"aljs", "value": "aljs2"}
}`

	n := node.NewNode()
	err := n.Unpack(cb.Ctx(), system.MustPackString(s), false)
	require.NoError(t, err)
	assert.Equal(t, "b", n.Value.(*data.Multi).Sri.GetString(cb.Ctx()).Value())
	assert.Equal(t, 2.0, n.Value.(*data.Multi).Nri.GetNumber(cb.Ctx()).Value())
	assert.Equal(t, true, n.Value.(*data.Multi).Bri.GetBool(cb.Ctx()).Value())

	assert.Equal(t, data.Aljb(true), *n.Value.(*data.Multi).Aljbi.GetAljb(cb.Ctx()))
	assert.Equal(t, data.Aljn(3.0), *n.Value.(*data.Multi).Aljni.GetAljn(cb.Ctx()))
	assert.Equal(t, data.Aljs("aljs2"), *n.Value.(*data.Multi).Aljsi.GetAljs(cb.Ctx()))

}
Beispiel #4
0
func TestFoo(t *testing.T) {
	cb := tests.Context("kego.io/system")
	typ := new(Type)
	in := Pack(map[string]interface{}{"type": "type", "id": "a"})
	err := typ.Unpack(cb.Ctx(), in, false)
	require.NoError(t, err)
	assert.Equal(t, "a", typ.Id.Name)
}
Beispiel #5
0
func TestNewReferenceFromString(t *testing.T) {
	cb := tests.Context("a.b/c")
	r, err := NewReferenceFromString(cb.Ctx(), "d")
	require.NoError(t, err)
	assert.Equal(t, NewReference("a.b/c", "d"), r)

	r, err = NewReferenceFromString(cb.Ctx(), "e:f")
	assert.IsError(t, err, "VXRGOQHWNB")

}
Beispiel #6
0
func TestObjectGetType(t *testing.T) {
	ty := &Type{Object: &Object{Id: NewReference("a.b/c", "foo")}}

	o := &Object{Type: NewReference("a.b/c", "t")}

	gt, ok := o.Type.GetType(tests.Context("a.b/c").Stype("t", ty).Ctx())
	assert.True(t, ok)
	assert.Equal(t, "a.b/c:foo", gt.Id.Value())

}
Beispiel #7
0
func TestGetInfo(t *testing.T) {
	cb := tests.Context("a.b/c").Wg().Sempty().Jsystem().TempGopath(false)
	defer cb.Cleanup()
	_, dirA := cb.TempPackage("a", map[string]string{
		"generated.go": "",
	})
	info, found, err := getInfo(cb.Ctx(), dirA)
	require.NoError(t, err)
	assert.Nil(t, info)
	assert.False(t, found)

}
Beispiel #8
0
func getUtilParser(t *testing.T) *Parser {
	cb := tests.Context("a.b/c").Ssystem(parser.Parse)
	data := `{
		"type": "system:type",
		"id": "foo"
	}`
	n, err := node.Unmarshal(cb.Ctx(), []byte(data))
	require.NoError(t, err)
	p, err := CreateParser(cb.Ctx(), n)
	require.NoError(t, err)
	return p
}
Beispiel #9
0
func Test(t *testing.T) {
	cb := tests.Context("kego.io/tests/data/alias").Jauto().Sauto(parser.Parse)
	s := `{
  "type": "main",
  "id": "data",
  "a": {"a": {"type":"simple", "js": "a0"}, "b": {"type":"simple", "js": "b0"}}
}`
	var m Main
	err := system.Unmarshal(cb.Ctx(), []byte(s), &m)
	require.NoError(t, err)
	require.Equal(t, "a0", m.A["a"].Js)
	require.Equal(t, "b0", m.A["b"].Js)

}
Beispiel #10
0
func Empty(t *testing.T) (*tests.ContextBuilder, *node.Node) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"m": { "type": "multi" },
	"am": [ { "type": "multi" }, { "type": "multi" } ],
	"mm": { "a": { "type": "multi" }, "b": { "type": "multi" } }
}`
	n := node.NewNode()
	require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
	return cb, n
}
Beispiel #11
0
func TestNode_Unpack3(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"a": "b"
}`

	n := node.NewNode()
	err := n.Unpack(cb.Ctx(), system.MustPackString(s), false)
	assert.HasError(t, err, "SRANLETJRS")

}
Beispiel #12
0
func TestNode_Print(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "simple",
	"js": "a"
}`

	n, err := node.Unmarshal(cb.Ctx(), []byte(s))
	require.NoError(t, err)
	require.Equal(t, `{"js":"a","type":"simple"}`, n.Print(cb.Ctx()))

}
Beispiel #13
0
func TestGenerateAll(t *testing.T) {

	cb := tests.Context("a.b/c").Wg().Sempty().Jsystem().TempGopath(true)
	defer cb.Cleanup()

	pathA, dirA := cb.TempPackage("a", map[string]string{
		"a.yml": `
id: a
type: system:type
fields:
	a:
		type: system:@string`,
	})

	err := GenerateAll(cb.Ctx(), pathA, map[string]bool{})
	assert.IsError(t, err, "XMVXECGDOX")

	cb.Path(pathA).Sauto(parser.Parse)

	done := map[string]bool{pathA: true}
	err = GenerateAll(cb.Ctx(), pathA, done)
	require.NoError(t, err)
	assert.Equal(t, 1, len(done))

	err = GenerateAll(cb.Ctx(), pathA, map[string]bool{})
	require.NoError(t, err)

	b, err := ioutil.ReadFile(filepath.Join(dirA, "generated.go"))
	require.NoError(t, err)
	assert.Contains(t, string(b), "type A struct")

	pathB, _ := cb.TempPackage("b", map[string]string{
		"pkg.yml": `
type: system:package
aliases: {"a": "` + pathA + `"}`,
		"b.yml": `
id: b
type: system:type
fields:
	a:
		type: system:@string`,
	})

	cb.Path(pathB).Sauto(parser.Parse)
	err = GenerateAll(cb.Ctx(), pathB, map[string]bool{})
	require.NoError(t, err)

}
Beispiel #14
0
func TestRuleWrapper_Kind(t *testing.T) {
	cb := tests.Context("a.b/c").Jempty()
	at := &Type{
		Object: &Object{Id: NewReference("a.b/c", "foo")},
		Native: NewString("object"),
	}
	ar := &fooRuleStruct{Rule: &Rule{}}
	aw := RuleWrapper{
		Ctx:       cb.Ctx(),
		Interface: ar,
		Struct:    ar.Rule,
		Parent:    at,
	}
	kind, alias := aw.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, KindStruct, kind)

	aw.Struct.Interface = true
	kind, alias = aw.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, KindInterface, kind)

	cr := &MapRule{Rule: &Rule{}, Items: &StringRule{Rule: &Rule{}}}
	aw.Interface = cr
	aw.Struct = cr.Rule
	aw.Parent.CustomKind = NewString(string(KindMap))
	kind, alias = aw.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, KindMap, kind)

	arr := &ArrayRule{Rule: &Rule{}, Items: &StringRule{Rule: &Rule{}}}
	aw.Interface = arr
	aw.Struct = arr.Rule
	aw.Parent.CustomKind = NewString(string(KindArray))
	kind, alias = aw.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, KindArray, kind)

	// DummyRule always implements CollectionRule, but we don't want to return
	// KindMap/KindArray unless GetItemsRule returns something.
	dr := &DummyRule{Rule: &Rule{}}
	aw.Interface = dr
	aw.Struct = dr.Rule
	aw.Parent.CustomKind = nil
	kind, alias = aw.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, KindStruct, kind)
}
Beispiel #15
0
func TestUnmarshal(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "simple",
	"js": "a"
}`

	n, err := node.Unmarshal(cb.Ctx(), []byte(s))
	require.NoError(t, err)
	m, ok := n.Value.(*data.Simple)
	require.True(t, ok)
	assert.Equal(t, "a", m.Js)

}
Beispiel #16
0
func TestNode_Unpack(t *testing.T) {

	cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

	s := `{
	"type": "multi",
	"js": "a"
}`

	n := node.NewNode()
	require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
	m, ok := n.Value.(*data.Multi)
	require.True(t, ok)
	assert.Equal(t, "a", m.Js)

}
Beispiel #17
0
func TestReferenceValue(t *testing.T) {

	r := NewReference("a.b/c", "d")
	assert.Equal(t, "a.b/c:d", r.Value())
	assert.Equal(t, "a.b/c:d", r.String())
	assert.Equal(t, "a.b/c:d", r.NativeString())

	r = NewReference("", "")
	assert.Equal(t, "", r.Value())
	assert.Equal(t, "", r.String())
	assert.Equal(t, "", r.NativeString())

	v, err := r.ValueContext(context.Background())
	require.NoError(t, err)
	assert.Equal(t, "", v)

	cb := tests.Context("a.b/c")
	v, err = r.ValueContext(cb.Ctx())
	require.NoError(t, err)
	assert.Equal(t, "", v)

	r = NewReference("a.b/c", "d")
	v, err = r.ValueContext(cb.Ctx())
	require.NoError(t, err)
	assert.Equal(t, "d", v)

	r = NewReference("kego.io/json", "a")
	v, err = r.ValueContext(cb.Ctx())
	require.NoError(t, err)
	assert.Equal(t, "json:a", v)

	r = NewReference("kego.io/system", "a")
	v, err = r.ValueContext(cb.Ctx())
	require.NoError(t, err)
	assert.Equal(t, "system:a", v)

	cb.Alias("g", "d.e/f")
	r = NewReference("d.e/f", "h")
	v, err = r.ValueContext(cb.Ctx())
	require.NoError(t, err)
	assert.Equal(t, "g:h", v)

	r = NewReference("i.j/k", "l")
	v, err = r.ValueContext(cb.Ctx())
	assert.IsError(t, err, "WGCDQQCFAD")

}
Beispiel #18
0
func TestNode_extractType(t *testing.T) {
	r := &system.RuleWrapper{
		Struct: &system.Rule{Interface: true},
		Parent: &system.Type{Interface: true},
	}
	_, err := extractType(context.Background(), system.Pack(nil), r)
	assert.IsError(t, err, "TDXTPGVFAK")

	ty, err := extractType(context.Background(), nil, nil)
	require.NoError(t, err)
	assert.Nil(t, ty)

	r = &system.RuleWrapper{
		Parent: &system.Type{Interface: true},
	}
	_, err = extractType(context.Background(), system.Pack(""), r)
	assert.IsError(t, err, "DLSQRFLINL")

	r = &system.RuleWrapper{
		Struct: &system.Rule{Interface: true},
	}
	ty, err = extractType(context.Background(), nil, r)
	require.NoError(t, err)
	assert.Nil(t, ty)

	r = &system.RuleWrapper{
		Struct: &system.Rule{Interface: true},
	}
	_, err = extractType(context.Background(), system.MustPackString(`[""]`), r)
	assert.IsError(t, err, "SNYLGBJYTM")

	_, err = extractType(context.Background(), system.MustPackString(`{}`), nil)
	assert.IsError(t, err, "HBJVDKAKBJ")

	r = &system.RuleWrapper{
		Parent: &system.Type{Interface: true},
	}
	_, err = extractType(context.Background(), system.MustPackString(`{}`), r)
	assert.IsError(t, err, "HBJVDKAKBJ")

	cb := tests.Context("a.b/c").Sempty()
	_, err = extractType(cb.Ctx(), system.MustPackString(`{"type": "a"}`), nil)
	assert.IsError(t, err, "IJFMJJWVCA")

}
Beispiel #19
0
func TestRuleWrapper_ZeroValue(t *testing.T) {
	cb := tests.Context("kego.io/system").Jauto().Sauto(parser.Parse)
	r := system.WrapRule(cb.Ctx(), &system.MapRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@map")},
		Rule:   &system.Rule{},
		Items: &system.StringRule{
			Object: &system.Object{Type: system.NewReference("kego.io/system", "@string")},
			Rule:   &system.Rule{},
		},
	})

	v, err := r.ZeroValue(true)
	require.NoError(t, err)
	assert.IsType(t, map[string]*system.String{}, v.Interface())
	assert.Nil(t, v.Interface())

	v, err = r.ZeroValue(false)
	require.NoError(t, err)
	assert.IsType(t, map[string]*system.String{}, v.Interface())
	assert.NotNil(t, v.Interface())
	vv := v.Interface().(map[string]*system.String)
	vv["a"] = system.NewString("")

	r = system.WrapRule(cb.Ctx(), &system.MapRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@array")},
		Rule:   &system.Rule{},
		Items: &system.StringRule{
			Object: &system.Object{Type: system.NewReference("kego.io/system", "@string")},
			Rule:   &system.Rule{},
		},
	})

	v, err = r.ZeroValue(true)
	require.NoError(t, err)
	assert.IsType(t, []*system.String{}, v.Interface())
	assert.Nil(t, v.Interface())

	v, err = r.ZeroValue(false)
	require.NoError(t, err)
	assert.IsType(t, []*system.String{}, v.Interface())
	assert.NotNil(t, v.Interface())
	va := v.Interface().([]*system.String)
	va = append(va, system.NewString(""))
}
Beispiel #20
0
func TestNode_extractFields(t *testing.T) {
	cb := tests.Context("a.b/c").Sempty()

	ty := &system.Type{}
	f := map[string]*system.Field{}
	err := extractFields(cb.Ctx(), f, ty)
	assert.IsError(t, err, "YRFWOTIGFT")

	cb.Ssystem(parser.Parse)

	ty = &system.Type{Embed: []*system.Reference{system.NewReference("a.b/c", "d")}}
	err = extractFields(cb.Ctx(), f, ty)
	assert.IsError(t, err, "SLIRILCARQ")

	f = map[string]*system.Field{"a": nil}
	ty = &system.Type{Fields: map[string]system.RuleInterface{"a": nil}}
	err = extractFields(cb.Ctx(), f, ty)
	assert.IsError(t, err, "BARXPFXQNB")
}
Beispiel #21
0
func TestReferenceGetType(t *testing.T) {

	ty := &Type{
		Object: &Object{Id: NewReference("a.b/c", "d"), Type: NewReference("kego.io/system", "type")},
	}

	ctx := tests.Context("a.b/c").Stype("d", ty).Ctx()

	r := NewReference("a.b/c", "d")
	typ, ok := r.GetType(ctx)
	assert.True(t, ok)
	assert.Equal(t, "a.b/c:d", typ.Id.Value())

	r = NewReference("a.b/c", "e")
	_, ok = r.GetType(ctx)
	assert.False(t, ok)

	r = &Reference{}
	_, ok = r.GetType(ctx)
	assert.False(t, ok)
}
Beispiel #22
0
func TestEditor(t *testing.T) {
	cb := tests.Context("a.b/c").Alias("g", "d.e/f")
	b, err := Editor(cb.Ctx(), cb.Env())
	require.NoError(t, err)
	assert.Contains(t, string(b), `package main

import (
	_ "a.b/c"
	_ "d.e/f"
	"fmt"
	"kego.io/editor/client"
	_ "kego.io/system"
)

func main() {
	if err := client.Start(); err != nil {
		fmt.Println(err.Error())
	}
}
`)
}
Beispiel #23
0
func TestRuleWrapperItemsRule1(t *testing.T) {
	cb := tests.Context("a.b/c").Jempty()
	fooType := &Type{Object: &Object{Id: NewReference("a.b/c", "foo")}}
	fooRule := &fooRuleStruct{Rule: &Rule{}}
	foo := RuleWrapper{
		Ctx:       cb.Ctx(),
		Interface: fooRule,
		Struct:    fooRule.Rule,
		Parent:    fooType,
	}

	// String isn't a collection
	fooType.Native = NewString("string")
	_, err := foo.ItemsRule()
	assert.IsError(t, err, "VPAGXSTQHM")

	// FooRule isn't a collection rule
	fooType.Native = NewString("map")
	_, err = foo.ItemsRule()
	assert.IsError(t, err, "TNRVQVJIFH")

	barType := &Type{Object: &Object{Id: NewReference("a.b/c", "bar")}, Native: NewString("map")}
	barRule := &barRuleStruct{
		Object: &Object{},
		Rule:   &Rule{},
	}
	bar := RuleWrapper{
		Ctx:       cb.Ctx(),
		Interface: barRule,
		Struct:    barRule.Rule,
		Parent:    barType,
	}
	// barRule has nil Items
	_, err = bar.ItemsRule()
	assert.IsError(t, err, "SUJLYBXPYS")

}
Beispiel #24
0
func TestEditorErr(t *testing.T) {
	cb := tests.Context("a.b/c").Alias("a", "\"")
	_, err := Editor(cb.Ctx(), cb.Env())
	assert.IsError(t, err, "CBTOLUQOGL")
	assert.HasError(t, err, "CRBYOUOHPG")
}
Beispiel #25
0
func TestGenerateSourceNoTypes(t *testing.T) {
	cb := tests.Context("a.b/c").Spkg("a.b/c")
	_, err := generate.Structs(cb.Ctx(), cb.Env())
	require.NoError(t, err)
}
Beispiel #26
0
func TestGenerateSourceErr1(t *testing.T) {
	cb := tests.Context("a.b/c").Sempty()
	_, err := generate.Structs(cb.Ctx(), cb.Env())
	assert.IsError(t, err, "DQVQWTKRSK")
}
Beispiel #27
0
func TestRuleWrapperHoldsDisplayType(t *testing.T) {
	cb := tests.Context("a.b/c").Jempty()
	fooType := &Type{
		Object: &Object{Id: NewReference("a.b/c", "foo")},
		Native: NewString("object"),
	}
	fooRule := &fooRuleStruct{Rule: &Rule{}}
	foo := RuleWrapper{
		Ctx:       cb.Ctx(),
		Interface: fooRule,
		Struct:    fooRule.Rule,
		Parent:    fooType,
	}

	fooType.Id.Package = "d.e/f"
	_, err := foo.DisplayType()
	assert.IsError(t, err, "OPIFCOHGWI")

	fooType.Id.Package = "a.b/c"
	val, err := foo.DisplayType()
	require.NoError(t, err)
	assert.Equal(t, "foo", val)

	fooRule.Interface = true
	val, err = foo.DisplayType()
	require.NoError(t, err)
	assert.Equal(t, "foo*", val)

	fooRule.Interface = false
	fooType.Interface = true
	val, err = foo.DisplayType()
	require.NoError(t, err)
	assert.Equal(t, "foo*", val)

	bazType := &Type{
		Object: &Object{Id: NewReference("a.b/c", "baz")},
		Native: NewString("object"),
	}
	cb.Stype("baz", bazType)
	barType := &Type{
		Object: &Object{Id: NewReference("a.b/c", "foo")},
		Native: NewString("array"),
	}
	barRule := &barRuleStruct{
		Rule:  &Rule{},
		Items: &IntRule{Rule: &Rule{}, Object: &Object{Type: NewReference("a.b/c", "@baz")}},
	}
	bar := RuleWrapper{
		Ctx:       cb.Ctx(),
		Interface: barRule,
		Struct:    barRule.Rule,
		Parent:    barType,
	}
	val, err = bar.DisplayType()
	require.NoError(t, err)
	assert.Equal(t, "[]baz", val)

	barType.Native = NewString("map")
	val, err = bar.DisplayType()
	require.NoError(t, err)
	assert.Equal(t, "map[]baz", val)

}
Beispiel #28
0
func Setup(t *testing.T) (*tests.ContextBuilder, *node.Node) {

	simple := false
	if !simple {
		cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)
		m := `"type": "multi",
			"js": "js1",
			"ss": "ss1",
			"sr": "sr1",
			"jn": 1.1,
			"sn": 1.2,
			"jb": true,
			"sb": false,
			"alajs": ["alajs0", "alajs1", "alajs2"],
			"alas": [{"type": "simple", "js": "alas0"}, {"type": "simple", "js": "alas1"}],
			"alass": ["alass0", "alass1", "alass2"],
			"aljb": true,
			"aljn": 2.0,
			"aljs": "aljs",
			"almjs": {"a": "almjsa", "b": "almjsb"},
			"alms": {"a": {"type": "simple", "js": "almsa"}, "b": {"type": "simple", "js": "almsb"}},
			"almss": {"a": "almssa", "b": "almssb"},
			"alss": "alss",
			"als": {"type": "als", "js": "als"},
			"i": { "type": "facea", "a": "ia" },
			"ajs": [ "ajs0", "ajs1", "ajs2", "ajs3" ],
			"ass": [ "ass0", "ass1", "ass2", "ass3" ],
			"ajn": [ 2.1, 2.2, 2.3, 2.4 ],
			"asn": [ 3.1, 3.2, 3.3, 3.4 ],
			"ajb": [ true, false, false, false ],
			"asb": [ false, true, false, false ],
			"mjs": { "a": "mjsa", "b": "mjsb" },
			"mss": { "a": "mssa", "b": "mssb" },
			"mjn": { "a": 4.1, "b": 4.2 },
			"msn": { "a": 5.1, "b": 5.2 },
			"mjb": { "a": true, "b": false },
			"msb": { "a": false, "b": true },
			"anri": [ "anri0", { "type": "facea", "a": "anri1" }, { "type": "multi", "ss": "anri2" } ],
			"mnri": { "a": "mnria", "b": { "type": "facea", "a": "mnrib" }, "c": { "type": "multi", "ss": "mnric" } }
						`
		mm := m + `,
			"m": { "type": "multi" },
			"am": [ { "type": "multi", "js": "amjs0" }, { "type": "multi", "js": "amjs1" } ],
			"mm": { "a": { "type": "multi", "js": "mmjsa" }, "b": { "type": "multi", "js": "mmjsb" } }`

		s := `{
				` + m + `,
				"m": {` + mm + `},
				"am": [ {` + mm + `}, {` + mm + `} ],
				"mm": { "a": {` + mm + `}, "b": {` + mm + `} }
			}`
		n := node.NewNode()
		require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
		return cb, n

	} else {
		cb := tests.Context("kego.io/tests/data").Jauto().Sauto(parser.Parse)

		m := `"type": "multi",
		"alms": {"a": {"type": "simple", "js": "almsa"}, "b": {"type": "simple", "js": "almsb"}}
			`
		mm := m + `,
			"m": { "type": "multi" },
			"am": [ { "type": "multi", "js": "amjs0" }, { "type": "multi", "js": "amjs1" } ],
			"mm": { "a": { "type": "multi", "js": "mmjsa" }, "b": { "type": "multi", "js": "mmjsb" } }`

		s := `{
		` + m + `,
		"m": {` + mm + `},
		"am": [ {` + mm + `}, {` + mm + `} ],
		"mm": { "a": {` + mm + `}, "b": {` + mm + `} }
	}`
		n := node.NewNode()
		require.NoError(t, n.Unpack(cb.Ctx(), system.MustPackString(s), false))
		return cb, n
	}

}
Beispiel #29
0
func TestReferenceUnmarshal(t *testing.T) {

	reset := func() *Reference {
		// Let's pre-load with some values so we check that when we
		// load a null value, we clear all the fields
		r := NewReference("a.b/c", "d")
		return r
	}

	r := reset()
	err := r.Unpack(envctx.Empty, Pack(nil), false)
	assert.IsError(t, err, "MOQVSKJXRB")

	r = reset()
	err = r.Unpack(envctx.Empty, Pack(1.0), false)
	assert.IsError(t, err, "RFLQSBPMYM")

	r = reset()
	err = r.Unpack(envctx.Empty, Pack("a.b/c:d"), false)
	assert.IsError(t, err, "MSXBLEIGVJ")
	assert.HasError(t, err, "KJSOXDESFD")
	p, ok := kerr.Source(err).(UnknownPackageError)
	assert.True(t, ok)
	assert.Equal(t, "a.b/c", p.UnknownPackage)

	ctx := tests.Context("").Alias("c", "a.b/c").Ctx()

	r = reset()
	err = r.Unpack(ctx, Pack("a.b/c:d"), false)
	require.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "a.b/c", r.Package)
	assert.Equal(t, "d", r.Name)
	assert.Equal(t, "a.b/c:d", r.Value())

	r = reset()
	err = r.Unpack(ctx, Pack(map[string]interface{}{
		"type":  "system:reference",
		"value": "a.b/c:d",
	}), false)
	require.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "a.b/c", r.Package)
	assert.Equal(t, "d", r.Name)
	assert.Equal(t, "a.b/c:d", r.Value())

	r = reset()
	err = r.Unpack(ctx, Pack("a.b/c:@d"), false)
	require.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "a.b/c", r.Package)
	assert.Equal(t, "@d", r.Name)
	assert.Equal(t, "a.b/c:@d", r.Value())

	r = reset()
	err = r.Unpack(envctx.Empty, Pack("a:b"), false)
	assert.IsError(t, err, "MSXBLEIGVJ")
	assert.HasError(t, err, "DKKFLKDKYI")
	p, ok = kerr.Source(err).(UnknownPackageError)
	assert.True(t, ok)
	assert.Equal(t, "a", p.UnknownPackage)

	r = reset()
	err = r.UnmarshalInterface(envctx.Empty, 1)
	require.NoError(t, err)
	assert.Equal(t, *NewReference("", ""), *r)

	r = reset()
	err = r.UnmarshalInterface(envctx.Empty, "")
	require.NoError(t, err)
	assert.Equal(t, *NewReference("", ""), *r)

	r = reset()
	err = r.UnmarshalInterface(envctx.Empty, "a.b/c:d")
	assert.IsError(t, err, "ETLPLMMWCC")

	r = reset()
	err = r.UnmarshalInterface(tests.Context("a.b/c").Ctx(), "a.b/c:d")
	require.NoError(t, err)
	assert.Equal(t, *NewReference("a.b/c", "d"), *r)
}
Beispiel #30
0
func runTestsInDirectory(t *testing.T, baseDirectory string) {
	var testDocuments = make(map[string]*node.Node)
	var testSelectors = make(map[string]string)
	var testOutput = make(map[string][]string)

	files, err := ioutil.ReadDir(baseDirectory)
	if err != nil {
		t.Error("Error encountered while loading conformance tests ", err)
	}

	cb := tests.Context("kego.io/process/validate/selectors/tests").Jauto().Sauto(parser.Parse)

	for _, fileInfo := range files {
		name := fileInfo.Name()
		if strings.HasSuffix(name, ".json") {
			json_document, err := ioutil.ReadFile(baseDirectory + name)
			if err != nil {
				t.Error("Error encountered while reading ", name, ": ", err)
				continue
			}
			n, err := node.Unmarshal(cb.Ctx(), json_document)
			require.NoError(t, err, name)
			require.NotNil(t, n)

			testDocuments[name[0:len(name)-len(".json")]] = n
		} else if strings.HasSuffix(name, ".output") {
			output_document, err := ioutil.ReadFile(baseDirectory + name)
			if err != nil {
				t.Error("Error encountered while reading ", name, ": ", err)
				continue
			}
			if strings.HasPrefix(string(output_document), "Error") {
				// We won't be handling errors in the same way.
				continue
			}
			var actualOutput []string
			var stringTemporary string
			for _, str := range strings.Split(string(output_document), "\n") {
				stringTemporary = stringTemporary + str
				// Try to parse -- if it works, we have the whole object
				if strings.Index(stringTemporary, "{") == 0 {
					if strings.Count(stringTemporary, "{") != strings.Count(stringTemporary, "}") {
						continue
					}
					actualOutput = append(actualOutput, stringTemporary)
					stringTemporary = ""
				} else if strings.Index(stringTemporary, "[") == 0 {
					if strings.Count(stringTemporary, "[") != strings.Count(stringTemporary, "]") {
						continue
					}
					actualOutput = append(actualOutput, stringTemporary)
					stringTemporary = ""
				} else if len(stringTemporary) > 0 {
					actualOutput = append(actualOutput, stringTemporary)
					stringTemporary = ""
				}
			}
			testOutput[name[0:len(name)-len(".output")]] = actualOutput
		} else if strings.HasSuffix(name, ".selector") {
			selector_document, err := ioutil.ReadFile(baseDirectory + name)
			if err != nil {
				t.Error("Error encountered while reading ", name, ": ", err)
				continue
			}
			testSelectors[name[0:len(name)-len(".selector")]] = string(selector_document)
		}
	}

	for testName := range testOutput {
		single := ""
		if single != "" && testName != single {
			continue
		}
		var passed bool = true
		//t.Log("Running test ", testName)
		parser, err := getTestParser(cb.Ctx(), testDocuments, testName)
		if err != nil {
			t.Error("Test ", testName, "failed: ", err)
			passed = false
		}
		selectorString := testSelectors[testName]
		expectedOutput := testOutput[testName]

		results, err := parser.GetNodes(selectorString)
		if err != nil {
			t.Error("Test ", testName, "failed: ", err)
			passed = false
		}

		//fmt.Println("Output")
		//for i, n := range results {
		//	fmt.Println(i, n.Type.Id.Value(), n.ValueString, n.Key, n.Null)
		//}

		if len(results) != len(expectedOutput) {
			t.Error("Test ", testName, " failed due to number of results being mismatched; ", len(results), " != ", len(expectedOutput), ": [Actual] ", results, " != [Expected] ", expectedOutput)
			passed = false
		} else {
			var expected = make([]interface{}, 0, 10)
			var actual = make([]*node.Node, 0, 10)
			//matchType := "string"

			for idx, result := range results {
				expectedEncoded := expectedOutput[idx]

				var expectedJson interface{}
				err := json.Unmarshal([]byte(expectedEncoded), &expectedJson)
				if err != nil {
					t.Error(
						"Test ", testName, " failed due to a JSON decoding error while decoding expectation: ", err,
					)
					passed = false
				}
				expected = append(expected, expectedJson)
				actual = append(actual, result)
			}

			// Iterate over each of the actual elements; if:
			// * We find a match, remove the match from the expected.
			// * We do not find a match, report an error
			for _, actualElement := range actual {
				var matched bool = false
				for expectedIdx, expectedElement := range expected {
					// TODO: Should we ignore the error here? I guess so...
					matched, _ = comparison(actualElement, expectedElement)
					if matched {
						expected = append(
							expected[:expectedIdx],
							expected[expectedIdx+1:]...,
						)
						break
					}
				}
				if !matched {
					t.Error("Actual element", actualElement, "not found in expected.")
					passed = false
					break
				}
			}
			if len(expected) > 0 {
				for _, value := range expected {
					t.Error("Expected element", value, "not found in actual.")
				}
				passed = false
			}
		}
		if passed {
			//t.Log("Test ", testName, " PASSED")
		} else {
			t.Error("Test ", testName, " FAILED")
		}
	}
}