Tests: Added Lexer::Type::date tests

This commit is contained in:
Paul Beckingham 2015-03-28 12:05:17 -04:00
parent b41d7c4582
commit 39d330631d

View file

@ -36,7 +36,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (556); UnitTest t (628);
std::vector <std::pair <std::string, Lexer::Type> > tokens; std::vector <std::pair <std::string, Lexer::Type> > tokens;
std::string token; std::string token;
@ -401,87 +401,89 @@ int main (int argc, char** argv)
{ {
const char* token; const char* token;
Lexer::Type type; Lexer::Type type;
} results[3]; } results[5];
} lexerTests[] = } lexerTests[] =
{ {
{ "/foo/", { { "/foo/", Lexer::Type::pattern }, NO, NO }, }, { "/foo/", { { "/foo/", Lexer::Type::pattern }, NO, NO, NO, NO }, },
{ "/a\\/b/", { { "/a\\/b/", Lexer::Type::pattern }, NO, NO }, }, { "/a\\/b/", { { "/a\\/b/", Lexer::Type::pattern }, NO, NO, NO, NO }, },
{ "/'/", { { "/'/", Lexer::Type::pattern }, NO, NO }, }, { "/'/", { { "/'/", Lexer::Type::pattern }, NO, NO, NO, NO }, },
{ "desc~pattern", { { "desc", Lexer::Type::dom }, { "desc~pattern", { { "desc", Lexer::Type::dom },
{ "~", Lexer::Type::op }, { "~", Lexer::Type::op },
{ "pattern", Lexer::Type::dom }, }, }, { "pattern", Lexer::Type::dom }, NO, NO }, },
{ "desc.cont:pattern", { { "desc.cont:pattern", Lexer::Type::pair }, NO, NO }, }, { "desc.cont:pattern", { { "desc.cont:pattern", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "/from/to/g", { { "/from/to/g", Lexer::Type::substitution }, NO, NO }, }, { "/from/to/g", { { "/from/to/g", Lexer::Type::substitution }, NO, NO, NO, NO }, },
{ "/from/to/", { { "/from/to/", Lexer::Type::substitution }, NO, NO }, }, { "/from/to/", { { "/from/to/", Lexer::Type::substitution }, NO, NO, NO, NO }, },
{ "+tag", { { "+tag", Lexer::Type::tag }, NO, NO }, }, { "+tag", { { "+tag", Lexer::Type::tag }, NO, NO, NO, NO }, },
{ "-tag", { { "-tag", Lexer::Type::tag }, NO, NO }, }, { "-tag", { { "-tag", Lexer::Type::tag }, NO, NO, NO, NO }, },
{ "foo", { { "foo", Lexer::Type::dom }, NO, NO }, }, { "foo", { { "foo", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "/long/path/to/file.txt", { { "/long/path/to/file.txt", Lexer::Type::path }, NO, NO }, }, { "/long/path/to/file.txt", { { "/long/path/to/file.txt", Lexer::Type::path }, NO, NO, NO, NO }, },
{ "Çirçös", { { "Çirçös", Lexer::Type::dom }, NO, NO }, }, { "Çirçös", { { "Çirçös", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "", { { "", Lexer::Type::dom }, NO, NO }, }, { "", { { "", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "name", { { "name", Lexer::Type::dom }, NO, NO }, }, { "name", { { "name", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "f1", { { "f1", Lexer::Type::dom }, NO, NO }, }, { "f1", { { "f1", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "foo.bar", { { "foo.bar", Lexer::Type::dom }, NO, NO }, }, { "foo.bar", { { "foo.bar", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "1.foo.bar", { { "1.foo.bar", Lexer::Type::dom }, NO, NO }, }, { "1.foo.bar", { { "1.foo.bar", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "a360fc44-315c-4366-b70c-ea7e7520b749.foo.bar", { { "a360fc44-315c-4366-b70c-ea7e7520b749.foo.bar", Lexer::Type::dom }, NO, NO }, }, { "a360fc44-315c-4366-b70c-ea7e7520b749.foo.bar", { { "a360fc44-315c-4366-b70c-ea7e7520b749.foo.bar", Lexer::Type::dom }, NO, NO, NO, NO }, },
{ "http://tasktools.org", { { "http://tasktools.org", Lexer::Type::url }, NO, NO }, }, { "http://tasktools.org", { { "http://tasktools.org", Lexer::Type::url }, NO, NO, NO, NO }, },
{ "https://bug.tasktools.org", { { "https://bug.tasktools.org", Lexer::Type::url }, NO, NO }, }, { "https://bug.tasktools.org", { { "https://bug.tasktools.org", Lexer::Type::url }, NO, NO, NO, NO }, },
{ "'one two'", { { "'one two'", Lexer::Type::string }, NO, NO }, }, { "'one two'", { { "'one two'", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "\\\"three\\\"", { { "\\\"three\\\"", Lexer::Type::string }, NO, NO }, }, { "\\\"three\\\"", { { "\\\"three\\\"", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "'\\''", { { "'\\''", Lexer::Type::string }, NO, NO }, }, { "'\\''", { { "'\\''", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "\"\\\"\"", { { "\"\\\"\"", Lexer::Type::string }, NO, NO }, }, { "\"\\\"\"", { { "\"\\\"\"", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "\"\tfoo\t\"", { { "\"\tfoo\t\"", Lexer::Type::string }, NO, NO }, }, { "\"\tfoo\t\"", { { "\"\tfoo\t\"", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "\"\\u20A43\"", { { "\"₤3\"", Lexer::Type::string }, NO, NO }, }, { "\"\\u20A43\"", { { "\"₤3\"", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "\"U+20AC4\"", { { "\"€4\"", Lexer::Type::string }, NO, NO }, }, { "\"U+20AC4\"", { { "\"€4\"", Lexer::Type::string }, NO, NO, NO, NO }, },
{ "1", { { "1", Lexer::Type::number }, NO, NO }, }, { "1", { { "1", Lexer::Type::number }, NO, NO, NO, NO }, },
{ "3.14", { { "3.14", Lexer::Type::number }, NO, NO }, }, { "3.14", { { "3.14", Lexer::Type::number }, NO, NO, NO, NO }, },
{ "6.02217e23", { { "6.02217e23", Lexer::Type::number }, NO, NO }, }, { "6.02217e23", { { "6.02217e23", Lexer::Type::number }, NO, NO, NO, NO }, },
{ "1.2e-3.4", { { "1.2e-3.4", Lexer::Type::number }, NO, NO }, }, { "1.2e-3.4", { { "1.2e-3.4", Lexer::Type::number }, NO, NO, NO, NO }, },
{ "0x2f", { { "0x2f", Lexer::Type::hex }, NO, NO }, }, { "0x2f", { { "0x2f", Lexer::Type::hex }, NO, NO, NO, NO }, },
{ "name:value", { { "name:value", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "pro:'P 1'", { { "pro:'P 1'", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "pro:PROJECT", { { "pro:PROJECT", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "rc:x", { { "rc:x", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "rc.name:value", { { "rc.name:value", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "rc.name=value", { { "rc.name=value", Lexer::Type::pair }, NO, NO, NO, NO }, },
{ "*", { { "*", Lexer::Type::op }, NO, NO, NO, NO }, },
{ ">=", { { ">=", Lexer::Type::op }, NO, NO, NO, NO }, },
{ "xor", { { "xor", Lexer::Type::op }, NO, NO, NO, NO }, },
{ "_hastag_", { { "_hastag_", Lexer::Type::op }, NO, NO, NO, NO }, },
{ "a360fc44-315c-4366-b70c-ea7e7520b749", { { "a360fc44-315c-4366-b70c-ea7e7520b749", Lexer::Type::uuid }, NO, NO, NO, NO }, },
{ "a360fc44-315c-4366-b70c-ea7e752", { { "a360fc44-315c-4366-b70c-ea7e752", Lexer::Type::uuid }, NO, NO, NO, NO }, },
{ "a360fc44-315c-4366-b70c", { { "a360fc44-315c-4366-b70c", Lexer::Type::uuid }, NO, NO, NO, NO }, },
{ "a360fc44-315c-4366", { { "a360fc44-315c-4366", Lexer::Type::uuid }, NO, NO, NO, NO }, },
{ "a360fc44-315c", { { "a360fc44-315c", Lexer::Type::uuid }, NO, NO, NO, NO }, },
{ "a360fc44", { { "a360fc44", Lexer::Type::uuid }, NO, NO, NO, NO }, },
{ "a360fc44,b7f8c869", { { "a360fc44", Lexer::Type::uuid },
{ ",", Lexer::Type::list },
{ "b7f8c869", Lexer::Type::uuid }, NO, NO }, },
{ "today", { { "today", Lexer::Type::date }, NO, NO, NO, NO }, },
{ "23rd", { { "23rd", Lexer::Type::date }, NO, NO, NO, NO }, },
{ "2015-W01", { { "2015-W01", Lexer::Type::date }, NO, NO, NO, NO }, },
{ "2015-02-17", { { "2015-02-17", Lexer::Type::date }, NO, NO, NO, NO }, },
{ "year", { { "year", Lexer::Type::duration }, NO, NO, NO, NO }, },
{ "4weeks", { { "4weeks", Lexer::Type::duration }, NO, NO, NO, NO }, },
{ "PT23H", { { "PT23H", Lexer::Type::duration }, NO, NO, NO, NO }, },
{ "--", { { "--", Lexer::Type::separator }, NO, NO, NO, NO }, },
// IDs // IDs
// 2,3 // 2,3
// 4,5-6 // 4,5-6
{ "name:value", { { "name:value", Lexer::Type::pair }, NO, NO }, }, { "name=value", { { "name", Lexer::Type::dom },
{ "pro:'P 1'", { { "pro:'P 1'", Lexer::Type::pair }, NO, NO }, }, { "=", Lexer::Type::op },
{ "pro:PROJECT", { { "pro:PROJECT", Lexer::Type::pair }, NO, NO }, }, { "value", Lexer::Type::dom }, NO, NO }, },
{ "rc:x", { { "rc:x", Lexer::Type::pair }, NO, NO }, },
{ "rc.name:value", { { "rc.name:value", Lexer::Type::pair }, NO, NO }, },
{ "rc.name=value", { { "rc.name=value", Lexer::Type::pair }, NO, NO }, },
{ "*", { { "*", Lexer::Type::op }, NO, NO }, },
{ ">=", { { ">=", Lexer::Type::op }, NO, NO }, },
{ "xor", { { "xor", Lexer::Type::op }, NO, NO }, },
{ "_hastag_", { { "_hastag_", Lexer::Type::op }, NO, NO }, },
// Expressions // Expressions
// name=value
// due:'eow - 2d' // due:'eow - 2d'
// due:eom-2w // due:eom-2w
// due < eom + 1w + 1d // due < eom + 1w + 1d
// ( /pattern/ or 8ad2e3db-914d-4832-b0e6-72fa04f6e331,3b6218f9-726a-44fc-aa63-889ff52be442 ) // ( /pattern/ or 8ad2e3db-914d-4832-b0e6-72fa04f6e331,3b6218f9-726a-44fc-aa63-889ff52be442 )
// (1+2) { "(1+2)", { { "(", Lexer::Type::op },
{ "1", Lexer::Type::number },
{ "+", Lexer::Type::op },
{ "2", Lexer::Type::number },
{ ")", Lexer::Type::op }, }, },
{ "a360fc44-315c-4366-b70c-ea7e7520b749", { { "a360fc44-315c-4366-b70c-ea7e7520b749", Lexer::Type::uuid }, NO, NO }, },
{ "a360fc44-315c-4366-b70c-ea7e752", { { "a360fc44-315c-4366-b70c-ea7e752", Lexer::Type::uuid }, NO, NO }, },
{ "a360fc44-315c-4366-b70c", { { "a360fc44-315c-4366-b70c", Lexer::Type::uuid }, NO, NO }, },
{ "a360fc44-315c-4366", { { "a360fc44-315c-4366", Lexer::Type::uuid }, NO, NO }, },
{ "a360fc44-315c", { { "a360fc44-315c", Lexer::Type::uuid }, NO, NO }, },
{ "a360fc44", { { "a360fc44", Lexer::Type::uuid }, NO, NO }, },
{ "a360fc44,b7f8c869", { { "a360fc44", Lexer::Type::uuid },
{ ",", Lexer::Type::list },
{ "b7f8c869", Lexer::Type::uuid }, }, },
// Dates
// today
// 23rd
// 2015-W01
// 2015-02-17
{ "year", { { "year", Lexer::Type::duration }, NO, NO }, },
{ "4weeks", { { "4weeks", Lexer::Type::duration }, NO, NO }, },
{ "PT23H", { { "PT23H", Lexer::Type::duration }, NO, NO }, },
{ "--", { { "--", Lexer::Type::separator }, NO, NO }, },
}; };
#define NUM_TESTS (sizeof (lexerTests) / sizeof (lexerTests[0])) #define NUM_TESTS (sizeof (lexerTests) / sizeof (lexerTests[0]))
@ -490,10 +492,7 @@ int main (int argc, char** argv)
// The isolated test puts the input string directly into the Lexer. // The isolated test puts the input string directly into the Lexer.
Lexer isolated (lexerTests[i].input); Lexer isolated (lexerTests[i].input);
// The embedded test surrounds the input string with a space. for (int j = 0; j < 5; j++)
Lexer embedded (std::string (" ") + lexerTests[i].input + " ");
for (int j = 0; j < 3; j++)
{ {
if (lexerTests[i].results[j].token[0]) if (lexerTests[i].results[j].token[0])
{ {
@ -501,7 +500,16 @@ int main (int argc, char** argv)
t.ok (isolated.token (token, type), "Isolated Lexer::token(...) --> true"); t.ok (isolated.token (token, type), "Isolated Lexer::token(...) --> true");
t.is (token, lexerTests[i].results[j].token, " token --> " + token); t.is (token, lexerTests[i].results[j].token, " token --> " + token);
t.is ((int)type, (int)lexerTests[i].results[j].type, " type --> Lexer::Type::" + Lexer::typeToString (type)); t.is ((int)type, (int)lexerTests[i].results[j].type, " type --> Lexer::Type::" + Lexer::typeToString (type));
}
}
// The embedded test surrounds the input string with a space.
Lexer embedded (std::string (" ") + lexerTests[i].input + " ");
for (int j = 0; j < 5; j++)
{
if (lexerTests[i].results[j].token[0])
{
// Embedded: "<token>" // Embedded: "<token>"
t.ok (embedded.token (token, type), "Embedded Lexer::token(...) --> true"); t.ok (embedded.token (token, type), "Embedded Lexer::token(...) --> true");
t.is (token, lexerTests[i].results[j].token, " token --> " + token); t.is (token, lexerTests[i].results[j].token, " token --> " + token);