Move query-parser test functions into a submodule.
This commit is contained in:
parent
932a42866c
commit
f1a55c9f12
1 changed files with 93 additions and 81 deletions
|
@ -160,65 +160,6 @@ impl<I> FindSp<I>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! assert_parses_to {
|
|
||||||
( $parser: path, $input: expr, $expected: expr ) => {{
|
|
||||||
let mut par = $parser();
|
|
||||||
let result = par.parse(&$input[..]);
|
|
||||||
assert_eq!(result, Ok(($expected, &[][..])));
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_find_sp_variable() {
|
|
||||||
let sym = edn::PlainSymbol::new("?x");
|
|
||||||
let input = [edn::Value::PlainSymbol(sym.clone())];
|
|
||||||
assert_parses_to!(FindSp::variable, input, Variable(sym));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_find_scalar() {
|
|
||||||
let sym = edn::PlainSymbol::new("?x");
|
|
||||||
let period = edn::PlainSymbol::new(".");
|
|
||||||
let input = [edn::Value::PlainSymbol(sym.clone()), edn::Value::PlainSymbol(period.clone())];
|
|
||||||
assert_parses_to!(FindSp::find_scalar,
|
|
||||||
input,
|
|
||||||
FindSpec::FindScalar(Element::Variable(Variable(sym))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_find_coll() {
|
|
||||||
let sym = edn::PlainSymbol::new("?x");
|
|
||||||
let period = edn::PlainSymbol::new("...");
|
|
||||||
let input = [edn::Value::Vector(vec![edn::Value::PlainSymbol(sym.clone()),
|
|
||||||
edn::Value::PlainSymbol(period.clone())])];
|
|
||||||
assert_parses_to!(FindSp::find_coll,
|
|
||||||
input,
|
|
||||||
FindSpec::FindColl(Element::Variable(Variable(sym))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_find_rel() {
|
|
||||||
let vx = edn::PlainSymbol::new("?x");
|
|
||||||
let vy = edn::PlainSymbol::new("?y");
|
|
||||||
let input = [edn::Value::PlainSymbol(vx.clone()), edn::Value::PlainSymbol(vy.clone())];
|
|
||||||
assert_parses_to!(FindSp::find_rel,
|
|
||||||
input,
|
|
||||||
FindSpec::FindRel(vec![Element::Variable(Variable(vx)),
|
|
||||||
Element::Variable(Variable(vy))]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_find_tuple() {
|
|
||||||
let vx = edn::PlainSymbol::new("?x");
|
|
||||||
let vy = edn::PlainSymbol::new("?y");
|
|
||||||
let input = [edn::Value::Vector(vec![edn::Value::PlainSymbol(vx.clone()),
|
|
||||||
edn::Value::PlainSymbol(vy.clone())])];
|
|
||||||
assert_parses_to!(FindSp::find_tuple,
|
|
||||||
input,
|
|
||||||
FindSpec::FindTuple(vec![Element::Variable(Variable(vx)),
|
|
||||||
Element::Variable(Variable(vy))]));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a sequence of values into one of four find specs.
|
// Parse a sequence of values into one of four find specs.
|
||||||
//
|
//
|
||||||
// `:find` must be an array of plain var symbols (?foo), pull expressions, and aggregates.
|
// `:find` must be an array of plain var symbols (?foo), pull expressions, and aggregates.
|
||||||
|
@ -238,28 +179,99 @@ pub fn find_seq_to_find_spec(find: &[edn::Value]) -> FindParseResult {
|
||||||
.map_err(|_| FindParseError::Err)
|
.map_err(|_| FindParseError::Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_find_processing() {
|
mod test {
|
||||||
let vx = edn::PlainSymbol::new("?x");
|
extern crate combine;
|
||||||
let vy = edn::PlainSymbol::new("?y");
|
extern crate edn;
|
||||||
let ellipsis = edn::PlainSymbol::new("...");
|
extern crate mentat_query;
|
||||||
let period = edn::PlainSymbol::new(".");
|
|
||||||
|
|
||||||
let scalar = [edn::Value::PlainSymbol(vx.clone()), edn::Value::PlainSymbol(period.clone())];
|
use self::combine::Parser;
|
||||||
let tuple = [edn::Value::Vector(vec![edn::Value::PlainSymbol(vx.clone()),
|
use self::mentat_query::{Element, FindSpec, Variable};
|
||||||
edn::Value::PlainSymbol(vy.clone())])];
|
|
||||||
let coll = [edn::Value::Vector(vec![edn::Value::PlainSymbol(vx.clone()),
|
|
||||||
edn::Value::PlainSymbol(ellipsis.clone())])];
|
|
||||||
let rel = [edn::Value::PlainSymbol(vx.clone()), edn::Value::PlainSymbol(vy.clone())];
|
|
||||||
|
|
||||||
assert_eq!(Ok(FindSpec::FindScalar(Element::Variable(Variable(vx.clone())))),
|
use super::*;
|
||||||
find_seq_to_find_spec(&scalar));
|
|
||||||
assert_eq!(Ok(FindSpec::FindTuple(vec![Element::Variable(Variable(vx.clone())),
|
macro_rules! assert_parses_to {
|
||||||
Element::Variable(Variable(vy.clone()))])),
|
( $parser: path, $input: expr, $expected: expr ) => {{
|
||||||
find_seq_to_find_spec(&tuple));
|
let mut par = $parser();
|
||||||
assert_eq!(Ok(FindSpec::FindColl(Element::Variable(Variable(vx.clone())))),
|
let result = par.parse(&$input[..]);
|
||||||
find_seq_to_find_spec(&coll));
|
assert_eq!(result, Ok(($expected, &[][..])));
|
||||||
assert_eq!(Ok(FindSpec::FindRel(vec![Element::Variable(Variable(vx.clone())),
|
}}
|
||||||
Element::Variable(Variable(vy.clone()))])),
|
}
|
||||||
find_seq_to_find_spec(&rel));
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_sp_variable() {
|
||||||
|
let sym = edn::PlainSymbol::new("?x");
|
||||||
|
let input = [edn::Value::PlainSymbol(sym.clone())];
|
||||||
|
assert_parses_to!(FindSp::variable, input, Variable(sym));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_scalar() {
|
||||||
|
let sym = edn::PlainSymbol::new("?x");
|
||||||
|
let period = edn::PlainSymbol::new(".");
|
||||||
|
let input = [edn::Value::PlainSymbol(sym.clone()), edn::Value::PlainSymbol(period.clone())];
|
||||||
|
assert_parses_to!(FindSp::find_scalar,
|
||||||
|
input,
|
||||||
|
FindSpec::FindScalar(Element::Variable(Variable(sym))));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_coll() {
|
||||||
|
let sym = edn::PlainSymbol::new("?x");
|
||||||
|
let period = edn::PlainSymbol::new("...");
|
||||||
|
let input = [edn::Value::Vector(vec![edn::Value::PlainSymbol(sym.clone()),
|
||||||
|
edn::Value::PlainSymbol(period.clone())])];
|
||||||
|
assert_parses_to!(FindSp::find_coll,
|
||||||
|
input,
|
||||||
|
FindSpec::FindColl(Element::Variable(Variable(sym))));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_rel() {
|
||||||
|
let vx = edn::PlainSymbol::new("?x");
|
||||||
|
let vy = edn::PlainSymbol::new("?y");
|
||||||
|
let input = [edn::Value::PlainSymbol(vx.clone()), edn::Value::PlainSymbol(vy.clone())];
|
||||||
|
assert_parses_to!(FindSp::find_rel,
|
||||||
|
input,
|
||||||
|
FindSpec::FindRel(vec![Element::Variable(Variable(vx)),
|
||||||
|
Element::Variable(Variable(vy))]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_tuple() {
|
||||||
|
let vx = edn::PlainSymbol::new("?x");
|
||||||
|
let vy = edn::PlainSymbol::new("?y");
|
||||||
|
let input = [edn::Value::Vector(vec![edn::Value::PlainSymbol(vx.clone()),
|
||||||
|
edn::Value::PlainSymbol(vy.clone())])];
|
||||||
|
assert_parses_to!(FindSp::find_tuple,
|
||||||
|
input,
|
||||||
|
FindSpec::FindTuple(vec![Element::Variable(Variable(vx)),
|
||||||
|
Element::Variable(Variable(vy))]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_processing() {
|
||||||
|
let vx = edn::PlainSymbol::new("?x");
|
||||||
|
let vy = edn::PlainSymbol::new("?y");
|
||||||
|
let ellipsis = edn::PlainSymbol::new("...");
|
||||||
|
let period = edn::PlainSymbol::new(".");
|
||||||
|
|
||||||
|
let scalar = [edn::Value::PlainSymbol(vx.clone()), edn::Value::PlainSymbol(period.clone())];
|
||||||
|
let tuple = [edn::Value::Vector(vec![edn::Value::PlainSymbol(vx.clone()),
|
||||||
|
edn::Value::PlainSymbol(vy.clone())])];
|
||||||
|
let coll = [edn::Value::Vector(vec![edn::Value::PlainSymbol(vx.clone()),
|
||||||
|
edn::Value::PlainSymbol(ellipsis.clone())])];
|
||||||
|
let rel = [edn::Value::PlainSymbol(vx.clone()), edn::Value::PlainSymbol(vy.clone())];
|
||||||
|
|
||||||
|
assert_eq!(Ok(FindSpec::FindScalar(Element::Variable(Variable(vx.clone())))),
|
||||||
|
find_seq_to_find_spec(&scalar));
|
||||||
|
assert_eq!(Ok(FindSpec::FindTuple(vec![Element::Variable(Variable(vx.clone())),
|
||||||
|
Element::Variable(Variable(vy.clone()))])),
|
||||||
|
find_seq_to_find_spec(&tuple));
|
||||||
|
assert_eq!(Ok(FindSpec::FindColl(Element::Variable(Variable(vx.clone())))),
|
||||||
|
find_seq_to_find_spec(&coll));
|
||||||
|
assert_eq!(Ok(FindSpec::FindRel(vec![Element::Variable(Variable(vx.clone())),
|
||||||
|
Element::Variable(Variable(vy.clone()))])),
|
||||||
|
find_seq_to_find_spec(&rel));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue