Exemple #1
0
func TestLoopDetectorInner(t *testing.T) {
	s1 := &mockStore1{}
	s2 := &mockStore2{}
	s3 := &mockStore3{}
	s4 := &mockStore4{}
	s5 := &mockStore5{}
	s6 := &mockStore6{}
	d := newLoopDetector()

	found, store := d.request(s1, s2)
	assert.False(t, found)
	assert.Nil(t, store)
	found, store = d.request(s2, s3)
	assert.False(t, found)
	assert.Nil(t, store)
	found, store = d.request(s3, s1)
	assert.True(t, found)
	assert.Equal(t, s1, store)
	found, store = d.request(s4, s5)
	assert.False(t, found)
	assert.Nil(t, store)
	found, store = d.request(s6, s4)
	assert.False(t, found)
	assert.Nil(t, store)
}
Exemple #2
0
func TestComparePackageHash(t *testing.T) {
	cb := tests.New().TempGopath(true)
	path, _ := cb.TempPackage("a", map[string]string{
		"a.yaml": "type: system:package",
	})
	cb.Path(path).Jempty().Spkg(path)

	// "a.b/c" not found in scache.
	changes, err := comparePackageHash(cb.Ctx(), "a.b/c")
	assert.IsError(t, err, "NHXWLPHCHL")
	assert.False(t, changes)

	// path not found in jcache
	changes, err = comparePackageHash(cb.Ctx(), path)
	require.NoError(t, err)
	assert.True(t, changes)

	cb.Jsystem().Jpkg(path, 999).Sauto(parser.Parse)

	// hash changed
	changes, err = comparePackageHash(cb.Ctx(), path)
	require.NoError(t, err)
	assert.True(t, changes)

	scache := sysctx.FromContext(cb.Ctx())
	pi, _ := scache.Get(path)
	cb.Jpkg(path, pi.Hash)

	// hash correct
	changes, err = comparePackageHash(cb.Ctx(), path)
	require.NoError(t, err)
	assert.False(t, changes)

	pi.Aliases["c"] = "a.b/c"

	changes, err = comparePackageHash(cb.Ctx(), path)
	assert.IsError(t, err, "DGJTLHQOCQ")
	assert.False(t, changes)

	pi1 := scache.Set("a.b/c")

	changes, err = comparePackageHash(cb.Ctx(), path)
	require.NoError(t, err)
	assert.True(t, changes)

	cb.Jpkg("a.b/c", 1)
	pi1.Hash = 2

	changes, err = comparePackageHash(cb.Ctx(), path)
	require.NoError(t, err)
	assert.True(t, changes)

	pi1.Hash = 1

	changes, err = comparePackageHash(cb.Ctx(), path)
	require.NoError(t, err)
	assert.False(t, changes)

}
Exemple #3
0
func TestIsNull(t *testing.T) {
	assert.True(t, isNull(exprElement{typ: system.J_NULL, value: nil}))
	assert.True(t, isNull(exprElement{typ: system.J_MAP, value: nil}))
	assert.True(t, isNull(exprElement{typ: system.J_MAP, value: &node.Node{Null: true}}))
	assert.True(t, isNull(exprElement{typ: system.J_MAP, value: &node.Node{Missing: true}}))
	assert.False(t, isNull(exprElement{typ: system.J_MAP, value: &node.Node{}}))
	assert.False(t, isNull(exprElement{typ: system.J_MAP, value: 1}))
}
Exemple #4
0
func TestGetBool(t *testing.T) {
	assert.False(t, getBool(nil))
	assert.False(t, getBool(&node.Node{ValueBool: false}))
	assert.True(t, getBool(&node.Node{ValueBool: true}))
	assert.True(t, getBool(system.NewBool(true)))
	assert.False(t, getBool(system.NewBool(false)))
	assert.True(t, getBool(true))
	assert.False(t, getBool(false))
	assert.False(t, getBool("a"))
}
Exemple #5
0
func TestArrayRule_Enforce(t *testing.T) {
	r := ArrayRule{MaxItems: NewInt(2)}
	fail, messages, err := r.Enforce(envctx.Empty, []int{1, 2})
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

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

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

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

	r = ArrayRule{UniqueItems: true}
	fail, messages, err = r.Enforce(envctx.Empty, []int{1, 2, 3, 4})
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, []int{1, 2, 3, 3})
	require.NoError(t, err)
	assert.Equal(t, "UniqueItems: array contains duplicate item 3", messages[0])
	assert.True(t, fail)

	fail, messages, err = r.Enforce(envctx.Empty, []string{"foo", "bar", "foo"})
	require.NoError(t, err)
	assert.Equal(t, "UniqueItems: array contains duplicate item foo", messages[0])
	assert.True(t, fail)

	r = ArrayRule{}
	fail, messages, err = r.Enforce(envctx.Empty, []int{1, 2})
	require.NoError(t, err)
	assert.Equal(t, 0, len(messages))
	assert.False(t, fail)

	r = ArrayRule{MaxItems: NewInt(2)}
	fail, messages, err = r.Enforce(envctx.Empty, map[string]int{"foo": 1, "bar": 2})
	assert.IsError(t, err, "OWTAUVVFBL")

	s := &StringRule{}
	var cr CollectionRule = &ArrayRule{Items: s}
	assert.Equal(t, s, cr.GetItemsRule())
}
Exemple #6
0
func TestExprElementIsTruthy(t *testing.T) {
	assert.False(t, exprElementIsTruthy(exprElement{typ: system.J_STRING, value: ""}))
	assert.True(t, exprElementIsTruthy(exprElement{typ: system.J_STRING, value: "a"}))
	assert.False(t, exprElementIsTruthy(exprElement{typ: system.J_NUMBER, value: 0.0}))
	assert.True(t, exprElementIsTruthy(exprElement{typ: system.J_NUMBER, value: 0.1}))
	assert.True(t, exprElementIsTruthy(exprElement{typ: system.J_OBJECT}))
	assert.True(t, exprElementIsTruthy(exprElement{typ: system.J_MAP}))
	assert.True(t, exprElementIsTruthy(exprElement{typ: system.J_ARRAY}))
	assert.False(t, exprElementIsTruthy(exprElement{typ: system.J_BOOL, value: false}))
	assert.True(t, exprElementIsTruthy(exprElement{typ: system.J_BOOL, value: true}))
	assert.False(t, exprElementIsTruthy(exprElement{typ: system.J_NULL}))
	assert.False(t, exprElementIsTruthy(exprElement{typ: system.J_OPERATOR}))

}
Exemple #7
0
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)

}
Exemple #8
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)
}
Exemple #9
0
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())

}
Exemple #10
0
func TestNewBool(t *testing.T) {
	b := NewBool(true)
	assert.True(t, b.Value())

	b1 := NewBool(false)
	assert.False(t, b1.Value())
}
Exemple #11
0
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)

}
Exemple #12
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)

}
Exemple #13
0
func TestKegoProduction(t *testing.T) {
	p := getParser(t)
	f := p.kegoProduction(nil)
	b, err := f(&node.Node{Null: true})
	assert.False(t, b)
	require.NoError(t, err)

}
Exemple #14
0
func TestNewLoopDetector(t *testing.T) {
	s1 := &mockStore1{}
	s2 := &mockStore2{}
	d := newLoopDetector()

	found, store := d.request(s1, s2)
	assert.False(t, found)
	assert.Nil(t, store)

	found, store = d.request(s2, s1)
	assert.True(t, found)
	assert.Equal(t, s1, store)

	d.finished(s1)
	found, store = d.request(s2, s1)
	assert.False(t, found)
	assert.Nil(t, store)
}
Exemple #15
0
func TestStringRule_Validate(t *testing.T) {
	r := &StringRule{}
	fail, messages, err := r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.False(t, fail)
	assert.Equal(t, 0, len(messages))

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

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

	r = &StringRule{MaxLength: NewInt(10)}
	fail, messages, err = r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.False(t, fail)
	assert.Equal(t, 0, len(messages))

	r = &StringRule{MaxLength: NewInt(10), MinLength: NewInt(5)}
	fail, messages, err = r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.False(t, fail)
	assert.Equal(t, 0, len(messages))

	r = &StringRule{MaxLength: NewInt(5), MinLength: NewInt(5)}
	fail, messages, err = r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.False(t, fail)
	assert.Equal(t, 0, len(messages))

	r = &StringRule{MaxLength: NewInt(4), MinLength: NewInt(5)}
	fail, messages, err = r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.True(t, fail)
	assert.Equal(t, "MaxLength 4 must not be less than MinLength 5", messages[0])
}
Exemple #16
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)
}
Exemple #17
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)

}
Exemple #18
0
func TestRulesApplyToObjects(t *testing.T) {

	//_, isRule := object.(RuleInterface)
	//_, isType := object.(*Type)
	//_, isObject := object.(ObjectInterface)
	//return !isRule && !isType && isObject

	r := &StringRule{}
	assert.False(t, RulesApplyToObjects(r)) // isRule = true

	ty := &Type{}
	assert.False(t, RulesApplyToObjects(ty)) // isType = true

	ruleBase := &Rule{}
	assert.False(t, RulesApplyToObjects(ruleBase)) // isObject = false

	s := NewString("")
	assert.False(t, RulesApplyToObjects(s)) // isObject = false

	p := &Package{}
	assert.True(t, RulesApplyToObjects(p))
}
Exemple #19
0
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)
}
Exemple #20
0
func TestReferenceRule_Validate(t *testing.T) {
	r := &ReferenceRule{}
	fail, messages, err := r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.False(t, fail)
	assert.Equal(t, 0, len(messages))

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

	r = &ReferenceRule{PatternNot: NewString("[")}
	fail, messages, err = r.Validate(envctx.Empty)
	require.NoError(t, err)
	assert.True(t, fail)
	assert.Equal(t, "PatternNot: regex does not compile: [", messages[0])
}
Exemple #21
0
func TestNode_InitialiseArrayChild(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.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 4))
		require.NoError(t, c.AddToArray(cb.Ctx(), n.Map["ajs"], 4, true))
		assert.Equal(t, 5, len(n.Map["ajs"].Array))
		assert.True(t, n.Map["ajs"].Array[4].Null)
		assert.False(t, n.Map["ajs"].Array[4].Missing)
		assert.Equal(t, c, n.Map["ajs"].Array[4])
		assert.Equal(t, []string{"ajs0", "ajs1", "ajs2", "ajs3", ""}, m.Ajs)

		c1 := node.NewNode()
		require.NoError(t, c1.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 4))
		require.NoError(t, c1.AddToArray(cb.Ctx(), n.Map["ass"], 4, true))
		assert.Equal(t, 5, len(m.Ass))
		assert.Nil(t, m.Ass[4])

		c2 := node.NewNode()
		require.NoError(t, c2.InitialiseArrayItem(cb.Ctx(), n.Map["ajs"], 0))
		require.NoError(t, c2.AddToArray(cb.Ctx(), n.Map["ajs"], 0, true))
		assert.Equal(t, 6, len(n.Map["ajs"].Array))
		assert.Equal(t, []string{"", "ajs0", "ajs1", "ajs2", "ajs3", ""}, m.Ajs)

		c3 := node.NewNode()
		require.NoError(t, c3.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 0))
		require.NoError(t, c3.AddToArray(cb.Ctx(), n.Map["ass"], 0, true))
		assert.Equal(t, 6, len(m.Ass))
		assert.Nil(t, m.Ass[0])

		c4 := node.NewNode()
		require.NoError(t, c4.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 2))
		require.NoError(t, c4.AddToArray(cb.Ctx(), n.Map["am"], 2, true))
		assert.Equal(t, 3, len(m.Am))
		assert.Nil(t, m.Am[2])
	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Exemple #22
