Skip to content

Commit 140a51b

Browse files
author
Markus Westerlind
authored
chore: Convert all parser test to use expect_test (#5208)
* chore: Add custom debug implementations for BaseNode and SourceLocation These shrink the size of `Debug` printed AST's which should make it feasible to use `expect_test` for the parser tests instead of manually writing it out which is tedious when adding tests and also very brittle since any change to one of the AST types may lead to several tests needing to be manually updated. * chore: Convert all parser tests to use expect_test * chore: Convert all type parser tests to use expect_test * chore: Convert all from parser tests to use expect_test * chore: Convert all error parser tests to use expect_test * chore: Convert all object parser tests to use expect_test * chore: Convert all string parser tests to use expect_test * chore: Convert all literal parser tests to use expect_test * test: Actually test the property list module * chore: Convert all property_list parser tests to use expect_test * chore: Convert all arrow_function parser tests to use expect_test * chore: Convert all operator_precedence parser tests to use expect_test * chore: make generate * chore: cargo fmt
1 parent 41ef75c commit 140a51b

File tree

12 files changed

+19548
-12148
lines changed

12 files changed

+19548
-12148
lines changed

libflux/flux-core/src/ast/mod.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl From<Position> for lsp_types::Position {
7474
}
7575

7676
/// Represents the location of a node in the AST.
77-
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
77+
#[derive(Default, PartialEq, Clone, Serialize, Deserialize)]
7878
pub struct SourceLocation {
7979
/// File is the optional file name.
8080
#[serde(skip_serializing_if = "skip_string_option")]
@@ -88,6 +88,33 @@ pub struct SourceLocation {
8888
pub source: Option<String>,
8989
}
9090

91+
// Custom debug implentation which reduces the size of `Debug` printing `AST`s
92+
impl fmt::Debug for SourceLocation {
93+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94+
let mut f = f.debug_struct("SourceLocation");
95+
96+
if let Some(file) = &self.file {
97+
f.field("file", file);
98+
}
99+
100+
// Render the positions on a single line so that `Debug` printing `AST`s are less verbose
101+
f.field(
102+
"start",
103+
&format!("line: {}, column: {}", self.start.line, self.start.column),
104+
);
105+
f.field(
106+
"end",
107+
&format!("line: {}, column: {}", self.end.line, self.end.column),
108+
);
109+
110+
if let Some(source) = &self.source {
111+
f.field("source", source);
112+
}
113+
114+
f.finish()
115+
}
116+
}
117+
91118
impl SourceLocation {
92119
#[allow(missing_docs)]
93120
pub fn is_valid(&self) -> bool {
@@ -391,7 +418,7 @@ pub struct Comment {
391418
}
392419

393420
/// BaseNode holds the attributes every expression or statement must have.
394-
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
421+
#[derive(Default, PartialEq, Clone, Serialize, Deserialize)]
395422
#[allow(missing_docs)]
396423
pub struct BaseNode {
397424
#[serde(default)]
@@ -409,6 +436,24 @@ pub struct BaseNode {
409436
pub errors: Vec<String>,
410437
}
411438

439+
// Custom debug implentation which reduces the size of `Debug` printing `AST`s
440+
impl fmt::Debug for BaseNode {
441+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
442+
let mut f = f.debug_struct("BaseNode");
443+
f.field("location", &self.location);
444+
445+
if !self.comments.is_empty() {
446+
f.field("comments", &self.comments);
447+
}
448+
449+
if !self.errors.is_empty() {
450+
f.field("errors", &self.errors);
451+
}
452+
453+
f.finish()
454+
}
455+
}
456+
412457
impl BaseNode {
413458
#[allow(missing_docs)]
414459
pub fn is_empty(&self) -> bool {

0 commit comments

Comments
 (0)