add more checks to IsEqual function

pull/49/head
joeybloggs 10 years ago
parent e0b68a7520
commit ae2d0646af
  1. 529
      validator_test.go

@ -119,6 +119,31 @@ func IsEqual(t *testing.T, val1, val2 interface{}) bool {
return true return true
} }
v1Underlying := reflect.Zero(reflect.TypeOf(v1)).Interface()
v2Underlying := reflect.Zero(reflect.TypeOf(v2)).Interface()
if v1 == v1Underlying {
if v2 == v2Underlying {
goto CASE4
} else {
goto CASE3
}
} else {
if v2 == v2Underlying {
goto CASE2
} else {
goto CASE1
}
}
CASE1:
return reflect.DeepEqual(v1.Interface(), v2.Interface())
CASE2:
return reflect.DeepEqual(v1.Interface(), v2)
CASE3:
return reflect.DeepEqual(v1, v2.Interface())
CASE4:
return reflect.DeepEqual(v1, v2) return reflect.DeepEqual(v1, v2)
} }
@ -444,340 +469,340 @@ func TestIsNeFieldValidation(t *testing.T) {
func TestIsNeValidation(t *testing.T) { func TestIsNeValidation(t *testing.T) {
// var j uint64 var j uint64
// var k float64 var k float64
s := "abcdef" s := "abcdef"
// i := 3 i := 3
// j = 2 j = 2
// k = 1.5434 k = 1.5434
// arr := []string{"test"} arr := []string{"test"}
// now := time.Now().UTC() now := time.Now().UTC()
err := validate.Field(s, "ne=abcd") err := validate.Field(s, "ne=abcd")
Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(i, "ne=1") err = validate.Field(i, "ne=1")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(j, "ne=1") err = validate.Field(j, "ne=1")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(k, "ne=1.543") err = validate.Field(k, "ne=1.543")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(arr, "ne=2") err = validate.Field(arr, "ne=2")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(arr, "ne=1") err = validate.Field(arr, "ne=1")
// NotEqual(t, err, nil) NotEqual(t, err, nil)
// PanicMatches(t, func() { validate.Field(now, "ne=now") }, "Bad field type time.Time") PanicMatches(t, func() { validate.Field(now, "ne=now") }, "Bad field type time.Time")
} }
// func TestIsEqFieldValidation(t *testing.T) { func TestIsEqFieldValidation(t *testing.T) {
// var j uint64
// var k float64
// s := "abcd"
// i := 1
// j = 1
// k = 1.543
// arr := []string{"test"}
// now := time.Now().UTC()
// var j2 uint64
// var k2 float64
// s2 := "abcd"
// i2 := 1
// j2 = 1
// k2 = 1.543
// arr2 := []string{"test"}
// arr3 := []string{"test", "test2"}
// now2 := now
// err := validate.FieldWithValue(s, s2, "eqfield")
// Equal(t, err, nil)
// err = validate.FieldWithValue(i2, i, "eqfield") var j uint64
// Equal(t, err, nil) var k float64
s := "abcd"
// err = validate.FieldWithValue(j2, j, "eqfield") i := 1
// Equal(t, err, nil) j = 1
k = 1.543
// err = validate.FieldWithValue(k2, k, "eqfield") arr := []string{"test"}
// Equal(t, err, nil) now := time.Now().UTC()
// err = validate.FieldWithValue(arr2, arr, "eqfield")
// Equal(t, err, nil)
// err = validate.FieldWithValue(now2, now, "eqfield")
// Equal(t, err, nil)
// err = validate.FieldWithValue(arr3, arr, "eqfield") var j2 uint64
// NotEqual(t, err, nil) var k2 float64
s2 := "abcd"
i2 := 1
j2 = 1
k2 = 1.543
arr2 := []string{"test"}
arr3 := []string{"test", "test2"}
now2 := now
// type Test struct { err := validate.FieldWithValue(s, s2, "eqfield")
// Start *time.Time `validate:"eqfield=End"` Equal(t, err, nil)
// End *time.Time
// }
// sv := &Test{ err = validate.FieldWithValue(i2, i, "eqfield")
// Start: &now, Equal(t, err, nil)
// End: &now,
// }
// errs := validate.Struct(sv) err = validate.FieldWithValue(j2, j, "eqfield")
// Equal(t, errs, nil) Equal(t, err, nil)
// now3 := time.Now().UTC() err = validate.FieldWithValue(k2, k, "eqfield")
Equal(t, err, nil)
// sv = &Test{ err = validate.FieldWithValue(arr2, arr, "eqfield")
// Start: &now, Equal(t, err, nil)
// End: &now3,
// }
// errs = validate.Struct(sv) err = validate.FieldWithValue(now2, now, "eqfield")
// NotEqual(t, errs, nil) Equal(t, err, nil)
// channel := make(chan string) err = validate.FieldWithValue(arr3, arr, "eqfield")
NotEqual(t, err, nil)
// PanicMatches(t, func() { validate.FieldWithValue(nil, 1, "eqfield") }, "struct not passed for cross validation") type Test struct {
// PanicMatches(t, func() { validate.FieldWithValue(5, channel, "eqfield") }, "Bad field type chan string") Start *time.Time `validate:"eqfield=End"`
// PanicMatches(t, func() { validate.FieldWithValue(5, now, "eqfield") }, "Bad Top Level field type") End *time.Time
}
// type Test2 struct { sv := &Test{
// Start *time.Time `validate:"eqfield=NonExistantField"` Start: &now,
// End *time.Time End: &now,
// } }
// sv2 := &Test2{ errs := validate.Struct(sv)
// Start: &now, Equal(t, errs, nil)
// End: &now,
// }
// PanicMatches(t, func() { validate.Struct(sv2) }, "Field \"NonExistantField\" not found in struct") now3 := time.Now().UTC()
// }
// func TestIsEqValidation(t *testing.T) { sv = &Test{
Start: &now,
End: &now3,
}
// var j uint64 errs = validate.Struct(sv)
// var k float64 NotEqual(t, errs, nil)
// s := "abcd"
// i := 1
// j = 1
// k = 1.543
// arr := []string{"test"}
// now := time.Now().UTC()
// err := validate.Field(s, "eq=abcd") channel := make(chan string)
// Equal(t, err, nil)
// err = validate.Field(i, "eq=1") PanicMatches(t, func() { validate.FieldWithValue(nil, 1, "eqfield") }, "struct not passed for cross validation")
// Equal(t, err, nil) PanicMatches(t, func() { validate.FieldWithValue(5, channel, "eqfield") }, "Bad field type chan string")
PanicMatches(t, func() { validate.FieldWithValue(5, now, "eqfield") }, "Bad Top Level field type")
// err = validate.Field(j, "eq=1") type Test2 struct {
// Equal(t, err, nil) Start *time.Time `validate:"eqfield=NonExistantField"`
End *time.Time
}
// err = validate.Field(k, "eq=1.543") sv2 := &Test2{
// Equal(t, err, nil) Start: &now,
End: &now,
}
// err = validate.Field(arr, "eq=1") PanicMatches(t, func() { validate.Struct(sv2) }, "Field \"NonExistantField\" not found in struct")
// Equal(t, err, nil) }
// err = validate.Field(arr, "eq=2") func TestIsEqValidation(t *testing.T) {
// NotEqual(t, err, nil)
// PanicMatches(t, func() { validate.Field(now, "eq=now") }, "Bad field type time.Time") var j uint64
// } var k float64
s := "abcd"
i := 1
j = 1
k = 1.543
arr := []string{"test"}
now := time.Now().UTC()
// func TestBase64Validation(t *testing.T) { err := validate.Field(s, "eq=abcd")
Equal(t, err, nil)
// s := "dW5pY29ybg==" err = validate.Field(i, "eq=1")
Equal(t, err, nil)
// err := validate.Field(s, "base64") err = validate.Field(j, "eq=1")
// Equal(t, err, nil) Equal(t, err, nil)
// s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ=" err = validate.Field(k, "eq=1.543")
// err = validate.Field(s, "base64") Equal(t, err, nil)
// Equal(t, err, nil)
// s = "" err = validate.Field(arr, "eq=1")
// err = validate.Field(s, "base64") Equal(t, err, nil)
// NotEqual(t, err, nil)
// s = "dW5pY29ybg== foo bar" err = validate.Field(arr, "eq=2")
// err = validate.Field(s, "base64") NotEqual(t, err, nil)
// NotEqual(t, err, nil)
// }
// func TestStructOnlyValidation(t *testing.T) { PanicMatches(t, func() { validate.Field(now, "eq=now") }, "Bad field type time.Time")
}
// type Inner struct { func TestBase64Validation(t *testing.T) {
// Test string `validate:"len=5"`
// }
// type Outer struct { s := "dW5pY29ybg=="
// InnerStruct *Inner `validate:"required,structonly"`
// }
// outer := &Outer{ err := validate.Field(s, "base64")
// InnerStruct: nil, Equal(t, err, nil)
// }
// errs := validate.Struct(outer).Flatten() s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ="
// NotEqual(t, errs, nil) err = validate.Field(s, "base64")
Equal(t, err, nil)
// inner := &Inner{ s = ""
// Test: "1234", err = validate.Field(s, "base64")
// } NotEqual(t, err, nil)
// outer = &Outer{ s = "dW5pY29ybg== foo bar"
// InnerStruct: inner, err = validate.Field(s, "base64")
// } NotEqual(t, err, nil)
}
// errs = validate.Struct(outer).Flatten() func TestStructOnlyValidation(t *testing.T) {
// NotEqual(t, errs, nil)
// Equal(t, len(errs), 0)
// }
// func (ms *MySuite) TestGtField(c *C) { type Inner struct {
Test string `validate:"len=5"`
}
// type TimeTest struct { type Outer struct {
// Start *time.Time `validate:"required,gt"` InnerStruct *Inner `validate:"required,structonly"`
// End *time.Time `validate:"required,gt,gtfield=Start"` }
// }
// now := time.Now() outer := &Outer{
// start := now.Add(time.Hour * 24) InnerStruct: nil,
// end := start.Add(time.Hour * 24) }
// timeTest := &TimeTest{ errs := validate.Struct(outer).Flatten()
// Start: &start, NotEqual(t, errs, nil)
// End: &end,
// }
// errs := validate.Struct(timeTest) inner := &Inner{
// c.Assert(errs, IsNil) Test: "1234",
}
// timeTest = &TimeTest{ outer = &Outer{
// Start: &end, InnerStruct: inner,
// End: &start, }
// }
// errs2 := validate.Struct(timeTest).Flatten() errs = validate.Struct(outer).Flatten()
// c.Assert(errs2, NotNil) NotEqual(t, errs, nil)
// AssertMapFieldError(errs2, "End", "gtfield", c) Equal(t, len(errs), 0)
}
// err3 := validate.FieldWithValue(&start, &end, "gtfield") func TestGtField(t *testing.T) {
// c.Assert(err3, IsNil)
// err3 = validate.FieldWithValue(&end, &start, "gtfield") // type TimeTest struct {
// c.Assert(err3, NotNil) // Start *time.Time `validate:"required,gt"`
// c.Assert(err3.Tag, Equals, "gtfield") // End *time.Time `validate:"required,gt,gtfield=Start"`
// }
// type IntTest struct { // now := time.Now()
// Val1 int `validate:"required"` // start := now.Add(time.Hour * 24)
// Val2 int `validate:"required,gtfield=Val1"` // end := start.Add(time.Hour * 24)
// }
// intTest := &IntTest{ // timeTest := &TimeTest{
// Val1: 1, // Start: &start,
// Val2: 5, // End: &end,
// } // }
// errs = validate.Struct(intTest) // errs := validate.Struct(timeTest)
// c.Assert(errs, IsNil) // Equal(t, errs, nil)
// intTest = &IntTest{ // timeTest = &TimeTest{
// Val1: 5, // Start: &end,
// Val2: 1, // End: &start,
// } // }
// errs2 = validate.Struct(intTest).Flatten() // errs2 := validate.Struct(timeTest).Flatten()
// c.Assert(errs2, NotNil) // NotEqual(t, errs2, nil)
// AssertMapFieldError(errs2, "Val2", "gtfield", c) // AssertMapFieldError(errs2, "End", "gtfield", c)
// err3 = validate.FieldWithValue(int(1), int(5), "gtfield") // err3 := validate.FieldWithValue(&start, &end, "gtfield")
// c.Assert(err3, IsNil) // c.Assert(err3, IsNil)
// err3 = validate.FieldWithValue(int(5), int(1), "gtfield") // err3 = validate.FieldWithValue(&end, &start, "gtfield")
// c.Assert(err3, NotNil) // c.Assert(err3, NotNil)
// c.Assert(err3.Tag, Equals, "gtfield") // c.Assert(err3.Tag, Equals, "gtfield")
// type IntTest struct {
// Val1 int `validate:"required"`
// Val2 int `validate:"required,gtfield=Val1"`
// }
// type UIntTest struct { // intTest := &IntTest{
// Val1 uint `validate:"required"` // Val1: 1,
// Val2 uint `validate:"required,gtfield=Val1"` // Val2: 5,
// } // }
// uIntTest := &UIntTest{ // errs = validate.Struct(intTest)
// Val1: 1, // c.Assert(errs, IsNil)
// Val2: 5,
// }
// errs = validate.Struct(uIntTest) // intTest = &IntTest{
// c.Assert(errs, IsNil) // Val1: 5,
// Val2: 1,
// }
// uIntTest = &UIntTest{ // errs2 = validate.Struct(intTest).Flatten()
// Val1: 5, // c.Assert(errs2, NotNil)
// Val2: 1, // AssertMapFieldError(errs2, "Val2", "gtfield", c)
// }
// errs2 = validate.Struct(uIntTest).Flatten() // err3 = validate.FieldWithValue(int(1), int(5), "gtfield")
// c.Assert(errs2, NotNil) // c.Assert(err3, IsNil)
// AssertMapFieldError(errs2, "Val2", "gtfield", c)
// err3 = validate.FieldWithValue(uint(1), uint(5), "gtfield") // err3 = validate.FieldWithValue(int(5), int(1), "gtfield")
// c.Assert(err3, IsNil) // c.Assert(err3, NotNil)
// c.Assert(err3.Tag, Equals, "gtfield")
// err3 = validate.FieldWithValue(uint(5), uint(1), "gtfield") // type UIntTest struct {
// c.Assert(err3, NotNil) // Val1 uint `validate:"required"`
// c.Assert(err3.Tag, Equals, "gtfield") // Val2 uint `validate:"required,gtfield=Val1"`
// }
// type FloatTest struct { // uIntTest := &UIntTest{
// Val1 float64 `validate:"required"` // Val1: 1,
// Val2 float64 `validate:"required,gtfield=Val1"` // Val2: 5,
// } // }
// floatTest := &FloatTest{ // errs = validate.Struct(uIntTest)
// Val1: 1, // c.Assert(errs, IsNil)
// Val2: 5,
// }
// errs = validate.Struct(floatTest) // uIntTest = &UIntTest{
// c.Assert(errs, IsNil) // Val1: 5,
// Val2: 1,
// }
// floatTest = &FloatTest{ // errs2 = validate.Struct(uIntTest).Flatten()
// Val1: 5, // c.Assert(errs2, NotNil)
// Val2: 1, // AssertMapFieldError(errs2, "Val2", "gtfield", c)
// }
// errs2 = validate.Struct(floatTest).Flatten() // err3 = validate.FieldWithValue(uint(1), uint(5), "gtfield")
// c.Assert(errs2, NotNil) // c.Assert(err3, IsNil)
// AssertMapFieldError(errs2, "Val2", "gtfield", c)
// err3 = validate.FieldWithValue(float32(1), float32(5), "gtfield") // err3 = validate.FieldWithValue(uint(5), uint(1), "gtfield")
// c.Assert(err3, IsNil) // c.Assert(err3, NotNil)
// c.Assert(err3.Tag, Equals, "gtfield")
// err3 = validate.FieldWithValue(float32(5), float32(1), "gtfield") // type FloatTest struct {
// c.Assert(err3, NotNil) // Val1 float64 `validate:"required"`
// c.Assert(err3.Tag, Equals, "gtfield") // Val2 float64 `validate:"required,gtfield=Val1"`
// }
// c.Assert(func() { validate.FieldWithValue(nil, 1, "gtfield") }, PanicMatches, "struct not passed for cross validation") // floatTest := &FloatTest{
// c.Assert(func() { validate.FieldWithValue(5, "T", "gtfield") }, PanicMatches, "Bad field type string") // Val1: 1,
// c.Assert(func() { validate.FieldWithValue(5, start, "gtfield") }, PanicMatches, "Bad Top Level field type") // Val2: 5,
// }
// errs = validate.Struct(floatTest)
// c.Assert(errs, IsNil)
// floatTest = &FloatTest{
// Val1: 5,
// Val2: 1,
// }
// type TimeTest2 struct { // errs2 = validate.Struct(floatTest).Flatten()
// Start *time.Time `validate:"required"` // c.Assert(errs2, NotNil)
// End *time.Time `validate:"required,gtfield=NonExistantField"` // AssertMapFieldError(errs2, "Val2", "gtfield", c)
// }
// timeTest2 := &TimeTest2{ // err3 = validate.FieldWithValue(float32(1), float32(5), "gtfield")
// Start: &start, // c.Assert(err3, IsNil)
// End: &end,
// } // err3 = validate.FieldWithValue(float32(5), float32(1), "gtfield")
// c.Assert(err3, NotNil)
// c.Assert(err3.Tag, Equals, "gtfield")
// c.Assert(func() { validate.FieldWithValue(nil, 1, "gtfield") }, PanicMatches, "struct not passed for cross validation")
// c.Assert(func() { validate.FieldWithValue(5, "T", "gtfield") }, PanicMatches, "Bad field type string")
// c.Assert(func() { validate.FieldWithValue(5, start, "gtfield") }, PanicMatches, "Bad Top Level field type")
// type TimeTest2 struct {
// Start *time.Time `validate:"required"`
// End *time.Time `validate:"required,gtfield=NonExistantField"`
// }
// c.Assert(func() { validate.Struct(timeTest2) }, PanicMatches, "Field \"NonExistantField\" not found in struct") // timeTest2 := &TimeTest2{
// } // Start: &start,
// End: &end,
// }
// c.Assert(func() { validate.Struct(timeTest2) }, PanicMatches, "Field \"NonExistantField\" not found in struct")
}
// func (ms *MySuite) TestLtField(c *C) { // func (ms *MySuite) TestLtField(c *C) {

Loading…
Cancel
Save