package glossary import "testing" func TestPunctuationEdgedSpellings(t *testing.T) { entries := []Entry{{ID: "dotnet", Name: ".NET"}, {ID: "cpp", Name: "C++"}, {ID: "ward", Name: "[Ward]"}} vocab := NewProtectedVocabulary(Vocabulary(entries, nil)) for _, tc := range []struct { text, identity string }{ {".NET", "dotnet"}, {"Using C++.", "cpp"}, {"Take ([Ward]).", "ward"}, {"x.NET", ""}, {"C++like", ""}, {"x[Ward]", ""}, } { t.Run(tc.text, func(t *testing.T) { mentioned := vocab.MentionedIdentities(tc.text) if tc.identity == "" { if len(mentioned) != 0 || !vocab.AllowsChange(tc.text, "", false) { t.Fatalf("embedded spelling matched: %v", mentioned) } return } if _, found := mentioned[Identity{ID: tc.identity}]; !found || len(mentioned) != 1 || vocab.AllowsChange(tc.text, "", false) { t.Fatalf("punctuation-edged spelling missing: %v", mentioned) } }) } } func TestSharedSpellingContributesEveryIdentity(t *testing.T) { entries := []Entry{ {Collection: "a", ID: "first", Name: "First", Aliases: []string{"Shared"}}, {Collection: "b", ID: "second", Name: "Second", Aliases: []string{"Shared"}}, } vocab := NewProtectedVocabulary(Vocabulary(entries, nil)) ids := vocab.MentionedIdentities("Shared shared", "Shared") if len(ids) != 2 { t.Fatalf("mentioned identities: %v", ids) } for _, entry := range entries { id := Identity{Collection: entry.Collection, ID: entry.ID} if _, found := ids[id]; !found { t.Errorf("missing identity: %v", id) } } if vocab.AllowsChange("Shared", "First", false) { t.Fatal("second identity disappeared") } }