-
Notifications
You must be signed in to change notification settings - Fork 164
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
Implement lab05 stringer for anz-bank go-course #526
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
type IPAddr [4]byte | ||
|
||
func (ip IPAddr) String() string { | ||
return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3]) | ||
} | ||
|
||
func main() { | ||
fmt.Fprintln(out, IPAddr{127, 0, 0, 1}) | ||
} |
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 ( | ||
"bytes" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMainOutput(t *testing.T) { | ||
var buf bytes.Buffer | ||
out = &buf | ||
|
||
main() | ||
|
||
want := strconv.Quote("127.0.0.1\n") | ||
got := strconv.Quote(buf.String()) | ||
|
||
assert.Equal(t, got, want) | ||
} | ||
|
||
var stringerData = []struct { | ||
name string | ||
input IPAddr | ||
want string | ||
}{ | ||
{name: "localhost", | ||
input: IPAddr{127, 0, 0, 1}, | ||
want: "127.0.0.1"}, | ||
undrewb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{name: "four octet addr", | ||
input: IPAddr{68, 2, 44, 125}, | ||
want: "68.2.44.125"}, | ||
{name: "three octet addr", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation on this is inconsistent with the rest, can you please fix this so this line starts on the one below |
||
input: IPAddr{68, 2, 125}, | ||
want: "68.2.125.0"}, | ||
{name: "two octet addr", | ||
input: IPAddr{9, 5}, | ||
want: "9.5.0.0"}, | ||
{name: "one octet addr", | ||
input: IPAddr{144}, | ||
want: "144.0.0.0"}, | ||
{name: "empty", | ||
input: IPAddr{}, | ||
want: "0.0.0.0"}, | ||
} | ||
|
||
func TestStringer(t *testing.T) { | ||
for _, tt := range stringerData { | ||
undrewb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := fmt.Sprint(tt.input) | ||
assert.Equal(t, got, tt.want) | ||
undrewb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
} |
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.
Why
strconv.Quote
?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.
Actually, this seems to be a common pattern among go-course PRs. Is it recommended somewhere to do it this way?
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.
It saves you typing `""127.0.0.1\n"" - which is the output of main, which needs to be tested for coverage. that's all.