| 🛟 Updated. 28d5985 k33g 17h ago | 1 | package syntax |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func 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 | |
| 18 | func 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 | |
| 29 | func 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 | |
| 39 | func 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 | |
| 55 | func 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 | |
| 71 | func 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. |
| 86 | func firstClass(spans []Span) (Class, bool) { |
| 87 | if len(spans) == 0 { |
| 88 | return 0, false |
| 89 | } |
| 90 | return spans[0].Class, true |
| 91 | } |