Skip to content

Commit 64cf92e

Browse files
committed
Add IMPLEMENTATION.md
Also switch FilteringPipe tests to be table-driven.
1 parent 121f209 commit 64cf92e

File tree

2 files changed

+97
-45
lines changed

2 files changed

+97
-45
lines changed

projects/interfaces/IMPLEMENTATION.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Implementation of interfaces project
2+
3+
## How to run
4+
5+
The primary artifacts of this project are a struct implementation, and a series of tests.
6+
7+
To run the tests for this project, `cd` to this directory and run `go test ./...`. You should see all the tests pass.
8+
9+
## Comparing the tests
10+
11+
There are two different implementations of this test suite.
12+
13+
In [`buffer_table_test.go`](./buffer_table_test.go) there is a table-driven test. In [`buffer_test.go`](./buffer_test.go) there is a non-table-driven test.
14+
15+
Often we use [table-driven tests](https://dave.cheney.net/2019/05/07/prefer-table-driven-tests) in Go because they make it easy to do similar things many times (factoring out commonality to be [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)), and make it really easy to add extra test-cases.
16+
17+
In this example, it's not obvious that table-driven tests are better than non-table-driven tests. Take a bit of time to look at both tests and think about the trade-offs involved here:
18+
* Which set of tests would you prefer to write?
19+
* Which set of tests would you prefer to read?
20+
* Which set of tests would you prefer to maintain?
21+
22+
Think about this yourself before reading the next section.
23+
24+
### Advantages of non-table-driven tests
25+
26+
In this example, the table-driven tests are harder to read and verify they're obviously correct.
27+
28+
In the non-table-driven tests, each test reads top-to-bottom quite clearly.
29+
30+
In the table-driven tests, you need to understand quite a lot about the `operations` field. If you want to verify the exact behaviours, you need to read several interface implementations, and understand how they interact.
31+
32+
We don't often write tests for our tests, so in tests, we strongly value being able to see that they're obviously correct just by reading them. One could argue that the non-table-driven test does this better, in this example.
33+
34+
### Advantages of table-driven tests
35+
36+
#### Adding more tests
37+
38+
In this example, the table-driven tests are more re-usable. If we wanted to add lots more tests with different permutations ("write then read then write again then write again then read"), it would be easier to add these extra tests in the table-driven way. Making it easy to write more tests is generally a good thing.
39+
40+
#### Changing all the tests
41+
42+
Imagine we changed the implementation of our buffer (e.g. added an `error` return to some function, or changed how we construct a buffer). In the table-driven tests, we only have a few actual uses of the buffer API - probably one per `operation` implementation.
43+
44+
In the non-table-driven test version, we need to make that change in lots of places. By avoiding repeating ourselves and factoring away the commonality, we reduced the number of places we need to change our tests if something changes in our implementation.
45+
46+
### Summary
47+
48+
Neither approach is obviously _better_. They have different trade-offs. This is a common situation when writing software.
49+
50+
If we were testing something a little simpler, like testing the `FilteringPipe` type, the tests would have fewer differences between them (e.g. just differing in what's written, rather than having lots of different operations), a table-driven test would probably be a more obvious winner.
51+
52+
We need to choose what we're optimising for: Are we optimising for making it easy to add lots more complex ordering tests? Are we optimising for making it easy to read and verify the test works? How do we imagine needing to change the code in the future, and what will make _that_ easy?
53+
54+
## Implementation notes on `OurByteBuffer`
55+
56+
#### Struct vs Pointer
57+
58+
When implementing a method on a struct, we can choose to implement it on the struct type itself (`func (b OurByteBuffer) someMethod()`) or on a pointer to the struct type (`func (b *OurByteBuffer) someMethod()`).
59+
60+
We chose to implement the methods on a pointer to the struct, rather than just the struct.
61+
62+
This is because we have information which we need to be preserved across the different methods. If we implemented the methods on the struct, each call to `Write` would get a new copy of `OurByteBuffer`, so when we update `b.bytes` it would only get updated _in that copy_.

projects/interfaces/filtering_pipe_test.go

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,39 @@ import (
77
"github.com/stretchr/testify/require"
88
)
99

10-
func TestWriteNoNumbers(t *testing.T) {
11-
buf := bytes.NewBufferString("")
12-
13-
fp := NewFilteringPipe(buf)
14-
15-
n, err := fp.Write([]byte("hello"))
16-
require.NoError(t, err)
17-
require.Equal(t, 5, n)
18-
require.Equal(t, "hello", buf.String())
19-
}
20-
21-
func TestWriteJustNumbers(t *testing.T) {
22-
buf := bytes.NewBufferString("")
23-
24-
fp := NewFilteringPipe(buf)
25-
26-
n, err := fp.Write([]byte("123"))
27-
require.NoError(t, err)
28-
require.Equal(t, 3, n)
29-
require.Equal(t, "", buf.String())
30-
}
31-
32-
func TestMultipleWrites(t *testing.T) {
33-
buf := bytes.NewBufferString("")
34-
35-
fp := NewFilteringPipe(buf)
36-
37-
n, err := fp.Write([]byte("start="))
38-
require.NoError(t, err)
39-
require.Equal(t, 6, n)
40-
n, err = fp.Write([]byte("1, end=10"))
41-
require.NoError(t, err)
42-
require.Equal(t, 9, n)
43-
require.Equal(t, "start=, end=", buf.String())
44-
}
45-
46-
func TestWriteMixedNumbersAndLetters(t *testing.T) {
47-
buf := bytes.NewBufferString("")
48-
49-
fp := NewFilteringPipe(buf)
50-
51-
n, err := fp.Write([]byte("start=1, end=10"))
52-
require.NoError(t, err)
53-
require.Equal(t, 15, n)
54-
require.Equal(t, "start=, end=", buf.String())
10+
func TestFilteringPipe(t *testing.T) {
11+
for name, tc := range map[string]struct {
12+
inputs []string
13+
output string
14+
}{
15+
"no_numbers_in_input": {
16+
inputs: []string{"hello"},
17+
output: "hello",
18+
},
19+
"just_numbers": {
20+
inputs: []string{"123"},
21+
output: "",
22+
},
23+
"mixed_numbers_and_letters": {
24+
inputs: []string{"start=1, end=10"},
25+
output: "start=, end=",
26+
},
27+
"multiple_writes": {
28+
inputs: []string{"start=", "1, end=10"},
29+
output: "start=, end=",
30+
},
31+
} {
32+
t.Run(name, func(t *testing.T) {
33+
buf := bytes.NewBufferString("")
34+
35+
fp := NewFilteringPipe(buf)
36+
37+
for _, input := range tc.inputs {
38+
n, err := fp.Write([]byte(input))
39+
require.NoError(t, err)
40+
require.Equal(t, len(input), n)
41+
}
42+
require.Equal(t, tc.output, buf.String())
43+
})
44+
}
5545
}

0 commit comments

Comments
 (0)