-
Notifications
You must be signed in to change notification settings - Fork 414
Strengthen fuzzy_match edge-case contracts in stringutil tests #35077
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,34 @@ func TestFindClosestMatches(t *testing.T) { | |
| maxResults: 3, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "nil candidates returns nil", | ||
| target: "copilot", | ||
| candidates: nil, | ||
| maxResults: 3, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "maxResults zero returns nil", | ||
| target: "copiliot", | ||
| candidates: []string{"copilot", "claude"}, | ||
| maxResults: 0, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "distance four candidate excluded", | ||
| target: "abc", | ||
| candidates: []string{"abx", "abcdefg"}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The test correctly excludes 💡 Suggestion{
name: "distance four candidate excluded",
target: "abc",
// "abcdefg" has distance 4 (> maxDistance of 3), so only "abx" (distance 1) is returned.
candidates: []string{"abx", "abcdefg"},
maxResults: 3,
want: []string{"abx"},
},This is especially useful because the |
||
| maxResults: 3, | ||
| want: []string{"abx"}, | ||
| }, | ||
| { | ||
| name: "alphabetical tie breaking for equal distances", | ||
| target: "zzzz", | ||
| candidates: []string{"zzzb", "zzza"}, | ||
| maxResults: 2, | ||
| want: []string{"zzza", "zzzb"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
|
|
@@ -112,6 +140,8 @@ func TestLevenshteinDistance(t *testing.T) { | |
| {name: "push vs pus", a: "push", b: "pus", want: 1}, | ||
| {name: "contents vs scope typo", a: "contents", b: "cntents", want: 1}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Great documentation of the byte-vs-rune semantic. One follow-up worth considering: 💡 Suggested additional test for FindClosestMatchesA companion test in {
// strings.ToLower is rune-aware; LevenshteinDistance is byte-based.
// A multibyte candidate may appear closer or farther than expected.
name: "multibyte candidate utf8 byte distance",
target: "e",
candidates: []string{"é"}, // byte distance 2 — exceeds cutoff, excluded
maxResults: 3,
want: nil,
},This is not a bug introduced by this PR — it's pre-existing — but the byte-level test only covers |
||
| {name: "completely different", a: "xyz", b: "abc", want: 3}, | ||
| // LevenshteinDistance operates on bytes, not runes: "é" is two bytes in UTF-8. | ||
| {name: "multibyte utf8 compares bytes", a: "é", b: "e", want: 2}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd] This
nilcandidates case duplicates the contract already proven by"empty candidates returns nothing"— both paths hit the samerangeno-op.💡 Suggestion
Either remove this case, or add a clarifying comment that distinguishes why
nilis worth a separate assertion (e.g., to guard against panics from a nil check that doesn't exist today but could be added):Without that rationale the reader may wonder why both exist.