Skip to content

Commit

Permalink
test(ast): add snapshot tests and tooling
Browse files Browse the repository at this point in the history
Co-authored-by: Samyak S Sarnayak <[email protected]>
  • Loading branch information
ishwar00 and Samyak2 committed Sep 24, 2023
1 parent be7cef4 commit d1025a5
Show file tree
Hide file tree
Showing 18 changed files with 2,163 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/src/bakugo.pest
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ WHITESPACE = _{ "\u{0020}" | "\u{0009}" | "\u{000D}" | "\u{000A}" }

/// No comments containg `NewLine`.
// Refer TODO[3]
COMMENT = _{ "/*" ~ (!("*/" | NewLine) ~ ANY)* ~ "*/" }
COMMENT = _{ "/*" ~ (!("*/") ~ ANY)* ~ "*/" }

/// Punctuation
Semicolon = { ";" }
Expand Down
1 change: 1 addition & 0 deletions compiler/tests/examples/01_empty_main.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
func main() {};
1 change: 0 additions & 1 deletion compiler/tests/examples/01_sanity.bakugo

This file was deleted.

22 changes: 22 additions & 0 deletions compiler/tests/examples/02_comments.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func main() {
/* */

/**/

/* hey there */

/*hey there*/

/*bakugo*/

/*

*/

/*
*/

/*
bakugo
*/
};
1 change: 0 additions & 1 deletion compiler/tests/examples/02_sanity.bakugo

This file was deleted.

2 changes: 1 addition & 1 deletion compiler/tests/examples/04_expressions.bakugo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func expr(int) {
func expr() {
-4 + 2 * 3;
4 & 3 | 1;
4;
Expand Down
2 changes: 2 additions & 0 deletions compiler/tests/examples/error/01_func_parameters.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func expr(int) {
};
44 changes: 41 additions & 3 deletions compiler/tests/parser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
use bakugo::parser::*;
use insta::{assert_snapshot, assert_yaml_snapshot, glob};
use std::{fmt::Display, fs};

use bakugo::{ast::BakugoParsingErrorDisplay, parser::*};
use insta::{
assert_debug_snapshot, assert_display_snapshot, assert_snapshot, assert_yaml_snapshot, glob,
};
use miette::{GraphicalReportHandler, GraphicalTheme, NamedSource};
use pest::Parser;

struct BakugoParsingErrDebug(BakugoParsingErrorDisplay);

impl Display for BakugoParsingErrDebug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let report_handler = GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor());
report_handler.render_report(f, &self.0)
}
}

#[test]
fn test_parser() {
glob!("examples/*.bakugo", |_| {});
glob!("examples/**/*.bakugo", |path| {
let input = fs::read_to_string(path).unwrap();
let parsed = parse_string(&input);
match parsed {
Ok(parsed) => assert_yaml_snapshot!(parsed),
Err(err) => assert_snapshot!(err.to_string()),
}
});
}

#[test]
fn test_ast() {
glob!("examples/**/*.bakugo", |path| {
let input = fs::read_to_string(path).unwrap();
let mut parsed = parse_string(&input).unwrap();
let source_file = parsed.next().unwrap();
let source = NamedSource::new(
path.file_name().unwrap().to_str().unwrap().to_string(),
input.clone(),
);
match construct_ast(source, source_file) {
Ok(parsed) => assert_debug_snapshot!(parsed),
Err(err) => assert_display_snapshot!(BakugoParsingErrDebug(err)),
}
});
}

#[test]
Expand Down
29 changes: 29 additions & 0 deletions compiler/tests/snapshots/parser__ast@01_empty_main.bakugo.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: tests/parser.rs
expression: parsed
input_file: tests/examples/01_empty_main.bakugo
---
SourceFile {
top_level: [
FnDecl(
FnDecl {
name: Ident {
value: "main",
span: Span {
str: "main",
start: 5,
end: 9,
},
},
params: [],
result_kind: None,
body: [],
span: Span {
str: "func main() {}",
start: 0,
end: 14,
},
},
),
],
}
29 changes: 29 additions & 0 deletions compiler/tests/snapshots/parser__ast@02_comments.bakugo.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: tests/parser.rs
expression: parsed
input_file: tests/examples/02_comments.bakugo
---
SourceFile {
top_level: [
FnDecl(
FnDecl {
name: Ident {
value: "main",
span: Span {
str: "main",
start: 5,
end: 9,
},
},
params: [],
result_kind: None,
body: [],
span: Span {
str: "func main() {\n\t/* */\n\n\t/**/\n\n\t/* hey there */\n\n\t/*hey there*/\n\n\t/*bakugo*/\n\n\t/*\n\n\t*/\n\n\t/*\n\t*/\n\n\t/*\n\t\tbakugo\n\t*/\n}",
start: 0,
end: 114,
},
},
),
],
}
Loading

0 comments on commit d1025a5

Please sign in to comment.