Something went wrong. Try again.
Fork of micro-editor/micro
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160package buffer
import ( "testing" "time"
"github.com/micro-editor/micro/v2/pkg/syntax")
func TestNoProviderKeepsWorking(t *testing.T) { syntax.Clear() b := NewBufferFromString("hello\nworld", "file.definitely-not-a-known-ext", BTDefault) defer b.Close()
if b.hl != nil { t.Fatal("unexpected provider session") } b.UpdateProvider() if b.Match(0) != nil || b.Match(1) != nil { t.Fatal("unexpected highlight match without a provider") }}
func TestApplySpansDropsStaleGeneration(t *testing.T) { syntax.Clear() defer syntax.Clear() syntax.Register(&syntax.Provider{ Name: "test-provider", Match: syntax.Matcher{FileType: "go"}, Request: func(syntax.Request) {}, })
old := HighlightDebounce HighlightDebounce = time.Hour defer func() { HighlightDebounce = old }()
b := NewBufferFromString("hello", "x.go", BTDefault) defer b.Close() if b.hl == nil { t.Fatal("provider did not attach") } if b.hl.timer != nil { b.hl.timer.Stop() b.hl.timer = nil }
gen := b.hlGen g := syntax.Intern("comment") spans := []syntax.Span{{Line: 0, StartByte: 0, EndByte: 5, Group: g}}
if b.ApplySpans(gen-1, spans) { t.Fatal("stale generation was applied") } if b.Match(0) != nil { t.Fatal("stale generation changed matches") } if !b.ApplySpans(gen, spans) { t.Fatal("current generation was rejected") } if got := b.Match(0)[0]; got != g { t.Fatalf("match[0] = %v, want %v", got, g) }
b.MarkModified(0, 0) if b.ApplySpans(gen, spans) { t.Fatal("reply from before an edit was applied") } if b.hl.timer != nil { b.hl.timer.Stop() b.hl.timer = nil }}
func TestApplySpansClearsUnmentionedLines(t *testing.T) { syntax.Clear() defer syntax.Clear() syntax.Register(&syntax.Provider{ Name: "test-provider", Match: syntax.Matcher{FileType: "go"}, Request: func(syntax.Request) {}, })
old := HighlightDebounce HighlightDebounce = time.Hour defer func() { HighlightDebounce = old }()
b := NewBufferFromString("one\ntwo", "x.go", BTDefault) defer b.Close() if b.hl.timer != nil { b.hl.timer.Stop() b.hl.timer = nil }
g := syntax.Intern("comment") if !b.ApplySpans(b.hlGen, []syntax.Span{{Line: 0, StartByte: 0, EndByte: 3, Group: g}}) { t.Fatal("spans rejected") } if b.Match(0)[0] != g { t.Fatal("line 0 not highlighted") } if m := b.Match(1); len(m) != 1 || m[0] != syntax.NoGroup { t.Fatalf("unmentioned line was not cleared: %v", m) }}
func TestSharedViewsShareProviderID(t *testing.T) { syntax.Clear() first := NewBufferFromString("x", "same.go", BTDefault) defer first.Close() second := NewBufferFromString("x", "same.go", BTDefault) defer second.Close()
if first.hlID == 0 || first.hlID != second.hlID { t.Fatalf("ids = %d and %d", first.hlID, second.hlID) }}
func TestSyntaxOffStopsProvider(t *testing.T) { syntax.Clear() defer syntax.Clear() syntax.Register(&syntax.Provider{ Name: "test-provider", Match: syntax.Matcher{FileType: "go"}, Request: func(syntax.Request) {}, })
old := HighlightDebounce HighlightDebounce = time.Hour defer func() { HighlightDebounce = old }()
b := NewBufferFromString("package main", "x.go", BTDefault) defer b.Close() if b.hl == nil { t.Fatal("provider did not attach") } if b.hl.timer != nil { b.hl.timer.Stop() b.hl.timer = nil }
b.Settings["syntax"] = false b.UpdateProvider() if b.hl != nil { t.Fatal("provider was not detached") } if b.FileType() != "go" { t.Fatalf("filetype = %q, want go", b.FileType()) }
b.Settings["syntax"] = true b.UpdateProvider() if b.hl == nil { t.Fatal("provider was not attached again") } if b.hl.timer != nil { b.hl.timer.Stop() b.hl.timer = nil }}