Skip to content

Commit 201c892

Browse files
joeybloggsjoeybloggs
authored andcommitted
Split out assertions and put in external repo
split our assertion functions like IsEqual, Equal, NotEqual, PanicMatches etc... into another library https://github.com/bluesuncorp/assert so that I can use them in other projects.
1 parent 781f596 commit 201c892

File tree

1 file changed

+34
-144
lines changed

1 file changed

+34
-144
lines changed

validator_test.go

Lines changed: 34 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package validator
22

33
import (
44
"fmt"
5-
"path"
65
"reflect"
7-
"runtime"
86
"testing"
97
"time"
8+
9+
. "gopkg.in/bluesuncorp/assert.v1"
1010
)
1111

1212
// NOTES:
@@ -110,116 +110,6 @@ type TestSlice struct {
110110

111111
var validate = New(Config{TagName: "validate", ValidationFuncs: BakedInValidators})
112112

113-
func IsEqual(t *testing.T, val1, val2 interface{}) bool {
114-
v1 := reflect.ValueOf(val1)
115-
v2 := reflect.ValueOf(val2)
116-
117-
if v1.Kind() == reflect.Ptr {
118-
v1 = v1.Elem()
119-
}
120-
121-
if v2.Kind() == reflect.Ptr {
122-
v2 = v2.Elem()
123-
}
124-
125-
if !v1.IsValid() && !v2.IsValid() {
126-
return true
127-
}
128-
129-
switch v1.Kind() {
130-
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
131-
if v1.IsNil() {
132-
v1 = reflect.ValueOf(nil)
133-
}
134-
}
135-
136-
switch v2.Kind() {
137-
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
138-
if v2.IsNil() {
139-
v2 = reflect.ValueOf(nil)
140-
}
141-
}
142-
143-
v1Underlying := reflect.Zero(reflect.TypeOf(v1)).Interface()
144-
v2Underlying := reflect.Zero(reflect.TypeOf(v2)).Interface()
145-
146-
if v1 == v1Underlying {
147-
if v2 == v2Underlying {
148-
goto CASE4
149-
} else {
150-
goto CASE3
151-
}
152-
} else {
153-
if v2 == v2Underlying {
154-
goto CASE2
155-
} else {
156-
goto CASE1
157-
}
158-
}
159-
160-
CASE1:
161-
// fmt.Println("CASE 1")
162-
return reflect.DeepEqual(v1.Interface(), v2.Interface())
163-
CASE2:
164-
// fmt.Println("CASE 2")
165-
return reflect.DeepEqual(v1.Interface(), v2)
166-
CASE3:
167-
// fmt.Println("CASE 3")
168-
return reflect.DeepEqual(v1, v2.Interface())
169-
CASE4:
170-
// fmt.Println("CASE 4")
171-
return reflect.DeepEqual(v1, v2)
172-
}
173-
174-
func Equal(t *testing.T, val1, val2 interface{}) {
175-
EqualSkip(t, 2, val1, val2)
176-
}
177-
178-
func EqualSkip(t *testing.T, skip int, val1, val2 interface{}) {
179-
180-
if !IsEqual(t, val1, val2) {
181-
182-
_, file, line, _ := runtime.Caller(skip)
183-
fmt.Printf("%s:%d %v does not equal %v\n", path.Base(file), line, val1, val2)
184-
t.FailNow()
185-
}
186-
}
187-
188-
func NotEqual(t *testing.T, val1, val2 interface{}) {
189-
NotEqualSkip(t, 2, val1, val2)
190-
}
191-
192-
func NotEqualSkip(t *testing.T, skip int, val1, val2 interface{}) {
193-
194-
if IsEqual(t, val1, val2) {
195-
_, file, line, _ := runtime.Caller(skip)
196-
fmt.Printf("%s:%d %v should not be equal %v\n", path.Base(file), line, val1, val2)
197-
t.FailNow()
198-
}
199-
}
200-
201-
func PanicMatches(t *testing.T, fn func(), matches string) {
202-
PanicMatchesSkip(t, 2, fn, matches)
203-
}
204-
205-
func PanicMatchesSkip(t *testing.T, skip int, fn func(), matches string) {
206-
207-
_, file, line, _ := runtime.Caller(skip)
208-
209-
defer func() {
210-
if r := recover(); r != nil {
211-
err := fmt.Sprintf("%s", r)
212-
213-
if err != matches {
214-
fmt.Printf("%s:%d Panic... expected [%s] received [%s]", path.Base(file), line, matches, err)
215-
t.FailNow()
216-
}
217-
}
218-
}()
219-
220-
fn()
221-
}
222-
223113
func AssertError(t *testing.T, errs ValidationErrors, key, field, expectedTag string) {
224114

225115
val, ok := errs[key]
@@ -864,11 +754,11 @@ func TestSSNValidation(t *testing.T) {
864754
errs := validate.Field(test.param, "ssn")
865755

866756
if test.expected == true {
867-
if !IsEqual(t, errs, nil) {
757+
if !IsEqual(errs, nil) {
868758
t.Fatalf("Index: %d SSN failed Error: %s", i, errs)
869759
}
870760
} else {
871-
if IsEqual(t, errs, nil) {
761+
if IsEqual(errs, nil) {
872762
t.Fatalf("Index: %d SSN failed Error: %s", i, errs)
873763
} else {
874764
val := errs[""]
@@ -898,11 +788,11 @@ func TestLongitudeValidation(t *testing.T) {
898788
errs := validate.Field(test.param, "longitude")
899789

900790
if test.expected == true {
901-
if !IsEqual(t, errs, nil) {
791+
if !IsEqual(errs, nil) {
902792
t.Fatalf("Index: %d Longitude failed Error: %s", i, errs)
903793
}
904794
} else {
905-
if IsEqual(t, errs, nil) {
795+
if IsEqual(errs, nil) {
906796
t.Fatalf("Index: %d Longitude failed Error: %s", i, errs)
907797
} else {
908798
val := errs[""]
@@ -932,11 +822,11 @@ func TestLatitudeValidation(t *testing.T) {
932822
errs := validate.Field(test.param, "latitude")
933823

934824
if test.expected == true {
935-
if !IsEqual(t, errs, nil) {
825+
if !IsEqual(errs, nil) {
936826
t.Fatalf("Index: %d Latitude failed Error: %s", i, errs)
937827
}
938828
} else {
939-
if IsEqual(t, errs, nil) {
829+
if IsEqual(errs, nil) {
940830
t.Fatalf("Index: %d Latitude failed Error: %s", i, errs)
941831
} else {
942832
val := errs[""]
@@ -972,11 +862,11 @@ func TestDataURIValidation(t *testing.T) {
972862
errs := validate.Field(test.param, "datauri")
973863

974864
if test.expected == true {
975-
if !IsEqual(t, errs, nil) {
865+
if !IsEqual(errs, nil) {
976866
t.Fatalf("Index: %d DataURI failed Error: %s", i, errs)
977867
}
978868
} else {
979-
if IsEqual(t, errs, nil) {
869+
if IsEqual(errs, nil) {
980870
t.Fatalf("Index: %d DataURI failed Error: %s", i, errs)
981871
} else {
982872
val := errs[""]
@@ -1010,11 +900,11 @@ func TestMultibyteValidation(t *testing.T) {
1010900
errs := validate.Field(test.param, "multibyte")
1011901

1012902
if test.expected == true {
1013-
if !IsEqual(t, errs, nil) {
903+
if !IsEqual(errs, nil) {
1014904
t.Fatalf("Index: %d Multibyte failed Error: %s", i, errs)
1015905
}
1016906
} else {
1017-
if IsEqual(t, errs, nil) {
907+
if IsEqual(errs, nil) {
1018908
t.Fatalf("Index: %d Multibyte failed Error: %s", i, errs)
1019909
} else {
1020910
val := errs[""]
@@ -1049,11 +939,11 @@ func TestPrintableASCIIValidation(t *testing.T) {
1049939
errs := validate.Field(test.param, "printascii")
1050940

1051941
if test.expected == true {
1052-
if !IsEqual(t, errs, nil) {
942+
if !IsEqual(errs, nil) {
1053943
t.Fatalf("Index: %d Printable ASCII failed Error: %s", i, errs)
1054944
}
1055945
} else {
1056-
if IsEqual(t, errs, nil) {
946+
if IsEqual(errs, nil) {
1057947
t.Fatalf("Index: %d Printable ASCII failed Error: %s", i, errs)
1058948
} else {
1059949
val := errs[""]
@@ -1087,11 +977,11 @@ func TestASCIIValidation(t *testing.T) {
1087977
errs := validate.Field(test.param, "ascii")
1088978

1089979
if test.expected == true {
1090-
if !IsEqual(t, errs, nil) {
980+
if !IsEqual(errs, nil) {
1091981
t.Fatalf("Index: %d ASCII failed Error: %s", i, errs)
1092982
}
1093983
} else {
1094-
if IsEqual(t, errs, nil) {
984+
if IsEqual(errs, nil) {
1095985
t.Fatalf("Index: %d ASCII failed Error: %s", i, errs)
1096986
} else {
1097987
val := errs[""]
@@ -1122,11 +1012,11 @@ func TestUUID5Validation(t *testing.T) {
11221012
errs := validate.Field(test.param, "uuid5")
11231013

11241014
if test.expected == true {
1125-
if !IsEqual(t, errs, nil) {
1015+
if !IsEqual(errs, nil) {
11261016
t.Fatalf("Index: %d UUID5 failed Error: %s", i, errs)
11271017
}
11281018
} else {
1129-
if IsEqual(t, errs, nil) {
1019+
if IsEqual(errs, nil) {
11301020
t.Fatalf("Index: %d UUID5 failed Error: %s", i, errs)
11311021
} else {
11321022
val := errs[""]
@@ -1156,11 +1046,11 @@ func TestUUID4Validation(t *testing.T) {
11561046
errs := validate.Field(test.param, "uuid4")
11571047

11581048
if test.expected == true {
1159-
if !IsEqual(t, errs, nil) {
1049+
if !IsEqual(errs, nil) {
11601050
t.Fatalf("Index: %d UUID4 failed Error: %s", i, errs)
11611051
}
11621052
} else {
1163-
if IsEqual(t, errs, nil) {
1053+
if IsEqual(errs, nil) {
11641054
t.Fatalf("Index: %d UUID4 failed Error: %s", i, errs)
11651055
} else {
11661056
val := errs[""]
@@ -1189,11 +1079,11 @@ func TestUUID3Validation(t *testing.T) {
11891079
errs := validate.Field(test.param, "uuid3")
11901080

11911081
if test.expected == true {
1192-
if !IsEqual(t, errs, nil) {
1082+
if !IsEqual(errs, nil) {
11931083
t.Fatalf("Index: %d UUID3 failed Error: %s", i, errs)
11941084
}
11951085
} else {
1196-
if IsEqual(t, errs, nil) {
1086+
if IsEqual(errs, nil) {
11971087
t.Fatalf("Index: %d UUID3 failed Error: %s", i, errs)
11981088
} else {
11991089
val := errs[""]
@@ -1225,11 +1115,11 @@ func TestUUIDValidation(t *testing.T) {
12251115
errs := validate.Field(test.param, "uuid")
12261116

12271117
if test.expected == true {
1228-
if !IsEqual(t, errs, nil) {
1118+
if !IsEqual(errs, nil) {
12291119
t.Fatalf("Index: %d UUID failed Error: %s", i, errs)
12301120
}
12311121
} else {
1232-
if IsEqual(t, errs, nil) {
1122+
if IsEqual(errs, nil) {
12331123
t.Fatalf("Index: %d UUID failed Error: %s", i, errs)
12341124
} else {
12351125
val := errs[""]
@@ -1263,11 +1153,11 @@ func TestISBNValidation(t *testing.T) {
12631153
errs := validate.Field(test.param, "isbn")
12641154

12651155
if test.expected == true {
1266-
if !IsEqual(t, errs, nil) {
1156+
if !IsEqual(errs, nil) {
12671157
t.Fatalf("Index: %d ISBN failed Error: %s", i, errs)
12681158
}
12691159
} else {
1270-
if IsEqual(t, errs, nil) {
1160+
if IsEqual(errs, nil) {
12711161
t.Fatalf("Index: %d ISBN failed Error: %s", i, errs)
12721162
} else {
12731163
val := errs[""]
@@ -1300,11 +1190,11 @@ func TestISBN13Validation(t *testing.T) {
13001190
errs := validate.Field(test.param, "isbn13")
13011191

13021192
if test.expected == true {
1303-
if !IsEqual(t, errs, nil) {
1193+
if !IsEqual(errs, nil) {
13041194
t.Fatalf("Index: %d ISBN13 failed Error: %s", i, errs)
13051195
}
13061196
} else {
1307-
if IsEqual(t, errs, nil) {
1197+
if IsEqual(errs, nil) {
13081198
t.Fatalf("Index: %d ISBN13 failed Error: %s", i, errs)
13091199
} else {
13101200
val := errs[""]
@@ -1338,11 +1228,11 @@ func TestISBN10Validation(t *testing.T) {
13381228
errs := validate.Field(test.param, "isbn10")
13391229

13401230
if test.expected == true {
1341-
if !IsEqual(t, errs, nil) {
1231+
if !IsEqual(errs, nil) {
13421232
t.Fatalf("Index: %d ISBN10 failed Error: %s", i, errs)
13431233
}
13441234
} else {
1345-
if IsEqual(t, errs, nil) {
1235+
if IsEqual(errs, nil) {
13461236
t.Fatalf("Index: %d ISBN10 failed Error: %s", i, errs)
13471237
} else {
13481238
val := errs[""]
@@ -2702,11 +2592,11 @@ func TestUrl(t *testing.T) {
27022592
errs := validate.Field(test.param, "url")
27032593

27042594
if test.expected == true {
2705-
if !IsEqual(t, errs, nil) {
2595+
if !IsEqual(errs, nil) {
27062596
t.Fatalf("Index: %d URL failed Error: %s", i, errs)
27072597
}
27082598
} else {
2709-
if IsEqual(t, errs, nil) {
2599+
if IsEqual(errs, nil) {
27102600
t.Fatalf("Index: %d URL failed Error: %s", i, errs)
27112601
} else {
27122602
val := errs[""]
@@ -2766,11 +2656,11 @@ func TestUri(t *testing.T) {
27662656
errs := validate.Field(test.param, "uri")
27672657

27682658
if test.expected == true {
2769-
if !IsEqual(t, errs, nil) {
2659+
if !IsEqual(errs, nil) {
27702660
t.Fatalf("Index: %d URI failed Error: %s", i, errs)
27712661
}
27722662
} else {
2773-
if IsEqual(t, errs, nil) {
2663+
if IsEqual(errs, nil) {
27742664
t.Fatalf("Index: %d URI failed Error: %s", i, errs)
27752665
} else {
27762666
val := errs[""]

0 commit comments

Comments
 (0)