Skip to content

Commit 53c23ff

Browse files
committed
Switch emmylua_parser_desc to gtest
GTest prints nice diffs, which is useful for debugging.
1 parent d330fbf commit 53c23ff

File tree

9 files changed

+152
-135
lines changed

9 files changed

+152
-135
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/emmylua_parser_desc/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ emmylua_parser.workspace = true
1616

1717
# external
1818
rowan.workspace = true
19-
unicode-general-category.workspace = true
19+
unicode-general-category.workspace = true
20+
21+
[dev-dependencies]
22+
googletest.workspace = true

crates/emmylua_parser_desc/src/lang/json/json_lexer.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ impl<'a> JsonLexer<'a> {
198198
mod tests {
199199
use super::*;
200200
use emmylua_parser::Reader;
201+
use googletest::prelude::*;
201202

202-
#[test]
203+
#[gtest]
203204
fn test_json_lexer_basic() {
204205
let json = r#"
205206
{
@@ -235,19 +236,19 @@ mod tests {
235236
}
236237
}
237238

238-
assert!(string_count > 0, "Should find strings");
239-
assert!(number_count > 0, "Should find numbers");
240-
assert!(keyword_count > 0, "Should find keywords");
241-
assert!(brace_count > 0, "Should find braces");
242-
assert!(bracket_count > 0, "Should find brackets");
239+
expect_gt!(string_count, 0, "Should find strings");
240+
expect_gt!(number_count, 0, "Should find numbers");
241+
expect_gt!(keyword_count, 0, "Should find keywords");
242+
expect_gt!(brace_count, 0, "Should find braces");
243+
expect_gt!(bracket_count, 0, "Should find brackets");
243244

244245
println!(
245246
"Found {} strings, {} numbers, {} keywords, {} braces, {} brackets",
246247
string_count, number_count, keyword_count, brace_count, bracket_count
247248
);
248249
}
249250

250-
#[test]
251+
#[gtest]
251252
fn test_json_lexer_keywords() {
252253
let json = "true false null";
253254

@@ -260,10 +261,10 @@ mod tests {
260261
.filter(|t| t.kind == JsonTokenKind::TkKeyword)
261262
.collect();
262263

263-
assert_eq!(keywords.len(), 3, "Should find exactly 3 keywords");
264+
expect_eq!(keywords.len(), 3, "Should find exactly 3 keywords");
264265
}
265266

266-
#[test]
267+
#[gtest]
267268
fn test_json_lexer_numbers() {
268269
let json = "42 -17 3.14 -2.5 1e10 1E-5 -1.23e+4";
269270

@@ -276,10 +277,10 @@ mod tests {
276277
.filter(|t| t.kind == JsonTokenKind::TkNumber)
277278
.collect();
278279

279-
assert_eq!(numbers.len(), 7, "Should find exactly 7 numbers");
280+
expect_eq!(numbers.len(), 7, "Should find exactly 7 numbers");
280281
}
281282

282-
#[test]
283+
#[gtest]
283284
fn test_json_lexer_strings() {
284285
let json = r#""hello" "world with spaces" "escaped\"quote" "unicode\u0041""#;
285286

@@ -292,10 +293,10 @@ mod tests {
292293
.filter(|t| t.kind == JsonTokenKind::TkString)
293294
.collect();
294295

295-
assert_eq!(strings.len(), 4, "Should find exactly 4 strings");
296+
expect_eq!(strings.len(), 4, "Should find exactly 4 strings");
296297
}
297298

298-
#[test]
299+
#[gtest]
299300
fn test_json_lexer_structure() {
300301
let json = r#"{"key": ["value1", "value2"]}"#;
301302

@@ -315,11 +316,11 @@ mod tests {
315316
let has_colon = tokens.iter().any(|t| t.kind == JsonTokenKind::TkColon);
316317
let has_comma = tokens.iter().any(|t| t.kind == JsonTokenKind::TkComma);
317318

318-
assert!(has_left_brace, "Should have left brace");
319-
assert!(has_right_brace, "Should have right brace");
320-
assert!(has_left_bracket, "Should have left bracket");
321-
assert!(has_right_bracket, "Should have right bracket");
322-
assert!(has_colon, "Should have colon");
323-
assert!(has_comma, "Should have comma");
319+
expect_true!(has_left_brace, "Should have left brace");
320+
expect_true!(has_right_brace, "Should have right brace");
321+
expect_true!(has_left_bracket, "Should have left bracket");
322+
expect_true!(has_right_bracket, "Should have right bracket");
323+
expect_true!(has_colon, "Should have colon");
324+
expect_true!(has_comma, "Should have comma");
324325
}
325326
}

crates/emmylua_parser_desc/src/lang/vimscript/vim_lexer.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,9 @@ impl<'a> VimscriptLexer<'a> {
448448
mod tests {
449449
use super::*;
450450
use emmylua_parser::Reader;
451+
use googletest::prelude::*;
451452

452-
#[test]
453+
#[gtest]
453454
fn test_vim_lexer_basic() {
454455
let code = r#"
455456
" This is a comment
@@ -483,18 +484,18 @@ endfunction
483484
}
484485
}
485486

486-
assert!(keyword_count > 0, "Should find keywords");
487-
assert!(string_count > 0, "Should find strings");
488-
assert!(comment_count > 0, "Should find comments");
489-
assert!(number_count > 0, "Should find numbers");
487+
expect_gt!(keyword_count, 0, "Should find keywords");
488+
expect_gt!(string_count, 0, "Should find strings");
489+
expect_gt!(comment_count, 0, "Should find comments");
490+
expect_gt!(number_count, 0, "Should find numbers");
490491

491492
println!(
492493
"Found {} keywords, {} strings, {} comments, {} numbers",
493494
keyword_count, string_count, comment_count, number_count
494495
);
495496
}
496497

497-
#[test]
498+
#[gtest]
498499
fn test_vim_lexer_keywords() {
499500
let code = "function! if else endif let echo return";
500501

@@ -507,6 +508,6 @@ endfunction
507508
.filter(|t| t.kind == VimTokenKind::TkKeyword)
508509
.collect();
509510

510-
assert!(keywords.len() >= 5, "Should find multiple keywords");
511+
expect_ge!(keywords.len(), 5, "Should find multiple keywords");
511512
}
512513
}

crates/emmylua_parser_desc/src/md.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,9 +1465,10 @@ mod tests {
14651465
use super::*;
14661466
#[allow(unused)]
14671467
use crate::testlib::{print_result, test};
1468+
use googletest::prelude::*;
14681469

1469-
#[test]
1470-
fn test_md() {
1470+
#[gtest]
1471+
fn test_md() -> Result<()> {
14711472
let code = r#"
14721473
--- # Inline code
14731474
---
@@ -1720,11 +1721,12 @@ mod tests {
17201721
"#;
17211722

17221723
// print_result(&code, Box::new(MdParser::new(None)));
1723-
test(&code, Box::new(MdParser::new(None)), &expected);
1724+
test(&code, Box::new(MdParser::new(None)), &expected).or_fail()?;
1725+
Ok(())
17241726
}
17251727

1726-
#[test]
1727-
fn test_myst() {
1728+
#[gtest]
1729+
fn test_myst() -> Result<()> {
17281730
let code = r#"
17291731
--- # Inline
17301732
---
@@ -1823,11 +1825,12 @@ mod tests {
18231825
--- <Markup>$$</Markup> <Arg>(anchor)</Arg></Scope>
18241826
"#;
18251827

1826-
test(&code, Box::new(MdParser::new_myst(None, None)), &expected);
1828+
test(&code, Box::new(MdParser::new_myst(None, None)), &expected).or_fail()?;
1829+
Ok(())
18271830
}
18281831

1829-
#[test]
1830-
fn test_myst_primary_domain() {
1832+
#[gtest]
1833+
fn test_myst_primary_domain() -> Result<()> {
18311834
let code = r#"--- See {obj}`ref`"#;
18321835

18331836
let expected = r#"
@@ -1838,48 +1841,56 @@ mod tests {
18381841
&code,
18391842
Box::new(MdParser::new_myst(Some("lua".to_string()), None)),
18401843
&expected,
1841-
);
1844+
)
1845+
.or_fail()?;
1846+
Ok(())
18421847
}
18431848

1844-
#[test]
1845-
fn test_myst_search_at_offset() {
1849+
#[gtest]
1850+
fn test_myst_search_at_offset() -> Result<()> {
18461851
let code = r#"--- See {lua:obj}`x` {lua:obj}`ref`"#;
18471852
let expected = r#"--- See {lua:obj}`x` {lua:obj}`<Ref>ref</Ref>`"#;
18481853
test(
18491854
&code,
18501855
Box::new(MdParser::new_myst(None, Some(31))),
18511856
&expected,
1852-
);
1857+
)
1858+
.or_fail()?;
18531859
test(
18541860
&code,
18551861
Box::new(MdParser::new_myst(None, Some(32))),
18561862
&expected,
1857-
);
1863+
)
1864+
.or_fail()?;
18581865
test(
18591866
&code,
18601867
Box::new(MdParser::new_myst(None, Some(34))),
18611868
&expected,
1862-
);
1869+
)
1870+
.or_fail()?;
18631871

18641872
let code = r#"--- See {lua:obj}`x` {lua:obj}`"#;
18651873
let expected = r#"--- See {lua:obj}`x` {lua:obj}`<Ref></Ref>"#;
18661874
test(
18671875
&code,
18681876
Box::new(MdParser::new_myst(None, Some(31))),
18691877
&expected,
1870-
);
1878+
)
1879+
.or_fail()?;
18711880

18721881
let code = r#"--- See {lua:obj}`x` {lua:obj}``..."#;
18731882
let expected = r#"--- See {lua:obj}`x` {lua:obj}`<Ref>`...</Ref>"#;
18741883
test(
18751884
&code,
18761885
Box::new(MdParser::new_myst(None, Some(31))),
18771886
&expected,
1878-
);
1887+
)
1888+
.or_fail()?;
1889+
Ok(())
18791890
}
18801891

1881-
#[test]
1882-
fn test_md_no_indent() {
1892+
#[gtest]
1893+
fn test_md_no_indent() -> Result<()> {
18831894
let code = r#"
18841895
---```lua
18851896
---
@@ -1906,6 +1917,7 @@ local t = 123
19061917
local t = 123
19071918
"#;
19081919

1909-
test(&code, Box::new(MdParser::new(None)), &expected);
1920+
test(&code, Box::new(MdParser::new(None)), &expected).or_fail()?;
1921+
Ok(())
19101922
}
19111923
}

crates/emmylua_parser_desc/src/ref_target.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,12 @@ fn eat_string(reader: &mut Reader) -> bool {
203203
#[cfg(test)]
204204
mod tests {
205205
use super::*;
206+
use googletest::prelude::*;
206207

207-
#[test]
208+
#[gtest]
208209
fn test_parse_ref_target_simple() {
209210
let res = parse_ref_target("a.b.c.d", TextRange::up_to(7.into()), 7.into());
210-
assert_eq!(
211+
expect_eq!(
211212
res,
212213
Some(vec![
213214
(
@@ -230,10 +231,10 @@ mod tests {
230231
)
231232
}
232233

233-
#[test]
234+
#[gtest]
234235
fn test_parse_ref_target_simple_partial() {
235236
let res = parse_ref_target("a.abc.d", TextRange::up_to(7.into()), 2.into());
236-
assert_eq!(
237+
expect_eq!(
237238
res,
238239
Some(vec![
239240
(
@@ -248,7 +249,7 @@ mod tests {
248249
);
249250

250251
let res = parse_ref_target("a.abc.d", TextRange::up_to(7.into()), 3.into());
251-
assert_eq!(
252+
expect_eq!(
252253
res,
253254
Some(vec![
254255
(
@@ -263,7 +264,7 @@ mod tests {
263264
);
264265

265266
let res = parse_ref_target("a.abc.d", TextRange::up_to(7.into()), 5.into());
266-
assert_eq!(
267+
expect_eq!(
267268
res,
268269
Some(vec![
269270
(
@@ -278,10 +279,10 @@ mod tests {
278279
);
279280
}
280281

281-
#[test]
282+
#[gtest]
282283
fn test_parse_ref_target_type() {
283284
let res = parse_ref_target("a.b.[c.d].e", TextRange::up_to(11.into()), 11.into());
284-
assert_eq!(
285+
expect_eq!(
285286
res,
286287
Some(vec![
287288
(
@@ -304,10 +305,10 @@ mod tests {
304305
)
305306
}
306307

307-
#[test]
308+
#[gtest]
308309
fn test_parse_ref_target_type_at_end() {
309310
let res = parse_ref_target("a.b.[c.d]", TextRange::up_to(9.into()), 9.into());
310-
assert_eq!(
311+
expect_eq!(
311312
res,
312313
Some(vec![
313314
(
@@ -326,14 +327,14 @@ mod tests {
326327
)
327328
}
328329

329-
#[test]
330+
#[gtest]
330331
fn test_parse_ref_target_type_braces_strings() {
331332
let res = parse_ref_target(
332333
"a.b.[fun(x: table<int, string>): { n: int, lit: \"}]\" }]",
333334
TextRange::up_to(55.into()),
334335
55.into(),
335336
);
336-
assert_eq!(
337+
expect_eq!(
337338
res,
338339
Some(vec![
339340
(
@@ -354,10 +355,10 @@ mod tests {
354355
)
355356
}
356357

357-
#[test]
358+
#[gtest]
358359
fn test_parse_ref_target_type_string_literal() {
359360
let res = parse_ref_target("a.b.['c']", TextRange::up_to(9.into()), 9.into());
360-
assert_eq!(
361+
expect_eq!(
361362
res,
362363
Some(vec![
363364
(

0 commit comments

Comments
 (0)