2024-09-23 14:49:29 +00:00
|
|
|
package clockface
|
|
|
|
|
|
|
|
import (
|
2024-09-23 15:24:40 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
2024-09-23 14:49:29 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-09-23 15:24:40 +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"`
|
2024-09-23 16:01:35 +00:00
|
|
|
Circle Circle `xml:"circle"`
|
|
|
|
Line []Line `xml:"line"`
|
2024-09-23 15:24:40 +00:00
|
|
|
}
|
2024-09-23 14:49:29 +00:00
|
|
|
|
2024-09-23 16:01:35 +00:00
|
|
|
type Circle struct {
|
|
|
|
Cx float64 `xml:"cx,attr"`
|
|
|
|
Cy float64 `xml:"cy,attr"`
|
|
|
|
R float64 `xml:"r,attr"`
|
|
|
|
}
|
2024-09-23 14:49:29 +00:00
|
|
|
|
2024-09-23 16:01:35 +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"`
|
|
|
|
}
|
2024-09-23 14:49:29 +00:00
|
|
|
|
2024-09-23 16:01:35 +00:00
|
|
|
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}},
|
|
|
|
}
|
2024-09-23 15:24:40 +00:00
|
|
|
|
2024-09-23 16:01:35 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(testName(c.time), func(t *testing.T) {
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
SVGWriter(&b, c.time)
|
2024-09-23 14:49:29 +00:00
|
|
|
|
2024-09-23 16:01:35 +00:00
|
|
|
svg := Svg{}
|
|
|
|
xml.Unmarshal(b.Bytes(), &svg)
|
2024-09-23 14:49:29 +00:00
|
|
|
|
2024-09-23 16:01:35 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 15:24:40 +00:00
|
|
|
|
2024-09-23 20:16:06 +00:00
|
|
|
func TestSVGWriterMinuteHand(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
time time.Time
|
|
|
|
line Line
|
|
|
|
}{
|
|
|
|
{simpleTime(0, 0, 0), Line{150, 150, 150, 70}},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 minute hand line %+v in the SVG lines %v",
|
|
|
|
c.line,
|
|
|
|
svg.Line,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 20:31:05 +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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 16:01:35 +00:00
|
|
|
func containsLine(l Line, ls []Line) bool {
|
|
|
|
for _, line := range ls {
|
|
|
|
if line == l {
|
|
|
|
return true
|
2024-09-23 15:24:40 +00:00
|
|
|
}
|
2024-09-23 14:49:29 +00:00
|
|
|
}
|
2024-09-23 16:01:35 +00:00
|
|
|
return false
|
2024-09-23 14:49:29 +00:00
|
|
|
}
|