Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: fix data race in TestCheckReplace #5

Merged
merged 4 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build: ## build misspell
go build ./cmd/misspell

test: ## run all tests
go test -v .
CGO_ENABLED=1 go test -v -race .

lint: ## run linter
golangci-lint run
Expand Down
120 changes: 68 additions & 52 deletions replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,61 +83,77 @@ func TestReplace(t *testing.T) {
}

func TestCheckReplace(t *testing.T) {
r := Replacer{
engine: NewStringReplacer("foo", "foobar", "runing", "running"),
corrected: map[string]string{
"foo": "foobar",
"runing": "running",
},
}
t.Run("Nothing at all", func(t *testing.T) {
t.Parallel()

s := "nothing at all"
news, diffs := r.Replace(s)
if s != news || len(diffs) != 0 {
t.Errorf("Basic recheck failed: %q vs %q", s, news)
}
r := Replacer{
engine: NewStringReplacer("foo", "foobar"),
corrected: map[string]string{"foo": "foobar"},
}

//
// Test single, correct,.Correctedacements
//
testCases := []struct {
orig string
expected string
}{
{
orig: "foo",
expected: "foobar",
},
{
orig: "foo junk",
expected: "foobar junk",
},
{
orig: "junk foo",
expected: "junk foobar",
},
{
orig: "junk foo junk",
expected: "junk foobar junk",
},
}
s := "nothing at all"
news, diffs := r.Replace(s)
if s != news || len(diffs) != 0 {
t.Errorf("Basic recheck failed: %q vs %q", s, news)
}
})

t.Run("Single, correct,.Correctedacements", func(t *testing.T) {
t.Parallel()

testCases := []struct {
orig string
expected string
}{
{
orig: "foo",
expected: "foobar",
},
{
orig: "foo junk",
expected: "foobar junk",
},
{
orig: "junk foo",
expected: "junk foobar",
},
{
orig: "junk foo junk",
expected: "junk foobar junk",
},
}

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

r := Replacer{
engine: NewStringReplacer("foo", "foobar", "runing", "running"),
corrected: map[string]string{"foo": "foobar", "runing": "running"},
}

news, diffs := r.Replace(orig)
if news != expected || len(diffs) != 1 || diffs[0].Original != "foo" && diffs[0].Corrected != expected && diffs[0].Column != 0 {
t.Errorf("basic recheck failed %q vs %q", expected, news)
}
})
}
})

news, diffs = r.Replace(test.orig)
if news != test.expected || len(diffs) != 1 || diffs[0].Original != "foo" && diffs[0].Corrected != test.expected && diffs[0].Column != 0 {
t.Errorf("basic recheck failed %q vs %q", s, news)
}
})
}
t.Run("Incorrect.Correctedacements", func(t *testing.T) {
t.Parallel()

// Incorrect.Correctedacements
s = "food pruning"
news, _ = r.Replace(s)
if news != s {
t.Errorf("incorrect.Correctedacement failed: %q vs %q", s, news)
}
r := Replacer{
engine: NewStringReplacer("foo", "foobar"),
corrected: map[string]string{"foo": "foobar"},
}

s := "food pruning"
news, _ := r.Replace(s)
if news != s {
t.Errorf("incorrect.Correctedacement failed: %q vs %q", s, news)
}
})
}