-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
illicitonion
wants to merge
3
commits into
main
Choose a base branch
from
impl/interfaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?