0
func TestDeleteMutation(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["jb"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["jb"].ValueBool)
		assert.False(t, m.Jb)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["jb"].ValueBool)
		assert.True(t, m.Jb)

		b = node.NewNode()
		d = n.Map["ss"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["ss"].Missing)
		assert.Equal(t, "", n.Map["ss"].ValueString)
		assert.Nil(t, m.Ss)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["ss"].Missing)
		assert.Equal(t, "ss1", n.Map["ss"].ValueString)
		require.NotNil(t, m.Ss)
		assert.Equal(t, "ss1", m.Ss.Value())

		b = node.NewNode()
		d = n.Map["i"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["i"].Missing)
		assert.Nil(t, m.I)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["i"].Missing)
		assert.Equal(t, "ia", n.Map["i"].Value.(*data.Facea).A.Value())
		require.NotNil(t, m.I)
		assert.Equal(t, "ia", m.I.Face())

		b = node.NewNode()
		d = n.Map["ass"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["ass"].Missing)
		assert.Equal(t, 0, len(m.Ass))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["ass"].Missing)
		assert.Equal(t, 4, len(m.Ass))

		b = node.NewNode()
		d = n.Map["mss"]
		p = n
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.True(t, n.Map["mss"].Missing)
		assert.Nil(t, m.Mss)
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.False(t, n.Map["mss"].Missing)
		require.NotNil(t, m.Mss)
		assert.Equal(t, 2, len(m.Mss))
		require.NotNil(t, m.Mss["a"])
		assert.Equal(t, "mssa", m.Mss["a"].Value())

		b = node.NewNode()
		d = n.Map["ass"].Array[0]
		p = n.Map["ass"]
		require.NoError(t, mutateDeleteNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 3, len(n.Map["ass"].Array))
		assert.Equal(t, 3, len(m.Ass))
		require.NoError(t, mutateRestoreNode(cb.Ctx(), d, p, b))
		assert.Equal(t, 4, len(n.Map["ass"].Array))
		assert.Equal(t, 4, len(m.Ass))
		assert.Equal(t, "ass0", n.Map["ass"].Array[0].ValueString)
		require.NotNil(t, m.Ass[0])
		assert.Equal(t, "ass0", m.Ass[0].Value())

	}

	data.Run(t, n, n.Value.(*data.Multi), test)
}
Exemple #23
0
func TestNumberRule_Enforce(t *testing.T) {
	r := NumberRule{Rule: &Rule{Optional: false}, Minimum: NewNumber(1.5)}
	fail, messages, err := r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Minimum: value must exist", messages[0])
	assert.True(t, fail)

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

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

	fail, messages, err = r.Enforce(envctx.Empty, NewNumber(1))
	require.NoError(t, err)
	assert.Equal(t, "Minimum: value 1 must not be less than 1.5", messages[0])
	assert.True(t, fail)

	r = NumberRule{Rule: &Rule{Optional: false}, Minimum: NewNumber(1.5), ExclusiveMinimum: true}
	fail, messages, err = r.Enforce(envctx.Empty, NewNumber(1.5))
	require.NoError(t, err)
	assert.Equal(t, "Minimum (exclusive): value 1.5 must be greater than 1.5", messages[0])
	assert.True(t, fail)

	r = NumberRule{Rule: &Rule{Optional: false}, Maximum: NewNumber(1.5)}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Maximum: value must exist", messages[0])
	assert.True(t, fail)

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

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

	fail, messages, err = r.Enforce(envctx.Empty, NewNumber(2))
	require.NoError(t, err)
	assert.Equal(t, "Maximum: value 2 must not be greater than 1.5", messages[0])
	assert.True(t, fail)

	r = NumberRule{Rule: &Rule{Optional: false}, Maximum: NewNumber(1.5), ExclusiveMaximum: true}
	fail, messages, err = r.Enforce(envctx.Empty, NewNumber(1.5))
	require.NoError(t, err)
	assert.Equal(t, "Maximum (exclusive): value 1.5 must be less than 1.5", messages[0])
	assert.True(t, fail)

	r = NumberRule{Rule: &Rule{Optional: false}, MultipleOf: NewNumber(1.5)}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "MultipleOf: value must exist", messages[0])
	assert.True(t, fail)

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

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

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

	fail, messages, err = r.Enforce(envctx.Empty, NewNumber(4))
	require.NoError(t, err)
	assert.Equal(t, "MultipleOf: value 4 must be a multiple of 1.5", messages[0])
	assert.True(t, fail)

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

}
Exemple #24
0
func TestDirEqual(t *testing.T) {
	assert.True(t, DirEqual("/a/b", "/a/b/"))
	assert.False(t, DirEqual("/a/b", "/a/b/c"))
	assert.False(t, DirEqual("/a/b/", "/a/b/c"))
}
Exemple #25
0
func TestValidateMain(t *testing.T) {

	cb := tests.New().TempGopath(true)
	defer cb.Cleanup()

	dir := cb.SetTemp("kego.io/system")
	cb.Path("kego.io/system").Dir(dir).Jempty().Sempty()

	cancelled := false
	cancel := func() {
		cancelled = true
	}

	logged := ""
	log := func(message string) {
		logged = message
	}

	interrupt := make(chan os.Signal, 1)

	exitStatus := validateMain(cb.Ctx(), cancel, log, interrupt)
	assert.Equal(t, 1, exitStatus)
	assert.False(t, cancelled)
	assert.Contains(t, logged, "NHXWLPHCHL")
	logged = ""

	cb.Jauto().Sauto(parser.Parse)

	exitStatus = validateMain(cb.Ctx(), cancel, log, interrupt)
	assert.Equal(t, 0, exitStatus)
	assert.False(t, cancelled)
	assert.Equal(t, logged, "")

	cb.TempFile("a.yaml", `
		type: type
		id: a`)
	cb.Sauto(parser.Parse)

	exitStatus = validateMain(cb.Ctx(), cancel, log, interrupt)
	assert.Equal(t, 3, exitStatus)
	assert.False(t, cancelled)
	assert.Equal(t, logged, "")

	cb.RemoveTempFile("a.yaml").
		TempFile("a.yaml", `
			type: "@string"
			id: a
			max-length: 1
			min-length: 2`).
		Sauto(parser.Parse)

	exitStatus = validateMain(cb.Ctx(), cancel, log, interrupt)
	assert.Equal(t, 4, exitStatus)
	assert.False(t, cancelled)
	assert.Equal(t, logged, "a: MaxLength 1 must not be less than MinLength 2")
	logged = ""

	cb.RemoveTempFile("a.yaml").Sauto(parser.Parse).TempFile("b.yaml", `%`)

	exitStatus = validateMain(cb.Ctx(), cancel, log, interrupt)
	assert.Equal(t, 1, exitStatus)
	assert.False(t, cancelled)
	assert.Contains(t, logged, "IHSVWAUAYW")

}
Exemple #26
0
func TestIntRule_Enforce(t *testing.T) {
	r := IntRule{Rule: &Rule{Optional: false}, Minimum: NewInt(2)}
	fail, messages, err := r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Minimum: value must exist", messages[0])
	assert.True(t, fail)

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

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

	fail, messages, err = r.Enforce(envctx.Empty, NewInt(1))
	require.NoError(t, err)
	assert.Equal(t, "Minimum: value 1 must not be less than 2", messages[0])
	assert.True(t, fail)

	r = IntRule{Rule: &Rule{Optional: false}, Maximum: NewInt(2)}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "Maximum: value must exist", messages[0])
	assert.True(t, fail)

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

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

	fail, messages, err = r.Enforce(envctx.Empty, NewInt(3))
	require.NoError(t, err)
	assert.Equal(t, "Maximum: value 3 must not be greater than 2", messages[0])
	assert.True(t, fail)

	r = IntRule{Rule: &Rule{Optional: false}, MultipleOf: NewInt(3)}
	fail, messages, err = r.Enforce(envctx.Empty, nil)
	require.NoError(t, err)
	assert.Equal(t, "MultipleOf: value must exist", messages[0])
	assert.True(t, fail)

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

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

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

	fail, messages, err = r.Enforce(envctx.Empty, NewInt(4))
	require.NoError(t, err)
	assert.Equal(t, "MultipleOf: value 4 must be a multiple of 3", messages[0])
	assert.True(t, fail)

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

}
Exemple #27
0
func TestNode_SetValueZero(t *testing.T) {
	cb, n := data.Setup(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)
		facea, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "facea")
		assert.True(t, ok)
		faceb, ok := system.GetTypeFromCache(cb.Ctx(), "kego.io/tests/data", "faceb")
		assert.True(t, ok)

		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))
		require.NoError(t, c1.SetValueZero(cb.Ctx(), true, nil))
		assert.False(t, n.Map["mss"].Map["c"].Missing)
		assert.True(t, n.Map["mss"].Map["c"].Null)
		assert.Nil(t, m.Mss["c"])

		c1a := node.NewNode()
		require.NoError(t, c1a.InitialiseMapItem(cb.Ctx(), n.Map["mss"], "d"))
		require.NoError(t, c1a.AddToMap(cb.Ctx(), n.Map["mss"], "d", true))
		require.NoError(t, c1a.SetValueZero(cb.Ctx(), false, nil))
		assert.False(t, n.Map["mss"].Map["d"].Missing)
		assert.False(t, n.Map["mss"].Map["d"].Null)
		assert.NotNil(t, m.Mss["d"])
		assert.Equal(t, "", m.Mss["d"].Value())

		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))
		require.NoError(t, c2.SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Mm["c"])

		c2a := node.NewNode()
		require.NoError(t, c2a.InitialiseMapItem(cb.Ctx(), n.Map["mm"], "d"))
		require.NoError(t, c2a.AddToMap(cb.Ctx(), n.Map["mm"], "d", true))
		require.NoError(t, c2a.SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Mm["d"])
		assert.Equal(t, "", m.Mm["d"].Js)

		require.NoError(t, n.Map["i"].SetValueZero(cb.Ctx(), true, faceb))
		assert.Nil(t, m.I)
		assert.IsType(t, &data.Faceb{}, m.I)

		require.NoError(t, n.Map["sri"].SetValueZero(cb.Ctx(), false, facea))
		assert.NotNil(t, m.Sri)
		assert.IsType(t, &data.Facea{}, m.Sri)

		c3 := node.NewNode()
		require.NoError(t, c3.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 4))
		require.NoError(t, c3.AddToArray(cb.Ctx(), n.Map["ass"], 4, true))
		require.NoError(t, c3.SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Ass[4])

		c3a := node.NewNode()
		require.NoError(t, c3a.InitialiseArrayItem(cb.Ctx(), n.Map["ass"], 5))
		require.NoError(t, c3a.AddToArray(cb.Ctx(), n.Map["ass"], 5, true))
		require.NoError(t, c3a.SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Ass[5])

		c4 := node.NewNode()
		require.NoError(t, c4.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 2))
		require.NoError(t, c4.AddToArray(cb.Ctx(), n.Map["am"], 2, true))
		require.NoError(t, c4.SetValueZero(cb.Ctx(), true, nil))
		assert.Nil(t, m.Am[2])

		c4a := node.NewNode()
		require.NoError(t, c4a.InitialiseArrayItem(cb.Ctx(), n.Map["am"], 3))
		require.NoError(t, c4a.AddToArray(cb.Ctx(), n.Map["am"], 3, true))
		require.NoError(t, c4a.SetValueZero(cb.Ctx(), false, nil))
		assert.NotNil(t, m.Am[3])

		c5 := node.NewNode()
		require.NoError(t, c5.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 3))
		require.NoError(t, c5.AddToArray(cb.Ctx(), n.Map["anri"], 3, true))
		require.NoError(t, c5.SetValueZero(cb.Ctx(), true, sstring))
		assert.Nil(t, m.Anri[3])
		assert.IsType(t, system.NewString(""), m.Anri[3])

		c5a := node.NewNode()
		require.NoError(t, c5a.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 4))
		require.NoError(t, c5a.AddToArray(cb.Ctx(), n.Map["anri"], 4, true))
		require.NoError(t, c5a.SetValueZero(cb.Ctx(), false, sstring))
		assert.NotNil(t, m.Anri[4])
		assert.IsType(t, system.NewString(""), m.Anri[4])

		c6 := node.NewNode()
		require.NoError(t, c6.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 5))
		require.NoError(t, c6.AddToArray(cb.Ctx(), n.Map["anri"], 5, true))
		require.NoError(t, c6.SetValueZero(cb.Ctx(), true, facea))
		assert.Nil(t, m.Anri[5])
		assert.IsType(t, &data.Facea{}, m.Anri[5])

		c6a := node.NewNode()
		require.NoError(t, c6a.InitialiseArrayItem(cb.Ctx(), n.Map["anri"], 6))
		require.NoError(t, c6a.AddToArray(cb.Ctx(), n.Map["anri"], 6, true))
		require.NoError(t, c6a.SetValueZero(cb.Ctx(), false, facea))
		assert.NotNil(t, m.Anri[6])
		assert.IsType(t, &data.Facea{}, m.Anri[6])

		c7 := node.NewNode()
		require.NoError(t, c7.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "d"))
		require.NoError(t, c7.AddToMap(cb.Ctx(), n.Map["mnri"], "d", true))
		require.NoError(t, c7.SetValueZero(cb.Ctx(), true, sstring))
		assert.Nil(t, m.Mnri["d"])
		assert.IsType(t, system.NewString(""), m.Mnri["d"])

		c7a := node.NewNode()
		require.NoError(t, c7a.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "e"))
		require.NoError(t, c7a.AddToMap(cb.Ctx(), n.Map["mnri"], "e", true))
		require.NoError(t, c7a.SetValueZero(cb.Ctx(), false, sstring))
		assert.NotNil(t, m.Mnri["e"])
		assert.IsType(t, system.NewString(""), m.Mnri["e"])

		c8 := node.NewNode()
		require.NoError(t, c8.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "f"))
		require.NoError(t, c8.AddToMap(cb.Ctx(), n.Map["mnri"], "f", true))
		require.NoError(t, c8.SetValueZero(cb.Ctx(), true, facea))
		assert.Nil(t, m.Mnri["f"])
		assert.IsType(t, &data.Facea{}, m.Mnri["f"])

		c8a := node.NewNode()
		require.NoError(t, c8a.InitialiseMapItem(cb.Ctx(), n.Map["mnri"], "g"))
		require.NoError(t, c8a.AddToMap(cb.Ctx(), n.Map["mnri"], "g", true))
		require.NoError(t, c8a.SetValueZero(cb.Ctx(), false, facea))
		assert.NotNil(t, m.Mnri["g"])
		assert.IsType(t, &data.Facea{}, m.Mnri["g"])
	}
	data.Run(t, n, n.Value.(*data.Multi), test)

}
Exemple #28
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)

}
Exemple #29
0
func TestRuleWrapper_InnerType(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{},
		},
	})
	inner := r.InnerType(cb.Ctx())
	assert.Equal(t, "kego.io/system:string", inner.Id.String())

	kind, alias := r.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, kind, system.KindMap)

	assert.False(t, r.PassedAsPointer(cb.Ctx()))

	r = system.WrapRule(cb.Ctx(), &system.DummyRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@int")},
		Rule:   &system.Rule{},
	})
	assert.Equal(t, "kego.io/system:int", r.InnerType(cb.Ctx()).Id.String())

	kind, alias = r.Kind(cb.Ctx())
	assert.True(t, alias)
	assert.Equal(t, kind, system.KindValue)

	assert.True(t, r.PassedAsPointer(cb.Ctx()))

	r = system.WrapRule(cb.Ctx(), &system.DummyRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@int")},
		Rule: &system.Rule{
			Interface: true,
		},
	})
	assert.Equal(t, "kego.io/system:int", r.InnerType(cb.Ctx()).Id.String())

	kind, alias = r.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, kind, system.KindInterface)

	assert.False(t, r.PassedAsPointer(cb.Ctx()))

	r = system.WrapRule(cb.Ctx(), &system.DummyRule{
		Object: &system.Object{Type: system.NewReference("kego.io/json", "@string")},
		Rule:   &system.Rule{},
	})
	assert.Equal(t, "kego.io/json:string", r.InnerType(cb.Ctx()).Id.String())

	assert.False(t, r.PassedAsPointer(cb.Ctx()))

	r = system.WrapRule(cb.Ctx(), &system.DummyRule{
		Object: &system.Object{Type: system.NewReference("kego.io/system", "@package")},
		Rule:   &system.Rule{},
	})
	assert.Equal(t, "kego.io/system:package", r.InnerType(cb.Ctx()).Id.String())

	kind, alias = r.Kind(cb.Ctx())
	assert.False(t, alias)
	assert.Equal(t, kind, system.KindStruct)

	assert.True(t, r.PassedAsPointer(cb.Ctx()))
}