Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests #1295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}{
Expand All @@ -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")

Expand All @@ -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) {
Expand Down