|
| 1 | +package assert |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +type call struct { |
| 9 | + name string |
| 10 | + args []interface{} |
| 11 | +} |
| 12 | + |
| 13 | +// fakeT is a fake implementation of TestingT. |
| 14 | +// It records calls to its methods. |
| 15 | +// Its methods are not safe for concurrent use. |
| 16 | +type fakeT struct { |
| 17 | + calls []call |
| 18 | +} |
| 19 | + |
| 20 | +func (f *fakeT) Errorf(format string, args ...interface{}) { |
| 21 | + f.calls = append(f.calls, call{ |
| 22 | + name: "Errorf", |
| 23 | + args: append([]interface{}{format}, args...), |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +func (f *fakeT) Helper() { |
| 28 | + f.calls = append(f.calls, call{name: "Helper"}) |
| 29 | +} |
| 30 | + |
| 31 | +func TestEqual(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + giveWant interface{} |
| 35 | + giveGot interface{} |
| 36 | + giveMsgAndArgs []interface{} |
| 37 | + want []call |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "equal", |
| 41 | + giveWant: 1, |
| 42 | + giveGot: 1, |
| 43 | + want: nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "not equal shallow", |
| 47 | + giveWant: 1, |
| 48 | + giveGot: 2, |
| 49 | + want: []call{ |
| 50 | + {name: "Helper"}, |
| 51 | + {name: "Errorf", args: []interface{}{"not equal: want: 1, got: 2"}}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "not equal deep", |
| 56 | + giveWant: map[string]interface{}{"foo": struct{ bar string }{"baz"}}, |
| 57 | + giveGot: map[string]interface{}{"foo": struct{ bar string }{"foobar"}}, |
| 58 | + want: []call{ |
| 59 | + {name: "Helper"}, |
| 60 | + {name: "Errorf", args: []interface{}{"not equal: want: map[foo:{bar:baz}], got: map[foo:{bar:foobar}]"}}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "with message", |
| 65 | + giveWant: 1, |
| 66 | + giveGot: 2, |
| 67 | + giveMsgAndArgs: []interface{}{"user message"}, |
| 68 | + want: []call{ |
| 69 | + {name: "Helper"}, |
| 70 | + {name: "Errorf", args: []interface{}{"not equal: want: 1, got: 2: user message"}}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "with message and args", |
| 75 | + giveWant: 1, |
| 76 | + giveGot: 2, |
| 77 | + giveMsgAndArgs: []interface{}{"user message: %d %s", 1, "arg2"}, |
| 78 | + want: []call{ |
| 79 | + {name: "Helper"}, |
| 80 | + {name: "Errorf", args: []interface{}{"not equal: want: 1, got: 2: user message: 1 arg2"}}, |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "only args", |
| 85 | + giveWant: 1, |
| 86 | + giveGot: 2, |
| 87 | + giveMsgAndArgs: []interface{}{1, "arg2"}, |
| 88 | + want: []call{ |
| 89 | + {name: "Helper"}, |
| 90 | + {name: "Errorf", args: []interface{}{"not equal: want: 1, got: 2: [1 arg2]"}}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + var f fakeT |
| 97 | + Equal(&f, tt.giveWant, tt.giveGot, tt.giveMsgAndArgs...) |
| 98 | + // Since we're asserting ourselves it might be possible to introduce a subtle bug. |
| 99 | + // However, the code is straightforward so it's not a big deal. |
| 100 | + Equal(t, tt.want, f.calls) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestNoError(t *testing.T) { |
| 106 | + tests := []struct { |
| 107 | + name string |
| 108 | + giveErr error |
| 109 | + giveMsgAndArgs []interface{} |
| 110 | + want []call |
| 111 | + }{ |
| 112 | + { |
| 113 | + name: "no error", |
| 114 | + giveErr: nil, |
| 115 | + want: nil, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "with error", |
| 119 | + giveErr: errors.New("foo"), |
| 120 | + want: []call{ |
| 121 | + {name: "Helper"}, |
| 122 | + {name: "Errorf", args: []interface{}{"unexpected error: foo"}}, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "with message", |
| 127 | + giveErr: errors.New("foo"), |
| 128 | + giveMsgAndArgs: []interface{}{"user message"}, |
| 129 | + want: []call{ |
| 130 | + {name: "Helper"}, |
| 131 | + {name: "Errorf", args: []interface{}{"unexpected error: foo: user message"}}, |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "with message and args", |
| 136 | + giveErr: errors.New("foo"), |
| 137 | + giveMsgAndArgs: []interface{}{"user message: %d %s", 1, "arg2"}, |
| 138 | + want: []call{ |
| 139 | + {name: "Helper"}, |
| 140 | + {name: "Errorf", args: []interface{}{"unexpected error: foo: user message: 1 arg2"}}, |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "only args", |
| 145 | + giveErr: errors.New("foo"), |
| 146 | + giveMsgAndArgs: []interface{}{1, "arg2"}, |
| 147 | + want: []call{ |
| 148 | + {name: "Helper"}, |
| 149 | + {name: "Errorf", args: []interface{}{"unexpected error: foo: [1 arg2]"}}, |
| 150 | + }, |
| 151 | + }, |
| 152 | + } |
| 153 | + for _, tt := range tests { |
| 154 | + t.Run(tt.name, func(t *testing.T) { |
| 155 | + var f fakeT |
| 156 | + NoError(&f, tt.giveErr, tt.giveMsgAndArgs...) |
| 157 | + Equal(t, tt.want, f.calls) |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments