Codebase list golang-github-matryer-is / d689ad47-508a-4609-84cc-d27d57a17bb7/upstream
Import upstream version 1.4.0+git20220516.1.02e4121 Debian Janitor 1 year, 6 months ago
5 changed file(s) with 76 addition(s) and 62 deletion(s). Raw diff Collapse all Expand all
0 github: matryer
11 Professional lightweight testing mini-framework for Go.
22
33 * Easy to write and read
4 * [Beautifully simple API](https://godoc.org/github.com/matryer/is) with everything you need: `is.Equal`, `is.True`, `is.NoErr`, and `is.Fail`
4 * [Beautifully simple API](https://pkg.go.dev/github.com/matryer/is) with everything you need: `is.Equal`, `is.True`, `is.NoErr`, and `is.Fail`
55 * Use comments to add descriptions (which show up when tests fail)
66
77 Failures are very easy to read:
1717 func Test(t *testing.T) {
1818
1919 is := is.New(t)
20
20
2121 signedin, err := isSignedIn(ctx)
2222 is.NoErr(err) // isSignedIn error
2323 is.Equal(signedin, true) // must be signed in
24
24
2525 body := readBody(r)
2626 is.True(strings.Contains(body, "Hi there"))
27
27
2828 }
2929 ```
3030
3131 ## Color
3232
33 To turn off the colors, run `go test` with the `-nocolor` flag, or with the env var `IS_NO_COLOR=true`.
33 To turn off the colors, run `go test` with the `-nocolor` flag,
34 or with the env var [`NO_COLOR` (with any value)](https://no-color.org).
3435
3536 ```
3637 go test -nocolor
3738 ```
3839
3940 ```
40 IS_NO_COLOR=true go test
41 NO_COLOR=1 go test
4142 ```
3232 // the purpose of decorating log messages.
3333 const maxStackLen = 50
3434
35 var reIsSourceFile = regexp.MustCompile("is(-1.7)?\\.go$")
35 var reIsSourceFile = regexp.MustCompile(`is(-1.7)?\.go$`)
3636
3737 func (is *I) callerinfo() (path string, line int, ok bool) {
3838 var pc [maxStackLen]uintptr
+55
-42
is.go less more
2121 // the helper methods:
2222 //
2323 // func Test(t *testing.T) {
24 //
2524 // // always start tests with this
2625 // is := is.New(t)
2726 //
3130 //
3231 // body := readBody(r)
3332 // is.True(strings.Contains(body, "Hi there"))
34 //
3533 // }
3634 package is
3735
4442 "os"
4543 "path/filepath"
4644 "reflect"
45 "strconv"
4746 "strings"
48 "testing"
4947 )
5048
5149 // T reports when failures occur.
7472 var noColorFlag bool
7573
7674 func init() {
77 envNoColor := os.Getenv("IS_NO_COLOR") == "true"
75 var envNoColor bool
76
77 // prefer https://no-color.org (with any value)
78 if _, ok := os.LookupEnv("NO_COLOR"); ok {
79 envNoColor = true
80 }
81
82 if v, ok := os.LookupEnv("IS_NO_COLOR"); ok {
83 if b, err := strconv.ParseBool(v); err == nil {
84 envNoColor = b
85 }
86 }
87
7888 flag.BoolVar(&noColorFlag, "nocolor", envNoColor, "turns off colors")
7989 }
8090
170180 // // TODO: test
171181 // })
172182 // }
173 func (is *I) New(t *testing.T) *I {
183 func (is *I) New(t T) *I {
174184 return New(t)
175185 }
176186
185195 // // TODO: test
186196 // })
187197 // }
188 func (is *I) NewRelaxed(t *testing.T) *I {
198 func (is *I) NewRelaxed(t T) *I {
189199 return NewRelaxed(t)
190200 }
191201
257267 s := bufio.NewScanner(f)
258268 i := 1
259269 for s.Scan() {
260 if i == line {
261 text := s.Text()
262 commentI := strings.Index(text, "// ")
263 if commentI == -1 {
264 return "", false // no comment
265 }
266 text = text[commentI+2:]
267 text = strings.TrimSpace(text)
268 return text, true
269 }
270 i++
270 if i != line {
271 i++
272 continue
273 }
274
275 text := s.Text()
276 commentI := strings.Index(text, "// ")
277 if commentI == -1 {
278 return "", false // no comment
279 }
280 text = text[commentI+2:]
281 text = strings.TrimSpace(text)
282 return text, true
271283 }
272284 return "", false
273285 }
283295 s := bufio.NewScanner(f)
284296 i := 1
285297 for s.Scan() {
286 if i == line {
287 text := s.Text()
288 braceI := strings.Index(text, "(")
289 if braceI == -1 {
290 return "", false
298 if i != line {
299 i++
300 continue
301 }
302 text := s.Text()
303 braceI := strings.Index(text, "(")
304 if braceI == -1 {
305 return "", false
306 }
307 text = text[braceI+1:]
308 cs := bufio.NewScanner(strings.NewReader(text))
309 cs.Split(bufio.ScanBytes)
310 j := 0
311 c := 1
312 for cs.Scan() {
313 switch cs.Text() {
314 case ")":
315 c--
316 case "(":
317 c++
291318 }
292 text = text[braceI+1:]
293 cs := bufio.NewScanner(strings.NewReader(text))
294 cs.Split(bufio.ScanBytes)
295 j := 0
296 c := 1
297 for cs.Scan() {
298 switch cs.Text() {
299 case ")":
300 c--
301 case "(":
302 c++
303 }
304 if c == 0 {
305 break
306 }
307 j++
319 if c == 0 {
320 break
308321 }
309 text = text[:j]
310 return text, true
311 }
312 i++
322 j++
323 }
324 text = text[:j]
325 return text, true
313326 }
314327 return "", false
315328 }
2727 {
2828 N: "Equal(1, 1)",
2929 F: func(is *I) {
30 is.Equal(1, 1) // 1 doesn't equal 2
30 is.Equal(1, 1) // 1 doesn't equal 1
3131 },
3232 Fail: ``,
3333 },
5656 {
5757 N: "Equal(false, false)",
5858 F: func(is *I) {
59 is.Equal(false, false) // nil doesn't equal 2
59 is.Equal(false, false) // false doesn't equal false
6060 },
6161 Fail: ``,
6262 },
7777 Fail: `map[value:1] != map[value:2] // maps`,
7878 },
7979 {
80 N: "Equal(true, map2)",
81 F: func(is *I) {
82 m1 := map[string]interface{}{"value": 1}
83 m2 := map[string]interface{}{"value": 2}
84 is.Equal(m1, m2) // maps
85 },
86 Fail: `map[value:1] != map[value:2] // maps`,
80 N: "Equal(true, map)",
81 F: func(is *I) {
82 m := map[string]interface{}{"value": 2}
83 is.Equal(true, m) // maps
84 },
85 Fail: `bool(true) != map[string]interface {}(map[value:2]) // maps`,
8786 },
8887 {
8988 N: "Equal(slice1, slice2)",
110109 s2 := []string{"one", "two", "three", "four"}
111110 is.Equal(s1, s2) // nil slice
112111 },
113 Fail: ` // nil slice`,
112 Fail: `<nil> != []string([one two three four]) // nil slice`,
114113 },
115114 {
116115 N: "Equal(nil, nil)",
128127 m2 := map[string]string{}
129128 is.Equal(m1, m2) // nil map
130129 },
131 Fail: ` // nil map`,
130 Fail: `<nil> != map[string]string(map[]) // nil map`,
132131 },
133132 {
134133 N: "Equal(nil, nil)",
146145 s2 := "0.2 VAT"
147146 is.Equal(s1, s2) // strings
148147 },
149 Fail: ` // strings`,
148 Fail: `20% VAT != 0.2 VAT // strings`,
150149 },
151150
152151 // Fail
176175 Fail: "err: nope // method shouldn't return error",
177176 },
178177
179 // OK
178 // True
180179 {
181180 N: "True(1 == 2)",
182181 F: func(is *I) {