clockface: refactorize acceptance test

This commit is contained in:
vinchent 2024-09-23 18:01:35 +02:00
parent 7b3fb0f91c
commit b58e2436cd

View File

@ -9,73 +9,61 @@ import (
type Svg struct {
XMLName xml.Name `xml:"svg"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Width string `xml:"width,attr"`
Height string `xml:"height,attr"`
ViewBox string `xml:"viewBox,attr"`
Version string `xml:"version,attr"`
Circle struct {
Text string `xml:",chardata"`
Cx string `xml:"cx,attr"`
Cy string `xml:"cy,attr"`
R string `xml:"r,attr"`
Style string `xml:"style,attr"`
} `xml:"circle"`
Line []struct {
Text string `xml:",chardata"`
X1 string `xml:"x1,attr"`
Y1 string `xml:"y1,attr"`
X2 string `xml:"x2,attr"`
Y2 string `xml:"y2,attr"`
Style string `xml:"style,attr"`
} `xml:"line"`
Circle Circle `xml:"circle"`
Line []Line `xml:"line"`
}
// func TestSecondHandAtMidnight(t *testing.T) {
// tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
//
// want := Point{X: 150, Y: 150 - 90}
// got := SecondHand(tm)
//
// if got != want {
// t.Errorf("Got %v, wanted %v", got, want)
// }
// }
type Circle struct {
Cx float64 `xml:"cx,attr"`
Cy float64 `xml:"cy,attr"`
R float64 `xml:"r,attr"`
}
// func TestSecondHandAt30Seconds(t *testing.T) {
// tm := time.Date(1337, time.January, 1, 0, 0, 30, 0, time.UTC)
//
// want := Point{X: 150, Y: 150 + 90}
// got := SecondHand(tm)
//
// if got != want {
// t.Errorf("Got %v, wanted %v", got, want)
// }
// }
type Line struct {
X1 float64 `xml:"x1,attr"`
Y1 float64 `xml:"y1,attr"`
X2 float64 `xml:"x2,attr"`
Y2 float64 `xml:"y2,attr"`
}
func TestSVGWriterAtMidnight(t *testing.T) {
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
b := bytes.Buffer{}
SVGWriter(&b, tm)
svg := Svg{}
xml.Unmarshal(b.Bytes(), &svg)
x2 := "150.000000"
y2 := "60.000000"
for _, line := range svg.Line {
if line.X2 == x2 && line.Y2 == y2 {
return
}
func TestSVGWriterSecondHand(t *testing.T) {
cases := []struct {
time time.Time
line Line
}{
{simpleTime(0, 0, 0), Line{150, 150, 150, 60}},
{simpleTime(0, 0, 30), Line{150, 150, 150, 240}},
}
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(),
)
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
b := bytes.Buffer{}
SVGWriter(&b, c.time)
svg := Svg{}
xml.Unmarshal(b.Bytes(), &svg)
if !containsLine(c.line, svg.Line) {
t.Errorf(
"Expected to find the second hand line %+v in the SVG lines %v",
c.line,
svg.Line,
)
}
})
}
}
func containsLine(l Line, ls []Line) bool {
for _, line := range ls {
if line == l {
return true
}
}
return false
}