package editor import ( "testing" "glint/internal/spell" ) func spellEditor(t *testing.T, content string) *Editor { t.Helper() d, err := spell.Load() if err != nil { t.Fatalf("spell.Load: %v", err) } e := New() e.SetContent([]byte(content)) e.SetDict(d) e.SetSpell(true) return e } // wavyWords returns the set of span texts rendered with the misspell undercurl. func wavyWords(e *Editor) map[string]bool { got := map[string]bool{} for _, vr := range e.buildVisual() { for _, sp := range vr.spans { if sp.Wavy { got[sp.Text] = true } } } return got } func TestSpellFlagsMisspelledProse(t *testing.T) { e := spellEditor(t, "This sentence has a recieve typo") w := wavyWords(e) if !w["recieve"] { t.Errorf("misspelled \"recieve\" not flagged; wavy=%v", w) } if w["sentence"] || w["typo"] || w["This"] { t.Errorf("correct words flagged; wavy=%v", w) } } func TestSpellSkipsInlineCode(t *testing.T) { e := spellEditor(t, "use the `recieve` function") if wavyWords(e)["recieve"] { t.Error("inline code content should not be spellchecked") } } func TestSpellSkipsURLs(t *testing.T) { e := spellEditor(t, "see https://recieve.example.com/seperate for more") w := wavyWords(e) if w["recieve"] || w["seperate"] { t.Errorf("URL words should not be flagged; wavy=%v", w) } } func TestSpellSkipsWikilinks(t *testing.T) { e := spellEditor(t, "linked [[recieve note]] here") if wavyWords(e)["recieve"] { t.Error("wikilink target should not be spellchecked") } } func TestSpellSkipsFrontmatter(t *testing.T) { e := spellEditor(t, "---\ntitle: recieve draft\n---\nbody text") if wavyWords(e)["recieve"] { t.Error("frontmatter value should not be spellchecked") } } func TestSpellSkipsAcronymsAndShortWords(t *testing.T) { e := spellEditor(t, "the NASA API is ok") w := wavyWords(e) if w["NASA"] || w["API"] || w["ok"] || w["is"] { t.Errorf("acronyms/short words should not be flagged; wavy=%v", w) } } func TestSpellOffWhenDisabled(t *testing.T) { e := spellEditor(t, "a recieve typo") e.SetSpell(false) if len(wavyWords(e)) != 0 { t.Error("no words should be flagged when spellcheck is off") } } func TestSpellOffForCodeFiles(t *testing.T) { e := spellEditor(t, "recieve := 1") e.SetLanguage("main.go") // code file: spellcheck must stay off if len(wavyWords(e)) != 0 { t.Error("code files should not be spellchecked") } } func TestSpellHonorsPersonalAdd(t *testing.T) { e := spellEditor(t, "my zzplonk word") if !wavyWords(e)["zzplonk"] { t.Fatal("precondition: zzplonk should flag") } e.AddToDictionary("zzplonk") if wavyWords(e)["zzplonk"] { t.Error("word added to dictionary should clear its underline live") } }