turbo-editors/turbo-corepublic Fork 0
28d59854361aeda8541d853093e732126f3d7bff
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

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