Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 223c5be

Browse files
committed
Add Coordinate String() method
1 parent 94f794c commit 223c5be

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

common/util/coordinate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package util
22

3+
import "fmt"
4+
35
type Coordinate struct {
46
X int
57
Y int
@@ -48,3 +50,7 @@ func (c Coordinate) SouthWest() Coordinate {
4850
func (c Coordinate) SouthEast() Coordinate {
4951
return Coordinate{c.X + 1, c.Y + 1}
5052
}
53+
54+
func (c Coordinate) String() string {
55+
return fmt.Sprintf("(%d,%d)", c.X, c.Y)
56+
}

common/util/coordinate_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@ func TestCoordinateDirections(t *testing.T) {
3030
}
3131
}
3232

33+
func TestCoordinate_String(t *testing.T) {
34+
tests := []struct {
35+
c Coordinate
36+
expected string
37+
}{
38+
{c: Coordinate{X: 123, Y: 456}, expected: "(123,456)"},
39+
{c: Coordinate{X: -1741, Y: 917}, expected: "(-1741,917)"},
40+
{c: Coordinate{X: 184701, Y: -10471}, expected: "(184701,-10471)"},
41+
}
42+
43+
for _, test := range tests {
44+
name := fmt.Sprintf("Coordinate{X:%d, Y:%d}.String()", test.c.X, test.c.Y)
45+
actual := test.c.String()
46+
47+
t.Run(name, func(t *testing.T) {
48+
if test.expected != actual {
49+
t.Errorf("expected %v but got %v", test.expected, actual)
50+
}
51+
})
52+
53+
name = fmt.Sprintf("Formatted print with %%s of Coordinate{X:%d, Y:%d}", test.c.X, test.c.Y)
54+
actual = fmt.Sprintf("%s", test.c)
55+
t.Run(name, func(t *testing.T) {
56+
if test.expected != test.c.String() {
57+
t.Errorf("expected %v but got %v", test.expected, actual)
58+
}
59+
})
60+
}
61+
}
62+
3363
func TestIn2DArray(t *testing.T) {
3464
matrix := [][]int{{0, 0}, {0, 0}}
3565
tests := []struct {

0 commit comments

Comments
 (0)