-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement lab05 stringer for anz-bank go-course
Update test to fix newline issue
- Loading branch information
Bucknell, Andrew
committed
Jul 1, 2019
1 parent
1fb10a5
commit d9597d8
Showing
2 changed files
with
76 additions
and
0 deletions.
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"}, | ||
{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) | ||
}) | ||
} | ||
} |