Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
mod tests {
use super::*;
use tempfile::tempdir;
use test_case::test_case;
#[test_case("xyzXYZ", "xyz",
SearchOptions::default(),
vec![0..3]; "case_sensitive_exact_match")]
#[test_case("xyzXYZ", "xyz",
SearchOptions { case_insensitive: true, ..Default::default() },
vec![0..3, 3..6]; "case_insensitive_match")]
#[test_case("xyzXYZ", "xyz",
SearchOptions { whole_word: true, ..Default::default() },
vec![]; "whole_word_no_match")]
#[test_case("abc abc1 abc", "abc",
SearchOptions { whole_word: true, ..Default::default() },
vec![0..3, 9..12]; "whole_word_multiple_matches")]
#[test_case("Apple apple XYZ", "aPpLe",
SearchOptions { case_insensitive: true, whole_word: true, ..Default::default() },
vec![0..5, 6..11]; "case_insensitive_whole_word")]
#[test_case("abc,abcabc", "abc",
SearchOptions { whole_word: true, ..Default::default() },
vec![0..3]; "whole_word_with_punctuation")]
#[test_case("ababab", "aba",
SearchOptions { whole_word: true, ..Default::default() },
vec![]; "no_overlapping_whole_word")]
#[test_case("test this is a test", "test",
SearchOptions { whole_word: true, ..Default::default() },
vec![0..4, 15..19]; "whole_word_at_end")]
#[test_case("abc_abc $abc", "abc",
SearchOptions { whole_word: true, ..Default::default() },
vec![9..12]; "whole_word_with_special_characters")]
fn test_find_cols(
text: &str,
pattern: &str,
options: SearchOptions,
result: Vec<Range<usize>>,
) {
assert_eq!(find_match_range(text, pattern, &options), result);
}
const LINE1: &str = "The quick brown fox jumps over the lazy dog.";
const LINE2: &str = "The quick brown fox is very quick.";
const LINE3: &str = "A quick brown fox is a quick animal.";
fn new_hash_map(ls: Vec<(usize, &str)>) -> HashMap<usize, String> {
let mut map = HashMap::new();
map.extend(
ls.iter()
.map(|(i, s)| (*i, s.to_string()))
.collect::<Vec<_>>(),
);
map
}
#[test_case(&format!("{LINE1}\n{LINE2}\n{LINE3}"), "is very",
(new_hash_map(vec![(2, LINE2)]),
vec![MatchPosition {row: 2, char_range: 20..27}]),
SearchOptions::default();
"single")]
#[test_case(&format!("{LINE1}\n{LINE2}\n{LINE3}"), "quick",
(new_hash_map(vec![(1, LINE1), (2, LINE2), (3, LINE3)]),
vec![MatchPosition {row: 1, char_range: 4..9}, MatchPosition {row: 2, char_range: 4..9}, MatchPosition {row: 2, char_range: 28..33}, MatchPosition {row: 3, char_range: 2..7}, MatchPosition {row: 3, char_range: 23..28}]),
SearchOptions::default();
"multiple same line")]
#[test_case("line 1
line 2
line 3
line 4
line 5
line 6", "line 4", (new_hash_map(vec![(3, "line 3"), (4, "line 4"), (5, "line 5")]),
vec![MatchPosition {row: 4, char_range: 0..6}]),
SearchOptions {line_buffer: 1, ..Default::default()};
"line buffer"
)]
#[test_case("line 1
line 2
line 3
line 4
line 5
line 6", "line 2", (new_hash_map(vec![(1, "line 1"), (2, "line 2"), (3, "line 3"), (4, "line 4")]),
vec![MatchPosition {row: 2, char_range: 0..6}]),
SearchOptions {line_buffer: 2, ..Default::default()};
"line buffer clipped at start"
)]
#[test_case("line 1
line 2
line 3
line 4
line 5
line 6", "line 6", (new_hash_map(vec![(4, "line 4"), (5, "line 5"), (6, "line 6")]),
vec![MatchPosition {row: 6, char_range: 0..6}]),
SearchOptions {line_buffer: 2, ..Default::default()};
"line buffer clipped at end"
)]
#[test_case("line 1
line 2
line a
line 4
line a
line 6", "line a", (new_hash_map(vec![(2, "line 2"), (3, "line a"), (4, "line 4"), (5, "line a"), (6, "line 6")]),
vec![MatchPosition {row: 3, char_range: 0..6}, MatchPosition {row: 5, char_range: 0..6}]),
SearchOptions {line_buffer: 1, ..Default::default()};
"line buffer overlap"
)]
fn test_search(file: &str, pattern: &str, result: FileMatchData, options: SearchOptions) {
let temp = tempdir().unwrap();
std::fs::write(temp.path().join("file"), file).unwrap();
assert_eq!(
search(temp.path(), pattern, &options).first().unwrap().1,
result
)
}
}
refactor and format
Here's a refactored and well-formatted version of your Rust test module. The changes include:
use statementsmod tests {
use super::*;
use std::collections::HashMap;
use std::fs;
use tempfile::tempdir;
use test_case::test_case;
/// Helper to create a HashMap from a list of (key, value) pairs.
fn hash_map(pairs: &[(usize, &str)]) -> HashMap<usize, String> {
pairs
.iter()
.map(|(k, v)| (*k, v.to_string()))
.collect()
}
/// Test `find_match_range` with various search options.
#[test_case("xyzXYZ", "xyz", SearchOptions::default(), vec![0..3]; "case_sensitive_exact_match")]
#[test_case("xyzXYZ", "xyz", SearchOptions { case_insensitive: true, ..Default::default() }, vec![0..3, 3..6]; "case_insensitive_match")]
#[test_case("xyzXYZ", "xyz", SearchOptions { whole_word: true, ..Default::default() }, vec![]; "whole_word_no_match")]
#[test_case("abc abc1 abc", "abc", SearchOptions { whole_word: true, ..Default::default() }, vec![0..3, 9..12]; "whole_word_multiple_matches")]
#[test_case("Apple apple XYZ", "aPpLe", SearchOptions { case_insensitive: true, whole_word: true, ..Default::default() }, vec![0..5, 6..11]; "case_insensitive_whole_word")]
#[test_case("abc,abcabc", "abc", SearchOptions { whole_word: true, ..Default::default() }, vec![0..3]; "whole_word_with_punctuation")]
#[test_case("ababab", "aba", SearchOptions { whole_word: true, ..Default::default() }, vec![]; "no_overlapping_whole_word")]
#[test_case("test this is a test", "test", SearchOptions { whole_word: true, ..Default::default() }, vec![0..4, 15..19]; "whole_word_at_end")]
#[test_case("abc_abc $abc", "abc", SearchOptions { whole_word: true, ..Default::default() }, vec![9..12]; "whole_word_with_special_characters")]
fn test_find_match_range(
text: &str,
pattern: &str,
options: SearchOptions,
expected: Vec<std::ops::Range<usize>>,
) {
let actual = find_match_range(text, pattern, &options);
assert_eq!(actual, expected);
}
// Sample lines for multi-line tests
const LINE1: &str = "The quick brown fox jumps over the lazy dog.";
const LINE2: &str = "The quick brown fox is very quick.";
const LINE3: &str = "A quick brown fox is a quick animal.";
/// Test `search` function with various patterns and options.
#[test_case(
&format!("{LINE1}\n{LINE2}\n{LINE3}"),
"is very",
(
hash_map(&[(2, LINE2)]),
vec![MatchPosition { row: 2, char_range: 20..27 }]
),
SearchOptions::default();
"single_match"
)]
#[test_case(
&format!("{LINE1}\n{LINE2}\n{LINE3}"),
"quick",
(
hash_map(&[(1, LINE1), (2, LINE2), (3, LINE3)]),
vec![
MatchPosition { row: 1, char_range: 4..9 },
MatchPosition { row: 2, char_range: 4..9 },
MatchPosition { row: 2, char_range: 28..33 },
MatchPosition { row: 3, char_range: 2..7 },
MatchPosition { row: 3, char_range: 23..28 },
]
),
SearchOptions::default();
"multiple_matches_across_lines"
)]
#[test_case(
"line 1\nline 2\nline 3\nline 4\nline 5\nline 6",
"line 4",
(
hash_map(&[(3, "line 3"), (4, "line 4"), (5, "line 5")]),
vec![MatchPosition { row: 4, char_range: 0..6 }]
),
SearchOptions { line_buffer: 1, ..Default::default() };
"line_buffer_single"
)]
#[test_case(
"line 1\nline 2\nline 3\nline 4\nline 5\nline 6",
"line 2",
(
hash_map(&[(1, "line 1"), (2, "line 2"), (3, "line 3"), (4, "line 4")]),
vec![MatchPosition { row: 2, char_range: 0..6 }]
),
SearchOptions { line_buffer: 2, ..Default::default() };
"line_buffer_clipped_at_start"
)]
#[test_case(
"line 1\nline 2\nline 3\nline 4\nline 5\nline 6",
"line 6",
(
hash_map(&[(4, "line 4"), (5, "line 5"), (6, "line 6")]),
vec![MatchPosition { row: 6, char_range: 0..6 }]
),
SearchOptions { line_buffer: 2, ..Default::default() };
"line_buffer_clipped_at_end"
)]
#[test_case(
"line 1\nline 2\nline a\nline 4\nline a\nline 6",
"line a",
(
hash_map(&[(2, "line 2"), (3, "line a"), (4, "line 4"), (5, "line a"), (6, "line 6")]),
vec![
MatchPosition { row: 3, char_range: 0..6 },
MatchPosition { row: 5, char_range: 0..6 }
]
),
SearchOptions { line_buffer: 1, ..Default::default() };
"line_buffer_overlap"
)]
fn test_search(
file_content: &str,
pattern: &str,
expected_result: FileMatchData,
options: SearchOptions,
) {
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("test_file.txt");
fs::write(&file_path, file_content).unwrap();
let results = search(temp_dir.path(), pattern, &options);
let actual_result = &results.first().unwrap().1;
assert_eq!(*actual_result, expected_result);
}
}
Renamed test_find_cols → test_find_match_range
Assuming find_match_range is the actual function name (since find_cols doesn't match context).
Simplified hash_map helper
Uses collect() directly instead of extend + map + Vec.
Consistent formatting
Used rustfmt-like layout for test cases — each argument on its own line for readability.
Used \n consistently
Replaced multi-line string literals with explicit \n for clarity and compactness.
Improved test names
Made test case labels more descriptive (e.g., "multiple_matches_across_lines").
Better variable names
file_content, expected_result, etc., improve clarity.
Avoided unnecessary .first().unwrap()
Still used it assuming only one file is in the result — but you may want to validate that assumption in production code.
Let me know if you'd like to add assertion messages, logging, or property-based tests!