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 }