Exemple #1
0
func testObjKVs(t *testing.T) {
	counter, countingValidator := createCountingValidator()

	sch := v.Object(
		v.ObjKV(nil, v.And(v.String(v.StrIs("1")), v.Function(countingValidator))),
		v.ObjKV("1", v.And(v.String(v.StrIs("b")), v.Function(countingValidator))),
		v.ObjKV(true, v.And(v.Number(v.NumIs(3)), v.Function(countingValidator))),
	)
	m := maep{
		nil:  "1",
		"1":  "b",
		true: 3,
	}
	test(t, "mixed objkvs", true, sch, m)
	if *counter != 3 {
		t.Fatalf("value counter should be 3, got %d", *counter)
	}

	m = maep{
		nil:  "1",
		"1":  2,
		true: 3,
	}
	test(t, "!mixed objkvs", false, sch, m)

	m = maep{
		nil:  "1",
		"1":  nil,
		true: 3,
	}
	test(t, "!mixed objkvs (nil)", false, sch, m)
}
Exemple #2
0
func TestString(t *testing.T) {
	var np *string
	var nnp string = "a"

	test(t, "non-string", false, v.String(), 3)
	test(t, "basic string", true, v.String(), "")
	test(t, "nil", false, v.String(), nil)
	test(t, "nil string pointer", false, v.String(), np)
	test(t, "string pointer", true, v.String(), &nnp)

	test(t, "equals", true, v.String(v.StrIs("abc")), "abc")
	test(t, "!equals", false, v.String(v.StrIs("abc")), "abd")

	test(t, "minlen1", false, v.String(v.StrMin(3)), "aa")
	test(t, "minlen2", true, v.String(v.StrMin(3)), "aaa")
	test(t, "minlen3", true, v.String(v.StrMin(3)), "aaaa")

	test(t, "maxlen1", true, v.String(v.StrMax(4)), "aaa")
	test(t, "maxlen2", true, v.String(v.StrMax(4)), "aaaa")
	test(t, "maxlen3", false, v.String(v.StrMax(4)), "aaaaa")

	test(t, "regexp1", true, v.String(v.StrRegExp("^.{3}$")), "bbb")
	test(t, "regexp2", false, v.String(v.StrRegExp("^.{3}$")), "bbbb")
	test(t, "regexp3", false, v.String(v.StrRegExp("[")), "c")

	test(t, "combination1", false, v.String(v.StrMin(3), v.StrMax(3)), "cc")
	test(t, "combination2", true, v.String(v.StrMin(3), v.StrMax(3)), "ccc")
	test(t, "combination1", false, v.String(v.StrMin(3), v.StrMax(3)), "cccc")

}
Exemple #3
0
func TestOptional(t *testing.T) {
	var np *bool
	test(t, "string", true, v.Optional(v.String(v.StrIs("a"))), "a")
	test(t, "wrong string", false, v.Optional(v.String(v.StrIs("a"))), "b")

	test(t, "nil ptr", true, v.Optional(v.String(v.StrIs("a"))), np)
}
Exemple #4
0
func TestOr(t *testing.T) {
	test(t, "combination1", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), "a")
	test(t, "combination2", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), "b")
	test(t, "combination3", false, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b"))), 3)

	test(t, "combination4", true, v.Or(v.String(v.StrIs("a")), v.String(v.StrIs("b")), v.String(v.StrIs("c"))), "b")

	test(t, "combination5", true, v.Or(v.String(v.StrIs("a"))), "a")
	test(t, "combination6", false, v.Or(v.String(v.StrIs("a"))), "b")

	test(t, "combination7", true, v.Or(), nil)
}