Skip to content

Commit

Permalink
Share utils between sway-parse unit tests (FuelLabs#4265)
Browse files Browse the repository at this point in the history
This PR adds `mod sway_parse::test_utils` containing two utils, `fn
parse<T>` and `fn parse_to_end<T>`, to replace non-generic utils like
`fn parse_item` or `fn parse_path_expr`.
  • Loading branch information
AlicanC authored Mar 11, 2023
1 parent 9934103 commit 94171f5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 58 deletions.
15 changes: 2 additions & 13 deletions sway-parse/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,13 @@ impl ParseToEnd for Attribute {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::parse;
use insta::*;
use std::sync::Arc;
use sway_ast::ItemFn;

fn parse_annotated<T>(input: &str) -> Annotated<T>
where
T: Parse,
{
let handler = <_>::default();
let ts = crate::token::lex(&handler, &Arc::from(input), 0, input.len(), None).unwrap();
Parser::new(&handler, &ts)
.parse()
.unwrap_or_else(|_| panic!("Parse error: {:?}", handler.consume().0))
}

#[test]
fn parse_annotated_fn() {
assert_ron_snapshot!(parse_annotated::<ItemFn>(r#"
assert_ron_snapshot!(parse::<Annotated<ItemFn>>(r#"
// I will be ignored.
//! I will be ignored.
/// This is a doc comment.
Expand Down
40 changes: 16 additions & 24 deletions sway-parse/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,9 @@ impl Parse for FnSignature {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use crate::test_utils::parse;
use sway_ast::{AttributeDecl, Item, ItemTraitItem};

fn parse_item(input: &str) -> Item {
let handler = <_>::default();
let ts = crate::token::lex(&handler, &Arc::from(input), 0, input.len(), None).unwrap();
Parser::new(&handler, &ts)
.parse()
.unwrap_or_else(|_| panic!("Parse error: {:?}", handler.consume().0))
}

// Attribute name and its list of parameters
type ParameterizedAttr<'a> = (&'a str, Option<Vec<&'a str>>);

Expand All @@ -204,7 +196,7 @@ mod tests {

#[test]
fn parse_doc_comment() {
let item = parse_item(
let item = parse::<Item>(
r#"
// I will be ignored.
//! I will be ignored.
Expand All @@ -225,7 +217,7 @@ mod tests {

#[test]
fn parse_doc_comment_struct() {
let item = parse_item(
let item = parse::<Item>(
r#"
// I will be ignored.
//! I will be ignored.
Expand Down Expand Up @@ -264,7 +256,7 @@ mod tests {

#[test]
fn parse_attributes_none() {
let item = parse_item(
let item = parse::<Item>(
r#"
fn f() -> bool {
false
Expand All @@ -278,7 +270,7 @@ mod tests {

#[test]
fn parse_attributes_fn_basic() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[foo]
fn f() -> bool {
Expand All @@ -293,7 +285,7 @@ mod tests {

#[test]
fn parse_attributes_fn_two_basic() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[foo]
#[bar]
Expand All @@ -313,7 +305,7 @@ mod tests {

#[test]
fn parse_attributes_fn_one_arg() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[foo(one)]
fn f() -> bool {
Expand All @@ -331,7 +323,7 @@ mod tests {

#[test]
fn parse_attributes_fn_empty_parens() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[foo()]
fn f() -> bool {
Expand All @@ -349,7 +341,7 @@ mod tests {

#[test]
fn parse_attributes_fn_zero_and_one_arg() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[bar]
#[foo(one)]
Expand All @@ -368,7 +360,7 @@ mod tests {

#[test]
fn parse_attributes_fn_one_and_zero_arg() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[foo(one)]
#[bar]
Expand All @@ -387,7 +379,7 @@ mod tests {

#[test]
fn parse_attributes_fn_two_args() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[foo(one, two)]
fn f() -> bool {
Expand All @@ -405,7 +397,7 @@ mod tests {

#[test]
fn parse_attributes_fn_zero_one_and_three_args() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[bar]
#[foo(one)]
Expand All @@ -429,7 +421,7 @@ mod tests {

#[test]
fn parse_attributes_fn_zero_one_and_three_args_in_one_attribute_decl() {
let item = parse_item(
let item = parse::<Item>(
r#"
#[bar, foo(one), baz(two,three,four)]
fn f() -> bool {
Expand All @@ -451,7 +443,7 @@ mod tests {

#[test]
fn parse_attributes_trait() {
let item = parse_item(
let item = parse::<Item>(
r#"
trait T {
#[foo(one)]
Expand Down Expand Up @@ -505,7 +497,7 @@ mod tests {

#[test]
fn parse_attributes_abi() {
let item = parse_item(
let item = parse::<Item>(
r#"
abi A {
#[bar(one, two, three)]
Expand Down Expand Up @@ -564,7 +556,7 @@ mod tests {

#[test]
fn parse_attributes_doc_comment() {
let item = parse_item(
let item = parse::<Item>(
r#"
/// This is a doc comment.
/// This is another doc comment.
Expand Down
2 changes: 2 additions & 0 deletions sway-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod pattern;
mod priv_prelude;
mod punctuated;
mod submodule;
#[cfg(test)]
mod test_utils;
mod token;
mod ty;
mod where_clause;
Expand Down
13 changes: 2 additions & 11 deletions sway-parse/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,12 @@ impl ParseToEnd for Annotated<Module> {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::parse_to_end;
use insta::*;
use std::sync::Arc;

fn parse_annotated_module(input: &str) -> Annotated<Module> {
let handler = <_>::default();
let ts = crate::token::lex(&handler, &Arc::from(input), 0, input.len(), None).unwrap();
Parser::new(&handler, &ts)
.parse_to_end()
.map(|(m, _)| m)
.unwrap_or_else(|_| panic!("Parse error: {:?}", handler.consume().0))
}

#[test]
fn parse_noop_script_module() {
assert_ron_snapshot!(parse_annotated_module(r#"
assert_ron_snapshot!(parse_to_end::<Annotated<Module>>(r#"
script;
fn main() {
Expand Down
12 changes: 2 additions & 10 deletions sway-parse/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,12 @@ impl Parse for QualifiedPathRoot {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::parse;
use insta::*;
use std::sync::Arc;

fn parse_path_expr(input: &str) -> PathExpr {
let handler = <_>::default();
let ts = crate::token::lex(&handler, &Arc::from(input), 0, input.len(), None).unwrap();
Parser::new(&handler, &ts)
.parse()
.unwrap_or_else(|_| panic!("Parse error: {:?}", handler.consume().0))
}

#[test]
fn parse_nested_path() {
assert_ron_snapshot!(parse_path_expr(r#"
assert_ron_snapshot!(parse::<PathExpr>(r#"
std::vec::Vec
"#,), @r###"
PathExpr(
Expand Down
25 changes: 25 additions & 0 deletions sway-parse/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::{priv_prelude::ParseToEnd, Parse, Parser};
use std::sync::Arc;

pub fn parse<T>(input: &str) -> T
where
T: Parse,
{
let handler = <_>::default();
let ts = crate::token::lex(&handler, &Arc::from(input), 0, input.len(), None).unwrap();
Parser::new(&handler, &ts)
.parse()
.unwrap_or_else(|_| panic!("Parse error: {:?}", handler.consume().0))
}

pub fn parse_to_end<T>(input: &str) -> T
where
T: ParseToEnd,
{
let handler = <_>::default();
let ts = crate::token::lex(&handler, &Arc::from(input), 0, input.len(), None).unwrap();
Parser::new(&handler, &ts)
.parse_to_end()
.map(|(m, _)| m)
.unwrap_or_else(|_| panic!("Parse error: {:?}", handler.consume().0))
}

0 comments on commit 94171f5

Please sign in to comment.