Skip to content
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

Snapshot-based testing with cargo-insta #666

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ once_cell = "1.8.0"

[build-dependencies]
sha-1 = { version = "0.10", default-features = false }

[dev-dependencies]
insta = "1.16.0"
8 changes: 4 additions & 4 deletions meta/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ mod tests {
Box::new(Expr::Str(String::from("b"))),
);
let mut top_down = expr.iter_top_down();
assert_eq!(top_down.next(), Some(expr));
assert_eq!(top_down.next(), Some(Expr::Str(String::from("a"))));
assert_eq!(top_down.next(), Some(Expr::Str(String::from("b"))));
assert_eq!(top_down.next(), None);
insta::assert_debug_snapshot!(top_down.next());
insta::assert_debug_snapshot!(top_down.next());
insta::assert_debug_snapshot!(top_down.next());
insta::assert_debug_snapshot!(top_down.next());
}

#[test]
Expand Down
145 changes: 12 additions & 133 deletions meta/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,8 @@ mod tests {
)),
}]
};
let rotated = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Choice(
Str(String::from("a")),
Choice(
Str(String::from("b")),
Choice(Str(String::from("c")), Str(String::from("d")))
)
)),
}]
};

assert_eq!(optimize(rules), rotated);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -325,13 +311,8 @@ mod tests {
))),
}]
};
let skipped = vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
expr: OptimizedExpr::Skip(vec![String::from("a"), String::from("b")]),
}];

assert_eq!(optimize(rules), skipped);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -347,13 +328,8 @@ mod tests {
)),
}]
};
let concatenated = vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
expr: OptimizedExpr::Str(String::from("abcd")),
}];

assert_eq!(optimize(rules), concatenated);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -363,19 +339,8 @@ mod tests {
ty: RuleType::Atomic,
expr: Expr::RepExact(Box::new(Expr::Ident(String::from("a"))), 3),
}];
let unrolled = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
expr: box_tree!(Seq(
Ident(String::from("a")),
Seq(Ident(String::from("a")), Ident(String::from("a")))
)),
}]
};

assert_eq!(optimize(rules), unrolled);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -385,19 +350,8 @@ mod tests {
ty: RuleType::Atomic,
expr: Expr::RepMax(Box::new(Expr::Str("a".to_owned())), 3),
}];
let unrolled = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
expr: box_tree!(Seq(
Opt(Str(String::from("a"))),
Seq(Opt(Str(String::from("a"))), Opt(Str(String::from("a"))))
)),
}]
};

assert_eq!(optimize(rules), unrolled);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -407,19 +361,8 @@ mod tests {
ty: RuleType::Atomic,
expr: Expr::RepMin(Box::new(Expr::Str("a".to_owned())), 2),
}];
let unrolled = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
expr: box_tree!(Seq(
Str(String::from("a")),
Seq(Str(String::from("a")), Rep(Str(String::from("a"))))
)),
}]
};

assert_eq!(optimize(rules), unrolled);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -429,26 +372,8 @@ mod tests {
ty: RuleType::Atomic,
expr: Expr::RepMinMax(Box::new(Expr::Str("a".to_owned())), 2, 3),
}];
let unrolled = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
/* TODO possible room for improvement here:
* if the sequences were rolled out in the opposite
* order, we could further optimize the strings
* in cases like this.
Str(String::from(("aa")),
Opt(Str(String::from("a")))
*/
expr: box_tree!(Seq(
Str(String::from("a")),
Seq(Str(String::from("a")), Opt(Str(String::from("a"))))
)),
}]
};

assert_eq!(optimize(rules), unrolled);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -464,13 +389,8 @@ mod tests {
)),
}]
};
let concatenated = vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Atomic,
expr: OptimizedExpr::Insens(String::from("abcd")),
}];

assert_eq!(optimize(rules), concatenated);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -492,22 +412,8 @@ mod tests {
)),
}]
};
let optimized = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Silent,
expr: box_tree!(Seq(
Ident(String::from("a")),
Seq(
Ident(String::from("b")),
Choice(Ident(String::from("c")), Ident(String::from("d")))
)
)),
}]
};

assert_eq!(optimize(rules), optimized);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -523,16 +429,8 @@ mod tests {
)),
}]
};
let optimized = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Silent,
expr: box_tree!(Seq(Ident(String::from("a")), Opt(Ident(String::from("b"))))),
}]
};

assert_eq!(optimize(rules), optimized);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -548,16 +446,8 @@ mod tests {
)),
}]
};
let optimized = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Silent,
expr: box_tree!(Ident(String::from("a"))),
}]
};

assert_eq!(optimize(rules), optimized);
insta::assert_debug_snapshot!(optimize(rules));
}

#[test]
Expand All @@ -573,18 +463,7 @@ mod tests {
)),
}]
};
let optimized = {
use crate::optimizer::OptimizedExpr::*;
vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Silent,
expr: box_tree!(Seq(
Ident(String::from("a")),
Rep(Seq(Ident(String::from("b")), Ident(String::from("a"))))
)),
}]
};

assert_eq!(optimize(rules), optimized);
insta::assert_debug_snapshot!(optimize(rules));
}
}
27 changes: 4 additions & 23 deletions meta/src/optimizer/restorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ fn child_modifies_state(

#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;

use super::*;
use crate::optimizer::OptimizedExpr::*;

Expand All @@ -116,16 +118,7 @@ mod tests {
expr: box_tree!(Rep(Push(Str("a".to_string())))),
}];

let restored = OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Rep(RestoreOnErr(Push(Str("a".to_string()))))),
};

assert_eq!(
restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
restored
);
insta::assert_debug_snapshot!(restore_on_err(rules[0].clone(), &to_hash_map(&rules)));
}

#[test]
Expand All @@ -136,18 +129,6 @@ mod tests {
expr: box_tree!(Choice(Push(Str("a".to_string())), Str("a".to_string()))),
}];

let restored = OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Choice(
RestoreOnErr(Push(Str("a".to_string()))),
Str("a".to_string())
)),
};

assert_eq!(
restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
restored
);
assert_debug_snapshot!(restore_on_err(rules[0].clone(), &to_hash_map(&rules)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: meta/src/optimizer/restorer.rs
expression: "restore_on_err(rules[0].clone(), &to_hash_map(&rules))"
---
OptimizedRule {
name: "rule",
ty: Normal,
expr: Choice(
RestoreOnErr(
Push(
Str(
"a",
),
),
),
Str(
"a",
),
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: meta/src/optimizer/restorer.rs
expression: "restore_on_err(rules[0].clone(), &to_hash_map(&rules))"
---
OptimizedRule {
name: "rule",
ty: Normal,
expr: Rep(
RestoreOnErr(
Push(
Str(
"a",
),
),
),
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: meta/src/optimizer/mod.rs
expression: optimize(rules)
---
[
OptimizedRule {
name: "rule",
ty: Atomic,
expr: Insens(
"abcd",
),
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: meta/src/optimizer/mod.rs
expression: optimize(rules)
---
[
OptimizedRule {
name: "rule",
ty: Atomic,
expr: Str(
"abcd",
),
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: meta/src/optimizer/mod.rs
expression: optimize(rules)
---
[
OptimizedRule {
name: "rule",
ty: Silent,
expr: Ident(
"a",
),
},
]
Loading