Debian Codegraph @

highlight_test.go

raw symbols SCIP
1 package syntaxhighlight
2
3 import (
4 "bytes"
5 "flag"
6 "io/ioutil"
7 "path/filepath"
8 "reflect"
9 "strings"
10 "testing"
11
12 "github.com/kr/pretty"
13 "github.com/sourcegraph/annotate"
14 )
15
16 var saveExp = flag.Bool("exp", false, "overwrite all expected output files with actual output (returning a failure)")
17 var match = flag.String("m", "", "only run tests whose name contains this string")
18
19 func testExpected(t *testing.T, fn, path, name, ext string, got []byte, err error) {
20 if err != nil {
21 t.Errorf("%s: %s: %s", fn, name, err)
22 return
23 }
24
25 expPath := path + ext
26 if *saveExp {
27 err = ioutil.WriteFile(expPath, got, 0700)
28 if err != nil {
29 t.Fatal(err)
30 }
31 return
32 }
33
34 want, err := ioutil.ReadFile(expPath)
35 if err != nil {
36 t.Fatal(err)
37 }
38
39 want = bytes.TrimSpace(want)
40 got = bytes.TrimSpace(got)
41
42 if !bytes.Equal(want, got) {
43 t.Errorf("%s: %s:\nwant ==========\n%q\ngot ===========\n%q", fn, name, want, got)
44 return
45 }
46 return
47 }
48
49 func TestAsHTML(t *testing.T) {
50 dir := "testdata"
51 tests, err := ioutil.ReadDir(dir)
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 for _, test := range tests {
57 name := test.Name()
58 if !strings.Contains(name, *match) {
59 continue
60 }
61 if strings.HasSuffix(name, ".html") {
62 continue
63 }
64 if name == "net_http_client.go" {
65 // only use this file for benchmarking
66 continue
67 }
68 path := filepath.Join(dir, name)
69 input, err := ioutil.ReadFile(path)
70 if err != nil {
71 t.Fatal(err)
72 continue
73 }
74
75 got, err := AsHTML(input)
76 testExpected(t, "AsHTML", path, name, ".html", got, err)
77
78 got, err = AsHTML(input, OrderedList())
79 testExpected(t, "AsOrderedListHTML", path, name, ".ol.html", got, err)
80 }
81
82 if *saveExp {
83 t.Fatal("overwrote all expected output files with actual output (run tests again without -exp)")
84 }
85 }
86
87 func TestAnnotate(t *testing.T) {
88 src := []byte(`a:=2`)
89 want := annotate.Annotations{
90 {Start: 0, End: 1, Left: []byte(`<span class="pln">`), Right: []byte("</span>")},
91 {Start: 1, End: 2, Left: []byte(`<span class="pun">`), Right: []byte("</span>")},
92 {Start: 2, End: 3, Left: []byte(`<span class="pun">`), Right: []byte("</span>")},
93 {Start: 3, End: 4, Left: []byte(`<span class="dec">`), Right: []byte("</span>")},
94 }
95 got, err := Annotate(src, HTMLAnnotator(DefaultHTMLConfig))
96 if err != nil {
97 t.Fatal(err)
98 }
99
100 if !reflect.DeepEqual(want, got) {
101 t.Errorf("want %# v, got %# v\n\ndiff:\n%v", pretty.Formatter(want), pretty.Formatter(got), strings.Join(pretty.Diff(got, want), "\n"))
102 for _, g := range got {
103 t.Logf("%+v %q LEFT=%q RIGHT=%q", g, src[g.Start:g.End], g.Left, g.Right)
104 }
105 }
106 }
107
108 func BenchmarkAnnotate(b *testing.B) {
109 input, err := ioutil.ReadFile("testdata/net_http_client.go")
110 if err != nil {
111 b.Fatal(err)
112 }
113
114 b.ResetTimer()
115 for i := 0; i < b.N; i++ {
116 _, err := Annotate(input[:2000], HTMLAnnotator(DefaultHTMLConfig))
117 if err != nil {
118 b.Fatal(err)
119 }
120 }
121 }