Esempio n. 1
0
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)

}
Esempio n. 2
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)

}
Esempio n. 3
0
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)
}
Esempio n. 4
0
File: data_test.go Progetto: kego/ke
func TestData(t *testing.T) {
	_, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		require.Equal(t, "almsa", m.Alms["a"].Js)
	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Esempio n. 5
0
func TestAddMutationRoot(t *testing.T) {
	cb, _ := data.Setup(t)
	a := node.NewNode()
	ty, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "multi")
	require.True(t, ok)
	require.NoError(t, mutateAddNode(cb.Ctx(), a, nil, "", 2, ty, "z"))
	require.NotNil(t, a.Value.(*data.Multi).Id)
	require.Equal(t, "z", a.Value.(*data.Multi).Id.Name)
}
Esempio n. 6
0
func TestNode_SetIdField(t *testing.T) {
	cb, n := data.Setup(t)
	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		require.HasError(t, n.Map["js"].SetIdField(cb.Ctx(), "a"), "QVLAEAEWEB")
		require.NoError(t, n.Map["m"].SetIdField(cb.Ctx(), "a"))
		require.Equal(t, "a", n.Map["m"].Value.(*data.Multi).Id.Name)
	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Esempio n. 7
0
File: main.go Progetto: kego/ke
func main() {
	t := &testing.T{}
	_, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {
		require.Equal(t, "aljs", *m.Aljs)
	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Esempio n. 8
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()))

}
Esempio n. 9
0
File: scan_test.go Progetto: kego/ke
func getErrData(t *testing.T, args []ast.Expr, arg int, file string, pos token.Position) *errDef {
	require.True(t, len(args) > arg, "Not enough args (%s:%d)", file, pos.Line)
	b, ok := args[arg].(*ast.BasicLit)
	if !ok {
		return nil
	}
	require.Equal(t, b.Kind, token.STRING, "kind should be token.STRING (%s:%d)", file, pos.Line)
	id, err := strconv.Unquote(b.Value)
	require.NoError(t, err, "Error unquoting arg (%s:%d)", file, pos.Line)
	assert.True(t, ksrc.IsId(id), "Invalid kerr ID %s (%s:%d)", id, file, pos.Line)
	def, ok := all[id]
	if ok {
		return def
	}
	def = &errDef{}
	def.id = id
	all[id] = def
	return def
}
Esempio n. 10
0
func TestAddMutationRedo(t *testing.T) {
	cb, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		var a, p, b, a1, p1, b1 *node.Node
		a = node.NewNode()
		p = n.Map["am"]
		b = node.NewNode()
		ty, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "multi")
		require.True(t, ok)
		require.NoError(t, mutateAddNode(cb.Ctx(), a, p, "", 2, ty, ""))
		require.Equal(t, `[{"js":"amjs0","type":"multi"},{"js":"amjs1","type":"multi"},{"type":"multi"}]`, p.Print(cb.Ctx()))

		a1 = n.Map["am"].Array[2].Map["m"]
		p1 = n.Map["am"].Array[2]
		b1 = node.NewNode()
		require.NoError(t, mutateAddNode(cb.Ctx(), a1, p1, "m", -1, ty, ""))
		require.Equal(t, 3, len(n.Map["am"].Array))
		require.Equal(t, 3, len(m.Am))
		require.False(t, n.Map["am"].Array[2].Map["m"].Missing)
		require.Equal(t, `[{"js":"amjs0","type":"multi"},{"js":"amjs1","type":"multi"},{"m":{"type":"multi"},"type":"multi"}]`, p.Print(cb.Ctx()))

		require.NoError(t, mutateDeleteNode(cb.Ctx(), a, p, b))
		require.NoError(t, mutateDeleteNode(cb.Ctx(), a1, p1, b1))
		require.Equal(t, 2, len(n.Map["am"].Array))
		require.Equal(t, 2, len(m.Am))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), a, p, b))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), a1, p1, b1))
		require.Equal(t, 3, len(n.Map["am"].Array))
		require.Equal(t, 3, len(m.Am))
		require.NotNil(t, n.Map["am"].Array[2])
		require.NotNil(t, m.Am[2])
	}

	test(t, n.Map["m"], n.Value.(*data.Multi).M)
}
Esempio n. 11
0
func TestStringRule_Enforce(t *testing.T) {
	r := StringRule{Rule: &Rule{Optional: false}, Equal: NewString("a"), MaxLength: NewInt(1)}
	fail, messages, err := r.Enforce(envctx.Empty, NewString("a"))
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Equal: value must exist", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewString("b"))
	require.NoError(t, err)
	assert.Equal(t, "Equal: value \"b\" must equal 'a'", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewString("123456789012345678901234567890"))
	require.NoError(t, err)
	assert.Equal(t, "Equal: value \"12345678901234567...\" must equal 'a'", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, "a")
	assert.IsError(t, err, "SXFBXGQSEA")

	r = StringRule{Rule: &Rule{Optional: false}, MaxLength: NewInt(1)}
	fail, messages, err = r.Enforce(envctx.Empty, NewString("ab"))
	require.NoError(t, err)
	assert.Equal(t, "MaxLength: length of \"ab\" must not be greater than 1", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "MaxLength: value must exist", messages[0])
	assert.True(t, fail)

	r = StringRule{Rule: &Rule{Optional: false}, MinLength: NewInt(5)}
	fail, messages, err = r.Enforce(envctx.Empty, NewString("abcde"))
	require.NoError(t, err)
	assert.False(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, NewString("abcd"))
	require.NoError(t, err)
	assert.Equal(t, "MinLength: length of \"abcd\" must not be less than 5", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "MinLength: value must exist", messages[0])
	assert.True(t, fail)

	r = StringRule{Rule: &Rule{Optional: false}, Enum: []string{"a", "b"}}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Enum: value must exist", messages[0])
	assert.True(t, fail)

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

	fail, messages, err = r.Enforce(envctx.Empty, NewString("c"))
	require.NoError(t, err)
	assert.Equal(t, "Enum: value \"c\" must be one of: [a b]", messages[0])
	assert.True(t, fail)

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

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

	r = StringRule{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, NewString("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, NewString("foo1"))
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	r = StringRule{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, NewString("foo1"))
	require.NoError(t, err)
	require.Equal(t, 1, len(messages))
	assert.Equal(t, "PatternNot: value \"foo1\" must not match ^foo\\d", messages[0])
	assert.True(t, fail)

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

}
Esempio n. 12
0
func TestNode_NativeValue(t *testing.T) {
	_, n := data.Setup(t)

	test := func(t *testing.T, n *node.Node, m *data.Multi) {

		require.Equal(t, "js1", n.Map["js"].NativeValue())
		require.Equal(t, "ss1", n.Map["ss"].NativeValue())

		require.Equal(t, true, n.Map["jb"].NativeValue())
		require.Equal(t, false, n.Map["sb"].NativeValue())

		require.Equal(t, 1.1, n.Map["jn"].NativeValue())
		require.Equal(t, 1.2, n.Map["sn"].NativeValue())

		require.Equal(t, nil, n.Map["ajs"].NativeValue())
		require.Equal(t, nil, n.Map["mjs"].NativeValue())

		require.Equal(t, true, n.Map["aljb"].NativeValue())
		require.Equal(t, 2.0, n.Map["aljn"].NativeValue())
		require.Equal(t, "aljs", n.Map["aljs"].NativeValue())

	}

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