File: README.md

package info (click to toggle)
golang-github-gomagedon-expectate 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 104 kB
  • sloc: makefile: 2
file content (313 lines) | stat: -rw-r--r-- 6,534 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
---
# Expectate: A lightweight testing utility for golang
---

# Quick Start

### Install:

```
go get github.com/gomagedon/expectate
```

```golang
package mylibrary_test

import (
  "testing"
  "github.com/gomagedon/expectate"

  "github.com/foo/mylibrary"
)

func TestProject(t *testing.T) {
  expect := expectate.Expect(t)

  expect(mylibrary.HelloWorld()).ToBe("Hello world!\n")
}
```

## Test Equality of Two Structs

```golang

type Person struct {
  Name string
  Age int
  Occupation string
}

func TestPeopleAreTheSame(t *testing.T) {
  expect := expectate.Expect(t)
  person1 := Person{
    Name: "John Doe",
    Age: 31,
    Occupation: "Electrician",
  }
  person2 := Person{
    Name: "John Smith",
    Age: 31,
    Occupation: "Electrician",
  }

  expect(person1).ToEqual(person2)
}

```

Output:

```
--- FAIL: TestPeopleAreTheSame
    expect.go:46:   expectate_test.Person{
        -       Name:       "John Smith",
        +       Name:       "John Doe",
                Age:        31,
                Occupation: "Electrician",
          }
```

Note that expectate uses `google/go-cmp` for testing strict equality. Custom cmp options are not supported for the `ToEqual()` method, but you can always call the cmp library directly.

# Simple API

Expectate uses only 4 methods!

- ### [`Expect()`](#expect) <br>
- ### [`expect().ToBe()`](#tobe)
- ### [`expect().ToEqual()`](#toequal)
- ### [`expect().NotToBe()`](#nottobe)
- ### [`expect().NotToEqual()`](#nottoequal)

---

<h2 id="expect"><code>Expect()</code></h2>

The `Expect()` method is at the top level of the `expectate` package. It takes `*testing.T` as a parameter and returns an `ExpectorFunc` type.

Here's an example:

```golang
func TestSomething(t *testing.T) {
  expect := expectate.Expect(t)

  expect(something).ToBe(somethingElse)
  // ...some other expectations
}
```

Alternatively:

```golang
func TestSomething(t *testing.T) {
  expectate.Expect(t)(something).ToBe(somethingElse)
  // ...some other expectations
}
```

---

<h2 id="tobe"><code>expect().ToBe()</code></h2>

The `expect().ToBe()` method exists on the `Expector` type (which is what `expect()` returns). It takes any value and performs a simple equality check with that value and the initial value passed to `expect()`. If the two values are equal, the test passes. If not, it calls `t.Fatal()` with a generic error message.

#### Here's an example of a passing test:

```golang
func TestFooIsFoo(t *testing.T) {
  expect := expectate.Expect(t)

  expect("foo").ToBe("foo")
}
```

Result:

```
--- PASS (ok)
```

#### Here's an example of a failing test:

```golang
func TestFooIsBar(t *testing.T) {
  expect := expectate.Expect(t)

  expect("foo").ToBe("bar")
}
```

Result:

```
--- FAIL: TestFooIsBar
    expect.go:34: foo is not bar
```

---

<h2 id="toequal"><code>expect().ToEqual()</code></h2>

The `expect().ToEqual()` method exists on the `Expector` type (which is what `expect()` returns). It takes any value and performs a _deep_ equality check using [go-cmp](https://github.com/google/go-cmp) with that value and the initial value passed to `expect()`. If the two values are equal, the test passes. If not, it calls `t.Fatal()` with a go-cmp diff.

#### Here's an example of a passing test:

```golang
func TestPost1EqualsPost2(t *testing.T) {
  expect := expectate.Expect(t)

  post1 := Post{
    Title:   "Post",
    Content: "Content of Post",
    Likes:   2,
  }
  post2 := Post{
    Title:   "Post",
    Content: "Content of Post",
    Likes:   2,
  }

  expect(post1).ToEqual(post2)
}
```

Result:

```
--- PASS (ok)
```

#### Here's an example of a failing test:

```golang
func TestPost1EqualsPost2(t *testing.T) {
  expect := expectate.Expect(t)

  post1 := Post{
    Title:   "Post 1",
    Content: "Content of Post 1",
    Likes:   2,
  }
  post2 := Post{
    Title:   "Post 2",
    Content: "Content of Post 2",
    Likes:   1,
  }

  expect(post1).ToEqual(post2)
}
```

Output:

```
--- FAIL: TestPost1EqualsPost2
    expect.go:43:   main.Post{
        -       Title:   "Post 2",
        +       Title:   "Post 1",
        -       Content: "Content of Post 2",
        +       Content: "Content of Post 1",
        -       Likes:   1,
        +       Likes:   2,
          }
```

---

<h2 id="nottobe"><code>expect().NotToBe()</code></h2>

The `expect().NotToBe()` method exists on the `Expector` type (which is what `expect()` returns). It has the opposite behavior of `expect().ToBe()` It takes any value and performs a simple inequality check with that value and the initial value passed to `expect()`. If the two values are not equal, the test passes. If they are, it calls `t.Fatal()` with a generic error message.

#### Here's an example of a passing test:

```golang
func TestFooIsNotBar(t *testing.T) {
  expect := expectate.Expect(t)

  expect("foo").NotToBe("bar")
}
```

Result:

```
--- PASS (ok)
```

#### Here's an example of a failing test:

```golang
func TestFooIsNotFoo(t *testing.T) {
  expect := expectate.Expect(t)

  expect("foo").NotToBe("foo")
}
```

Result:

```
--- FAIL: TestFooIsBar
    expect.go:34: foo is foo
```

---

<h2 id="nottoequal"><code>expect().NotToEqual()</code></h2>

The `expect().NotToEqual()` method exists on the `Expector` type (which is what `expect()` returns). It has the opposite behavior as `expect().ToEqual()`. It takes any value and performs a _deep_ inequality check using [go-cmp](https://github.com/google/go-cmp) with that value and the initial value passed to `expect()`. If the two values are not equal, the test passes. If they are, it calls `t.Fatal()` with a generic error message.

#### Here's an example of a passing test:

```golang
func TestPost1_DoesNotEqual_Post2(t *testing.T) {
  expect := expectate.Expect(t)

  post1 := Post{
    Title:   "Post 1",
    Content: "Content of Post 1",
    Likes:   2,
  }
  post2 := Post{
    Title:   "Post 2",
    Content: "Content of Post 2",
    Likes:   1,
  }

  expect(post1).NotToEqual(post2)
}
```

Result:

```
--- PASS (ok)
```

#### Here's an example of a failing test:

```golang
func TestPost1_DoesNotEqual_Post2(t *testing.T) {
  expect := expectate.Expect(t)

  post1 := Post{
    Title:   "Post",
    Content: "Content of Post",
    Likes:   2,
  }
  post2 := Post{
    Title:   "Post",
    Content: "Content of Post",
    Likes:   2,
  }

  expect(post1).NotToEqual(post2)
}
```

Output:

```
--- FAIL: TestPost1_DoesNotEqual_Post2
    expect.go:57: {Post 1 Content of Post 1 2} equals {Post 1 Content of Post 1 2}
```