Skip to content

Switch emmylua_parser_desc to gtest #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crates/emmylua_parser_desc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ emmylua_parser.workspace = true

# external
rowan.workspace = true
unicode-general-category.workspace = true
unicode-general-category.workspace = true

[dev-dependencies]
googletest.workspace = true
39 changes: 20 additions & 19 deletions crates/emmylua_parser_desc/src/lang/json/json_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ impl<'a> JsonLexer<'a> {
mod tests {
use super::*;
use emmylua_parser::Reader;
use googletest::prelude::*;

#[test]
#[gtest]
fn test_json_lexer_basic() {
let json = r#"
{
Expand Down Expand Up @@ -235,19 +236,19 @@ mod tests {
}
}

assert!(string_count > 0, "Should find strings");
assert!(number_count > 0, "Should find numbers");
assert!(keyword_count > 0, "Should find keywords");
assert!(brace_count > 0, "Should find braces");
assert!(bracket_count > 0, "Should find brackets");
expect_gt!(string_count, 0, "Should find strings");
expect_gt!(number_count, 0, "Should find numbers");
expect_gt!(keyword_count, 0, "Should find keywords");
expect_gt!(brace_count, 0, "Should find braces");
expect_gt!(bracket_count, 0, "Should find brackets");

println!(
"Found {} strings, {} numbers, {} keywords, {} braces, {} brackets",
string_count, number_count, keyword_count, brace_count, bracket_count
);
}

#[test]
#[gtest]
fn test_json_lexer_keywords() {
let json = "true false null";

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

assert_eq!(keywords.len(), 3, "Should find exactly 3 keywords");
expect_eq!(keywords.len(), 3, "Should find exactly 3 keywords");
}

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

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

assert_eq!(numbers.len(), 7, "Should find exactly 7 numbers");
expect_eq!(numbers.len(), 7, "Should find exactly 7 numbers");
}

#[test]
#[gtest]
fn test_json_lexer_strings() {
let json = r#""hello" "world with spaces" "escaped\"quote" "unicode\u0041""#;

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

assert_eq!(strings.len(), 4, "Should find exactly 4 strings");
expect_eq!(strings.len(), 4, "Should find exactly 4 strings");
}

#[test]
#[gtest]
fn test_json_lexer_structure() {
let json = r#"{"key": ["value1", "value2"]}"#;

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

assert!(has_left_brace, "Should have left brace");
assert!(has_right_brace, "Should have right brace");
assert!(has_left_bracket, "Should have left bracket");
assert!(has_right_bracket, "Should have right bracket");
assert!(has_colon, "Should have colon");
assert!(has_comma, "Should have comma");
expect_true!(has_left_brace, "Should have left brace");
expect_true!(has_right_brace, "Should have right brace");
expect_true!(has_left_bracket, "Should have left bracket");
expect_true!(has_right_bracket, "Should have right bracket");
expect_true!(has_colon, "Should have colon");
expect_true!(has_comma, "Should have comma");
}
}
15 changes: 8 additions & 7 deletions crates/emmylua_parser_desc/src/lang/vimscript/vim_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,9 @@ impl<'a> VimscriptLexer<'a> {
mod tests {
use super::*;
use emmylua_parser::Reader;
use googletest::prelude::*;

#[test]
#[gtest]
fn test_vim_lexer_basic() {
let code = r#"
" This is a comment
Expand Down Expand Up @@ -483,18 +484,18 @@ endfunction
}
}

assert!(keyword_count > 0, "Should find keywords");
assert!(string_count > 0, "Should find strings");
assert!(comment_count > 0, "Should find comments");
assert!(number_count > 0, "Should find numbers");
expect_gt!(keyword_count, 0, "Should find keywords");
expect_gt!(string_count, 0, "Should find strings");
expect_gt!(comment_count, 0, "Should find comments");
expect_gt!(number_count, 0, "Should find numbers");

println!(
"Found {} keywords, {} strings, {} comments, {} numbers",
keyword_count, string_count, comment_count, number_count
);
}

