MCPcopy
hub / github.com/google/go-cmp / TestDiff

Function TestDiff

cmp/compare_test.go:124–194  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

122}
123
124func TestDiff(t *testing.T) {
125 var tests []test
126 tests = append(tests, comparerTests()...)
127 tests = append(tests, transformerTests()...)
128 tests = append(tests, reporterTests()...)
129 tests = append(tests, embeddedTests()...)
130 tests = append(tests, methodTests()...)
131 tests = append(tests, cycleTests()...)
132 tests = append(tests, project1Tests()...)
133 tests = append(tests, project2Tests()...)
134 tests = append(tests, project3Tests()...)
135 tests = append(tests, project4Tests()...)
136
137 const goldenFile = "testdata/diffs"
138 gotDiffs := []struct{ Name, Data string }{}
139 wantDiffs := mustParseGolden(goldenFile)
140 for _, tt := range tests {
141 tt := tt
142 t.Run(tt.label, func(t *testing.T) {
143 if !*update {
144 t.Parallel()
145 }
146 var gotDiff, gotPanic string
147 func() {
148 defer func() {
149 if ex := recover(); ex != nil {
150 if s, ok := ex.(string); ok {
151 gotPanic = s
152 } else {
153 panic(ex)
154 }
155 }
156 }()
157 gotDiff = cmp.Diff(tt.x, tt.y, tt.opts...)
158 }()
159
160 switch {
161 case strings.Contains(t.Name(), "#"):
162 panic("unique test name must be provided")
163 case tt.reason == "":
164 panic("reason must be provided")
165 case tt.wantPanic == "":
166 if gotPanic != "" {
167 t.Fatalf("unexpected panic message: %s\nreason: %v", gotPanic, tt.reason)
168 }
169 if *update {
170 if gotDiff != "" {
171 gotDiffs = append(gotDiffs, struct{ Name, Data string }{t.Name(), gotDiff})
172 }
173 } else {
174 wantDiff := wantDiffs[t.Name()]
175 if diff := cmp.Diff(wantDiff, gotDiff); diff != "" {
176 t.Fatalf("Diff:\ngot:\n%s\nwant:\n%s\ndiff: (-want +got)\n%s\nreason: %v", gotDiff, wantDiff, diff, tt.reason)
177 }
178 }
179 gotEqual := gotDiff == ""
180 if gotEqual != tt.wantEqual {
181 t.Fatalf("Equal = %v, want %v\nreason: %v", gotEqual, tt.wantEqual, tt.reason)

Callers

nothing calls this directly

Calls 14

DiffFunction · 0.92
comparerTestsFunction · 0.85
transformerTestsFunction · 0.85
reporterTestsFunction · 0.85
embeddedTestsFunction · 0.85
methodTestsFunction · 0.85
cycleTestsFunction · 0.85
project1TestsFunction · 0.85
project2TestsFunction · 0.85
project3TestsFunction · 0.85
project4TestsFunction · 0.85
mustParseGoldenFunction · 0.85

Tested by

no test coverage detected