Beispiel #1
0
func TestNode_SetValueZero2(t *testing.T) {
	cb, n := data.Empty(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		sstring, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/system", "string")
		assert.True(t, ok)
		snumber, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/system", "number")
		assert.True(t, ok)

		require.NoError(t, n.Map["ss"].SetValueZero(cb.Ctx(), true, sstring))
		assert.Nil(t, m.Ss)

		require.NoError(t, n.Map["sn"].SetValueZero(cb.Ctx(), false, snumber))
		assert.NotNil(t, m.Sn)

		require.NoError(t, n.Map["mnri"].SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Mnri)

		require.NoError(t, n.Map["mi"].SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Mi)
		assert.Equal(t, 0, len(m.Mi))

		require.NoError(t, n.Map["anri"].SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Anri)

		require.NoError(t, n.Map["ai"].SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Ai)
		assert.Equal(t, 0, len(m.Ai))

	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Beispiel #2
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)

}
Beispiel #3
0
func TestNumberInterfaces(t *testing.T) {
	var nn NativeNumber = NewNumber(99.9)
	assert.Equal(t, 99.9, nn.NativeNumber())

	var dr DefaultRule = &NumberRule{Default: NewNumber(22.2)}
	assert.Equal(t, 22.2, dr.GetDefault().(*Number).Value())
}
Beispiel #4
0
func TestNode_DeleteObjectChild(t *testing.T) {
	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "js"))
		assert.True(t, n.Map["js"].Missing)
		assert.Equal(t, "", m.Js)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "ss"))
		assert.Nil(t, m.Ss)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "sr"))
		assert.Nil(t, m.Sr)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "jn"))
		assert.Equal(t, 0.0, m.Jn)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "sn"))
		assert.Nil(t, m.Sn)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "jb"))
		assert.Equal(t, false, m.Jb)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "sb"))
		assert.Nil(t, m.Sb)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "i"))
		assert.Nil(t, m.I)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "ajs"))
		assert.Nil(t, m.Ajs)
		require.NoError(t, n.DeleteObjectChild(cb.Ctx(), "mjs"))
		assert.Nil(t, m.Mjs)

	}
	data.Run(t, n, n.Value.(*data.Multi), test)
}
Beispiel #5
0
func TestNode_InitialiseMapChild(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		c := node.NewNode()
		require.NoError(t, c.InitialiseMapItem(cb.Ctx(), n.Map["mjs"], "c"))
		require.NoError(t, c.AddToMap(cb.Ctx(), n.Map["mjs"], "c", true))
		assert.Equal(t, 3, len(n.Map["mjs"].Map))
		assert.True(t, n.Map["mjs"].Map["c"].Null)
		assert.False(t, n.Map["mjs"].Map["c"].Missing)
		assert.Equal(t, c, n.Map["mjs"].Map["c"])
		assert.Equal(t, "", m.Mjs["c"])

		c1 := node.NewNode()
		require.NoError(t, c1.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "c"))
		require.NoError(t, c1.AddToMap(cb.Ctx(), n.Map["mss"], "c", true))
		assert.Equal(t, 3, len(n.Map["mss"].Map))
		assert.Nil(t, m.Mss["c"])

		c2 := node.NewNode()
		require.NoError(t, c2.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "c"))
		require.NoError(t, c2.AddToMap(cb.Ctx(), n.Map["mm"], "c", true))
		assert.Equal(t, 3, len(m.Mm))
		assert.Nil(t, m.Mm["c"])
	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Beispiel #6
