clockface/clockface_acceptance_test.go

115 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-01-20 04:26:08 +00:00
package clockface
import (
"bytes"
"encoding/xml"
2022-01-20 04:26:08 +00:00
"testing"
"time"
)
2022-01-20 05:45:27 +00:00
type SVG struct {
XMLName xml.Name `xml:"svg"`
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"`
2022-01-20 05:45:27 +00:00
Circle Circle `xml:"circle"`
Line []Line `xml:"line"`
}
2022-01-20 05:45:27 +00:00
type Circle struct {
Cx float64 `xml:"cx,attr"`
Cy float64 `xml:"cy,attr"`
R float64 `xml:"r,attr"`
}
2022-01-20 04:26:08 +00:00
2022-01-20 05:45:27 +00:00
type Line struct {
X1 float64 `xml:"x1,attr"`
Y1 float64 `xml:"y1,attr"`
X2 float64 `xml:"x2,attr"`
Y2 float64 `xml:"y2,attr"`
}
2022-01-20 04:26:08 +00:00
2022-01-20 05:45:27 +00:00
func containsLine(l Line, ls []Line) bool {
for _, line := range ls {
if line == l {
return true
}
}
return false
}
2022-01-20 04:26:08 +00:00
2022-01-20 05:45:27 +00:00
func TestSVGWriterAtMidnight(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}},
}
2022-01-20 04:26:08 +00:00
2022-01-20 05:45:27 +00:00
for _, c := range cases {
t.Run(testName(c.time), func(t *testing.T) {
b := bytes.Buffer{}
SVGWriter(&b, c.time)
2022-01-20 04:26:08 +00:00
2022-01-20 05:45:27 +00:00
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 TestSVGWriterMinuteHand(t *testing.T) {
cases := []struct {
time time.Time
line Line
}{
{simpleTime(0, 0, 0),
Line{150, 150, 150, 70}},
2022-01-20 04:26:08 +00:00
}
2022-01-20 05:45:27 +00:00
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)
}
})
}
2022-01-20 04:26:08 +00:00
}
2022-01-20 06:07:29 +00:00
func TestSVGWriterHourHand(t *testing.T) {
cases := []struct {
time time.Time
line Line
}{
{simpleTime(6, 0, 0),
Line{150, 150, 150, 200}},
}
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 hour hand line %+v, in the SVG lines %+v", c.line, svg.Line)
}
})
}
}