Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of interfaces project #131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions projects/interfaces/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

type OurByteBuffer struct {
bytes []byte
readPosition int
}

func NewBufferString(s string) OurByteBuffer {
return OurByteBuffer{
bytes: []byte(s),
readPosition: 0,
}
}

func (b *OurByteBuffer) Bytes() []byte {
return b.bytes
}

func (b *OurByteBuffer) Write(bytes []byte) (int, error) {
b.bytes = append(b.bytes, bytes...)
return len(bytes), nil
}

func (b *OurByteBuffer) Read(to []byte) (int, error) {
remainingBytes := len(b.bytes) - b.readPosition
bytesToRead := len(to)
if remainingBytes < bytesToRead {
bytesToRead = remainingBytes
}
copy(to, b.bytes[b.readPosition:b.readPosition+bytesToRead])
b.readPosition += bytesToRead
return bytesToRead, nil
}
57 changes: 57 additions & 0 deletions projects/interfaces/buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"testing"

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

func TestInitialBytesAreRead(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be done as a table driven test, which is better go idiom imo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I bother you to sketch out what the per-testcase struct would look like here?

I put together my best guess in buffer_table_test.go, but it feels like quite a bit of added complexity, and like it makes the tests harder to read and interpret. Maybe I'm just not looking at it the right way, and that's not what you had in mind?

want := []byte("hello")

b := NewBufferString("hello")

got := b.Bytes()

require.Equal(t, want, got)
}

func TestSubsequentWritesAreAppended(t *testing.T) {
want := []byte("hello world")

b := NewBufferString("hello")

_, err := b.Write([]byte(" world"))
require.NoError(t, err)

got := b.Bytes()

require.Equal(t, want, got)
}

func TestReadWithSliceBigEnoughForWholeBuffer(t *testing.T) {
b := NewBufferString("hello world")

slice := make([]byte, 50)

n, err := b.Read(slice)
require.NoError(t, err)
require.Equal(t, 11, n)
require.Equal(t, []byte("hello world"), slice[:n])
}

func TestReadWithSliceSmallerThanWholeBuffer(t *testing.T) {
b := NewBufferString("hello world")

slice := make([]byte, 6)

n, err := b.Read(slice)
require.NoError(t, err)
require.Equal(t, 6, n)
require.Equal(t, []byte("hello "), slice)

n, err = b.Read(slice)
require.NoError(t, err)
require.Equal(t, 5, n)
require.Equal(t, []byte("world"), slice[:n])
}
27 changes: 27 additions & 0 deletions projects/interfaces/filtering_pipe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"io"
)

type FilteringPipe struct {
writer io.Writer
}

func NewFilteringPipe(writer io.Writer) FilteringPipe {
return FilteringPipe{
writer: writer,
}
}

func (fp *FilteringPipe) Write(bytes []byte) (int, error) {
for i := range bytes {
if bytes[i] < '0' || bytes[i] > '9' {
if _, err := fp.writer.Write(bytes[i : i+1]); err != nil {
return i, err
}
}
}
// We return len(bytes) because io.Writer is documented to return how many bytes were processed, not how many were actually used.
return len(bytes), nil
}
55 changes: 55 additions & 0 deletions projects/interfaces/filtering_pipe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"bytes"
"testing"

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

func TestWriteNoNumbers(t *testing.T) {
buf := bytes.NewBufferString("")

fp := NewFilteringPipe(buf)

n, err := fp.Write([]byte("hello"))
require.NoError(t, err)
require.Equal(t, 5, n)
require.Equal(t, "hello", buf.String())
}

func TestWriteJustNumbers(t *testing.T) {
buf := bytes.NewBufferString("")

fp := NewFilteringPipe(buf)

n, err := fp.Write([]byte("123"))
require.NoError(t, err)
require.Equal(t, 3, n)
require.Equal(t, "", buf.String())
}

func TestMultipleWrites(t *testing.T) {
buf := bytes.NewBufferString("")

fp := NewFilteringPipe(buf)

n, err := fp.Write([]byte("start="))
require.NoError(t, err)
require.Equal(t, 6, n)
n, err = fp.Write([]byte("1, end=10"))
require.NoError(t, err)
require.Equal(t, 9, n)
require.Equal(t, "start=, end=", buf.String())
}

func TestWriteMixedNumbersAndLetters(t *testing.T) {
buf := bytes.NewBufferString("")

fp := NewFilteringPipe(buf)

n, err := fp.Write([]byte("start=1, end=10"))
require.NoError(t, err)
require.Equal(t, 15, n)
require.Equal(t, "start=, end=", buf.String())
}
10 changes: 10 additions & 0 deletions projects/interfaces/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/CodeYourFuture/immersive-go-course/projects/interfaces

go 1.19

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions projects/interfaces/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=