From 934bef0ce5dc7875aa372600e3518a9a7617b971 Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Fri, 27 Nov 2015 19:58:28 -0500 Subject: [PATCH] Add new Anonymous test to hit new code * now have 100% test coverage again. --- validator_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/validator_test.go b/validator_test.go index 1761f33..7f70c61 100644 --- a/validator_test.go +++ b/validator_test.go @@ -296,6 +296,60 @@ type TestStructReturnValidationErrors struct { Inner1 *TestStructReturnValidationErrorsInner1 } +func TestAnonymous(t *testing.T) { + + v2 := New(&Config{TagName: "validate", FieldNameTag: "json"}) + + type Test struct { + Anonymous struct { + A string `validate:"required" json:"EH"` + } + AnonymousB struct { + B string `validate:"required" json:"BEE"` + } + anonymousC struct { + c string `validate:"required" json:"SEE"` + } + } + + tst := &Test{ + Anonymous: struct { + A string `validate:"required" json:"EH"` + }{ + A: "1", + }, + AnonymousB: struct { + B string `validate:"required" json:"BEE"` + }{ + B: "", + }, + anonymousC: struct { + c string `validate:"required" json:"SEE"` + }{ + c: "", + }, + } + + err := v2.Struct(tst) + NotEqual(t, err, nil) + + errs := err.(ValidationErrors) + + Equal(t, len(errs), 1) + AssertError(t, errs, "Test.AnonymousB.B", "B", "required") + Equal(t, errs["Test.AnonymousB.B"].Field, "B") + Equal(t, errs["Test.AnonymousB.B"].Name, "BEE") + + s := struct { + c string `validate:"required" json:"SEE"` + }{ + c: "", + } + + err = v2.Struct(s) + Equal(t, err, nil) +} + func TestStructLevelReturnValidationErrors(t *testing.T) { config := &Config{ TagName: "validate",