Skip to content

Commit

Permalink
Merge pull request #79 from JosiahWitt/improve-entable-tests
Browse files Browse the repository at this point in the history
Internally improve `enable` test coverage
  • Loading branch information
JosiahWitt authored Mar 3, 2023
2 parents 2ec9e16 + f08d6e4 commit a9f02d7
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 8 deletions.
4 changes: 4 additions & 0 deletions exp/entable/.ensure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mocks:
packages:
- path: github.com/JosiahWitt/ensure/ensuring
interfaces: [T]
2 changes: 1 addition & 1 deletion exp/entable/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
generate-mocks:
@# Not implemented
(cd ../../cmd/ensure; go build -o ../../tmp/ensure) && ../../tmp/ensure mocks generate

test:
go test ./...
Expand Down
8 changes: 5 additions & 3 deletions exp/entable/entable.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (t *Table[E]) Iterate(fn func(entry *E)) {
// It uses [ensuring.E.RunTableByIndex], so the same functionality is supported,
// including built-in support for mocks.
func (t *Table[E]) Run(ensure ensuring.E, fn func(ensure ensuring.E, entry *E)) {
ensure.T().Helper()
ensure.InterfaceT().Helper()

if t.name != "" {
ensure.Run(t.name, func(ensure ensuring.E) {
Expand All @@ -144,20 +144,22 @@ func (t *Table[E]) Run(ensure ensuring.E, fn func(ensure ensuring.E, entry *E))
}

func (t *Table[E]) run(ensure ensuring.E, fn func(ensure ensuring.E, entry *E)) {
ensure.T().Helper()
ensure.InterfaceT().Helper()

subtableNames := make(map[string]int, len(t.subtables))
for i, subtable := range t.subtables {
if subtable.name == "" {
ensure.Failf(
"Subtables are required to be named using WithName. Subtables[%d] was not named. "+
"If you want to append the entries from the table directly, use AppendTableEntries.",
"If you want to append the entries from the table directly instead of naming it, use AppendTableEntries.",
i,
)
return
}

if lastIdx, ok := subtableNames[subtable.name]; ok {
ensure.Failf("Subtables names are required to be unique. Subtables[%d] shares a name with Subtables[%d]: %s", i, lastIdx, subtable.name)
return
}

subtableNames[subtable.name] = i
Expand Down
50 changes: 50 additions & 0 deletions exp/entable/entable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/JosiahWitt/ensure"
"github.com/JosiahWitt/ensure/ensuring"
"github.com/JosiahWitt/ensure/exp/entable"
"github.com/JosiahWitt/ensure/exp/entable/internal/mocks/github.com/JosiahWitt/ensure/mock_ensuring"
"github.com/golang/mock/gomock"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -452,4 +454,52 @@ func TestRun(t *testing.T) {
},
})
})

ensure.Run("when subtables share names", func(ensure ensuring.E) {
mockT := mock_ensuring.NewMockT(ensure.GoMockController())
mockT.EXPECT().Helper().MinTimes(2)
mockT.EXPECT().Cleanup(gomock.Any()).AnyTimes()
mockEnsure := ensure.New(mockT)

type Entry struct{}

table := entable.New[Entry]()
table.AppendTable(
entable.New[Entry]().WithName("sub1"),
entable.New[Entry]().WithName("sub2"),
entable.New[Entry]().WithName("sub1"),
)

mockT.EXPECT().Fatalf("Subtables names are required to be unique. Subtables[%d] shares a name with Subtables[%d]: %s", 2, 0, "sub1")

numCalls := 0
table.Run(mockEnsure, func(ensure ensuring.E, entry *Entry) { numCalls++ })
ensure(numCalls).Equals(0)
})

ensure.Run("when subtable is missing a name", func(ensure ensuring.E) {
mockT := mock_ensuring.NewMockT(ensure.GoMockController())
mockT.EXPECT().Helper().MinTimes(2)
mockT.EXPECT().Cleanup(gomock.Any()).AnyTimes()
mockEnsure := ensure.New(mockT)

type Entry struct{}

table := entable.New[Entry]()
table.AppendTable(
entable.New[Entry]().WithName("sub1"),
entable.New[Entry](),
entable.New[Entry]().WithName("sub3"),
)

mockT.EXPECT().Fatalf(
"Subtables are required to be named using WithName. Subtables[%d] was not named. "+
"If you want to append the entries from the table directly instead of naming it, use AppendTableEntries.",
1,
)

numCalls := 0
table.Run(mockEnsure, func(ensure ensuring.E, entry *Entry) { numCalls++ })
ensure(numCalls).Equals(0)
})
}
6 changes: 4 additions & 2 deletions exp/entable/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module github.com/JosiahWitt/ensure/exp/entable

go 1.18

require github.com/JosiahWitt/ensure v0.4.0
require (
github.com/JosiahWitt/ensure v0.4.2
github.com/golang/mock v1.5.0
)

require (
github.com/JosiahWitt/erk v0.5.8 // indirect
github.com/go-test/deep v1.0.7 // indirect
github.com/golang/mock v1.5.0 // indirect
github.com/kr/pretty v0.2.2-0.20201124222238-a883a8422cd2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions exp/entable/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/JosiahWitt/ensure v0.3.1/go.mod h1:f9w6clasETP6j/VXbisqC2QWcmfBCzFHoEksUsSYOg8=
github.com/JosiahWitt/ensure v0.4.0 h1:A3gvCenDVC/tf1gzLnvC+D74JsmdswCcakEB4j0rT44=
github.com/JosiahWitt/ensure v0.4.0/go.mod h1:v9NPUdqtbbjKh5fhPPjTb701lTdYe5IyK0D+e52m1OA=
github.com/JosiahWitt/ensure v0.4.2 h1:8W5n+OLkhZsuUfr/3ELAkzQQUcbXEzs+usQV+fXeK1o=
github.com/JosiahWitt/ensure v0.4.2/go.mod h1:v9NPUdqtbbjKh5fhPPjTb701lTdYe5IyK0D+e52m1OA=
github.com/JosiahWitt/erk v0.5.6/go.mod h1:OiLS68mTg6Aa3Olx2CHoe3Dg38Uy8MsJzHawpT8aEfQ=
github.com/JosiahWitt/erk v0.5.8 h1:k1EjYn+0oKgMxd/tzVlF1uIfJtVUeqUmZdomRIOm5EI=
github.com/JosiahWitt/erk v0.5.8/go.mod h1:QJr+FBLfK1qK4OLui6wQpG82Y3YCeSJfTU5gtpqMe1o=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9f02d7

Please sign in to comment.