| 1 | //go:build demo |
| 2 | |
| 3 | // Package demo holds a deliberately failing test used to capture check's |
| 4 | // failure output (dump + diff) for the README. It is excluded from the |
| 5 | // normal build/test/lint by both the "demo" build tag and living under |
| 6 | // testdata/ (which "go build ./..." and golangci-lint skip by convention). |
| 7 | // |
| 8 | // Run it directly to see (and re-capture) the output: |
| 9 | // |
| 10 | // go test -tags demo -v ./testdata/demo/ |
| 11 | package demo |
| 12 | |
| 13 | import ( |
| 14 | "testing" |
| 15 | |
| 16 | "github.com/powerman/check" |
| 17 | ) |
| 18 | |
| 19 | // TestMain enables the pass/fail/todo statistics line, so the demo output |
| 20 | // also shows what check.TestMain adds beyond plain go test. |
| 21 | func TestMain(m *testing.M) { check.TestMain(m) } |
| 22 | |
| 23 | // Order is a stand-in for a real domain type. Customer/Items match between |
| 24 | // got/want on purpose - the point of the demo is that the Diff singles out |
| 25 | // the one field that's actually wrong instead of forcing you to eyeball two |
| 26 | // full dumps for what changed. |
| 27 | type Order struct { |
| 28 | ID string |
| 29 | Customer string |
| 30 | Total int |
| 31 | Items []string |
| 32 | } |
| 33 | |
| 34 | func TestDemoFailure(tt *testing.T) { |
| 35 | t := check.New(tt) |
| 36 | |
| 37 | got := Order{ID: "A-1", Customer: "Ann", Total: 42, Items: []string{"pen", "cup"}} |
| 38 | want := Order{ID: "A-1", Customer: "Ann", Total: 40, Items: []string{"pen", "cup"}} |
| 39 | |
| 40 | t.Equal(got.ID, want.ID, "id should be unchanged") // Passes. |
| 41 | t.DeepEqual(got, want, "order total should match after checkout") |
| 42 | } |