Skip to content

Commit a628cbe

Browse files
committed
Refactor text and path tests with rstest
1 parent 4c93615 commit a628cbe

File tree

2 files changed

+85
-105
lines changed

2 files changed

+85
-105
lines changed

src/parser/path.rs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,24 @@ pub fn extract_title_from_path(path: &str) -> String {
1313

1414
#[cfg(test)]
1515
mod tests {
16-
use super::*;
17-
18-
#[test]
19-
fn extracts_title_from_path() {
20-
assert_eq!(extract_title_from_path("foo.txt"), "foo");
21-
assert_eq!(extract_title_from_path("/home/quin/books/worm.epub"), "worm");
22-
assert_eq!(extract_title_from_path("C:\\Users\\Quin\\Desktop\\file.log"), "file");
23-
assert_eq!(extract_title_from_path("/path/with/trailing/slash/"), "Untitled");
24-
assert_eq!(extract_title_from_path("C:\\path\\with\\trailing\\slash\\"), "Untitled");
25-
assert_eq!(extract_title_from_path(" spaced.txt "), "spaced");
26-
assert_eq!(extract_title_from_path(""), "Untitled");
27-
}
16+
use rstest::rstest;
2817

29-
#[test]
30-
fn extracts_title_from_paths_without_extension() {
31-
assert_eq!(extract_title_from_path("README"), "README");
32-
assert_eq!(extract_title_from_path("/var/log/system"), "system");
33-
}
34-
35-
#[test]
36-
fn returns_untitled_for_whitespace_only_or_trailing_separator() {
37-
assert_eq!(extract_title_from_path(" "), "Untitled");
38-
assert_eq!(extract_title_from_path(" /tmp/dir/ "), "Untitled");
39-
}
18+
use super::*;
4019

41-
#[test]
42-
fn extracts_title_from_multi_dot_filename() {
43-
assert_eq!(extract_title_from_path("archive.tar.gz"), "archive.tar");
20+
#[rstest]
21+
#[case("foo.txt", "foo")]
22+
#[case("/home/quin/books/worm.epub", "worm")]
23+
#[case("C:\\Users\\Quin\\Desktop\\file.log", "file")]
24+
#[case("/path/with/trailing/slash/", "Untitled")]
25+
#[case("C:\\path\\with\\trailing\\slash\\", "Untitled")]
26+
#[case(" spaced.txt ", "spaced")]
27+
#[case("", "Untitled")]
28+
#[case("README", "README")]
29+
#[case("/var/log/system", "system")]
30+
#[case(" ", "Untitled")]
31+
#[case(" /tmp/dir/ ", "Untitled")]
32+
#[case("archive.tar.gz", "archive.tar")]
33+
fn extracts_title_from_path(#[case] input: &str, #[case] expected: &str) {
34+
assert_eq!(extract_title_from_path(input), expected);
4435
}
4536
}

src/text.rs

Lines changed: 68 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -136,41 +136,50 @@ fn to_alpha(mut n: i32, uppercase: bool) -> String {
136136

137137
#[cfg(test)]
138138
mod tests {
139+
use rstest::rstest;
140+
139141
use super::*;
140142

141-
#[test]
142-
fn test_remove_soft_hyphens() {
143-
assert_eq!(remove_soft_hyphens("hel\u{00AD}lo"), "hello");
144-
assert_eq!(remove_soft_hyphens("no hyphens"), "no hyphens");
145-
assert_eq!(remove_soft_hyphens("mul\u{00AD}ti\u{00AD}ple"), "multiple");
143+
#[rstest]
144+
#[case("hel\u{00AD}lo", "hello")]
145+
#[case("no hyphens", "no hyphens")]
146+
#[case("mul\u{00AD}ti\u{00AD}ple", "multiple")]
147+
fn test_remove_soft_hyphens(#[case] input: &str, #[case] expected: &str) {
148+
assert_eq!(remove_soft_hyphens(input), expected);
146149
}
147150

148-
#[test]
149-
fn test_url_decode() {
150-
assert_eq!(url_decode("hello%20world"), "hello world");
151-
assert_eq!(url_decode("test%2Fpath"), "test/path");
152-
assert_eq!(url_decode("100%25"), "100%");
153-
// Test UTF-8 encoded characters.
154-
assert_eq!(url_decode("caf%C3%A9"), "café");
151+
#[rstest]
152+
#[case("hello%20world", "hello world")]
153+
#[case("test%2Fpath", "test/path")]
154+
#[case("100%25", "100%")]
155+
#[case("caf%C3%A9", "café")]
156+
#[case("bad%ZZvalue", "bad%ZZvalue")]
157+
#[case("%", "%")]
158+
fn test_url_decode(#[case] input: &str, #[case] expected: &str) {
159+
assert_eq!(url_decode(input), expected);
155160
}
156161

157-
#[test]
158-
fn test_collapse_whitespace() {
159-
assert_eq!(collapse_whitespace("hello world"), "hello world");
160-
assert_eq!(collapse_whitespace("hello\n\nworld"), "hello world");
161-
assert_eq!(collapse_whitespace("hello\t\tworld"), "hello world");
162-
assert_eq!(collapse_whitespace(" spaces "), " spaces ");
163-
assert_eq!(collapse_whitespace("hello\u{00A0}\u{00A0}world"), "hello world");
164-
assert_eq!(collapse_whitespace("hello\u{200B}\u{200B}world"), "hello world");
162+
#[rstest]
163+
#[case("hello world", "hello world")]
164+
#[case("hello\n\nworld", "hello world")]
165+
#[case("hello\t\tworld", "hello world")]
166+
#[case(" spaces ", " spaces ")]
167+
#[case("hello\u{00A0}\u{00A0}world", "hello world")]
168+
#[case("hello\u{200B}\u{200B}world", "hello world")]
169+
#[case("\u{00A0}\u{200B}alpha\t \n beta\u{00A0}", " alpha beta ")]
170+
fn test_collapse_whitespace(#[case] input: &str, #[case] expected: &str) {
171+
assert_eq!(collapse_whitespace(input), expected);
165172
}
166173

167-
#[test]
168-
fn test_trim_string() {
169-
assert_eq!(trim_string(" hello "), "hello");
170-
assert_eq!(trim_string("\n\nhello\n\n"), "hello");
171-
assert_eq!(trim_string("\u{00A0}hello\u{00A0}"), "hello");
172-
assert_eq!(trim_string("\u{200B}hello\u{200B}"), "hello");
173-
assert_eq!(trim_string("hello"), "hello");
174+
#[rstest]
175+
#[case(" hello ", "hello")]
176+
#[case("\n\nhello\n\n", "hello")]
177+
#[case("\u{00A0}hello\u{00A0}", "hello")]
178+
#[case("\u{200B}hello\u{200B}", "hello")]
179+
#[case("hello", "hello")]
180+
#[case("\u{200B}\u{00A0} hello \u{00A0}\u{200B}", "hello")]
181+
fn test_trim_string(#[case] input: &str, #[case] expected: &str) {
182+
assert_eq!(trim_string(input), expected);
174183
}
175184

176185
#[cfg(windows)]
@@ -189,25 +198,30 @@ mod tests {
189198
assert_eq!(display_len("line\nwrap"), 9);
190199
}
191200

192-
#[test]
193-
fn test_format_list_item() {
194-
assert_eq!(format_list_item(1, "1"), "1");
195-
assert_eq!(format_list_item(5, "1"), "5");
196-
assert_eq!(format_list_item(1, "a"), "a");
197-
assert_eq!(format_list_item(26, "a"), "z");
198-
assert_eq!(format_list_item(27, "a"), "aa");
199-
assert_eq!(format_list_item(1, "A"), "A");
200-
assert_eq!(format_list_item(26, "A"), "Z");
201-
assert_eq!(format_list_item(27, "A"), "AA");
202-
assert_eq!(format_list_item(1, "i"), "i");
203-
assert_eq!(format_list_item(4, "i"), "iv");
204-
assert_eq!(format_list_item(1994, "i"), "mcmxciv");
205-
assert_eq!(format_list_item(1, "I"), "I");
206-
assert_eq!(format_list_item(4, "I"), "IV");
207-
assert_eq!(format_list_item(1994, "I"), "MCMXCIV");
208-
assert_eq!(format_list_item(10, "unknown"), "10");
209-
assert_eq!(format_list_item(0, "a"), "0");
210-
assert_eq!(format_list_item(-5, "i"), "-5");
201+
#[rstest]
202+
#[case(1, "1", "1")]
203+
#[case(5, "1", "5")]
204+
#[case(1, "a", "a")]
205+
#[case(26, "a", "z")]
206+
#[case(27, "a", "aa")]
207+
#[case(1, "A", "A")]
208+
#[case(26, "A", "Z")]
209+
#[case(27, "A", "AA")]
210+
#[case(1, "i", "i")]
211+
#[case(4, "i", "iv")]
212+
#[case(1994, "i", "mcmxciv")]
213+
#[case(1, "I", "I")]
214+
#[case(4, "I", "IV")]
215+
#[case(1994, "I", "MCMXCIV")]
216+
#[case(10, "unknown", "10")]
217+
#[case(0, "a", "0")]
218+
#[case(-5, "i", "-5")]
219+
#[case(52, "a", "az")]
220+
#[case(53, "a", "ba")]
221+
#[case(52, "A", "AZ")]
222+
#[case(53, "A", "BA")]
223+
fn test_format_list_item(#[case] number: i32, #[case] list_type: &str, #[case] expected: &str) {
224+
assert_eq!(format_list_item(number, list_type), expected);
211225
}
212226

213227
#[test]
@@ -233,38 +247,13 @@ mod tests {
233247
assert_eq!(text, "Topic #rust and issue #x1");
234248
}
235249

236-
#[test]
237-
fn test_url_decode_invalid_sequences_are_left_as_is() {
238-
assert_eq!(url_decode("bad%ZZvalue"), "bad%ZZvalue");
239-
assert_eq!(url_decode("%"), "%");
240-
}
241-
242-
#[test]
243-
fn test_collapse_whitespace_handles_mixed_space_types() {
244-
let input = "\u{00A0}\u{200B}alpha\t \n beta\u{00A0}";
245-
assert_eq!(collapse_whitespace(input), " alpha beta ");
246-
}
247-
248-
#[test]
249-
fn test_trim_string_handles_mixed_space_like_chars() {
250-
let input = "\u{200B}\u{00A0} hello \u{00A0}\u{200B}";
251-
assert_eq!(trim_string(input), "hello");
252-
}
253-
254-
#[test]
255-
fn test_is_space_like_variants() {
256-
assert!(is_space_like(' '));
257-
assert!(is_space_like('\n'));
258-
assert!(is_space_like('\u{00A0}'));
259-
assert!(is_space_like('\u{200B}'));
260-
assert!(!is_space_like('x'));
261-
}
262-
263-
#[test]
264-
fn test_format_list_item_alpha_boundaries() {
265-
assert_eq!(format_list_item(52, "a"), "az");
266-
assert_eq!(format_list_item(53, "a"), "ba");
267-
assert_eq!(format_list_item(52, "A"), "AZ");
268-
assert_eq!(format_list_item(53, "A"), "BA");
250+
#[rstest]
251+
#[case(' ', true)]
252+
#[case('\n', true)]
253+
#[case('\u{00A0}', true)]
254+
#[case('\u{200B}', true)]
255+
#[case('x', false)]
256+
fn test_is_space_like_variants(#[case] ch: char, #[case] expected: bool) {
257+
assert_eq!(is_space_like(ch), expected);
269258
}
270259
}

0 commit comments

Comments
 (0)