| 1 |
package approvaltests |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/xml" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/approvals/go-approval-tests/reporters" |
| 11 |
) |
| 12 |
|
| 13 |
func TestMain(m *testing.M) { |
| 14 |
r := UseReporter(reporters.NewBeyondCompareReporter()) |
| 15 |
defer r.Close() |
| 16 |
|
| 17 |
os.Exit(m.Run()) |
| 18 |
} |
| 19 |
|
| 20 |
func TestVerifyStringApproval(t *testing.T) { |
| 21 |
r := UseReporter(reporters.NewIntelliJReporter()) |
| 22 |
defer r.Close() |
| 23 |
|
| 24 |
VerifyString(t, "Hello World!") |
| 25 |
} |
| 26 |
|
| 27 |
func TestReporterFromSetup(t *testing.T) { |
| 28 |
VerifyString(t, "Hello World!") |
| 29 |
} |
| 30 |
|
| 31 |
func TestVerifyXMLStruct(t *testing.T) { |
| 32 |
json := struct { |
| 33 |
XMLName xml.Name `xml:"Test"` |
| 34 |
Title string |
| 35 |
Name string |
| 36 |
Age int |
| 37 |
}{ |
| 38 |
Title: "Hello World!", |
| 39 |
Name: "Peter Pan", |
| 40 |
Age: 100, |
| 41 |
} |
| 42 |
|
| 43 |
VerifyXMLStruct(t, json) |
| 44 |
} |
| 45 |
|
| 46 |
func TestVerifyBadXMLStruct(t *testing.T) { |
| 47 |
xml := struct { |
| 48 |
Title string |
| 49 |
}{ |
| 50 |
Title: "Hello World!", |
| 51 |
} |
| 52 |
|
| 53 |
VerifyXMLStruct(t, xml) |
| 54 |
} |
| 55 |
|
| 56 |
func TestVerifyXMLBytes(t *testing.T) { |
| 57 |
xmlb := []byte("<Test><Title>Hello World!</Title><Name>Peter Pan</Name><Age>100</Age></Test>") |
| 58 |
VerifyXMLBytes(t, xmlb) |
| 59 |
} |
| 60 |
|
| 61 |
func TestVerifyBadXMLBytes(t *testing.T) { |
| 62 |
xmlb := []byte("Test></Test>") |
| 63 |
VerifyXMLBytes(t, xmlb) |
| 64 |
} |
| 65 |
|
| 66 |
func TestVerifyJSONStruct(t *testing.T) { |
| 67 |
json := struct { |
| 68 |
Title string |
| 69 |
Name string |
| 70 |
Age int |
| 71 |
}{ |
| 72 |
Title: "Hello World!", |
| 73 |
Name: "Peter Pan", |
| 74 |
Age: 100, |
| 75 |
} |
| 76 |
|
| 77 |
VerifyJSONStruct(t, json) |
| 78 |
} |
| 79 |
|
| 80 |
func TestVerifyJSONBytes(t *testing.T) { |
| 81 |
jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }") |
| 82 |
VerifyJSONBytes(t, jsonb) |
| 83 |
} |
| 84 |
|
| 85 |
func TestVerifyBadJSONBytes(t *testing.T) { |
| 86 |
jsonb := []byte("{ foo: \"bar\", \"age\": 42, \"bark\": \"woof\" }") |
| 87 |
VerifyJSONBytes(t, jsonb) |
| 88 |
} |
| 89 |
|
| 90 |
func TestVerifyMap(t *testing.T) { |
| 91 |
m := map[string]string{ |
| 92 |
"dog": "bark", |
| 93 |
"cat": "meow", |
| 94 |
} |
| 95 |
|
| 96 |
VerifyMap(t, m) |
| 97 |
} |
| 98 |
|
| 99 |
func TestVerifyMapBadMap(t *testing.T) { |
| 100 |
m := "foo" |
| 101 |
VerifyMap(t, m) |
| 102 |
} |
| 103 |
|
| 104 |
func TestVerifyMapEmptyMap(t *testing.T) { |
| 105 |
m := map[string]string{} |
| 106 |
VerifyMap(t, m) |
| 107 |
} |
| 108 |
|
| 109 |
func TestVerifyArray(t *testing.T) { |
| 110 |
xs := []string{"dog", "cat", "bird"} |
| 111 |
VerifyArray(t, xs) |
| 112 |
} |
| 113 |
|
| 114 |
func TestVerifyArrayBadArray(t *testing.T) { |
| 115 |
xs := "string" |
| 116 |
VerifyArray(t, xs) |
| 117 |
} |
| 118 |
|
| 119 |
func TestVerifyArrayEmptyArray(t *testing.T) { |
| 120 |
var xs []string |
| 121 |
VerifyArray(t, xs) |
| 122 |
} |
| 123 |
|
| 124 |
func TestVerifyArrayTransformation(t *testing.T) { |
| 125 |
xs := []string{"Christopher", "Llewellyn"} |
| 126 |
VerifyAll(t, "uppercase", xs, func(x interface{}) string { return fmt.Sprintf("%s => %s", x, strings.ToUpper(x.(string))) }) |
| 127 |
} |
| 128 |
|
| 129 |
func TestVerifyAllCombinationsFor1(t *testing.T) { |
| 130 |
xs := []string{"Christopher", "Llewellyn"} |
| 131 |
VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, xs) |
| 132 |
} |
| 133 |
|
| 134 |
func TestVerifyAllCombinationsForSkipped(t *testing.T) { |
| 135 |
xs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
| 136 |
VerifyAllCombinationsFor1( |
| 137 |
t, |
| 138 |
"skipped divisible by 3", |
| 139 |
func(x interface{}) string { |
| 140 |
if x.(int)%3 == 0 { |
| 141 |
return SkipThisCombination |
| 142 |
} |
| 143 |
return fmt.Sprintf("%v", x) |
| 144 |
}, |
| 145 |
xs) |
| 146 |
} |
| 147 |
|
| 148 |
func TestVerifyAllCombinationsFor2(t *testing.T) { |
| 149 |
xs1 := []string{"Christopher", "Llewellyn"} |
| 150 |
xs2 := []int{0, 1} |
| 151 |
VerifyAllCombinationsFor2( |
| 152 |
t, |
| 153 |
"character at", |
| 154 |
func(s interface{}, i interface{}) string { return fmt.Sprintf("%c", s.(string)[i.(int)]) }, |
| 155 |
xs1, |
| 156 |
xs2) |
| 157 |
} |
| 158 |
|
| 159 |
func TestVerifyAllCombinationsFor9(t *testing.T) { |
| 160 |
xs1 := []string{"Christopher"} |
| 161 |
|
| 162 |
VerifyAllCombinationsFor9( |
| 163 |
t, |
| 164 |
"sum numbers", |
| 165 |
func(s, i2, i3, i4, i5, i6, i7, i8, i9 interface{}) string { |
| 166 |
sum := i2.(int) + i3.(int) + i4.(int) + i5.(int) + i6.(int) + i7.(int) + i8.(int) + i9.(int) |
| 167 |
return fmt.Sprintf("%v[%v]", s, sum) |
| 168 |
}, |
| 169 |
xs1, |
| 170 |
[]int{0, 1}, |
| 171 |
[]int{2, 3}, |
| 172 |
[]int{4, 5}, |
| 173 |
[]int{6, 7}, |
| 174 |
[]int{8, 9}, |
| 175 |
[]int{10, 11}, |
| 176 |
[]int{12, 13}, |
| 177 |
[]int{14, 15}) |
| 178 |
} |