1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package syntax
import "testing"
func TestCacheScansOncePerRevision(t *testing.T) {
c := NewCache(LanguageTOML)
c.Update("name = \"turbo\"\n", 1)
first := c.Line(0)
c.Update("this is different but the revision did not move", 1)
if got := c.Line(0); len(got) != len(first) || got[0] != first[0] {
t.Error("the cache re-scanned although the revision was unchanged")
}
}
func TestCacheRescansWhenTheRevisionMoves(t *testing.T) {
c := NewCache(LanguageTOML)
c.Update("name = \"turbo\"\n", 1)
c.Update("# only a comment\n", 2)
if got, ok := firstClass(c.Line(0)); !ok || got != ClassComment {
t.Errorf("after the revision moved, line 0 is %v, want comment", got)
}
}
func TestCacheScansOnItsFirstUpdateEvenAtRevisionZero(t *testing.T) {
c := NewCache(LanguageTOML)
c.Update("name = \"turbo\"", 0)
if len(c.Line(0)) == 0 {
t.Error("the first Update produced nothing; revision zero must still be scanned")
}
}
func TestADisabledCacheColoursNothing(t *testing.T) {
c := NewCache(LanguageNone)
c.Update("name = \"turbo\"", 1)
if c.Enabled() {
t.Error("Enabled() = true on a cache created disabled")
}
if len(c.Line(0)) != 0 {
t.Error("a disabled cache produced spans")
}
if c.LineCount() != 0 {
t.Errorf("LineCount() = %d on a disabled cache, want 0", c.LineCount())
}
}
func TestSetEnabledDiscardsWhatWasScanned(t *testing.T) {
c := NewCache(LanguageTOML)
c.Update("name = \"turbo\"", 1)
c.SetLanguage(LanguageNone)
if len(c.Line(0)) != 0 {
t.Error("turning colouring off left the previous scan behind")
}
c.SetLanguage(LanguageTOML)
c.Update("name = \"turbo\"", 1) // the same revision as before
if len(c.Line(0)) == 0 {
t.Error("turning colouring back on must force a re-scan, not trust the stale revision")
}
}
func TestCacheLineOutOfRange(t *testing.T) {
c := NewCache(LanguageTOML)
c.Update("a = 1\nb = 2", 1)
if c.LineCount() != 2 {
t.Fatalf("LineCount() = %d, want 2", c.LineCount())
}
for _, i := range []int{-1, 2, 99} {
if c.Line(i) != nil {
t.Errorf("Line(%d) returned spans, want nil", i)
}
}
}
// firstClass returns the class of the first span, if there is one.
func firstClass(spans []Span) (Class, bool) {
if len(spans) == 0 {
return 0, false
}
return spans[0].Class, true
}
|