func TestNode_DeleteArrayChild(t *testing.T) { _, n := data.Setup(t) test := func(t *testing.T, n *node.Node, m *data.Multi) { require.NoError(t, n.Map["ajs"].DeleteArrayChild(1)) assert.Equal(t, 3, len(n.Map["ajs"].Array)) assert.Equal(t, 0, n.Map["ajs"].Array[0].Index) assert.Equal(t, 1, n.Map["ajs"].Array[1].Index) assert.Equal(t, 2, n.Map["ajs"].Array[2].Index) assert.Equal(t, []string{"ajs0", "ajs2", "ajs3"}, m.Ajs) require.NoError(t, n.Map["ajs"].DeleteArrayChild(2)) assert.Equal(t, 2, len(n.Map["ajs"].Array)) assert.Equal(t, 0, n.Map["ajs"].Array[0].Index) assert.Equal(t, 1, n.Map["ajs"].Array[1].Index) assert.Equal(t, []string{"ajs0", "ajs2"}, m.Ajs) require.NoError(t, n.Map["ajs"].DeleteArrayChild(0)) assert.Equal(t, 1, len(n.Map["ajs"].Array)) assert.Equal(t, 0, n.Map["ajs"].Array[0].Index) assert.Equal(t, []string{"ajs2"}, m.Ajs) require.NoError(t, n.Map["ajs"].DeleteArrayChild(0)) assert.Equal(t, 0, len(n.Map["ajs"].Array)) assert.Equal(t, []string{}, m.Ajs) } data.Run(t, n, n.Value.(*data.Multi), test) }
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) }
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) }
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) }
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) }
func testGenerated(t *testing.T, path string) { cb := tests.New().Path(path).Jauto().Sauto(parser.Parse) pi, err := parser.Parse(cb.Ctx(), path) require.NoError(t, err) generatedBytes, err := generate.Structs(cb.Ctx(), pi.Env) require.NoError(t, err) generatedString := string(generatedBytes) existingFilePath := filepath.Join(pi.Dir, "generated.go") existingBytes, err := ioutil.ReadFile(existingFilePath) require.NoError(t, err) existingString := string(existingBytes) // TODO: The "goimports" tool will often re-order the imports, so this is // a kludge to remove it before comparing. This is not ideal! importsRegex := regexp.MustCompile(`(?ms:\nimport \(\n.*\n\)\n)`) generatedString = importsRegex.ReplaceAllString(generatedString, "-") existingString = importsRegex.ReplaceAllString(existingString, "-") if generatedString != existingString { fmt.Println("Generated code for " + path + " is not what is present:") fmt.Println(generatedString) } require.Equal(t, generatedString, existingString) }
func TestDeleteMutation1(t *testing.T) { cb, n := data.Setup(t) test := func(t *testing.T, n *node.Node, m *data.Multi) { var b, d, p *node.Node b = node.NewNode() d = n.Map["am"].Array[0] p = n.Map["am"] assert.Equal(t, "amjs0", n.Map["am"].Array[0].Map["js"].ValueString) assert.Equal(t, "amjs1", n.Map["am"].Array[1].Map["js"].ValueString) assert.Equal(t, "amjs0", m.Am[0].Js) assert.Equal(t, "amjs1", m.Am[1].Js) require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b)) assert.Equal(t, 1, len(n.Map["am"].Array)) assert.Equal(t, 1, len(m.Am)) assert.Equal(t, "amjs1", n.Map["am"].Array[0].Map["js"].ValueString) assert.Equal(t, "amjs1", m.Am[0].Js) require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b)) assert.Equal(t, 2, len(n.Map["am"].Array)) assert.Equal(t, 2, len(m.Am)) assert.Equal(t, "amjs0", n.Map["am"].Array[0].Map["js"].ValueString) assert.Equal(t, "amjs1", n.Map["am"].Array[1].Map["js"].ValueString) assert.Equal(t, "amjs0", m.Am[0].Js) assert.Equal(t, "amjs1", m.Am[1].Js) } test(t, n.Map["m"], n.Value.(*data.Multi).M) }
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) }
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 }
func TestNode_Backup(t *testing.T) { cb, n := data.Setup(t) test := func(t *testing.T, n *node.Node, m *data.Multi) { var err error b := n.Backup() err = n.RecomputeHash(cb.Ctx(), true) require.NoError(t, err) err = b.RecomputeHash(cb.Ctx(), true) require.NoError(t, err) require.Equal(t, b.Hash(), n.Hash()) require.Equal(t, b.Value, n.Value) r := &node.Node{} r.Restore(cb.Ctx(), b) err = r.RecomputeHash(cb.Ctx(), true) require.NoError(t, err) require.Equal(t, r.Hash(), n.Hash()) require.Equal(t, r.Value, n.Value) } data.Run(t, n, n.Value.(*data.Multi), test) }
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)) }
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 }
func TestUniversalProduction(t *testing.T) { p := getParser(t) f := p.universalProduction("a") out, err := f(&node.Node{}) require.NoError(t, err) assert.False(t, out) f = p.universalProduction("*") out, err = f(&node.Node{}) require.NoError(t, err) assert.True(t, out) }
func TestNodeUnpack(t *testing.T) { ctx, _, err := process.Initialise(context.Background(), &process.Options{ Path: "kego.io/demo/site", }) require.NoError(t, err) j := `{"type":"system:package","aliases":{"images":"kego.io/demo/common/images","units":"kego.io/demo/common/units","words":"kego.io/demo/common/words"}}` _, err = node.Unmarshal(ctx, []byte(j)) require.NoError(t, err) }
func TestReferenceMarshalJson(t *testing.T) { var r *Reference b, _, _, _, err := r.Repack(envctx.Empty) require.NoError(t, err) assert.Equal(t, nil, b) r = NewReference("a.b/c", "d") b, _, _, _, err = r.Repack(envctx.Empty) require.NoError(t, err) assert.Equal(t, "a.b/c:d", b) }
func TestStringMarshalJSON(t *testing.T) { var s *String ba, _, _, _, err := s.Repack(envctx.Empty) require.NoError(t, err) assert.Equal(t, nil, ba) s = NewString(`foo "bar"`) ba, _, _, _, err = s.Repack(envctx.Empty) require.NoError(t, err) assert.Equal(t, `foo "bar"`, ba) }
func TestNode_InitialiseArrayChild2(t *testing.T) { cb, n := data.Setup(t) c := node.NewNode() require.NoError(t, c.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 5)) err := c.AddToArray(cb.Ctx(), n.Map["ajs"], 5, true) assert.IsError(t, err, "GHJIDXABLL") // kludge the Val to be empty n.Map["ajs"].Val.Set(reflect.ValueOf([]string{})) require.NoError(t, c.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 1)) err = c.AddToArray(cb.Ctx(), n.Map["ajs"], 1, true) assert.IsError(t, err, "YFKMXFUPHY") }
func TestNode_DeleteMapChild(t *testing.T) { _, n := data.Setup(t) test := func(t *testing.T, n *node.Node, m *data.Multi) { require.NoError(t, n.Map["mjs"].DeleteMapChild("a")) assert.Equal(t, 1, len(n.Map["mjs"].Map)) assert.Equal(t, 1, len(m.Mjs)) require.NoError(t, n.Map["mjs"].DeleteMapChild("b")) assert.Equal(t, 0, len(n.Map["mjs"].Map)) assert.Equal(t, 0, len(m.Mjs)) require.NoError(t, n.Map["mm"].DeleteMapChild("b")) assert.Equal(t, 1, len(n.Map["mm"].Map)) assert.Equal(t, 1, len(m.Mm)) } data.Run(t, n, n.Value.(*data.Multi), test) }
func testUnpack(t *testing.T, path string) { ctx, _, err := process.Initialise(context.Background(), &process.Options{ Path: path, }) require.NoError(t, err) env := envctx.FromContext(ctx) files := scanner.ScanDirToFiles(ctx, env.Dir, env.Recursive) bytes := scanner.ScanFilesToBytes(ctx, files) for b := range bytes { _, err := node.Unmarshal(ctx, b.Bytes) require.NoError(t, err, b.File) } }
func TestNode_DisplayType(t *testing.T) { cb, n := data.Setup(t) dt, err := n.DisplayType(cb.Ctx()) require.NoError(t, err) assert.Equal(t, "multi", dt) dt, err = n.Map["ai"].DisplayType(cb.Ctx()) require.NoError(t, err) assert.Equal(t, "null", dt) dt, err = n.Map["ajs"].DisplayType(cb.Ctx()) require.NoError(t, err) assert.Equal(t, "[]json:string", dt) }
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) }
func TestImport(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, _ := cb.TempPackage("a", map[string]string{ "a.json": `{ "type": "system:type", "id": "a" }`, }) pathB, dirB := cb.TempPackage("b", map[string]string{ "package.json": `{ "type": "system:package", "aliases": { "a": "` + pathA + `" } }`, "b.json": `{ "type": "a:a", "id": "a" }`, "c.json": `{ "type": "system:foo", "id": "c" }`, }) cb.Path(pathB).Dir(dirB).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathB) require.NoError(t, err) }
func TestValidate_error1(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("b", map[string]string{ "b.json": `{ "description": "b", "type": "system:type", "id": "b", "fields": { "b": { "type": "system:@string", "min-length": 10, "max-length": 5 } } }`, }) cb.Path(path).Dir(dir).Jsystem().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) // @string is invalid because minLength > maxLength require.NoError(t, err) assert.IsError(t, errors[0], "KULDIJUYFB") }
func TestRuleHasExtraRules(t *testing.T) { cb := tests.New().TempGopath(true).CopyToTemp("kego.io/process/validate/tests") defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.yml": ` type: system:package id: a aliases: tests: kego.io/process/validate/tests `, "b.yml": ` type: tests:f id: b d: foo `, }) cb.Path(path).Dir(dir).Alias("tests", "kego.io/process/validate/tests").Jauto().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.IsError(t, errors[0], "HLKQWDCMRN") assert.Equal(t, "MinLength: length of \"foo\" must not be less than 7", errors[0].Description) }
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()) }
func TestServer_Data(t *testing.T) { source := []byte(` package main import "fmt" func main(){ fmt.Println("foo") }`) c, err := Compile(context.Background(), source, false) require.NoError(t, err) assert.True(t, len(c) > 100000) // should be ~700K m, err := Compile(context.Background(), source, true) require.NoError(t, err) assert.True(t, len(m) > 10000) // should be ~35K }
func TestFieldExtraRulesObject(t *testing.T) { cb := tests.New().TempGopath(true).CopyToTemp("kego.io/process/validate/tests") defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.yml": ` type: system:package id: a aliases: tests: kego.io/process/validate/tests `, "b.yml": ` type: tests:f id: b a: type: tests:a b: foo `, }) cb.Path(path).Dir(dir).Alias("tests", "kego.io/process/validate/tests").Jauto().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.IsError(t, errors[0], "HLKQWDCMRN") }
func TestParse1(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.json": `{ "description": "a", "type": "system:type", "id": "b", "fields": { "c": { "type": "system:@string" } } }`, }) cb.Path(path).Dir(dir).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), path) require.NoError(t, err) scache := sysctx.FromContext(cb.Ctx()) i, ok := scache.GetType(path, "b") assert.True(t, ok) ty, ok := i.(*system.Type) assert.True(t, ok) assert.Equal(t, "a", ty.Description) }
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())) }
func TestValidate_NeedsTypes(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.json": `{ "description": "a", "type": "system:type", "id": "a", "fields": { "a": { "type": "system:@string" } } }`, }) cb.Path(path).Dir(dir).Jsystem().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.Equal(t, 0, len(errors)) }