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
}
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)
}
@ -444,203 +469,203 @@ func TestIsNeFieldValidation(t *testing.T) {
func TestIsNeValidation(t *testing.T) {
// var j uint64
// var k float64
var j uint64
var k float64
s := "abcdef"
// i := 3
// j = 2
// k = 1.5434
// arr := []string{"test"}
// now := time.Now().UTC()
i := 3
j = 2
k = 1.5434
arr := []string{"test"}
now := time.Now().UTC()
err := validate.Field(s, "ne=abcd")
Equal(t, err, nil)
// err = validate.Field(i, "ne=1")
// Equal(t, err, nil)
err = validate.Field(i, "ne=1")
Equal(t, err, nil)
// err = validate.Field(j, "ne=1")
// Equal(t, err, nil)
err = validate.Field(j, "ne=1")
Equal(t, err, nil)
// err = validate.Field(k, "ne=1.543")
// Equal(t, err, nil)
err = validate.Field(k, "ne=1.543")
Equal(t, err, nil)
// err = validate.Field(arr, "ne=2")
// Equal(t, err, nil)
err = validate.Field(arr, "ne=2")
Equal(t, err, nil)
// err = validate.Field(arr, "ne=1")
// NotEqual(t, err, nil)
err = validate.Field(arr, "ne=1")
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 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
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(s, s2, "eqfield")
Equal(t, err, nil)
// err = validate.FieldWithValue(i2, i, "eqfield")
// Equal(t, err, nil)
err = validate.FieldWithValue(i2, i, "eqfield")
Equal(t, err, nil)
// err = validate.FieldWithValue(j2, j, "eqfield")
// Equal(t, err, nil)
err = validate.FieldWithValue(j2, j, "eqfield")
Equal(t, err, nil)
// err = validate.FieldWithValue(k2, k, "eqfield")
// Equal(t, err, nil)
err = validate.FieldWithValue(k2, k, "eqfield")
Equal(t, err, nil)
// err = validate.FieldWithValue(arr2, arr, "eqfield")
// Equal(t, err, nil)
err = validate.FieldWithValue(arr2, arr, "eqfield")
Equal(t, err, nil)
// err = validate.FieldWithValue(now2, now, "eqfield")
// Equal(t, err, nil)
err = validate.FieldWithValue(now2, now, "eqfield")
Equal(t, err, nil)
// err = validate.FieldWithValue(arr3, arr, "eqfield")
// NotEqual(t, err, nil)
err = validate.FieldWithValue(arr3, arr, "eqfield")
NotEqual(t, err, nil)
// type Test struct {
// Start *time.Time `validate:"eqfield=End"`
// End *time.Time
// }
type Test struct {
Start *time.Time `validate:"eqfield=End"`
End *time.Time
}
// sv := &Test{
// Start: &now,
// End: &now,
// }
sv := &Test{
Start: &now,
End: &now,
}
// errs := validate.Struct(sv)
// Equal(t, errs, nil)
errs := validate.Struct(sv)
Equal(t, errs, nil)
// now3 := time.Now().UTC()
now3 := time.Now().UTC()
// sv = &Test{
// Start: &now,
// End: &now3,
// }
sv = &Test{
Start: &now,
End: &now3,
}
// errs = validate.Struct(sv)
// NotEqual(t, errs, nil)
errs = validate.Struct(sv)
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(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(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, now, "eqfield") }, "Bad Top Level field type")
// type Test2 struct {
// Start *time.Time `validate:"eqfield=NonExistantField"`
// End *time.Time
// }
type Test2 struct {
Start *time.Time `validate:"eqfield=NonExistantField"`
End *time.Time
}
// sv2 := &Test2{
// Start: &now,
// End: &now,
// }
sv2 := &Test2{
Start: &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 k float64
// s := "abcd"
// i := 1
// j = 1
// k = 1.543
// arr := []string{"test"}
// now := time.Now().UTC()
var j uint64
var k float64
s := "abcd"
i := 1
j = 1
k = 1.543
arr := []string{"test"}
now := time.Now().UTC()
// err := validate.Field(s, "eq=abcd")
// Equal(t, err, nil)
err := validate.Field(s, "eq=abcd")
Equal(t, err, nil)
// err = validate.Field(i, "eq=1")
// Equal(t, err, nil)
err = validate.Field(i, "eq=1")
Equal(t, err, nil)
// err = validate.Field(j, "eq=1")
// Equal(t, err, nil)
err = validate.Field(j, "eq=1")
Equal(t, err, nil)
// err = validate.Field(k, "eq=1.543")
// Equal(t, err, nil)
err = validate.Field(k, "eq=1.543")
Equal(t, err, nil)
// err = validate.Field(arr, "eq=1")
// Equal(t, err, nil)
err = validate.Field(arr, "eq=1")
Equal(t, err, nil)
// err = validate.Field(arr, "eq=2")
// NotEqual(t, err, nil)
err = validate.Field(arr, "eq=2")
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")
// Equal(t, err, nil)
err := validate.Field(s, "base64")
Equal(t, err, nil)
// s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ="
// err = validate.Field(s, "base64")
// Equal(t, err, nil)
s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ="
err = validate.Field(s, "base64")
Equal(t, err, nil)
// s = ""
// err = validate.Field(s, "base64")
// NotEqual(t, err, nil)
s = ""
err = validate.Field(s, "base64")
NotEqual(t, err, nil)
// s = "dW5pY29ybg== foo bar"
// err = validate.Field(s, "base64")
// NotEqual(t, err, nil)
// }
s = "dW5pY29ybg== foo bar"
err = validate.Field(s, "base64")
NotEqual(t, err, nil)
}
// func TestStructOnlyValidation(t *testing.T) {
func TestStructOnlyValidation(t *testing.T) {
// type Inner struct {
// Test string `validate:"len=5"`
// }
type Inner struct {
Test string `validate:"len=5"`
}
// type Outer struct {
// InnerStruct *Inner `validate:"required,structonly"`
// }
type Outer struct {
InnerStruct *Inner `validate:"required,structonly"`
}
// outer := &Outer{
// InnerStruct: nil,
// }
outer := &Outer{
InnerStruct: nil,
}
// errs := validate.Struct(outer).Flatten()
// NotEqual(t, errs, nil)
errs := validate.Struct(outer).Flatten()
NotEqual(t, errs, nil)
// inner := &Inner{
// Test: "1234",
// }
inner := &Inner{
Test: "1234",
}
// outer = &Outer{
// InnerStruct: inner,
// }
outer = &Outer{
InnerStruct: inner,
}
// errs = validate.Struct(outer).Flatten()
// NotEqual(t, errs, nil)
// Equal(t, len(errs), 0)
// }
errs = validate.Struct(outer).Flatten()
NotEqual(t, errs, nil)
Equal(t, len(errs), 0)
}
// func (ms *MySuite) TestGtField(c *C) {
func TestGtField(t *testing.T) {
// type TimeTest struct {
// Start *time.Time `validate:"required,gt"`
@ -657,7 +682,7 @@ func TestIsNeValidation(t *testing.T) {
// }
// errs := validate.Struct(timeTest)
// c.Assert(errs, IsNil)
// Equal(t, errs, nil)
// timeTest = &TimeTest{
// Start: &end,
@ -665,7 +690,7 @@ func TestIsNeValidation(t *testing.T) {
// }
// errs2 := validate.Struct(timeTest).Flatten()
// c.Assert(errs2, NotNil)
// NotEqual(t, errs2, nil)
// AssertMapFieldError(errs2, "End", "gtfield", c)
// 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")
// }
}
// func (ms *MySuite) TestLtField(c *C) {

Loading…
Cancel
Save