| 1 | package gitgraph |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestSVGUsesRoundedOrthogonalBends(t *testing.T) { |
| 11 | refs := []Ref{ |
| 12 | {Name: "master", SHA: "m", Kind: "branch"}, |
| 13 | {Name: "feature", SHA: "f", Kind: "branch"}, |
| 14 | } |
| 15 | g := Layout(commits("m:c,f", "f:b", "c:b", "b:"), refs, "master") |
| 16 | svg := g.SVG() |
| 17 | if svg == "" { |
| 18 | t.Fatal("lege svg") |
| 19 | } |
| 20 | if strings.Contains(svg, " C ") || strings.Contains(svg, `"C `) || strings.Contains(svg, " C") { |
| 21 | // cubische beziers geven diagonale lijnen; we willen knikken |
| 22 | if strings.Contains(svg, " C ") { |
| 23 | t.Fatalf("svg gebruikt nog een diagonale bezier: %s", svg) |
| 24 | } |
| 25 | } |
| 26 | if !strings.Contains(svg, " Q ") { |
| 27 | t.Fatalf("verwacht afgeronde bochten (Q) in %s", svg) |
| 28 | } |
| 29 | if !strings.Contains(svg, " V ") && !strings.Contains(svg, "V ") { |
| 30 | t.Fatalf("verwacht verticale stukken in %s", svg) |
| 31 | } |
| 32 | if !strings.Contains(svg, " H ") { |
| 33 | t.Fatalf("verwacht horizontale stukken in %s", svg) |
| 34 | } |
| 35 | if strings.Contains(svg, "<circle") { |
| 36 | t.Fatalf("knopen horen avatars te zijn, geen svg-cirkels: %s", svg) |
| 37 | } |
| 38 | if !strings.Contains(svg, `data-branch="master"`) { |
| 39 | t.Fatalf("gloed-attribuut voor master ontbreekt: %s", svg) |
| 40 | } |
| 41 | if !strings.Contains(svg, `data-branch="feature"`) { |
| 42 | t.Fatalf("gloed-attribuut voor feature ontbreekt: %s", svg) |
| 43 | } |
| 44 | if !strings.Contains(svg, `stroke="var(--blue)"`) { |
| 45 | t.Fatalf("blauwe hoofdlijn ontbreekt: %s", svg) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestOrthogonalPathShape(t *testing.T) { |
| 50 | // zelfde lane: alleen verticaal |
| 51 | d := orthogonalPath(12, 14, 12, 70, 7) |
| 52 | if d != "M 12.0 14.0 V 70.0" { |
| 53 | t.Fatalf("verticaal: %s", d) |
| 54 | } |
| 55 | // andere lane: V, Q, H, Q, V |
| 56 | d = orthogonalPath(12, 14, 36, 70, 7) |
| 57 | if !strings.Contains(d, " Q ") || !strings.Contains(d, " H ") || !strings.Contains(d, " V ") { |
| 58 | t.Fatalf("bocht mist commando's: %s", d) |
| 59 | } |
| 60 | if strings.Contains(d, " C ") { |
| 61 | t.Fatalf("geen bezier: %s", d) |
| 62 | } |
| 63 | // de bocht gaat eerst naar beneden, dan opzij, dan weer naar beneden |
| 64 | if !strings.HasPrefix(d, "M 12.0 14.0 V ") { |
| 65 | t.Fatalf("start niet verticaal: %s", d) |
| 66 | } |
| 67 | if !strings.HasSuffix(d, " V 70.0") { |
| 68 | t.Fatalf("eindigt niet verticaal op het doel: %s", d) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestSVGHasNoViewBoxAndIsOneToOne(t *testing.T) { |
| 73 | refs := []Ref{ |
| 74 | {Name: "master", SHA: "m", Kind: "branch"}, |
| 75 | {Name: "feature", SHA: "f", Kind: "branch"}, |
| 76 | } |
| 77 | g := Layout(commits("m:c,f", "f:b", "c:b", "b:"), refs, "master") |
| 78 | svg := g.SVG() |
| 79 | if strings.Contains(svg, "viewBox") { |
| 80 | t.Fatalf("viewBox schaalt de tekening, dat mag niet: %s", svg) |
| 81 | } |
| 82 | want := `style="width:` + itoa(g.Width) + `px;height:` + itoa(g.Height) + `px;left:` + itoa(RefW) + `px"` |
| 83 | if !strings.Contains(svg, want) { |
| 84 | t.Fatalf("verwacht 1:1 inline-maat %s in %s", want, svg) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func itoa(n int) string { return strconv.Itoa(n) } |
| 89 | |
| 90 | var pathDRe = regexp.MustCompile(`d="([^"]+)"`) |
| 91 | |
| 92 | // svgPathXs leest de x-coördinaten uit de padcommando's van onze SVG |
| 93 | // (M x y, V y, H x, Q cx cy, x y). |
| 94 | func svgPathXs(svg string) []float64 { |
| 95 | var xs []float64 |
| 96 | for _, m := range pathDRe.FindAllStringSubmatch(svg, -1) { |
| 97 | xs = append(xs, pathXs(m[1])...) |
| 98 | } |
| 99 | return xs |
| 100 | } |
| 101 | |
| 102 | func pathXs(d string) []float64 { |
| 103 | var xs []float64 |
| 104 | tokens := strings.FieldsFunc(d, func(r rune) bool { |
| 105 | return r == ',' || r == ' ' || r == '\t' |
| 106 | }) |
| 107 | cmd := "" |
| 108 | arg := 0 |
| 109 | for _, tok := range tokens { |
| 110 | if tok == "" { |
| 111 | continue |
| 112 | } |
| 113 | if strings.ContainsAny(tok[:1], "MLHVCSQTAZmlhvcsqtaz") { |
| 114 | cmd = tok[:1] |
| 115 | arg = 0 |
| 116 | tok = tok[1:] |
| 117 | if tok == "" { |
| 118 | continue |
| 119 | } |
| 120 | } |
| 121 | n, err := strconv.ParseFloat(tok, 64) |
| 122 | if err != nil { |
| 123 | continue |
| 124 | } |
| 125 | switch cmd { |
| 126 | case "M", "L", "Q", "T", "S", "C", "A": |
| 127 | // even args are x |
| 128 | if arg%2 == 0 { |
| 129 | xs = append(xs, n) |
| 130 | } |
| 131 | arg++ |
| 132 | case "H": |
| 133 | xs = append(xs, n) |
| 134 | arg++ |
| 135 | case "V": |
| 136 | arg++ |
| 137 | } |
| 138 | } |
| 139 | return xs |
| 140 | } |
| 141 | |
| 142 | func TestSVGPathXsStayLeftOfText(t *testing.T) { |
| 143 | refs := []Ref{ |
| 144 | {Name: "master", SHA: "m", Kind: "branch"}, |
| 145 | {Name: "feature", SHA: "e", Kind: "branch"}, |
| 146 | {Name: "docs", SHA: "d", Kind: "branch"}, |
| 147 | } |
| 148 | g := Layout(commits("d:m", "m:c,e", "e:b", "c:b", "b:a", "a:"), refs, "master") |
| 149 | xs := svgPathXs(g.SVG()) |
| 150 | if len(xs) == 0 { |
| 151 | t.Fatal("geen pad-x gevonden") |
| 152 | } |
| 153 | limit := float64(g.TextX - TextPad) |
| 154 | for _, x := range xs { |
| 155 | if x > limit { |
| 156 | t.Fatalf("pad-x %.1f ligt rechts van TextX-pad %.1f", x, limit) |
| 157 | } |
| 158 | } |
| 159 | } |