Debian Codegraph @

readme/readme_test.go

raw symbols SCIP
1 package readme
2
3 import (
4 "testing"
5
6 "github.com/nbio/st"
7 )
8
9 func TestExample(t *testing.T) {
10 st.Expect(t, "a", "a")
11 st.Reject(t, 42, int64(42))
12
13 st.Assert(t, "t", "t")
14 st.Refute(t, 99, int64(99))
15 }
16
17 func TestTableExample(t *testing.T) {
18 examples := []struct{ a, t string }{
19 {"first", "first"},
20 {"second", "second"},
21 }
22
23 // Pass example index to improve the error message for table-based tests.
24 for i, ex := range examples {
25 st.Expect(t, ex, ex, i)
26 st.Reject(t, ex, &ex, i)
27 }
28
29 // Cannot pass index into Assert or Refute, they fail fast.
30 for _, ex := range examples {
31 st.Assert(t, ex, ex)
32 st.Refute(t, ex, &ex)
33 }
34 }
35
36 // Prints failure output, including the correct line number.
37 func TestFailedExpectationMessages(t *testing.T) {
38 t.Log("Tests purposely fail to demonstrate output")
39 st.Expect(t, 1, 2)
40 st.Reject(t, "same", "same")
41 var typedNil *string
42 st.Expect(t, typedNil, nil) // in Go, a typed nil != nil
43 }
44
45 // Prints failure output, including the correct line number.
46 func TestFailedAssertMessage(t *testing.T) {
47 type chicken struct{}
48 type egg struct{}
49 st.Assert(t, egg{}, chicken{})
50 }
51
52 // Prints failure output, including the correct line number.
53 func TestFailedRefuteMessage(t *testing.T) {
54 st.Reject(t, 42, 7*6)
55 }
56
57 // Prints failure output, including the correct line number and example index.
58 func TestFailedTableMessages(t *testing.T) {
59 table := []struct{ val int }{
60 {0}, {1}, {2},
61 }
62 // Continues if expectation fails
63 for i, example := range table {
64 st.Expect(t, example.val, 1, i)
65 }
66 // Stops when first assertion fails
67 for _, example := range table {
68 st.Assert(t, example.val, 1)
69 }
70 }
71
72 // Allows comparing non-comparable types to prevent panics when comparing slices
73 // or maps.
74 func TestDeeperEquality(t *testing.T) {
75 type testStr string
76 slice1 := []interface{}{"A", 1, []byte("steak sauce")}
77 slice2 := []interface{}{"R", 2, 'd', int64(2)}
78 map1 := map[string]string{"clever": "crafty", "modest": "prim"}
79 map2 := map[string]string{"silk": "scarf", "wool": "sweater"}
80 str1 := "same"
81 str2 := testStr("same")
82
83 st.Expect(t, slice1, slice2)
84 st.Reject(t, slice1, slice1)
85 st.Expect(t, map1, map2)
86 st.Reject(t, map1, map1)
87 st.Expect(t, str1, str2)
88 st.Reject(t, str1, str1)
89 }