diff --git a/05_stringer/mohitnag/main.go b/05_stringer/mohitnag/main.go new file mode 100644 index 000000000..15aa2751e --- /dev/null +++ b/05_stringer/mohitnag/main.go @@ -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}) +} diff --git a/05_stringer/mohitnag/main_test.go b/05_stringer/mohitnag/main_test.go new file mode 100644 index 000000000..22f568cf3 --- /dev/null +++ b/05_stringer/mohitnag/main_test.go @@ -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) +}