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.
This commit is contained in:
Martin von Zweigbergk 2021-04-21 12:33:07 -07:00
parent 6bc1361b84
commit 744f209e76
2 changed files with 128 additions and 125 deletions

View file

@ -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()))
})
);
}
}

View file

@ -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<CommitId> {
let expression = parse(revset_str).unwrap();
evaluate_expression(repo, &expression)