Skip to content

Commit 9117894

Browse files
committed
check test failure message for Empty and NotEmpty
Only the tests are updated, code is unchanged. Previously, the tests were checking only the result of the asserter. Using captureTestingT helper allows to check the error message
1 parent 016e2e9 commit 9117894

File tree

1 file changed

+112
-17
lines changed

1 file changed

+112
-17
lines changed

assert/assertions_test.go

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,16 +1718,72 @@ func TestEmpty(t *testing.T) {
17181718
True(t, Empty(mockT, sP), "ptr to nil value is empty")
17191719
True(t, Empty(mockT, [1]int{}), "array is state")
17201720

1721-
False(t, Empty(mockT, "something"), "Non Empty string is not empty")
1722-
False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty")
1723-
False(t, Empty(mockT, []string{"something"}), "Non empty string array is not empty")
1724-
False(t, Empty(mockT, 1), "Non-zero int value is not empty")
1725-
False(t, Empty(mockT, true), "True value is not empty")
1726-
False(t, Empty(mockT, chWithValue), "Channel with values is not empty")
1727-
False(t, Empty(mockT, TStruct{x: 1}), "struct with initialized values is empty")
1728-
False(t, Empty(mockT, TString("abc")), "non-empty aliased string is empty")
1729-
False(t, Empty(mockT, xP), "ptr to non-nil value is not empty")
1730-
False(t, Empty(mockT, [1]int{42}), "array is not state")
1721+
tests := []struct {
1722+
name string
1723+
value interface{}
1724+
expectedErrMsg string
1725+
}{
1726+
{
1727+
name: "Non Empty string is not empty",
1728+
value: "something",
1729+
expectedErrMsg: "Should be empty, but was something\n",
1730+
},
1731+
{
1732+
name: "Non nil object is not empty",
1733+
value: errors.New("something"),
1734+
expectedErrMsg: "Should be empty, but was something\n",
1735+
},
1736+
{
1737+
name: "Non empty string array is not empty",
1738+
value: []string{"something"},
1739+
expectedErrMsg: "Should be empty, but was [something]\n",
1740+
},
1741+
{
1742+
name: "Non-zero int value is not empty",
1743+
value: 1,
1744+
expectedErrMsg: "Should be empty, but was 1\n",
1745+
},
1746+
{
1747+
name: "True value is not empty",
1748+
value: true,
1749+
expectedErrMsg: "Should be empty, but was true\n",
1750+
},
1751+
{
1752+
name: "Channel with values is not empty",
1753+
value: chWithValue,
1754+
expectedErrMsg: fmt.Sprintf("Should be empty, but was %v\n", chWithValue),
1755+
},
1756+
{
1757+
name: "struct with initialized values is empty",
1758+
value: TStruct{x: 1},
1759+
expectedErrMsg: "Should be empty, but was {1}\n",
1760+
},
1761+
{
1762+
name: "non-empty aliased string is empty",
1763+
value: TString("abc"),
1764+
expectedErrMsg: "Should be empty, but was abc\n",
1765+
},
1766+
{
1767+
name: "ptr to non-nil value is not empty",
1768+
value: xP,
1769+
expectedErrMsg: fmt.Sprintf("Should be empty, but was %p\n", xP),
1770+
},
1771+
{
1772+
name: "array is not state",
1773+
value: [1]int{42},
1774+
expectedErrMsg: "Should be empty, but was [42]\n",
1775+
},
1776+
}
1777+
1778+
for _, tt := range tests {
1779+
tt := tt
1780+
t.Run(tt.name, func(t *testing.T) {
1781+
mockCT := new(captureTestingT)
1782+
res := Empty(mockCT, tt.value)
1783+
expectedResult := false // we expect a failure
1784+
mockCT.checkResultAndErrMsg(t, res, expectedResult, tt.expectedErrMsg)
1785+
})
1786+
}
17311787
}
17321788

17331789
func TestNotEmpty(t *testing.T) {
@@ -1736,13 +1792,52 @@ func TestNotEmpty(t *testing.T) {
17361792
chWithValue := make(chan struct{}, 1)
17371793
chWithValue <- struct{}{}
17381794

1739-
False(t, NotEmpty(mockT, ""), "Empty string is empty")
1740-
False(t, NotEmpty(mockT, nil), "Nil is empty")
1741-
False(t, NotEmpty(mockT, []string{}), "Empty string array is empty")
1742-
False(t, NotEmpty(mockT, 0), "Zero int value is empty")
1743-
False(t, NotEmpty(mockT, false), "False value is empty")
1744-
False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty")
1745-
False(t, NotEmpty(mockT, [1]int{}), "array is state")
1795+
tests := []struct {
1796+
name string
1797+
value interface{}
1798+
expectedErrMsg string
1799+
}{
1800+
{
1801+
name: "Empty string is empty",
1802+
value: "",
1803+
expectedErrMsg: `Should NOT be empty, but was ` + "\n", // TODO FIX THIS strange error message
1804+
},
1805+
{
1806+
name: "Nil is empty",
1807+
value: nil,
1808+
expectedErrMsg: "Should NOT be empty, but was <nil>\n",
1809+
},
1810+
{
1811+
name: "Empty string array is empty",
1812+
value: []string{},
1813+
expectedErrMsg: "Should NOT be empty, but was []\n",
1814+
},
1815+
{
1816+
name: "Zero int value is empty",
1817+
value: 0,
1818+
expectedErrMsg: "Should NOT be empty, but was 0\n",
1819+
},
1820+
{
1821+
name: "False value is empty",
1822+
value: false,
1823+
expectedErrMsg: "Should NOT be empty, but was false\n",
1824+
},
1825+
{
1826+
name: "array is state",
1827+
value: [1]int{},
1828+
expectedErrMsg: "Should NOT be empty, but was [0]\n",
1829+
},
1830+
}
1831+
1832+
for _, tt := range tests {
1833+
tt := tt
1834+
t.Run(tt.name, func(t *testing.T) {
1835+
mockCT := new(captureTestingT)
1836+
res := NotEmpty(mockCT, tt.value)
1837+
expectedResult := false // we expect a failure, as they are not empty
1838+
mockCT.checkResultAndErrMsg(t, expectedResult, res, tt.expectedErrMsg)
1839+
})
1840+
}
17461841

17471842
True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty")
17481843
True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty")

0 commit comments

Comments
 (0)