|
1 | 1 | package clockface_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/xml" |
4 | 6 | "strings" |
5 | 7 | "testing" |
6 | 8 | "time" |
7 | 9 |
|
8 | 10 | clockface "github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface_sample/clockface" |
9 | 11 | ) |
10 | 12 |
|
| 13 | +type Svg struct { |
| 14 | + XMLName xml.Name `xml:"svg"` |
| 15 | + Text string `xml:",chardata"` |
| 16 | + Xmlns string `xml:"xmlns,attr"` |
| 17 | + Width string `xml:"width,attr"` |
| 18 | + Height string `xml:"height,attr"` |
| 19 | + ViewBox string `xml:"viewBox,attr"` |
| 20 | + 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"` |
| 36 | +} |
| 37 | + |
11 | 38 | func TestSVGWriterAtMidnight(t *testing.T) { |
12 | 39 | tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC) |
13 | 40 |
|
14 | | - var b strings.Builder |
| 41 | + b := bytes.Buffer{} |
15 | 42 | clockface.SVGWriter(&b, tm) |
16 | | - got := b.String() |
| 43 | + svg := Svg{} |
17 | 44 |
|
18 | | - want := `<line x1="150" y1="150" x2="150.000" y2="60.000"` |
| 45 | + xml.Unmarshal(b.Bytes(), &svg) |
19 | 46 |
|
20 | | - if !strings.Contains(got, want) { |
21 | | - t.Errorf("Expected to find the second hand %v, in the SVG output %v", want, got) |
| 47 | + x2 := "150.000" |
| 48 | + y2 := "60.000" |
| 49 | + |
| 50 | + for _, line := range svg.Line { |
| 51 | + if line.X2 == x2 && line.Y2 == y2 { |
| 52 | + return |
| 53 | + } |
22 | 54 | } |
| 55 | + |
| 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()) |
23 | 57 | } |
24 | 58 |
|
25 | 59 | func TestSVGWriterAt30Seconds(t *testing.T) { |
|
0 commit comments