From 744f209e765a0b074977b5a7059120f20ad490cc Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 21 Apr 2021 12:33:07 -0700 Subject: [PATCH] revsets: move parse-tests to to revset module (from separate test module) The tests don't need any complex set up (no repo necessary), so they can be in the `revset` module itself. I'm sure we'll need to split up that module later (at least separate out the parsing), but that's a separate problem. --- lib/src/revset.rs | 127 +++++++++++++++++++++++++++++++++++++++ lib/tests/test_revset.rs | 126 +------------------------------------- 2 files changed, 128 insertions(+), 125 deletions(-) diff --git a/lib/src/revset.rs b/lib/src/revset.rs index bf75dcc6f..be5f0a137 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -677,3 +677,130 @@ fn non_obsolete_heads<'revset, 'repo: 'revset>( index_entries.sort_by_key(|b| Reverse(b.position())); Box::new(EagerRevset { index_entries }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_revset() { + assert_eq!(parse("@"), Ok(RevsetExpression::Symbol("@".to_string()))); + assert_eq!( + parse("foo"), + Ok(RevsetExpression::Symbol("foo".to_string())) + ); + assert_eq!( + parse("(foo)"), + Ok(RevsetExpression::Symbol("foo".to_string())) + ); + assert_eq!( + parse(":@"), + Ok(RevsetExpression::Parents(Box::new( + RevsetExpression::Symbol("@".to_string()) + ))) + ); + assert_eq!( + parse(":(@)"), + Ok(RevsetExpression::Parents(Box::new( + RevsetExpression::Symbol("@".to_string()) + ))) + ); + assert_eq!( + parse("*:@"), + Ok(RevsetExpression::Ancestors(Box::new( + RevsetExpression::Symbol("@".to_string()) + ))) + ); + assert_eq!( + parse(" *:@ "), + Ok(RevsetExpression::Ancestors(Box::new( + RevsetExpression::Symbol("@".to_string()) + ))) + ); + assert_eq!( + parse(" description( arg1 , arg2 ) - parents( arg1 ) - all_heads( ) "), + Ok(RevsetExpression::Difference( + Box::new(RevsetExpression::Difference( + Box::new(RevsetExpression::Description { + needle: "arg1".to_string(), + base_expression: Box::new(RevsetExpression::Symbol("arg2".to_string())) + }), + Box::new(RevsetExpression::Parents(Box::new( + RevsetExpression::Symbol("arg1".to_string()) + ))) + )), + Box::new(RevsetExpression::AllHeads) + )) + ); + } + + #[test] + fn test_parse_revset_function() { + assert_eq!( + parse("parents(@)"), + Ok(RevsetExpression::Parents(Box::new( + RevsetExpression::Symbol("@".to_string()) + ))) + ); + assert_eq!( + parse("parents((@))"), + Ok(RevsetExpression::Parents(Box::new( + RevsetExpression::Symbol("@".to_string()) + ))) + ); + assert_eq!( + parse("parents(\"@\")"), + Err(RevsetParseError::InvalidFunctionArguments { + name: "parents".to_string(), + message: "Expected function argument of type expression, found: \"@\"".to_string() + }) + ); + assert_eq!( + parse("ancestors(parents(@))"), + Ok(RevsetExpression::Ancestors(Box::new( + RevsetExpression::Parents(Box::new(RevsetExpression::Symbol("@".to_string()))) + ))) + ); + assert_eq!( + parse("parents(@"), + Err(RevsetParseError::SyntaxError( + "Failed to parse revset \"parents(@\" past position 7".to_string() + )) + ); + assert_eq!( + parse("parents(@,@)"), + Err(RevsetParseError::InvalidFunctionArguments { + name: "parents".to_string(), + message: "Expected 1 argument".to_string() + }) + ); + assert_eq!( + parse("description(foo,bar)"), + Ok(RevsetExpression::Description { + needle: "foo".to_string(), + base_expression: Box::new(RevsetExpression::Symbol("bar".to_string())) + }) + ); + assert_eq!( + parse("description(foo(),bar)"), + Err(RevsetParseError::InvalidFunctionArguments { + name: "description".to_string(), + message: "Expected function argument of type string, found: foo()".to_string() + }) + ); + assert_eq!( + parse("description((foo),bar)"), + Err(RevsetParseError::InvalidFunctionArguments { + name: "description".to_string(), + message: "Expected function argument of type string, found: (foo)".to_string() + }) + ); + assert_eq!( + parse("description(\"(foo)\",bar)"), + Ok(RevsetExpression::Description { + needle: "(foo)".to_string(), + base_expression: Box::new(RevsetExpression::Symbol("bar".to_string())) + }) + ); + } +} diff --git a/lib/tests/test_revset.rs b/lib/tests/test_revset.rs index a6b4ddee3..fdcaa025b 100644 --- a/lib/tests/test_revset.rs +++ b/lib/tests/test_revset.rs @@ -14,9 +14,7 @@ use jujube_lib::commit_builder::CommitBuilder; use jujube_lib::repo::RepoRef; -use jujube_lib::revset::{ - evaluate_expression, parse, resolve_symbol, RevsetError, RevsetExpression, RevsetParseError, -}; +use jujube_lib::revset::{evaluate_expression, parse, resolve_symbol, RevsetError}; use jujube_lib::store::{CommitId, MillisSinceEpoch, Signature, Timestamp}; use jujube_lib::testutils; use test_case::test_case; @@ -226,128 +224,6 @@ fn test_resolve_symbol_git_refs() { tx.discard(); } -#[test] -fn test_parse_revset() { - assert_eq!(parse("@"), Ok(RevsetExpression::Symbol("@".to_string()))); - assert_eq!( - parse("foo"), - Ok(RevsetExpression::Symbol("foo".to_string())) - ); - assert_eq!( - parse("(foo)"), - Ok(RevsetExpression::Symbol("foo".to_string())) - ); - assert_eq!( - parse(":@"), - Ok(RevsetExpression::Parents(Box::new( - RevsetExpression::Symbol("@".to_string()) - ))) - ); - assert_eq!( - parse(":(@)"), - Ok(RevsetExpression::Parents(Box::new( - RevsetExpression::Symbol("@".to_string()) - ))) - ); - assert_eq!( - parse("*:@"), - Ok(RevsetExpression::Ancestors(Box::new( - RevsetExpression::Symbol("@".to_string()) - ))) - ); - assert_eq!( - parse(" *:@ "), - Ok(RevsetExpression::Ancestors(Box::new( - RevsetExpression::Symbol("@".to_string()) - ))) - ); - assert_eq!( - parse(" description( arg1 , arg2 ) - parents( arg1 ) - all_heads( ) "), - Ok(RevsetExpression::Difference( - Box::new(RevsetExpression::Difference( - Box::new(RevsetExpression::Description { - needle: "arg1".to_string(), - base_expression: Box::new(RevsetExpression::Symbol("arg2".to_string())) - }), - Box::new(RevsetExpression::Parents(Box::new( - RevsetExpression::Symbol("arg1".to_string()) - ))) - )), - Box::new(RevsetExpression::AllHeads) - )) - ); -} - -#[test] -fn test_parse_revset_function() { - assert_eq!( - parse("parents(@)"), - Ok(RevsetExpression::Parents(Box::new( - RevsetExpression::Symbol("@".to_string()) - ))) - ); - assert_eq!( - parse("parents((@))"), - Ok(RevsetExpression::Parents(Box::new( - RevsetExpression::Symbol("@".to_string()) - ))) - ); - assert_eq!( - parse("parents(\"@\")"), - Err(RevsetParseError::InvalidFunctionArguments { - name: "parents".to_string(), - message: "Expected function argument of type expression, found: \"@\"".to_string() - }) - ); - assert_eq!( - parse("ancestors(parents(@))"), - Ok(RevsetExpression::Ancestors(Box::new( - RevsetExpression::Parents(Box::new(RevsetExpression::Symbol("@".to_string()))) - ))) - ); - assert_eq!( - parse("parents(@"), - Err(RevsetParseError::SyntaxError( - "Failed to parse revset \"parents(@\" past position 7".to_string() - )) - ); - assert_eq!( - parse("parents(@,@)"), - Err(RevsetParseError::InvalidFunctionArguments { - name: "parents".to_string(), - message: "Expected 1 argument".to_string() - }) - ); - assert_eq!( - parse("description(foo,bar)"), - Ok(RevsetExpression::Description { - needle: "foo".to_string(), - base_expression: Box::new(RevsetExpression::Symbol("bar".to_string())) - }) - ); - assert_eq!( - parse("description(foo(),bar)"), - Err(RevsetParseError::InvalidFunctionArguments { - name: "description".to_string(), - message: "Expected function argument of type string, found: foo()".to_string() - }) - ); - assert_eq!( - parse("description((foo),bar)"), - Err(RevsetParseError::InvalidFunctionArguments { - name: "description".to_string(), - message: "Expected function argument of type string, found: (foo)".to_string() - }) - ); - assert_eq!( - parse("description(\"(foo)\",bar)"), - Ok(RevsetExpression::Description { - needle: "(foo)".to_string(), - base_expression: Box::new(RevsetExpression::Symbol("bar".to_string())) - }) - ); -} - fn resolve_commit_ids(repo: RepoRef, revset_str: &str) -> Vec { let expression = parse(revset_str).unwrap(); evaluate_expression(repo, &expression)