From 75afdb4efc5a4778a16a172a88d833b5d0fa3161 Mon Sep 17 00:00:00 2001 From: nodivbyzero Date: Fri, 19 Jul 2024 17:32:32 -0700 Subject: [PATCH] Add more unit tests --- validator_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/validator_test.go b/validator_test.go index 0f96b777..ce22dbcd 100644 --- a/validator_test.go +++ b/validator_test.go @@ -10951,6 +10951,32 @@ func TestStartsWithValidation(t *testing.T) { } } } +func TestStartsNotWithValidation(t *testing.T) { + tests := []struct { + Value string `validate:"startsnotwith=(/^ヮ^)/*:・゚✧"` + Tag string + ExpectedNil bool + }{ + {Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "startsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: false}, + {Value: "abcd", Tag: "startsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: true}, + } + + validate := New() + + for i, s := range tests { + errs := validate.Var(s.Value, s.Tag) + + if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) { + t.Fatalf("Index: %d failed Error: %s", i, errs) + } + + errs = validate.Struct(s) + + if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) { + t.Fatalf("Index: %d failed Error: %s", i, errs) + } + } +} func TestEndsWithValidation(t *testing.T) { tests := []struct { @@ -10979,6 +11005,33 @@ func TestEndsWithValidation(t *testing.T) { } } +func TestEndsNotWithValidation(t *testing.T) { + tests := []struct { + Value string `validate:"endsnotwith=(/^ヮ^)/*:・゚✧"` + Tag string + ExpectedNil bool + }{ + {Value: "glitter (/^ヮ^)/*:・゚✧", Tag: "endsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: false}, + {Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "endsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: true}, + } + + validate := New() + + for i, s := range tests { + errs := validate.Var(s.Value, s.Tag) + + if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) { + t.Fatalf("Index: %d failed Error: %s", i, errs) + } + + errs = validate.Struct(s) + + if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) { + t.Fatalf("Index: %d failed Error: %s", i, errs) + } + } +} + func TestRequiredIf(t *testing.T) { type Inner struct { Field *string @@ -12790,7 +12843,7 @@ func TestIsIso4217Validation(t *testing.T) { } func TestIsIso4217NumericValidation(t *testing.T) { - tests := []struct { + testsInt := []struct { value int `validate:"iso4217_numeric"` expected bool }{ @@ -12801,7 +12854,31 @@ func TestIsIso4217NumericValidation(t *testing.T) { validate := New() - for i, test := range tests { + for i, test := range testsInt { + + errs := validate.Var(test.value, "iso4217_numeric") + + if test.expected { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs) + } + } + } + + testsUInt := []struct { + value uint `validate:"iso4217_numeric"` + expected bool + }{ + {8, true}, + {12, true}, + {13, false}, + } + + for i, test := range testsUInt { errs := validate.Var(test.value, "iso4217_numeric") @@ -12815,6 +12892,8 @@ func TestIsIso4217NumericValidation(t *testing.T) { } } } + + PanicMatches(t, func() { _ = validate.Var(2.0, "iso4217_numeric") }, "Bad field type float64") } func TestTimeZoneValidation(t *testing.T) {