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

Implement lab05 stringer for anz-bank go-course #526

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
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
19 changes: 19 additions & 0 deletions 05_stringer/undrewb/main.go
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})
}
69 changes: 69 additions & 0 deletions 05_stringer/undrewb/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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())
Copy link
Member

Choose a reason for hiding this comment

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

Why strconv.Quote?

Copy link
Member

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?

Copy link
Contributor

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.


assert.Equal(t, want, got)
}

var stringerData = []struct {
name string
input IPAddr
want string
}{
{
name: "localhost",
input: IPAddr{127, 0, 0, 1},
want: "127.0.0.1",
},
{
name: "four octet addr",
input: IPAddr{68, 2, 44, 125},
want: "68.2.44.125",
},
{
name: "three octet addr",

Choose a reason for hiding this comment

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

File is not goimports-ed (from goimports)

Suggested change
name: "three octet addr",
name: "three octet addr",

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, tt.want, got)
})
}
}