Skip to content

Commit

Permalink
Merge pull request anz-bank#412 from mohitnag/lab5
Browse files Browse the repository at this point in the history
Lab5 - Stringer
  • Loading branch information
mohitnag authored Jul 26, 2019
2 parents 49fc662 + f00c297 commit 4501995
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 05_stringer/mohitnag/main.go
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})
}
38 changes: 38 additions & 0 deletions 05_stringer/mohitnag/main_test.go
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)
}

0 comments on commit 4501995

Please sign in to comment.