|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/errata-ai/vale/v3/internal/core" |
| 7 | + "github.com/errata-ai/vale/v3/internal/nlp" |
| 8 | +) |
| 9 | + |
| 10 | +// A sequence position names the word it accepts, so it has to match the whole |
| 11 | +// token. |
| 12 | +// |
| 13 | +// The regex that finds candidate positions is deliberately unanchored -- it is |
| 14 | +// run against the whole sentence -- and reusing it to test a token made |
| 15 | +// `pattern: self` accept the single token `self-worth`, so a rule for |
| 16 | +// `your self` fired on `your self-worth`. |
| 17 | +func TestSequenceMatchesWholeTokens(t *testing.T) { |
| 18 | + rule, err := NewSequence(testConfig(), baseCheck{ |
| 19 | + "extends": "sequence", |
| 20 | + "name": "Test.Yourself", |
| 21 | + "level": "error", |
| 22 | + "message": "Did you mean 'yourself'?", |
| 23 | + "tokens": []interface{}{ |
| 24 | + map[string]interface{}{"pattern": "your"}, |
| 25 | + map[string]interface{}{"pattern": "self"}, |
| 26 | + }, |
| 27 | + }, "Test.Yourself") |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("building rule: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + cases := []struct { |
| 33 | + name string |
| 34 | + text string |
| 35 | + want int |
| 36 | + }{ |
| 37 | + {"exact words", "Ask your self what matters.", 1}, |
| 38 | + {"hyphenated compound", "Question your self-worth sometimes.", 0}, |
| 39 | + {"longer word", "Consider your selfishness here.", 0}, |
| 40 | + } |
| 41 | + |
| 42 | + for _, c := range cases { |
| 43 | + t.Run(c.name, func(t *testing.T) { |
| 44 | + f := &core.File{NLP: nlp.Info{}} |
| 45 | + |
| 46 | + alerts, rerr := rule.Run(nlp.NewBlock(c.text, c.text, "text"), f, testConfig()) |
| 47 | + if rerr != nil { |
| 48 | + t.Fatalf("running rule: %v", rerr) |
| 49 | + } |
| 50 | + if len(alerts) != c.want { |
| 51 | + t.Errorf("%q produced %d alerts, want %d", c.text, len(alerts), c.want) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func testConfig() *core.Config { |
| 58 | + return &core.Config{WordTemplate: wordTemplate} |
| 59 | +} |
0 commit comments