Skip to content

Commit

Permalink
chore: review tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Oct 12, 2022
1 parent 6890413 commit 0813272
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 84 deletions.
6 changes: 3 additions & 3 deletions case.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func CaseStyle(word string) WordCase {
}

// CaseVariations returns:
// If AllUpper or First-Letter-Only is upcased: add the all upper case version.
// If AllLower, add the original, the title and upcase forms.
// If Mixed, return the original, and the all upcase form.
// If AllUpper or First-Letter-Only is upper-cased: add the all upper case version.
// If AllLower, add the original, the title and upper-case forms.
// If Mixed, return the original, and the all upper-case form.
func CaseVariations(word string, style WordCase) []string {
switch style {
case CaseLower:
Expand Down
49 changes: 30 additions & 19 deletions case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,48 @@ import (
)

func TestCaseStyle(t *testing.T) {
cases := []struct {
testCases := []struct {
word string
want WordCase
}{
{"lower", CaseLower},
{"what's", CaseLower},
{"UPPER", CaseUpper},
{"Title", CaseTitle},
{"CamelCase", CaseUnknown},
{"camelCase", CaseUnknown},
{word: "lower", want: CaseLower},
{word: "what's", want: CaseLower},
{word: "UPPER", want: CaseUpper},
{word: "Title", want: CaseTitle},
{word: "CamelCase", want: CaseUnknown},
{word: "camelCase", want: CaseUnknown},
}

for pos, tt := range cases {
got := CaseStyle(tt.word)
if tt.want != got {
t.Errorf("Case %d %q: want %v got %v", pos, tt.word, tt.want, got)
}
for _, test := range testCases {
test := test
t.Run(test.word, func(t *testing.T) {
t.Parallel()

got := CaseStyle(test.word)
if test.want != got {
t.Errorf("want %v got %v", test.want, got)
}
})
}
}

func TestCaseVariations(t *testing.T) {
cases := []struct {
testCases := []struct {
word string
want []string
}{
{"that's", []string{"that's", "That's", "THAT'S"}},
{word: "that's", want: []string{"that's", "That's", "THAT'S"}},
}
for pos, tt := range cases {
got := CaseVariations(tt.word, CaseStyle(tt.word))
if !reflect.DeepEqual(tt.want, got) {
t.Errorf("Case %d %q: want %v got %v", pos, tt.word, tt.want, got)
}

for _, test := range testCases {
test := test
t.Run(test.word, func(t *testing.T) {
t.Parallel()

got := CaseVariations(test.word, CaseStyle(test.word))
if !reflect.DeepEqual(test.want, got) {
t.Errorf("want %v got %v", test.want, got)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/misspell/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func main() {
// Log is routine safe.
// we see it, so it doesn't use a prefix or include a time stamp.
switch {
case *quietFlag || *outFlag == "/dev/null":
case *quietFlag || *outFlag == os.DevNull:
stdout = log.New(io.Discard, "", 0)
case *outFlag == "/dev/stderr" || *outFlag == "stderr":
stdout = log.New(os.Stderr, "", 0)
Expand Down
19 changes: 13 additions & 6 deletions falsepositives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestFalsePositives(t *testing.T) {
cases := []string{
testCases := []string{
"importEnd",
"drinkeries",
"subscripting",
Expand Down Expand Up @@ -125,12 +125,19 @@ func TestFalsePositives(t *testing.T) {
"\\nto", // https://github.com/client9/misspell/issues/93
"4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358", // https://github.com/client9/misspell/issues/97
}

r := New()
r.Debug = true
for casenum, tt := range cases {
got, _ := r.Replace(tt)
if got != tt {
t.Errorf("%d: %q got converted to %q", casenum, tt, got)
}

for _, test := range testCases {
test := test
t.Run(test, func(t *testing.T) {
t.Parallel()

got, _ := r.Replace(test)
if got != test {
t.Errorf("%q got converted to %q", test, got)
}
})
}
}
3 changes: 3 additions & 0 deletions ignore/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func Parse(src []byte) (*MultiMatch, error) {
var matchers []Matcher
lines := bytes.Split(src, []byte{'\n'})

for _, line := range lines {
if len(line) == 0 || len(bytes.TrimSpace(line)) == 0 {
continue
Expand All @@ -29,7 +30,9 @@ func Parse(src []byte) (*MultiMatch, error) {
if err != nil {
return nil, err
}

matchers = append(matchers, m)
}

return NewMultiMatch(matchers), nil
}
84 changes: 43 additions & 41 deletions ignore/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,57 @@ import (
)

func TestParseMatchSingle(t *testing.T) {
cases := []struct {
testCases := []struct {
pattern string
filename string
want bool
}{
{"*.c", "foo.c", true},
{"*.c", "foo/bar.c", true},
{"Documentation/*.html", "Documentation/git.html", true},
{"Documentation/*.html", "Documentation/ppc/ppc.html", false},
{"/*.c", "cat-file.c", true},
{"/*.c", "mozilla-sha1/sha1.c", false},
{"foo", "foo", true},
{"**/foo", "./foo", true}, // <--- leading './' required
{"**/foo", "junk/foo", true},
{"**/foo/bar", "./foo/bar", true}, // <--- leading './' required
{"**/foo/bar", "junk/foo/bar", true},
{"abc/**", "abc/foo", true},
{"abc/**", "abc/foo/bar", true},
{"a/**/b", "a/b", true},
{"a/**/b", "a/x/b", true},
{"a/**/b", "a/x/y/b", true},

{"*_test*", "foo_test.go", true},
{"*_test*", "junk/foo_test.go", true},
{"junk\n!junk", "foo", false},
{"junk\n!junk", "junk", false},

{"*.html\n!foo.html", "junk.html", true},
{"*.html\n!foo.html", "foo.html", false},

{"/*\n!/foo\n/foo/*\n!/foo/bar", "crap", true},
{"/*\n!/foo\n/foo/*\n!/foo/bar", "foo/crap", true},
{"/*\n!/foo\n/foo/*\n!/foo/bar", "foo/bar", false},
{"/*\n!/foo\n/foo/*\n!/foo/bar", "foo/bar/other", false},
{"/*\n!/foo\n/foo/*\n!/foo/bar", "foo", false},
{pattern: "*.c", filename: "foo.c", want: true},
{pattern: "*.c", filename: "foo/bar.c", want: true},
{pattern: "Documentation/*.html", filename: "Documentation/git.html", want: true},
{pattern: "Documentation/*.html", filename: "Documentation/ppc/ppc.html"},
{pattern: "/*.c", filename: "cat-file.c", want: true},
{pattern: "/*.c", filename: "mozilla-sha1/sha1.c"},
{pattern: "foo", filename: "foo", want: true},
{pattern: "**/foo", filename: "./foo", want: true}, // <--- leading './' required
{pattern: "**/foo", filename: "junk/foo", want: true},
{pattern: "**/foo/bar", filename: "./foo/bar", want: true}, // <--- leading './' required
{pattern: "**/foo/bar", filename: "junk/foo/bar", want: true},
{pattern: "abc/**", filename: "abc/foo", want: true},
{pattern: "abc/**", filename: "abc/foo/bar", want: true},
{pattern: "a/**/b", filename: "a/b", want: true},
{pattern: "a/**/b", filename: "a/x/b", want: true},
{pattern: "a/**/b", filename: "a/x/y/b", want: true},

{pattern: "*_test*", filename: "foo_test.go", want: true},
{pattern: "*_test*", filename: "junk/foo_test.go", want: true},
{pattern: "junk\n!junk", filename: "foo"},
{pattern: "junk\n!junk", filename: "junk"},

{pattern: "*.html\n!foo.html", filename: "junk.html", want: true},
{pattern: "*.html\n!foo.html", filename: "foo.html"},

{pattern: "/*\n!/foo\n/foo/*\n!/foo/bar", filename: "crap", want: true},
{pattern: "/*\n!/foo\n/foo/*\n!/foo/bar", filename: "foo/crap", want: true},
{pattern: "/*\n!/foo\n/foo/*\n!/foo/bar", filename: "foo/bar"},
{pattern: "/*\n!/foo\n/foo/*\n!/foo/bar", filename: "foo/bar/other"},
{pattern: "/*\n!/foo\n/foo/*\n!/foo/bar", filename: "foo"},
}
for _, test := range cases {

for _, test := range testCases {
test := test
t.Run(test.pattern, func(t *testing.T) {
t.Parallel()
})

matcher, err := Parse([]byte(test.pattern))
if err != nil {
t.Errorf("error: %s", err)
}
got := matcher.Match(test.filename)
if test.want != got {
t.Errorf("%q.Match(%q) = %v, got %v", test.pattern, test.filename, test.want, got)
}
matcher, err := Parse([]byte(test.pattern))
if err != nil {
t.Errorf("error: %s", err)
}

got := matcher.Match(test.filename)
if test.want != got {
t.Errorf("%q.Match(%q) = %v, got %v", test.pattern, test.filename, test.want, got)
}
})
}
}
34 changes: 20 additions & 14 deletions notwords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ import (
)

func TestNotWords(t *testing.T) {
cases := []struct {
testCases := []struct {
word string
want string
}{
{" /foo/bar abc", " abc"},
{"X/foo/bar abc", "X/foo/bar abc"},
{"[/foo/bar] abc", "[ ] abc"},
{"/", "/"},
{"x nickg@client9.xxx y", "x y"},
{"x infinitie.net y", "x y"},
{"(s.svc.GetObject(", "( ("},
{"\\nto", " to"},
{word: " /foo/bar abc", want: " abc"},
{word: "X/foo/bar abc", want: "X/foo/bar abc"},
{word: "[/foo/bar] abc", want: "[ ] abc"},
{word: "/", want: "/"},
{word: "x nickg@client9.xxx y", want: "x y"},
{word: "x infinitie.net y", want: "x y"},
{word: "(s.svc.GetObject(", want: "( ("},
{word: "\\nto", want: " to"},
}
for pos, tt := range cases {
got := RemoveNotWords(tt.word)
if got != tt.want {
t.Errorf("%d want %q got %q", pos, tt.want, got)
}

for _, test := range testCases {
test := test
t.Run(test.word, func(t *testing.T) {
t.Parallel()

got := RemoveNotWords(test.word)
if got != test.want {
t.Errorf("want %q got %q", test.want, got)
}
})
}
}

0 comments on commit 0813272

Please sign in to comment.