diff options
author | John Wiegley <johnw@newartisans.com> | 2009-10-28 18:40:31 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-10-28 18:40:31 -0400 |
commit | 47df7dd60e9209db3be91a7b29a91911ee4a846b (patch) | |
tree | f67431dbeb4f8a9f2fa575a554e1fd570615f735 /test/unit | |
parent | 9408f3cbae5027734fe9b22ba3855e209d192eb1 (diff) | |
download | fork-ledger-47df7dd60e9209db3be91a7b29a91911ee4a846b.tar.gz fork-ledger-47df7dd60e9209db3be91a7b29a91911ee4a846b.tar.bz2 fork-ledger-47df7dd60e9209db3be91a7b29a91911ee4a846b.zip |
Rewrote the report query parser
It is now a full parser that parses report queries directly into value
expression trees. These then get rendered into text so that other
options may extend the expression.
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/t_expr.cc | 275 | ||||
-rw-r--r-- | test/unit/t_expr.h | 32 |
2 files changed, 307 insertions, 0 deletions
diff --git a/test/unit/t_expr.cc b/test/unit/t_expr.cc index 6c88a08a..970706a2 100644 --- a/test/unit/t_expr.cc +++ b/test/unit/t_expr.cc @@ -3,6 +3,7 @@ #include "t_expr.h" #include "expr.h" +#include "predicate.h" using namespace ledger; @@ -19,3 +20,277 @@ void ValueExprTestCase::tearDown() amount_t::shutdown(); times_shutdown(); } + +// 1. foo and bar +// 2. 'foo and bar' +// 3. (foo and bar) +// 4. ( foo and bar ) +// 5. '( foo and' bar) +// 6. =foo and bar +// 7. ='foo and bar' +// 8. 'expr foo and bar' +// 9. expr 'foo and bar' +// 10. expr foo and bar +// 11. foo and bar or baz +// 12. foo and bar | baz +// 13. foo and bar |baz +// 14. foo and bar| baz +// 15. foo and bar|baz +// 16. foo 'and bar|baz' + +void ValueExprTestCase::testPredicateTokenizer1() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer2() +{ + value_t args; + args.push_back(string_value("foo and bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer3() +{ + value_t args; + args.push_back(string_value("(foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar)")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::LPAREN, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::RPAREN, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer4() +{ + value_t args; + args.push_back(string_value("(")); + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + args.push_back(string_value(")")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::LPAREN, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::RPAREN, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer5() +{ + value_t args; + args.push_back(string_value("( foo and")); + args.push_back(string_value("bar)")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::LPAREN, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::RPAREN, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer6() +{ + value_t args; + args.push_back(string_value("=foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TOK_EQ, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer7() +{ + value_t args; + args.push_back(string_value("=foo and bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TOK_EQ, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer8() +{ + value_t args; + args.push_back(string_value("expr foo and bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TOK_EXPR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer9() +{ + value_t args; + args.push_back(string_value("expr")); + args.push_back(string_value("foo and bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TOK_EXPR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer10() +{ + value_t args; + args.push_back(string_value("expr")); + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TOK_EXPR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer11() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + args.push_back(string_value("or")); + args.push_back(string_value("baz")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_OR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer12() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + args.push_back(string_value("|")); + args.push_back(string_value("baz")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_OR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer13() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar")); + args.push_back(string_value("|baz")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_OR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer14() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar|")); + args.push_back(string_value("baz")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_OR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer15() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and")); + args.push_back(string_value("bar|baz")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_OR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} + +void ValueExprTestCase::testPredicateTokenizer16() +{ + value_t args; + args.push_back(string_value("foo")); + args.push_back(string_value("and bar|baz")); + + query_lexer_t tokens(args.begin(), args.end()); + + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_AND, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TOK_OR, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::TERM, tokens.next_token().kind); + assertEqual(query_lexer_t::token_t::END_REACHED, tokens.next_token().kind); +} diff --git a/test/unit/t_expr.h b/test/unit/t_expr.h index f1289143..2f22db3d 100644 --- a/test/unit/t_expr.h +++ b/test/unit/t_expr.h @@ -8,6 +8,22 @@ class ValueExprTestCase : public CPPUNIT_NS::TestCase CPPUNIT_TEST_SUITE(ValueExprTestCase); //CPPUNIT_TEST(testConstructors); + CPPUNIT_TEST(testPredicateTokenizer1); + CPPUNIT_TEST(testPredicateTokenizer2); + CPPUNIT_TEST(testPredicateTokenizer3); + CPPUNIT_TEST(testPredicateTokenizer4); + CPPUNIT_TEST(testPredicateTokenizer5); + CPPUNIT_TEST(testPredicateTokenizer6); + CPPUNIT_TEST(testPredicateTokenizer7); + CPPUNIT_TEST(testPredicateTokenizer8); + CPPUNIT_TEST(testPredicateTokenizer9); + CPPUNIT_TEST(testPredicateTokenizer10); + CPPUNIT_TEST(testPredicateTokenizer11); + CPPUNIT_TEST(testPredicateTokenizer12); + CPPUNIT_TEST(testPredicateTokenizer13); + CPPUNIT_TEST(testPredicateTokenizer14); + CPPUNIT_TEST(testPredicateTokenizer15); + CPPUNIT_TEST(testPredicateTokenizer16); CPPUNIT_TEST_SUITE_END(); @@ -19,6 +35,22 @@ public: virtual void tearDown(); //void testConstructors(); + void testPredicateTokenizer1(); + void testPredicateTokenizer2(); + void testPredicateTokenizer3(); + void testPredicateTokenizer4(); + void testPredicateTokenizer5(); + void testPredicateTokenizer6(); + void testPredicateTokenizer7(); + void testPredicateTokenizer8(); + void testPredicateTokenizer9(); + void testPredicateTokenizer10(); + void testPredicateTokenizer11(); + void testPredicateTokenizer12(); + void testPredicateTokenizer13(); + void testPredicateTokenizer14(); + void testPredicateTokenizer15(); + void testPredicateTokenizer16(); private: ValueExprTestCase(const ValueExprTestCase ©); |