Codebase list golang-github-nbio-st / 61d4dfb
Demonstrate deep equality output. Cameron Walters 10 years ago
3 changed file(s) with 96 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
8585 }
8686 }
8787
88 func TestDeeperEquality(t *testing.T) {
89 type testStr string
90 slice1 := []interface{}{"A", 1, []byte("steak sauce")}
91 slice2 := []interface{}{"R", 2, 'd', int64(2)}
92 map1 := map[string]string{"clever": "crafty", "modest": "prim"}
93 map2 := map[string]string{"silk": "scarf", "wool": "sweater"}
94 str1 := "same"
95 str2 := testStr("same")
96
97 st.Expect(t, slice1, slice2)
98 st.Reject(t, slice1, slice1)
99 st.Expect(t, map1, map2)
100 st.Reject(t, map1, map1)
101 st.Expect(t, str1, str2)
102 st.Reject(t, str1, str1)
103 }
88104 ```
89105
90106 ```console
91107 --- FAIL: TestFailedExpectationMessages (0.00 seconds)
92 st.go:31:
93 readme_test.go:38: actual should == expected
94 have (int): 2
95 want (int): 1
96 st.go:40:
97 readme_test.go:39: actual should != expected
98 have (string): same
99 and (string): same
100 st.go:31:
101 readme_test.go:41: actual should == expected
102 have (<nil>): <nil>
103 want (*string): <nil>
108 readme_test.go:38: Tests purposely fail to demonstrate output
109 st.go:41:
110 readme_test.go:39: actual should == expected
111 have: (int) 2
112 want: (int) 1
113 st.go:50:
114 readme_test.go:40: actual should != expected
115 have: (string) same
116 and : (string) same
117 st.go:41:
118 readme_test.go:42: actual should == expected
119 have: (<nil>) <nil>
120 want: (*string) <nil>
104121 --- FAIL: TestFailedAssertMessage (0.00 seconds)
105 st.go:49:
106 readme_test.go:48: actual should == expected
107 have (readme.chicken): {}
108 want (readme.egg): {}
122 st.go:59:
123 readme_test.go:49: actual should == expected
124 have: (readme.chicken) {}
125 want: (readme.egg) {}
109126 --- FAIL: TestFailedRefuteMessage (0.00 seconds)
110 st.go:40:
111 readme_test.go:53: actual should != expected
112 have (int): 42
113 and (int): 42
127 st.go:50:
128 readme_test.go:54: actual should != expected
129 have: (int) 42
130 and : (int) 42
114131 --- FAIL: TestFailedTableMessages (0.00 seconds)
115 st.go:31:
116 readme_test.go:63: actual should == expected
117 0. have (int): 1
118 want (int): 0
119 st.go:31:
120 readme_test.go:63: actual should == expected
121 2. have (int): 1
122 want (int): 2
123 st.go:49:
124 readme_test.go:67: actual should == expected
125 have (int): 1
126 want (int): 0
132 st.go:41:
133 readme_test.go:64: actual should == expected
134 0. have: (int) 1
135 want: (int) 0
136 st.go:41:
137 readme_test.go:64: actual should == expected
138 2. have: (int) 1
139 want: (int) 2
140 st.go:59:
141 readme_test.go:68: actual should == expected
142 have: (int) 1
143 want: (int) 0
144 --- FAIL: TestDeeperEquality (0.00 seconds)
145 st.go:41:
146 readme_test.go:83: actual should == expected
147 have: ([]interface {}) [R 2 100 2]
148 want: ([]interface {}) [A 1 [115 116 101 97 107 32 115 97 117 99 101]]
149 st.go:50:
150 readme_test.go:84: actual should != expected
151 have: ([]interface {}) [A 1 [115 116 101 97 107 32 115 97 117 99 101]]
152 and : ([]interface {}) [A 1 [115 116 101 97 107 32 115 97 117 99 101]]
153 st.go:41:
154 readme_test.go:85: actual should == expected
155 have: (map[string]string) map[silk:scarf wool:sweater]
156 want: (map[string]string) map[clever:crafty modest:prim]
157 st.go:50:
158 readme_test.go:86: actual should != expected
159 have: (map[string]string) map[clever:crafty modest:prim]
160 and : (map[string]string) map[clever:crafty modest:prim]
161 st.go:41:
162 readme_test.go:87: actual should == expected
163 have: (readme.testStr) same
164 want: (string) same
165 st.go:50:
166 readme_test.go:88: actual should != expected
167 have: (string) same
168 and : (string) same
127169 FAIL
128170 exit status 1
129 FAIL github.com/nbio/st/readme 0.010s
171 FAIL github.com/nbio/st/readme 0.012s
130172 ```
131173
132174 See [`package st`](https://godoc.org/github.com/nbio/st) documentation for more detail.
3434
3535 // Prints failure output, including the correct line number.
3636 func TestFailedExpectationMessages(t *testing.T) {
37 t.Log("Tests purposely fail to demonstrate output")
3738 st.Expect(t, 1, 2)
3839 st.Reject(t, "same", "same")
3940 var typedNil *string
6667 st.Assert(t, example.val, 1)
6768 }
6869 }
70
71 // Allows comparing non-comparable types to prevent panics when comparing slices
72 // or maps.
73 func TestDeeperEquality(t *testing.T) {
74 type testStr string
75 slice1 := []interface{}{"A", 1, []byte("steak sauce")}
76 slice2 := []interface{}{"R", 2, 'd', int64(2)}
77 map1 := map[string]string{"clever": "crafty", "modest": "prim"}
78 map2 := map[string]string{"silk": "scarf", "wool": "sweater"}
79 str1 := "same"
80 str2 := testStr("same")
81
82 st.Expect(t, slice1, slice2)
83 st.Reject(t, slice1, slice1)
84 st.Expect(t, map1, map2)
85 st.Reject(t, map1, map1)
86 st.Expect(t, str1, str2)
87 st.Reject(t, str1, str1)
88 }
1212
1313 import (
1414 "fmt"
15 "reflect"
1516 "runtime"
1617 "strings"
17 "reflect"
1818 )
1919
2020 const (