Skip to content

Commit 8915e5f

Browse files
committed
✨ (clockface): refactor svg struct for float64
1 parent 1a7f45e commit 8915e5f

File tree

3 files changed

+59
-51
lines changed

3 files changed

+59
-51
lines changed

clockface_sample/clockface/clockface.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ func secondsInRadians(t time.Time) float64 {
2929
return (math.Pi / (30 / (float64(t.Second()))))
3030
}
3131

32-
func simpleTime(hours, minutes, seconds int) time.Time {
33-
return time.Date(312, time.October, 28, hours, minutes, seconds, 0, time.UTC)
34-
}
35-
36-
func testName(t time.Time) string {
37-
return t.Format("15:04:05")
38-
}
39-
4032
func secondHandPoint(t time.Time) Point {
4133
angle := secondsInRadians(t)
4234
x := math.Sin(angle)

clockface_sample/clockface/clockface_acceptance_test.go

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,82 @@ package clockface_test
33
import (
44
"bytes"
55
"encoding/xml"
6-
"strings"
76
"testing"
87
"time"
98

109
clockface "github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface_sample/clockface"
1110
)
1211

13-
type Svg struct {
12+
type SVG struct {
1413
XMLName xml.Name `xml:"svg"`
15-
Text string `xml:",chardata"`
1614
Xmlns string `xml:"xmlns,attr"`
1715
Width string `xml:"width,attr"`
1816
Height string `xml:"height,attr"`
1917
ViewBox string `xml:"viewBox,attr"`
2018
Version string `xml:"version,attr"`
21-
Circle struct {
22-
Text string `xml:",chardata"`
23-
Cx string `xml:"cx,attr"`
24-
Cy string `xml:"cy,attr"`
25-
R string `xml:"r,attr"`
26-
Style string `xml:"style,attr"`
27-
} `xml:"circle"`
28-
Line []struct {
29-
Text string `xml:",chardata"`
30-
X1 string `xml:"x1,attr"`
31-
Y1 string `xml:"y1,attr"`
32-
X2 string `xml:"x2,attr"`
33-
Y2 string `xml:"y2,attr"`
34-
Style string `xml:"style,attr"`
35-
} `xml:"line"`
19+
Circle Circle `xml:"circle"`
20+
Line []Line `xml:"line"`
3621
}
3722

38-
func TestSVGWriterAtMidnight(t *testing.T) {
39-
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
40-
41-
b := bytes.Buffer{}
42-
clockface.SVGWriter(&b, tm)
43-
svg := Svg{}
44-
45-
xml.Unmarshal(b.Bytes(), &svg)
23+
type Circle struct {
24+
Cx float64 `xml:"cx,attr"`
25+
Cy float64 `xml:"cy,attr"`
26+
R float64 `xml:"r,attr"`
27+
}
4628

47-
x2 := "150.000"
48-
y2 := "60.000"
29+
type Line struct {
30+
X1 float64 `xml:"x1,attr"`
31+
Y1 float64 `xml:"y1,attr"`
32+
X2 float64 `xml:"x2,attr"`
33+
Y2 float64 `xml:"y2,attr"`
34+
}
4935

50-
for _, line := range svg.Line {
51-
if line.X2 == x2 && line.Y2 == y2 {
52-
return
53-
}
36+
func TestSVGWriterSecondHand(t *testing.T) {
37+
cases := []struct {
38+
time time.Time
39+
line Line
40+
}{
41+
{
42+
simpleTime(0, 0, 0),
43+
Line{150, 150, 150, 60},
44+
},
45+
{
46+
simpleTime(0, 0, 30),
47+
Line{150, 150, 150, 240},
48+
},
5449
}
5550

56-
t.Errorf("Expected to find the second hand with x2 of %v and y2 of %v, in the SVG output %v", x2, y2, b.String())
57-
}
51+
for _, c := range cases {
52+
t.Run(testName(c.time), func(t *testing.T) {
53+
b := bytes.Buffer{}
54+
clockface.SVGWriter(&b, c.time)
5855

59-
func TestSVGWriterAt30Seconds(t *testing.T) {
60-
tm := time.Date(1337, time.January, 1, 0, 0, 30, 0, time.UTC)
56+
svg := SVG{}
57+
xml.Unmarshal(b.Bytes(), &svg)
6158

62-
var b strings.Builder
63-
clockface.SVGWriter(&b, tm)
64-
got := b.String()
65-
66-
want := `<line x1="150" y1="150" x2="150.000" y2="240.000"`
59+
if !containsLine(c.line, svg.Line) {
60+
t.Errorf("Expected to find the second hand line %+v, in the SVG lines %+v", c.line, svg.Line)
61+
}
62+
})
63+
}
64+
}
6765

68-
if !strings.Contains(got, want) {
69-
t.Errorf("Expected to find the second hand %v, in the SVG output %v", want, got)
66+
func containsLine(l Line, ls []Line) bool {
67+
for _, line := range ls {
68+
if line == l {
69+
return true
70+
}
7071
}
72+
return false
7173
}
7274

75+
func simpleTime(hours, minutes, seconds int) time.Time {
76+
return time.Date(312, time.October, 28, hours, minutes, seconds, 0, time.UTC)
77+
}
78+
79+
func testName(t time.Time) string {
80+
return t.Format("15:04:05")
81+
}
7382
func TestSecondHandAtMidnight(t *testing.T) {
7483
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
7584

clockface_sample/clockface/clockface_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func TestSecondsInRadians(t *testing.T) {
2727
}
2828
}
2929

30+
func simpleTime(hours, minutes, seconds int) time.Time {
31+
return time.Date(312, time.October, 28, hours, minutes, seconds, 0, time.UTC)
32+
}
33+
34+
func testName(t time.Time) string {
35+
return t.Format("15:04:05")
36+
}
3037
func TestSecondHandPoint(t *testing.T) {
3138
cases := []struct {
3239
time time.Time

0 commit comments

Comments
 (0)