|
| 1 | +package golinters |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/golangci/misspell" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/golangci/golangci-lint/pkg/config" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_appendExtraWords(t *testing.T) { |
| 14 | + extraWords := []config.MisspellExtraWords{ |
| 15 | + { |
| 16 | + Typo: "iff", |
| 17 | + Correction: "if", |
| 18 | + }, |
| 19 | + { |
| 20 | + Typo: "canCELation", |
| 21 | + Correction: "canceLLaTION", |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + replacer := &misspell.Replacer{} |
| 26 | + |
| 27 | + err := appendExtraWords(replacer, extraWords) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + expected := []string{"iff", "if", "cancelation", "cancellation"} |
| 31 | + |
| 32 | + assert.Equal(t, replacer.Replacements, expected) |
| 33 | +} |
| 34 | + |
| 35 | +func Test_appendExtraWords_error(t *testing.T) { |
| 36 | + testCases := []struct { |
| 37 | + desc string |
| 38 | + extraWords []config.MisspellExtraWords |
| 39 | + expected string |
| 40 | + }{ |
| 41 | + { |
| 42 | + desc: "empty fields", |
| 43 | + extraWords: []config.MisspellExtraWords{{ |
| 44 | + Typo: "", |
| 45 | + Correction: "", |
| 46 | + }}, |
| 47 | + expected: `typo ("") and correction ("") fields should not be empty`, |
| 48 | + }, |
| 49 | + { |
| 50 | + desc: "empty typo", |
| 51 | + extraWords: []config.MisspellExtraWords{{ |
| 52 | + Typo: "", |
| 53 | + Correction: "if", |
| 54 | + }}, |
| 55 | + expected: `typo ("") and correction ("if") fields should not be empty`, |
| 56 | + }, |
| 57 | + { |
| 58 | + desc: "empty correction", |
| 59 | + extraWords: []config.MisspellExtraWords{{ |
| 60 | + Typo: "iff", |
| 61 | + Correction: "", |
| 62 | + }}, |
| 63 | + expected: `typo ("iff") and correction ("") fields should not be empty`, |
| 64 | + }, |
| 65 | + { |
| 66 | + desc: "invalid characters in typo", |
| 67 | + extraWords: []config.MisspellExtraWords{{ |
| 68 | + Typo: "i'ff", |
| 69 | + Correction: "if", |
| 70 | + }}, |
| 71 | + expected: `the word "i'ff" in the 'typo' field should only contain letters`, |
| 72 | + }, |
| 73 | + { |
| 74 | + desc: "invalid characters in correction", |
| 75 | + extraWords: []config.MisspellExtraWords{{ |
| 76 | + Typo: "iff", |
| 77 | + Correction: "i'f", |
| 78 | + }}, |
| 79 | + expected: `the word "i'f" in the 'correction' field should only contain letters`, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, test := range testCases { |
| 84 | + test := test |
| 85 | + t.Run(test.desc, func(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + |
| 88 | + replacer := &misspell.Replacer{} |
| 89 | + |
| 90 | + err := appendExtraWords(replacer, test.extraWords) |
| 91 | + require.EqualError(t, err, test.expected) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments