Skip to content

Commit

Permalink
Add table-driven test example
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion committed Jan 13, 2024
1 parent 38147ec commit 121f209
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions projects/interfaces/buffer_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestOurByteBuffer_Table(t *testing.T) {
for name, tc := range map[string]struct {
initialContent string
operations []operation
}{
"read_initial_bytes": {
initialContent: "hello",
operations: []operation{
&bytesOperation{
wantValue: "hello",
},
},
},
"subsequent_writes_are_appended": {
initialContent: "hello",
operations: []operation{
&writeOperation{
value: " world",
},
&bytesOperation{
wantValue: "hello world",
},
},
},
"read_oversized_slice": {
initialContent: "hello world",
operations: []operation{
&readOperation{
bufferSize: 50,
wantValue: "hello world",
},
},
},
"read_undersized_slices": {
initialContent: "hello world",
operations: []operation{
&readOperation{
bufferSize: 6,
wantValue: "hello ",
},
&readOperation{
bufferSize: 5,
wantValue: "world",
},
},
},
} {
t.Run(name, func(t *testing.T) {
b := NewBufferString(tc.initialContent)
for _, operation := range tc.operations {
operation.Do(t, &b)
}
})
}
}

type operation interface {
Do(t *testing.T, b *OurByteBuffer)
}

type readOperation struct {
bufferSize int
wantValue string
}

func (o *readOperation) Do(t *testing.T, b *OurByteBuffer) {
t.Helper()
buf := make([]byte, o.bufferSize)
n, err := b.Read(buf)
require.NoError(t, err)
require.Equal(t, len(o.wantValue), n)
require.Equal(t, o.wantValue, string(buf[:n]))
}

type writeOperation struct {
value string
}

func (o *writeOperation) Do(t *testing.T, b *OurByteBuffer) {
t.Helper()
n, err := b.Write([]byte(o.value))
require.NoError(t, err)
require.Equal(t, len(o.value), n)
}

type bytesOperation struct {
wantValue string
}

func (o *bytesOperation) Do(t *testing.T, b *OurByteBuffer) {
t.Helper()
got := string(b.Bytes())
require.Equal(t, o.wantValue, got)
}

0 comments on commit 121f209

Please sign in to comment.