Skip to content

Commit 8018d63

Browse files
committed
Rollup merge of rust-lang#54119 - phansch:unit_test_find_best_match_for_name, r=nikomatsakis
Add some unit tests for find_best_match_for_name There were only some UI tests that covered this function. Since there's more diagnostic work going on, I think it makes sense to have this unit tested.
2 parents 344dc53 + 09973d2 commit 8018d63

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/libsyntax/util/lev_distance.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::cmp;
1212
use symbol::Symbol;
1313

14-
/// To find the Levenshtein distance between two strings
14+
/// Find the Levenshtein distance between two strings
1515
pub fn lev_distance(a: &str, b: &str) -> usize {
1616
// cases which don't require further computation
1717
if a.is_empty() {
@@ -41,10 +41,12 @@ pub fn lev_distance(a: &str, b: &str) -> usize {
4141
} dcol[t_last + 1]
4242
}
4343

44-
/// To find the best match for a given string from an iterator of names
44+
/// Find the best match for a given word in the given iterator
45+
///
4546
/// As a loose rule to avoid the obviously incorrect suggestions, it takes
4647
/// an optional limit for the maximum allowable edit distance, which defaults
4748
/// to one-third of the given word.
49+
///
4850
/// Besides Levenshtein, we use case insensitive comparison to improve accuracy on an edge case with
4951
/// a lower(upper)case letters mismatch.
5052
pub fn find_best_match_for_name<'a, T>(iter_names: T,
@@ -105,3 +107,39 @@ fn test_lev_distance() {
105107
assert_eq!(lev_distance(b, c), 1);
106108
assert_eq!(lev_distance(c, b), 1);
107109
}
110+
111+
#[test]
112+
fn test_find_best_match_for_name() {
113+
use with_globals;
114+
with_globals(|| {
115+
let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
116+
assert_eq!(
117+
find_best_match_for_name(input.iter(), "aaaa", None),
118+
Some(Symbol::intern("aaab"))
119+
);
120+
121+
assert_eq!(
122+
find_best_match_for_name(input.iter(), "1111111111", None),
123+
None
124+
);
125+
126+
let input = vec![Symbol::intern("aAAA")];
127+
assert_eq!(
128+
find_best_match_for_name(input.iter(), "AAAA", None),
129+
Some(Symbol::intern("aAAA"))
130+
);
131+
132+
let input = vec![Symbol::intern("AAAA")];
133+
// Returns None because `lev_distance > max_dist / 3`
134+
assert_eq!(
135+
find_best_match_for_name(input.iter(), "aaaa", None),
136+
None
137+
);
138+
139+
let input = vec![Symbol::intern("AAAA")];
140+
assert_eq!(
141+
find_best_match_for_name(input.iter(), "aaaa", Some(4)),
142+
Some(Symbol::intern("AAAA"))
143+
);
144+
})
145+
}

0 commit comments

Comments
 (0)