|
9 | 9 | "io"
|
10 | 10 | "math"
|
11 | 11 | "os"
|
| 12 | + "path/filepath" |
12 | 13 | "reflect"
|
13 | 14 | "regexp"
|
14 | 15 | "runtime"
|
@@ -688,15 +689,59 @@ func TestContainsNotContains(t *testing.T) {
|
688 | 689 | }
|
689 | 690 | }
|
690 | 691 |
|
691 |
| -func TestContainsFailMessage(t *testing.T) { |
692 |
| - |
| 692 | +func TestContainsNotContainsFailMessage(t *testing.T) { |
693 | 693 | mockT := new(mockTestingT)
|
694 | 694 |
|
695 |
| - Contains(mockT, "Hello World", errors.New("Hello")) |
696 |
| - expectedFail := "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}" |
697 |
| - actualFail := mockT.errorString() |
698 |
| - if !strings.Contains(actualFail, expectedFail) { |
699 |
| - t.Errorf("Contains failure should include %q but was %q", expectedFail, actualFail) |
| 695 | + type nonContainer struct { |
| 696 | + Value string |
| 697 | + } |
| 698 | + |
| 699 | + cases := []struct { |
| 700 | + assertion func(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool |
| 701 | + container interface{} |
| 702 | + instance interface{} |
| 703 | + expected string |
| 704 | + }{ |
| 705 | + { |
| 706 | + assertion: Contains, |
| 707 | + container: "Hello World", |
| 708 | + instance: errors.New("Hello"), |
| 709 | + expected: "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}", |
| 710 | + }, |
| 711 | + { |
| 712 | + assertion: Contains, |
| 713 | + container: map[string]int{"one": 1}, |
| 714 | + instance: "two", |
| 715 | + expected: "map[string]int{\"one\":1} does not contain \"two\"\n", |
| 716 | + }, |
| 717 | + { |
| 718 | + assertion: NotContains, |
| 719 | + container: map[string]int{"one": 1}, |
| 720 | + instance: "one", |
| 721 | + expected: "map[string]int{\"one\":1} should not contain \"one\"", |
| 722 | + }, |
| 723 | + { |
| 724 | + assertion: Contains, |
| 725 | + container: nonContainer{Value: "Hello"}, |
| 726 | + instance: "Hello", |
| 727 | + expected: "assert.nonContainer{Value:\"Hello\"} could not be applied builtin len()\n", |
| 728 | + }, |
| 729 | + { |
| 730 | + assertion: NotContains, |
| 731 | + container: nonContainer{Value: "Hello"}, |
| 732 | + instance: "Hello", |
| 733 | + expected: "assert.nonContainer{Value:\"Hello\"} could not be applied builtin len()\n", |
| 734 | + }, |
| 735 | + } |
| 736 | + for _, c := range cases { |
| 737 | + name := filepath.Base(runtime.FuncForPC(reflect.ValueOf(c.assertion).Pointer()).Name()) |
| 738 | + t.Run(fmt.Sprintf("%v(%T, %T)", name, c.container, c.instance), func(t *testing.T) { |
| 739 | + c.assertion(mockT, c.container, c.instance) |
| 740 | + actualFail := mockT.errorString() |
| 741 | + if !strings.Contains(actualFail, c.expected) { |
| 742 | + t.Errorf("Contains failure should include %q but was %q", c.expected, actualFail) |
| 743 | + } |
| 744 | + }) |
700 | 745 | }
|
701 | 746 | }
|
702 | 747 |
|
|
0 commit comments