Skip to content

Commit

Permalink
Add basic edition inline parser test support
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jul 18, 2024
1 parent 7a5bf92 commit 34d01b0
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 166 deletions.
2 changes: 1 addition & 1 deletion crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub(super) fn atom_expr(
stmt_list(p);
m.complete(p, BLOCK_EXPR)
}
// test_err gen_blocks
// test gen_blocks 2024
// pub fn main() {
// gen { yield ""; };
// async gen { yield ""; };
Expand Down
26 changes: 18 additions & 8 deletions crates/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn lex(text: &str) -> String {
fn parse_ok() {
for case in TestCase::list("parser/ok") {
let _guard = stdx::panic_context::enter(format!("{:?}", case.rs));
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text);
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text, Edition::CURRENT);
assert!(!errors, "errors in an OK file {}:\n{actual}", case.rs.display());
expect_file![case.rast].assert_eq(&actual);
}
Expand All @@ -61,16 +61,16 @@ fn parse_ok() {
fn parse_err() {
for case in TestCase::list("parser/err") {
let _guard = stdx::panic_context::enter(format!("{:?}", case.rs));
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text);
let (actual, errors) = parse(TopEntryPoint::SourceFile, &case.text, Edition::CURRENT);
assert!(errors, "no errors in an ERR file {}:\n{actual}", case.rs.display());
expect_file![case.rast].assert_eq(&actual)
}
}

fn parse(entry: TopEntryPoint, text: &str) -> (String, bool) {
let lexed = LexedStr::new(Edition::CURRENT, text);
fn parse(entry: TopEntryPoint, text: &str, edition: Edition) -> (String, bool) {
let lexed = LexedStr::new(edition, text);
let input = lexed.to_input();
let output = entry.parse(&input, Edition::CURRENT);
let output = entry.parse(&input, edition);

let mut buf = String::new();
let mut errors = Vec::new();
Expand Down Expand Up @@ -153,9 +153,19 @@ impl TestCase {

#[track_caller]
fn run_and_expect_no_errors(path: &str) {
run_and_expect_no_errors_with_edition(path, Edition::CURRENT)
}

#[track_caller]
fn run_and_expect_errors(path: &str) {
run_and_expect_errors_with_edition(path, Edition::CURRENT)
}

#[track_caller]
fn run_and_expect_no_errors_with_edition(path: &str, edition: Edition) {
let path = PathBuf::from(path);
let text = std::fs::read_to_string(&path).unwrap();
let (actual, errors) = parse(TopEntryPoint::SourceFile, &text);
let (actual, errors) = parse(TopEntryPoint::SourceFile, &text, edition);
assert!(!errors, "errors in an OK file {}:\n{actual}", path.display());
let mut p = PathBuf::from("..");
p.push(path);
Expand All @@ -164,10 +174,10 @@ fn run_and_expect_no_errors(path: &str) {
}

#[track_caller]
fn run_and_expect_errors(path: &str) {
fn run_and_expect_errors_with_edition(path: &str, edition: Edition) {
let path = PathBuf::from(path);
let text = std::fs::read_to_string(&path).unwrap();
let (actual, errors) = parse(TopEntryPoint::SourceFile, &text);
let (actual, errors) = parse(TopEntryPoint::SourceFile, &text, edition);
assert!(errors, "no errors in an ERR file {}:\n{actual}", path.display());
let mut p = PathBuf::from("..");
p.push(path);
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/tests/top_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,6 @@ fn expr() {

#[track_caller]
fn check(entry: TopEntryPoint, input: &str, expect: expect_test::Expect) {
let (parsed, _errors) = super::parse(entry, input);
let (parsed, _errors) = super::parse(entry, input, crate::Edition::CURRENT);
expect.assert_eq(&parsed)
}
13 changes: 9 additions & 4 deletions crates/parser/test_data/generated/runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod ok {
use crate::tests::run_and_expect_no_errors;
use crate::tests::*;
#[test]
fn anonymous_const() {
run_and_expect_no_errors("test_data/parser/inline/ok/anonymous_const.rs");
Expand Down Expand Up @@ -267,6 +267,13 @@ mod ok {
run_and_expect_no_errors("test_data/parser/inline/ok/function_where_clause.rs");
}
#[test]
fn gen_blocks() {
run_and_expect_no_errors_with_edition(
"test_data/parser/inline/ok/gen_blocks.rs",
crate::Edition::Edition2024,
);
}
#[test]
fn generic_arg() { run_and_expect_no_errors("test_data/parser/inline/ok/generic_arg.rs"); }
#[test]
fn generic_param_attribute() {
Expand Down Expand Up @@ -666,7 +673,7 @@ mod ok {
fn yield_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/yield_expr.rs"); }
}
mod err {
use crate::tests::run_and_expect_errors;
use crate::tests::*;
#[test]
fn angled_path_without_qual() {
run_and_expect_errors("test_data/parser/inline/err/angled_path_without_qual.rs");
Expand Down Expand Up @@ -708,8 +715,6 @@ mod err {
run_and_expect_errors("test_data/parser/inline/err/fn_pointer_type_missing_fn.rs");
}
#[test]
fn gen_blocks() { run_and_expect_errors("test_data/parser/inline/err/gen_blocks.rs"); }
#[test]
fn gen_fn() { run_and_expect_errors("test_data/parser/inline/err/gen_fn.rs"); }
#[test]
fn generic_arg_list_recover() {
Expand Down
139 changes: 0 additions & 139 deletions crates/parser/test_data/parser/inline/err/gen_blocks.rast

This file was deleted.

101 changes: 101 additions & 0 deletions crates/parser/test_data/parser/inline/ok/gen_blocks.rast
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
SOURCE_FILE
FN
COMMENT "// 2024"
WHITESPACE "\n"
VISIBILITY
PUB_KW "pub"
WHITESPACE " "
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "main"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
BLOCK_EXPR
GEN_KW "gen"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
EXPR_STMT
YIELD_EXPR
YIELD_KW "yield"
WHITESPACE " "
LITERAL
STRING "\"\""
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
BLOCK_EXPR
ASYNC_KW "async"
WHITESPACE " "
GEN_KW "gen"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
EXPR_STMT
YIELD_EXPR
YIELD_KW "yield"
WHITESPACE " "
LITERAL
STRING "\"\""
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
BLOCK_EXPR
GEN_KW "gen"
WHITESPACE " "
MOVE_KW "move"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
EXPR_STMT
YIELD_EXPR
YIELD_KW "yield"
WHITESPACE " "
LITERAL
STRING "\"\""
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
BLOCK_EXPR
ASYNC_KW "async"
WHITESPACE " "
GEN_KW "gen"
WHITESPACE " "
MOVE_KW "move"
WHITESPACE " "
STMT_LIST
L_CURLY "{"
WHITESPACE " "
EXPR_STMT
YIELD_EXPR
YIELD_KW "yield"
WHITESPACE " "
LITERAL
STRING "\"\""
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// 2024
pub fn main() {
gen { yield ""; };
async gen { yield ""; };
Expand Down
Loading

0 comments on commit 34d01b0

Please sign in to comment.