Skip to content

Commit ff255a2

Browse files
committed
test: add coverage for new test aggregation method
1 parent fe67521 commit ff255a2

File tree

2 files changed

+292
-0
lines changed

2 files changed

+292
-0
lines changed

crates/cli/src/test.rs

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,10 @@ fn parse_test_content(name: String, content: &str, file_path: Option<PathBuf>) -
14531453

14541454
#[cfg(test)]
14551455
mod tests {
1456+
use serde_json::json;
1457+
1458+
use crate::tests::get_language;
1459+
14561460
use super::*;
14571461

14581462
#[test]
@@ -2110,4 +2114,290 @@ Test with cst marker
21102114
}
21112115
);
21122116
}
2117+
2118+
fn clear_parse_rate(result: &mut TestResult) {
2119+
let test_case_info = &mut result.info;
2120+
match test_case_info {
2121+
TestInfo::ParseTest {
2122+
ref mut parse_rate, ..
2123+
} => {
2124+
assert!(parse_rate.is_some());
2125+
*parse_rate = None;
2126+
}
2127+
TestInfo::Group { .. } | TestInfo::AssertionTest { .. } => {
2128+
panic!("Unexpected test result")
2129+
}
2130+
}
2131+
}
2132+
2133+
#[test]
2134+
fn run_tests_simple() {
2135+
let mut parser = Parser::new();
2136+
let language = get_language("c");
2137+
parser
2138+
.set_language(&language)
2139+
.expect("Failed to set language");
2140+
let mut languages = BTreeMap::new();
2141+
languages.insert("c", &language);
2142+
let opts = TestOptions {
2143+
path: PathBuf::from("foo"),
2144+
debug: true,
2145+
debug_graph: false,
2146+
include: None,
2147+
exclude: None,
2148+
file_name: None,
2149+
update: false,
2150+
open_log: false,
2151+
languages,
2152+
color: true,
2153+
show_fields: false,
2154+
overview_only: false,
2155+
};
2156+
2157+
// NOTE: The following test cases are combined to work around a race condition
2158+
// in the loader
2159+
{
2160+
let test_entry = TestEntry::Group {
2161+
name: "foo".to_string(),
2162+
file_path: None,
2163+
children: vec![TestEntry::Example {
2164+
name: "C Test 1".to_string(),
2165+
input: b"1;\n".to_vec(),
2166+
output: "(translation_unit (expression_statement (number_literal)))"
2167+
.to_string(),
2168+
header_delim_len: 25,
2169+
divider_delim_len: 3,
2170+
has_fields: false,
2171+
attributes_str: String::new(),
2172+
attributes: TestAttributes::default(),
2173+
file_name: None,
2174+
}],
2175+
};
2176+
2177+
let mut test_summary = TestSummary::new(true, TestStats::All, false, false, false);
2178+
let mut corrected_entries = Vec::new();
2179+
run_tests(
2180+
&mut parser,
2181+
test_entry,
2182+
&opts,
2183+
&mut test_summary,
2184+
&mut corrected_entries,
2185+
true,
2186+
)
2187+
.expect("Failed to run tests");
2188+
2189+
// parse rates will always be different, so we need to clear out these
2190+
// fields to reliably assert equality below
2191+
clear_parse_rate(&mut test_summary.parse_results.root_group[0]);
2192+
test_summary.parse_stats.total_duration = Duration::from_secs(0);
2193+
2194+
let json_results = serde_json::to_string(&test_summary).unwrap();
2195+
2196+
assert_eq!(
2197+
json_results,
2198+
json!({
2199+
"parse_results": [
2200+
{
2201+
"name": "C Test 1",
2202+
"outcome": "Passed",
2203+
"parse_rate": null,
2204+
"test_num": 1
2205+
}
2206+
],
2207+
"parse_failures": [],
2208+
"parse_stats": {
2209+
"successful_parses": 1,
2210+
"total_parses": 1,
2211+
"total_bytes": 3,
2212+
"total_duration": {
2213+
"secs": 0,
2214+
"nanos": 0,
2215+
}
2216+
},
2217+
"highlight_results": [],
2218+
"tag_results": [],
2219+
"query_results": []
2220+
})
2221+
.to_string()
2222+
);
2223+
}
2224+
{
2225+
let test_entry = TestEntry::Group {
2226+
name: "corpus".to_string(),
2227+
file_path: None,
2228+
children: vec![
2229+
TestEntry::Group {
2230+
name: "group1".to_string(),
2231+
// This test passes
2232+
children: vec![TestEntry::Example {
2233+
name: "C Test 1".to_string(),
2234+
input: b"1;\n".to_vec(),
2235+
output: "(translation_unit (expression_statement (number_literal)))"
2236+
.to_string(),
2237+
header_delim_len: 25,
2238+
divider_delim_len: 3,
2239+
has_fields: false,
2240+
attributes_str: String::new(),
2241+
attributes: TestAttributes::default(),
2242+
file_name: None,
2243+
}],
2244+
file_path: None,
2245+
},
2246+
TestEntry::Group {
2247+
name: "group2".to_string(),
2248+
children: vec![
2249+
// This test passes
2250+
TestEntry::Example {
2251+
name: "C Test 2".to_string(),
2252+
input: b"1;\n".to_vec(),
2253+
output:
2254+
"(translation_unit (expression_statement (number_literal)))"
2255+
.to_string(),
2256+
header_delim_len: 25,
2257+
divider_delim_len: 3,
2258+
has_fields: false,
2259+
attributes_str: String::new(),
2260+
attributes: TestAttributes::default(),
2261+
file_name: None,
2262+
},
2263+
// This test fails, and is marked with fail-fast
2264+
TestEntry::Example {
2265+
name: "C Test 3".to_string(),
2266+
input: b"1;\n".to_vec(),
2267+
output:
2268+
"(translation_unit (expression_statement (string_literal)))"
2269+
.to_string(),
2270+
header_delim_len: 25,
2271+
divider_delim_len: 3,
2272+
has_fields: false,
2273+
attributes_str: String::new(),
2274+
attributes: TestAttributes {
2275+
fail_fast: true,
2276+
..Default::default()
2277+
},
2278+
file_name: None,
2279+
},
2280+
],
2281+
file_path: None,
2282+
},
2283+
// This group never runs because of the previous failure
2284+
TestEntry::Group {
2285+
name: "group3".to_string(),
2286+
// This test fails, and is marked with fail-fast
2287+
children: vec![TestEntry::Example {
2288+
name: "C Test 4".to_string(),
2289+
input: b"1;\n".to_vec(),
2290+
output: "(translation_unit (expression_statement (number_literal)))"
2291+
.to_string(),
2292+
header_delim_len: 25,
2293+
divider_delim_len: 3,
2294+
has_fields: false,
2295+
attributes_str: String::new(),
2296+
attributes: TestAttributes::default(),
2297+
file_name: None,
2298+
}],
2299+
file_path: None,
2300+
},
2301+
],
2302+
};
2303+
2304+
let mut test_summary = TestSummary::new(true, TestStats::All, false, false, false);
2305+
let mut corrected_entries = Vec::new();
2306+
run_tests(
2307+
&mut parser,
2308+
test_entry,
2309+
&opts,
2310+
&mut test_summary,
2311+
&mut corrected_entries,
2312+
true,
2313+
)
2314+
.expect("Failed to run tests");
2315+
2316+
// parse rates will always be different, so we need to clear out these
2317+
// fields to reliably assert equality below
2318+
{
2319+
let test_group_1_info = &mut test_summary.parse_results.root_group[0].info;
2320+
match test_group_1_info {
2321+
TestInfo::Group {
2322+
ref mut children, ..
2323+
} => clear_parse_rate(&mut children[0]),
2324+
TestInfo::ParseTest { .. } | TestInfo::AssertionTest { .. } => {
2325+
panic!("Unexpected test result");
2326+
}
2327+
}
2328+
let test_group_2_info = &mut test_summary.parse_results.root_group[1].info;
2329+
match test_group_2_info {
2330+
TestInfo::Group {
2331+
ref mut children, ..
2332+
} => {
2333+
clear_parse_rate(&mut children[0]);
2334+
clear_parse_rate(&mut children[1]);
2335+
}
2336+
TestInfo::ParseTest { .. } | TestInfo::AssertionTest { .. } => {
2337+
panic!("Unexpected test result");
2338+
}
2339+
}
2340+
test_summary.parse_stats.total_duration = Duration::from_secs(0);
2341+
}
2342+
2343+
let json_results = serde_json::to_string(&test_summary).unwrap();
2344+
2345+
assert_eq!(
2346+
json_results,
2347+
json!({
2348+
"parse_results": [
2349+
{
2350+
"name": "group1",
2351+
"children": [
2352+
{
2353+
"name": "C Test 1",
2354+
"outcome": "Passed",
2355+
"parse_rate": null,
2356+
"test_num": 1
2357+
}
2358+
]
2359+
},
2360+
{
2361+
"name": "group2",
2362+
"children": [
2363+
{
2364+
"name": "C Test 2",
2365+
"outcome": "Passed",
2366+
"parse_rate": null,
2367+
"test_num": 2
2368+
},
2369+
{
2370+
"name": "C Test 3",
2371+
"outcome": "Failed",
2372+
"parse_rate": null,
2373+
"test_num": 3
2374+
}
2375+
]
2376+
}
2377+
],
2378+
"parse_failures": [
2379+
{
2380+
"name": "C Test 3",
2381+
"actual": "(translation_unit (expression_statement (number_literal)))",
2382+
"expected": "(translation_unit (expression_statement (string_literal)))",
2383+
"is_cst": false,
2384+
}
2385+
],
2386+
"parse_stats": {
2387+
"successful_parses": 2,
2388+
"total_parses": 3,
2389+
"total_bytes": 9,
2390+
"total_duration": {
2391+
"secs": 0,
2392+
"nanos": 0,
2393+
}
2394+
},
2395+
"highlight_results": [],
2396+
"tag_results": [],
2397+
"query_results": []
2398+
})
2399+
.to_string()
2400+
);
2401+
}
2402+
}
21132403
}

crates/cli/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub use crate::fuzz::{
2626
ITERATION_COUNT,
2727
};
2828

29+
pub use helpers::fixtures::get_language;
30+
2931
/// This is a simple wrapper around [`tree_sitter_generate::generate_parser_for_grammar`], because
3032
/// our tests do not need to pass in a version number, only the grammar JSON.
3133
fn generate_parser(grammar_json: &str) -> GenerateResult<(String, String)> {

0 commit comments

Comments
 (0)