Skip to content

Commit

Permalink
chore: Convert all parser test to use expect_test (#5208)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Markus Westerlind authored Sep 21, 2022
1 parent 41ef75c commit 140a51b
Show file tree
Hide file tree
Showing 12 changed files with 19,548 additions and 12,148 deletions.
49 changes: 47 additions & 2 deletions libflux/flux-core/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl From<Position> for lsp_types::Position {
}

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

// Custom debug implentation which reduces the size of `Debug` printing `AST`s
impl fmt::Debug for SourceLocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_struct("SourceLocation");

if let Some(file) = &self.file {
f.field("file", file);
}

// Render the positions on a single line so that `Debug` printing `AST`s are less verbose
f.field(
"start",
&format!("line: {}, column: {}", self.start.line, self.start.column),
);
f.field(
"end",
&format!("line: {}, column: {}", self.end.line, self.end.column),
);

if let Some(source) = &self.source {
f.field("source", source);
}

f.finish()
}
}

impl SourceLocation {
#[allow(missing_docs)]
pub fn is_valid(&self) -> bool {
Expand Down Expand Up @@ -391,7 +418,7 @@ pub struct Comment {
}

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

// Custom debug implentation which reduces the size of `Debug` printing `AST`s
impl fmt::Debug for BaseNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_struct("BaseNode");
f.field("location", &self.location);

if !self.comments.is_empty() {
f.field("comments", &self.comments);
}

if !self.errors.is_empty() {
f.field("errors", &self.errors);
}

f.finish()
}
}

impl BaseNode {
#[allow(missing_docs)]
pub fn is_empty(&self) -> bool {
Expand Down
Loading

0 comments on commit 140a51b

Please sign in to comment.