add more checks to IsEqual function

pull/49/head
joeybloggs 10 years ago
parent e0b68a7520
commit ae2d0646af
  1. 313
      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,203 +469,203 @@ 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 j uint64
// var k float64 var k float64
// s := "abcd" s := "abcd"
// i := 1 i := 1
// j = 1 j = 1
// k = 1.543 k = 1.543
// arr := []string{"test"} arr := []string{"test"}
// now := time.Now().UTC() now := time.Now().UTC()
// var j2 uint64 var j2 uint64
// var k2 float64 var k2 float64
// s2 := "abcd" s2 := "abcd"
// i2 := 1 i2 := 1
// j2 = 1 j2 = 1
// k2 = 1.543 k2 = 1.543
// arr2 := []string{"test"} arr2 := []string{"test"}
// arr3 := []string{"test", "test2"} arr3 := []string{"test", "test2"}
// now2 := now now2 := now
// err := validate.FieldWithValue(s, s2, "eqfield") err := validate.FieldWithValue(s, s2, "eqfield")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.FieldWithValue(i2, i, "eqfield") err = validate.FieldWithValue(i2, i, "eqfield")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.FieldWithValue(j2, j, "eqfield") err = validate.FieldWithValue(j2, j, "eqfield")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.FieldWithValue(k2, k, "eqfield") err = validate.FieldWithValue(k2, k, "eqfield")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.FieldWithValue(arr2, arr, "eqfield") err = validate.FieldWithValue(arr2, arr, "eqfield")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.FieldWithValue(now2, now, "eqfield") err = validate.FieldWithValue(now2, now, "eqfield")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.FieldWithValue(arr3, arr, "eqfield") err = validate.FieldWithValue(arr3, arr, "eqfield")
// NotEqual(t, err, nil) NotEqual(t, err, nil)
// type Test struct { type Test struct {
// Start *time.Time `validate:"eqfield=End"` Start *time.Time `validate:"eqfield=End"`
// End *time.Time End *time.Time
// } }
// sv := &Test{ sv := &Test{
// Start: &now, Start: &now,
// End: &now, End: &now,
// } }
// errs := validate.Struct(sv) errs := validate.Struct(sv)
// Equal(t, errs, nil) Equal(t, errs, nil)
// now3 := time.Now().UTC() now3 := time.Now().UTC()
// sv = &Test{ sv = &Test{
// Start: &now, Start: &now,
// End: &now3, End: &now3,
// } }
// errs = validate.Struct(sv) errs = validate.Struct(sv)
// NotEqual(t, errs, nil) NotEqual(t, errs, nil)
// channel := make(chan string) channel := make(chan string)
// PanicMatches(t, func() { validate.FieldWithValue(nil, 1, "eqfield") }, "struct not passed for cross validation") PanicMatches(t, func() { validate.FieldWithValue(nil, 1, "eqfield") }, "struct not passed for cross validation")
// PanicMatches(t, func() { validate.FieldWithValue(5, channel, "eqfield") }, "Bad field type chan string") 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") PanicMatches(t, func() { validate.FieldWithValue(5, now, "eqfield") }, "Bad Top Level field type")
// type Test2 struct { type Test2 struct {
// Start *time.Time `validate:"eqfield=NonExistantField"` Start *time.Time `validate:"eqfield=NonExistantField"`
// End *time.Time End *time.Time
// } }
// sv2 := &Test2{ sv2 := &Test2{
// Start: &now, Start: &now,
// End: &now, End: &now,
// } }
// PanicMatches(t, func() { validate.Struct(sv2) }, "Field \"NonExistantField\" not found in struct") PanicMatches(t, func() { validate.Struct(sv2) }, "Field \"NonExistantField\" not found in struct")
// } }
// func TestIsEqValidation(t *testing.T) { func TestIsEqValidation(t *testing.T) {
// var j uint64 var j uint64
// var k float64 var k float64
// s := "abcd" s := "abcd"
// i := 1 i := 1
// j = 1 j = 1
// k = 1.543 k = 1.543
// arr := []string{"test"} arr := []string{"test"}
// now := time.Now().UTC() now := time.Now().UTC()
// err := validate.Field(s, "eq=abcd") err := validate.Field(s, "eq=abcd")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(i, "eq=1") err = validate.Field(i, "eq=1")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(j, "eq=1") err = validate.Field(j, "eq=1")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(k, "eq=1.543") err = validate.Field(k, "eq=1.543")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(arr, "eq=1") err = validate.Field(arr, "eq=1")
// Equal(t, err, nil) Equal(t, err, nil)
// err = validate.Field(arr, "eq=2") err = validate.Field(arr, "eq=2")
// NotEqual(t, err, nil) NotEqual(t, err, nil)
// PanicMatches(t, func() { validate.Field(now, "eq=now") }, "Bad field type time.Time") PanicMatches(t, func() { validate.Field(now, "eq=now") }, "Bad field type time.Time")
// } }
// func TestBase64Validation(t *testing.T) { func TestBase64Validation(t *testing.T) {
// s := "dW5pY29ybg==" s := "dW5pY29ybg=="
// err := validate.Field(s, "base64") err := validate.Field(s, "base64")
// Equal(t, err, nil) Equal(t, err, nil)
// s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ=" s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ="
// err = validate.Field(s, "base64") err = validate.Field(s, "base64")
// Equal(t, err, nil) Equal(t, err, nil)
// s = "" s = ""
// err = validate.Field(s, "base64") err = validate.Field(s, "base64")
// NotEqual(t, err, nil) NotEqual(t, err, nil)
// s = "dW5pY29ybg== foo bar" s = "dW5pY29ybg== foo bar"
// err = validate.Field(s, "base64") err = validate.Field(s, "base64")
// NotEqual(t, err, nil) NotEqual(t, err, nil)
// } }
// func TestStructOnlyValidation(t *testing.T) { func TestStructOnlyValidation(t *testing.T) {
// type Inner struct { type Inner struct {
// Test string `validate:"len=5"` Test string `validate:"len=5"`
// } }
// type Outer struct { type Outer struct {
// InnerStruct *Inner `validate:"required,structonly"` InnerStruct *Inner `validate:"required,structonly"`
// } }
// outer := &Outer{ outer := &Outer{
// InnerStruct: nil, InnerStruct: nil,
// } }
// errs := validate.Struct(outer).Flatten() errs := validate.Struct(outer).Flatten()
// NotEqual(t, errs, nil) NotEqual(t, errs, nil)
// inner := &Inner{ inner := &Inner{
// Test: "1234", Test: "1234",
// } }
// outer = &Outer{ outer = &Outer{
// InnerStruct: inner, InnerStruct: inner,
// } }
// errs = validate.Struct(outer).Flatten() errs = validate.Struct(outer).Flatten()
// NotEqual(t, errs, nil) NotEqual(t, errs, nil)
// Equal(t, len(errs), 0) Equal(t, len(errs), 0)
// } }
// func (ms *MySuite) TestGtField(c *C) { func TestGtField(t *testing.T) {
// type TimeTest struct { // type TimeTest struct {
// Start *time.Time `validate:"required,gt"` // Start *time.Time `validate:"required,gt"`
@ -657,7 +682,7 @@ func TestIsNeValidation(t *testing.T) {
// } // }
// errs := validate.Struct(timeTest) // errs := validate.Struct(timeTest)
// c.Assert(errs, IsNil) // Equal(t, errs, nil)
// timeTest = &TimeTest{ // timeTest = &TimeTest{
// Start: &end, // Start: &end,
@ -665,7 +690,7 @@ func TestIsNeValidation(t *testing.T) {
// } // }
// errs2 := validate.Struct(timeTest).Flatten() // errs2 := validate.Struct(timeTest).Flatten()
// c.Assert(errs2, NotNil) // NotEqual(t, errs2, nil)
// AssertMapFieldError(errs2, "End", "gtfield", c) // AssertMapFieldError(errs2, "End", "gtfield", c)
// err3 := validate.FieldWithValue(&start, &end, "gtfield") // err3 := validate.FieldWithValue(&start, &end, "gtfield")
@ -777,7 +802,7 @@ func TestIsNeValidation(t *testing.T) {
// } // }
// c.Assert(func() { validate.Struct(timeTest2) }, PanicMatches, "Field \"NonExistantField\" not found in struct") // 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