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.
Merge pull request anz-bank#412 from mohitnag/lab5
Lab5 - Stringer
- Loading branch information
Showing
2 changed files
with
58 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,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// IPAddr is a type implementing fmt.Stringer | ||
type IPAddr [4]byte | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
func (ip IPAddr) String() string { | ||
return fmt.Sprintf("%d.%d.%d.%d", 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,38 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIpaddr(t *testing.T) { | ||
testData := []struct { | ||
Scenario string | ||
input IPAddr | ||
expected string | ||
}{ | ||
{"Happy Day", [4]byte{127, 0, 0, 1}, "127.0.0.1"}, | ||
{"empty", [4]byte{}, "0.0.0.0"}, | ||
{"one byte non zero", [4]byte{0, 0, 0, 10}, "0.0.0.10"}, | ||
{"boundary validation", [4]byte{255, 255, 255, 255}, "255.255.255.255"}, | ||
{"only one byte", [4]byte{10}, "10.0.0.0"}, | ||
} | ||
for _, td := range testData { | ||
td := td | ||
t.Run(td.Scenario, func(t *testing.T) { | ||
actual := td.input.String() | ||
assert.Equal(t, td.expected, actual) | ||
}) | ||
} | ||
} | ||
func TestMain(t *testing.T) { | ||
assert := assert.New(t) | ||
var buf bytes.Buffer | ||
out = &buf | ||
main() | ||
expected := "127.0.0.1\n" | ||
actual := buf.String() | ||
assert.Equal(expected, actual) | ||
} |