Skip to content

Commit

Permalink
Add lab 5 - Stringer (anz-bank#644)
Browse files Browse the repository at this point in the history
Add lab 5 - Stringer
  • Loading branch information
alextmz authored and Jim hejtmanek committed Jul 31, 2020
1 parent 6adc13f commit 5a76dfa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 05_stringer/alextmz/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"io"
"os"
)

type ipAddr [4]byte

var out io.Writer = os.Stdout

func main() {
fmt.Fprintln(out, ipAddr{127, 0, 0, 1})
}

func (i ipAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3])
}
53 changes: 53 additions & 0 deletions 05_stringer/alextmz/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"bytes"
"testing"

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

func TestIPAddrString(t *testing.T) {
var tests = map[string]struct {
arg ipAddr
want string
}{
"empty IPv4": {
ipAddr{},
"0.0.0.0",
},
"8bits IPv4": {
ipAddr{254},
"254.0.0.0",
},
"16bits IPv4": {
ipAddr{254, 254},
"254.254.0.0",
},
"24bits IPv4": {
ipAddr{254, 254, 254},
"254.254.254.0",
},
"full IPv4": {
ipAddr{254, 254, 254, 254},
"254.254.254.254",
},
}

for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
actual := test.arg.String()
assert.Equal(t, test.want, actual)
})
}
}

func TestMain(t *testing.T) {
want := "127.0.0.1\n"
var buf bytes.Buffer
out = &buf
main()
got := buf.String()
assert.Equal(t, want, got)
}

0 comments on commit 5a76dfa

Please sign in to comment.