#[test]
#[gtest]
fn test_vim_lexer_keywords() {
let code = "function! if else endif let echo return";

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

assert!(keywords.len() >= 5, "Should find multiple keywords");
expect_ge!(keywords.len(), 5, "Should find multiple keywords");
}
}
50 changes: 31 additions & 19 deletions crates/emmylua_parser_desc/src/md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,9 +1465,10 @@ mod tests {
use super::*;
#[allow(unused)]
use crate::testlib::{print_result, test};
use googletest::prelude::*;

#[test]
fn test_md() {
#[gtest]
fn test_md() -> Result<()> {
let code = r#"
--- # Inline code
---
Expand Down Expand Up @@ -1720,11 +1721,12 @@ mod tests {
"#;

// print_result(&code, Box::new(MdParser::new(None)));
test(&code, Box::new(MdParser::new(None)), &expected);
test(&code, Box::new(MdParser::new(None)), &expected).or_fail()?;
Ok(())
}

#[test]
fn test_myst() {
#[gtest]
fn test_myst() -> Result<()> {
let code = r#"
--- # Inline
---
Expand Down Expand Up @@ -1823,11 +1825,12 @@ mod tests {
--- <Markup>$$</Markup> <Arg>(anchor)</Arg></Scope>
"#;

test(&code, Box::new(MdParser::new_myst(None, None)), &expected);
test(&code, Box::new(MdParser::new_myst(None, None)), &expected).or_fail()?;
Ok(())
}

#[test]
fn test_myst_primary_domain() {
#[gtest]
fn test_myst_primary_domain() -> Result<()> {
let code = r#"--- See {obj}`ref`"#;

let expected = r#"
Expand All @@ -1838,48 +1841,56 @@ mod tests {
&code,
Box::new(MdParser::new_myst(Some("lua".to_string()), None)),
&expected,
);
)
.or_fail()?;
Ok(())
}

#[test]
fn test_myst_search_at_offset() {
#[gtest]
fn test_myst_search_at_offset() -> Result<()> {
let code = r#"--- See {lua:obj}`x` {lua:obj}`ref`"#;
let expected = r#"--- See {lua:obj}`x` {lua:obj}`<Ref>ref</Ref>`"#;
test(
&code,
Box::new(MdParser::new_myst(None, Some(31))),
&expected,
);
)
.or_fail()?;
test(
&code,
Box::new(MdParser::new_myst(None, Some(32))),
&expected,
);
)
.or_fail()?;
test(
&code,
Box::new(MdParser::new_myst(None, Some(34))),
&expected,
);
)
.or_fail()?;

let code = r#"--- See {lua:obj}`x` {lua:obj}`"#;
let expected = r#"--- See {lua:obj}`x` {lua:obj}`<Ref></Ref>"#;
test(
&code,
Box::new(MdParser::new_myst(None, Some(31))),
&expected,
);
)
.or_fail()?;

let code = r#"--- See {lua:obj}`x` {lua:obj}``..."#;
let expected = r#"--- See {lua:obj}`x` {lua:obj}`<Ref>`...</Ref>"#;
test(
&code,
Box::new(MdParser::new_myst(None, Some(31))),
&expected,
);
)
.or_fail()?;
Ok(())
}

#[test]
fn test_md_no_indent() {
#[gtest]
fn test_md_no_indent() -> Result<()> {
let code = r#"
---```lua
---
Expand All @@ -1906,6 +1917,7 @@ local t = 123
local t = 123
"#;

test(&code, Box::new(MdParser::new(None)), &expected);
test(&code, Box::new(MdParser::new(None)), &expected).or_fail()?;
Ok(())
}
}
29 changes: 15 additions & 14 deletions crates/emmylua_parser_desc/src/ref_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ fn eat_string(reader: &mut Reader) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use googletest::prelude::*;

#[test]
#[gtest]
fn test_parse_ref_target_simple() {
let res = parse_ref_target("a.b.c.d", TextRange::up_to(7.into()), 7.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -230,10 +231,10 @@ mod tests {
)
}

#[test]
#[gtest]
fn test_parse_ref_target_simple_partial() {
let res = parse_ref_target("a.abc.d", TextRange::up_to(7.into()), 2.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -248,7 +249,7 @@ mod tests {
);

let res = parse_ref_target("a.abc.d", TextRange::up_to(7.into()), 3.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -263,7 +264,7 @@ mod tests {
);

let res = parse_ref_target("a.abc.d", TextRange::up_to(7.into()), 5.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -278,10 +279,10 @@ mod tests {
);
}

#[test]
#[gtest]
fn test_parse_ref_target_type() {
let res = parse_ref_target("a.b.[c.d].e", TextRange::up_to(11.into()), 11.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -304,10 +305,10 @@ mod tests {
)
}

#[test]
#[gtest]
fn test_parse_ref_target_type_at_end() {
let res = parse_ref_target("a.b.[c.d]", TextRange::up_to(9.into()), 9.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -326,14 +327,14 @@ mod tests {
)
}

#[test]
#[gtest]
fn test_parse_ref_target_type_braces_strings() {
let res = parse_ref_target(
"a.b.[fun(x: table<int, string>): { n: int, lit: \"}]\" }]",
TextRange::up_to(55.into()),
55.into(),
);
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand All @@ -354,10 +355,10 @@ mod tests {
)
}

#[test]
#[gtest]
fn test_parse_ref_target_type_string_literal() {
let res = parse_ref_target("a.b.['c']", TextRange::up_to(9.into()), 9.into());
assert_eq!(
expect_eq!(
res,
Some(vec![
(
Expand Down
Loading
Loading