Skip to content

Commit 8ee028a

Browse files
committed
recursion test + ability to configure recursion limit for tests
1 parent 94d5466 commit 8ee028a

14 files changed

+346
-481
lines changed

src/parser/mod.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,7 +3509,6 @@ impl<'a> Parser<'a> {
35093509
}
35103510

35113511
/// Run a parser method `f`, reverting back to the current position if unsuccessful.
3512-
#[must_use]
35133512
pub fn maybe_parse<T, F>(&mut self, mut f: F) -> Result<Option<T>, ParserError>
35143513
where
35153514
F: FnMut(&mut Parser) -> Result<T, ParserError>,
@@ -12279,10 +12278,8 @@ mod tests {
1227912278
#[test]
1228012279
fn test_ansii_character_string_types() {
1228112280
// Character string types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-string-type>
12282-
let dialect = TestedDialects {
12283-
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
12284-
options: None,
12285-
};
12281+
let dialect =
12282+
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
1228612283

1228712284
test_parse_data_type!(dialect, "CHARACTER", DataType::Character(None));
1228812285

@@ -12409,10 +12406,8 @@ mod tests {
1240912406
#[test]
1241012407
fn test_ansii_character_large_object_types() {
1241112408
// Character large object types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-length>
12412-
let dialect = TestedDialects {
12413-
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
12414-
options: None,
12415-
};
12409+
let dialect =
12410+
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
1241612411

1241712412
test_parse_data_type!(
1241812413
dialect,
@@ -12442,10 +12437,9 @@ mod tests {
1244212437

1244312438
#[test]
1244412439
fn test_parse_custom_types() {
12445-
let dialect = TestedDialects {
12446-
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
12447-
options: None,
12448-
};
12440+
let dialect =
12441+
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
12442+
1244912443
test_parse_data_type!(
1245012444
dialect,
1245112445
"GEOMETRY",
@@ -12474,10 +12468,8 @@ mod tests {
1247412468
#[test]
1247512469
fn test_ansii_exact_numeric_types() {
1247612470
// Exact numeric types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type>
12477-
let dialect = TestedDialects {
12478-
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
12479-
options: None,
12480-
};
12471+
let dialect =
12472+
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
1248112473

1248212474
test_parse_data_type!(dialect, "NUMERIC", DataType::Numeric(ExactNumberInfo::None));
1248312475

@@ -12525,10 +12517,8 @@ mod tests {
1252512517
#[test]
1252612518
fn test_ansii_date_type() {
1252712519
// Datetime types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type>
12528-
let dialect = TestedDialects {
12529-
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
12530-
options: None,
12531-
};
12520+
let dialect =
12521+
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
1253212522

1253312523
test_parse_data_type!(dialect, "DATE", DataType::Date);
1253412524

@@ -12637,10 +12627,8 @@ mod tests {
1263712627
}};
1263812628
}
1263912629

12640-
let dialect = TestedDialects {
12641-
dialects: vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})],
12642-
options: None,
12643-
};
12630+
let dialect =
12631+
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})]);
1264412632

1264512633
test_parse_table_constraint!(
1264612634
dialect,
@@ -12759,10 +12747,7 @@ mod tests {
1275912747

1276012748
#[test]
1276112749
fn test_parse_multipart_identifier_positive() {
12762-
let dialect = TestedDialects {
12763-
dialects: vec![Box::new(GenericDialect {})],
12764-
options: None,
12765-
};
12750+
let dialect = TestedDialects::new(vec![Box::new(GenericDialect {})]);
1276612751

1276712752
// parse multipart with quotes
1276812753
let expected = vec![

src/test_utils.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use pretty_assertions::assert_eq;
4444
pub struct TestedDialects {
4545
pub dialects: Vec<Box<dyn Dialect>>,
4646
pub options: Option<ParserOptions>,
47+
pub recursion_limit: Option<usize>,
4748
}
4849

4950
impl TestedDialects {
@@ -52,16 +53,38 @@ impl TestedDialects {
5253
Self {
5354
dialects,
5455
options: None,
56+
recursion_limit: None,
5557
}
5658
}
5759

60+
pub fn new_with_options(dialects: Vec<Box<dyn Dialect>>, options: ParserOptions) -> Self {
61+
Self {
62+
dialects,
63+
options: Some(options),
64+
recursion_limit: None,
65+
}
66+
}
67+
68+
pub fn with_recursion_limit(mut self, recursion_limit: usize) -> Self {
69+
self.recursion_limit = Some(recursion_limit);
70+
self
71+
}
72+
5873
fn new_parser<'a>(&self, dialect: &'a dyn Dialect) -> Parser<'a> {
5974
let parser = Parser::new(dialect);
60-
if let Some(options) = &self.options {
75+
let parser = if let Some(options) = &self.options {
6176
parser.with_options(options.clone())
6277
} else {
6378
parser
64-
}
79+
};
80+
81+
let parser = if let Some(recursion_limit) = &self.recursion_limit {
82+
parser.with_recursion_limit(*recursion_limit)
83+
} else {
84+
parser
85+
};
86+
87+
parser
6588
}
6689

6790
/// Run the given function for all of `self.dialects`, assert that they

tests/sqlparser_bigquery.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ fn parse_literal_string() {
4040
r#""""triple-double\"escaped""", "#,
4141
r#""""triple-double"unescaped""""#,
4242
);
43-
let dialect = TestedDialects {
44-
dialects: vec![Box::new(BigQueryDialect {})],
45-
options: Some(ParserOptions::new().with_unescape(false)),
46-
};
43+
let dialect = TestedDialects::new_with_options(
44+
vec![Box::new(BigQueryDialect {})],
45+
ParserOptions::new().with_unescape(false),
46+
);
4747
let select = dialect.verified_only_select(sql);
4848
assert_eq!(10, select.projection.len());
4949
assert_eq!(
@@ -1936,17 +1936,14 @@ fn parse_big_query_declare() {
19361936
}
19371937

19381938
fn bigquery() -> TestedDialects {
1939-
TestedDialects {
1940-
dialects: vec![Box::new(BigQueryDialect {})],
1941-
options: None,
1942-
}
1939+
TestedDialects::new(vec![Box::new(BigQueryDialect {})])
19431940
}
19441941

19451942
fn bigquery_and_generic() -> TestedDialects {
1946-
TestedDialects {
1947-
dialects: vec![Box::new(BigQueryDialect {}), Box::new(GenericDialect {})],
1948-
options: None,
1949-
}
1943+
TestedDialects::new(vec![
1944+
Box::new(BigQueryDialect {}),
1945+
Box::new(GenericDialect {}),
1946+
])
19501947
}
19511948

19521949
#[test]

tests/sqlparser_clickhouse.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,15 +1613,12 @@ fn parse_explain_table() {
16131613
}
16141614

16151615
fn clickhouse() -> TestedDialects {
1616-
TestedDialects {
1617-
dialects: vec![Box::new(ClickHouseDialect {})],
1618-
options: None,
1619-
}
1616+
TestedDialects::new(vec![Box::new(ClickHouseDialect {})])
16201617
}
16211618

16221619
fn clickhouse_and_generic() -> TestedDialects {
1623-
TestedDialects {
1624-
dialects: vec![Box::new(ClickHouseDialect {}), Box::new(GenericDialect {})],
1625-
options: None,
1626-
}
1620+
TestedDialects::new(vec![
1621+
Box::new(ClickHouseDialect {}),
1622+
Box::new(GenericDialect {}),
1623+
])
16271624
}

0 commit comments

Comments
 (0)