0
func TestMalms(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	Run(t, ctx, `{
			"type": "multi",
			"malms": {
				"a": {
					"b": {"type":"simple", "js": "c"},
					"d": {"type":"simple", "js": "e"}
				},
				"f": {
					"g": {"type":"simple", "js": "h"},
					"i": {"type":"simple", "js": "j"}
				}
			}
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			require.Len(t, v.(*data.Multi).Malms, 2)
			require.Len(t, v.(*data.Multi).Malms["a"], 2)
			require.Len(t, v.(*data.Multi).Malms["f"], 2)
			assert.Equal(t, "c", v.(*data.Multi).Malms["a"]["b"].Js)
			assert.Equal(t, "e", v.(*data.Multi).Malms["a"]["d"].Js)
			assert.Equal(t, "h", v.(*data.Multi).Malms["f"]["g"].Js)
			assert.Equal(t, "j", v.(*data.Multi).Malms["f"]["i"].Js)
		}),
	)
}
Beispiel #7
0
func TestReference_Label(t *testing.T) {
	var r *Reference
	assert.Equal(t, "", r.Label(nil))

	r = NewReference("a", "b")
	assert.Equal(t, "b", r.Label(nil))
}
Beispiel #8
0
func TestBuilder(t *testing.T) {
	g := New("a.b/c")
	b, err := g.Build()
	require.NoError(t, err)
	assert.Equal(t, "package c\n", string(b))
	g.buffer.Reset()

	g.Imports.Anonymous("e.f/g")
	b, err = g.Build()
	require.NoError(t, err)
	assert.Equal(t, "package c\n\nimport (\n\t_ \"e.f/g\"\n)\n", string(b))
	g.buffer.Reset()

	alias := g.Imports.Add("h.i/j")
	assert.Equal(t, "j", alias)

	b, err = g.Build()
	require.NoError(t, err)
	assert.Equal(t, "package c\n\nimport (\n\t_ \"e.f/g\"\n\t\"h.i/j\"\n)\n", string(b))
	g.buffer.Reset()

	g.SetPackageComment("comment")
	g.SetIntroComment("intro")
	g.Print("var ")
	g.Println("foo string")
	g.Printf("var bar int\n%s", "var baz bool")
	g.Println("")
	g.Println("func foo() {")
	g.PrintFunctionCall("k.l/m", "n", "o", "p")
	g.Println("")
	g.PrintMethodCall("a", "b", "c", "d")
	g.Println("}")
	b, err = g.Build()
	require.NoError(t, err)
	assert.Contains(t, string(b), `// comment
package c

// intro

import (
	_ "e.f/g"
	"h.i/j"
	"k.l/m"
)

var foo string
var bar int
var baz bool

func foo() {
	m.n(o, p)
	a.b(c, d)
}
`)

	g.Print("dfskjsdf")
	_, err = g.Build()
	assert.IsError(t, err, "CRBYOUOHPG") // Error formatting source

}
Beispiel #9
0
func TestGetOptions(t *testing.T) {

	getTrue := func() *bool {
		val := true
		return &val
	}
	a := "a"
	b := 2
	f := Flags{
		Edit:     getTrue(),
		Update:   getTrue(),
		Log:      getTrue(),
		Validate: getTrue(),
		Debug:    getTrue(),
		Path:     &a,
		Port:     &b,
	}
	d := f.getOptions()
	assert.True(t, d.Edit)
	assert.True(t, d.Update)
	assert.True(t, d.Log)
	assert.True(t, d.Validate)
	assert.True(t, d.Debug)
	assert.Equal(t, 2, d.Port)
	assert.Equal(t, "a", d.Path)

}
Beispiel #10
0
func TestScanDirToFiles(t *testing.T) {
	cb := tests.New().TempGopath(false)
	defer cb.Cleanup()
	_, dir := cb.TempPackage("a", map[string]string{
		"b.json": "c",
	})
	// make a dir so we hit the IsRegular block
	require.NoError(t, os.Mkdir(filepath.Join(dir, "d"), 0777))

	// put another file in it
	require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "d", "e.json"), []byte("f"), 0777))

	ch := ScanDirToFiles(cb.Ctx(), dir, false)
	out := []File{}
	for f := range ch {
		require.NoError(t, f.Err)
		out = append(out, f)
	}
	assert.Equal(t, 1, len(out))
	assert.Equal(t, filepath.Join(dir, "b.json"), out[0].File)

	ch = ScanDirToFiles(cb.Ctx(), dir, true)
	out = []File{}
	for f := range ch {
		require.NoError(t, f.Err)
		out = append(out, f)
	}
	assert.Equal(t, 2, len(out))
	assert.Equal(t, filepath.Join(dir, "b.json"), out[0].File)
	assert.Equal(t, filepath.Join(dir, "d", "e.json"), out[1].File)

}
Beispiel #11
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)

}
Beispiel #12
0
func testType(t *testing.T, up unpacker.Interface) {

	ctx := initialise()

	data := `{
		"description": "This is the most basic type.",
		"type": "type",
		"id": "type",
		"fields": {
			"native": {
				"description": "This is the native json type that represents this type. If omitted, default is object.",
				"type": "@string",
				"enum": ["string", "number", "bool", "array", "object", "map"],
				"default": "object",
				"optional": true
			},
			"interface": {
				"description": "Is this type an interface?",
				"type": "@bool",
				"default": false,
				"optional": true
			},
			"fields": {
				"description": "Each field is listed with it's type",
				"type": "@map",
				"items": {
					"type": "@rule"
				},
				"optional": true
			},
			"rule": {
				"description": "Embedded type that defines restriction rules for this type. By convention, the ID should be this type prefixed with the @ character.",
				"type": "@type",
				"optional": true
			}
		},
		"rule": {
			"description": "Restriction rules for types",
			"type": "type",
			"embed": ["rule"]
		}
	}`

	var i interface{}
	err := up.Process(ctx, []byte(data), &i)
	require.NoError(t, err)
	f, ok := i.(*system.Type)
	assert.True(t, ok, "Type %T not correct", i)
	assert.NotNil(t, f)
	assert.Equal(t, "This is the most basic type.", f.Description)
	assert.NotNil(t, f.Native)
	assert.Equal(t, "object", f.Native.Value())
	assert.Equal(t, "Is this type an interface?", f.Fields["interface"].(system.ObjectInterface).GetObject(nil).Description)
	assert.Equal(t, true, f.Fields["interface"].GetRule(nil).Optional)
	r, ok := f.Fields["interface"].(*system.BoolRule)
	assert.True(t, ok, "Wrong type %T\n", f.Fields["interface"])
	assert.NotNil(t, r.Default)
	assert.Equal(t, false, r.Default.Value())

}
Beispiel #13
0
func TestDispatcher_Register(t *testing.T) {
	a := &st{}
	b := NewDispatcher(nil)
	b.Register(a)
	assert.Equal(t, 1, len(b.stores))
	assert.Equal(t, a, b.stores[0])
}
Beispiel #14
0
func TestGetIndex(t *testing.T) {
	p := getUtilParser(t)
	r := p.root
	i := p.getIndex(r)
	assert.Equal(t, -1, i)
	s := p.getSiblings(r)
	assert.Equal(t, 0, s)
}
Beispiel #15
0
func TestNewDispatcher(t *testing.T) {
	a := &st{}
	b := &st{}
	c := NewDispatcher(nil, a, b)
	assert.Equal(t, 2, len(c.stores))
	assert.Equal(t, a, c.stores[0])
	assert.Equal(t, b, c.stores[1])
}
Beispiel #16
0
func TestPackedArray(t *testing.T) {
	n1 := &Node{ValueString: "n1"}
	n2 := &Node{ValueString: "n2"}
	p := &packed{n: &Node{Array: []*Node{n1, n2}}}
	a := p.Array()
	assert.Equal(t, 2, len(a))
	assert.Equal(t, "n1", a[0].String())
	assert.Equal(t, "n2", a[1].String())
}
Beispiel #17
0
func TestSortableReferences(t *testing.T) {
	ra := []*Reference{NewReference("e.f/g", "h"), NewReference("a.b/c", "d")}
	sr := SortableReferences(ra)
	sort.Sort(sr)
	ra = []*Reference(sr)
	assert.Equal(t, 2, len(ra))
	assert.Equal(t, *NewReference("a.b/c", "d"), *ra[0])
	assert.Equal(t, *NewReference("e.f/g", "h"), *ra[1])
}
Beispiel #18
0
func TestPackedType(t *testing.T) {
	var p *packed
	assert.Equal(t, system.J_NULL, p.Type())
	p = &packed{n: nil}
	assert.Equal(t, system.J_NULL, p.Type())
	p = &packed{n: &Node{JsonType: system.J_OBJECT}}
	assert.Equal(t, system.J_MAP, p.Type())
	p = &packed{n: &Node{JsonType: system.J_STRING}}
	assert.Equal(t, system.J_STRING, p.Type())
}
Beispiel #19
0
func TestGetInt32(t *testing.T) {
	i := getInt32(2.0)
	assert.Equal(t, int32(2), i)

	i = getInt32(2)
	assert.Equal(t, int32(2), i)

	i = getInt32("a")
	assert.Equal(t, int32(-1), i)
}
Beispiel #20
0
func TestPackedMap(t *testing.T) {
	n1 := &Node{ValueString: "n1"}
	n2 := &Node{ValueString: "n2", Missing: true}
	n3 := &Node{ValueString: "n3"}
	p := &packed{n: &Node{Map: map[string]*Node{"n1": n1, "n2": n2, "n3": n3}}}
	m := p.Map()
	assert.Equal(t, 2, len(m))
	assert.Equal(t, "n1", m["n1"].String())
	assert.Equal(t, "n3", m["n3"].String())
}
Beispiel #21
0
func TestBuild(t *testing.T) {

	type a struct {
		As string
	}
	type b struct {
		Ba *a
	}
	foo := &b{&a{"c"}}

	p := []Pointer{}
	i := builder.Imports{}
	n := Build(foo, &p, "", i.Add)
	assert.Equal(t, "ptr1", n.Name)
	assert.Equal(t, `&literal.a{As:"c"}`, p[0].Source)
	assert.Equal(t, `&literal.b{Ba:ptr0}`, p[1].Source)

	p = []Pointer{}
	i = builder.Imports{"a.b/c": builder.Import{Path: "a.b/c", Name: "c", Alias: "a"}}
	n = Build(foo, &p, "kego.io/process/generate/literal", i.Add)
	assert.Equal(t, "ptr1", n.Name)
	assert.Equal(t, `&a{As:"c"}`, p[0].Source)
	assert.Equal(t, `&b{Ba:ptr0}`, p[1].Source)

	p = []Pointer{}
	i = builder.Imports{
		"foo.com/literal":                  builder.Import{Path: "foo.com/literal", Name: "literal", Alias: "literal"},
		"kego.io/process/generate/literal": builder.Import{Path: "kego.io/process/generate/literal", Name: "literal", Alias: "f"}}
	n = Build(foo, &p, "kego.io/system", i.Add)
	assert.Equal(t, "ptr1", n.Name)
	assert.Equal(t, `&f.a{As:"c"}`, p[0].Source)
	assert.Equal(t, `&f.b{Ba:ptr0}`, p[1].Source)

}
Beispiel #22
0
func TestStringString(t *testing.T) {

	var s *String
	str := s.String()
	assert.Equal(t, "", str)

	s = NewString(`foo "bar"`)
	str = s.String()
	assert.Equal(t, `foo "bar"`, str)

}
Beispiel #23
0
func TestNode_Root(t *testing.T) {
	r := node.NewNode()
	a := node.NewNode()
	b := node.NewNode()
	a.Parent = r
	b.Parent = a
	assert.Equal(t, r, a.Root())
	assert.Equal(t, r, b.Root())
	n := (*node.Node)(nil)
	assert.Nil(t, n.Root())
}
Beispiel #24
0
func TestMultiWriter(t *testing.T) {
	var p, w1, w2 []byte
	pb := bytes.NewBuffer(p)
	w1b := bytes.NewBuffer(w1)
	w2b := bytes.NewBuffer(w2)
	mw := MultiWriter(pb, w1b, w2b)
	mw.Write([]byte("a"))
	mw.Write([]byte("b"))
	assert.Equal(t, "ab", pb.String())
	assert.Equal(t, "ab", w1b.String())
	assert.Equal(t, "ab", w2b.String())
}
Beispiel #25
0
func TestValidateNode(t *testing.T) {
	cb := tests.New()
	errors, err := ValidateNode(cb.Ctx(), &node.Node{Value: nil})
	require.NoError(t, err)
	assert.Equal(t, 0, len(errors))
	errors, err = ValidateNode(cb.Ctx(), &node.Node{Value: 1, Null: true})
	require.NoError(t, err)
	assert.Equal(t, 0, len(errors))
	errors, err = ValidateNode(cb.Ctx(), &node.Node{Value: 1, Missing: true})
	require.NoError(t, err)
	assert.Equal(t, 0, len(errors))
}
Beispiel #26
0
func TestDispatcher_Dispatch(t *testing.T) {
	a := &st1{}
	b := &st1{}
	c := &st1{}
	d := NewDispatcher(nil, a, b)
	d.Register(c)
	done := d.Dispatch("e")
	<-done
	assert.Equal(t, "e", a.handled)
	assert.Equal(t, "e", b.handled)
	assert.Equal(t, "e", c.handled)
}
Beispiel #27
0
func TestSr(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	Run(t, ctx, `{
			"type": "multi",
			"sr": "a"
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			assert.Equal(t, "kego.io/tests/data", v.(*data.Multi).Sr.Package)
			assert.Equal(t, "a", v.(*data.Multi).Sr.Name)
		}),
	)
}
Beispiel #28
0
func TestMjb(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	Run(t, ctx, `{
			"type": "multi",
			"mjb": {"a": true, "b": false}
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			require.Len(t, v.(*data.Multi).Mjb, 2)
			assert.Equal(t, true, v.(*data.Multi).Mjb["a"])
			assert.Equal(t, false, v.(*data.Multi).Mjb["b"])
		}),
	)
}
Beispiel #29
0
func TestMjn(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	Run(t, ctx, `{
			"type": "multi",
			"mjn": {"a": 1.1, "b": 1.2}
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			require.Len(t, v.(*data.Multi).Mjn, 2)
			assert.Equal(t, 1.1, v.(*data.Multi).Mjn["a"])
			assert.Equal(t, 1.2, v.(*data.Multi).Mjn["b"])
		}),
	)
}
Beispiel #30
0
func TestMjs(t *testing.T) {
	ctx := ke.NewContext(context.Background(), "kego.io/tests/data", nil)
	Run(t, ctx, `{
			"type": "multi",
			"mjs": {"a": "b", "c": "d"}
		}`,
		TestValue(func(t *testing.T, v interface{}) {
			require.Len(t, v.(*data.Multi).Mjs, 2)
			assert.Equal(t, "b", v.(*data.Multi).Mjs["a"])
			assert.Equal(t, "d", v.(*data.Multi).Mjs["c"])
		}),
	)
}