forked from anz-bank/go-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lab 5 - Stringer
- Loading branch information
Showing
2 changed files
with
72 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" | ||
) | ||
|
||
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]) | ||
} |
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,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) | ||
} |