Skip to content

Commit

Permalink
Implement lab05 stringer for anz-bank go-course
Browse files Browse the repository at this point in the history
Update test to fix newline issue
  • Loading branch information
Bucknell, Andrew committed Jul 1, 2019
1 parent 1fb10a5 commit d9597d8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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})
}
57 changes: 57 additions & 0 deletions 05_stringer/undrewb/main_test.go
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"},
{name: "four octet addr",
input: IPAddr{68, 2, 44, 125},
want: "68.2.44.125"},
{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 {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := fmt.Sprint(tt.input)
assert.Equal(t, got, tt.want)
})
}
}

0 comments on commit d9597d8

Please sign in to comment.