mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-19 00:43:07 +02:00
Unit Tests
- Added the first few JSON unit tests.
This commit is contained in:
parent
690fa6e206
commit
bb24701f36
1 changed files with 101 additions and 112 deletions
213
test/json.t.cpp
213
test/json.t.cpp
|
@ -32,77 +32,113 @@
|
|||
|
||||
Context context;
|
||||
|
||||
const char *positive_tests[] =
|
||||
{
|
||||
"{}",
|
||||
|
||||
" { } ",
|
||||
|
||||
"[]",
|
||||
|
||||
"{\"one\":1}",
|
||||
|
||||
"{\n\"one\"\n:\n1\n}\n",
|
||||
|
||||
"{\"name\":123, \"array\":[1,2,3.4], \"object\":{\"m1\":\"v1\", \"m2\":\"v2\"}}",
|
||||
|
||||
"{\"name\":\"value\",\"array\":[\"one\",\"two\"],\"object\":{\"name2\":123,\"literal\":false}}",
|
||||
|
||||
"{\n"
|
||||
"\"ticket\": { \"type\":\"add\", \"client\":\"taskwarrior 2.x\"},\n"
|
||||
"\"auth\": { \"user\":\"paul\", \"org\":\"gbf\", \"key\":\".........\",\n"
|
||||
" \"locale\":\"en-US\" },\n"
|
||||
"\n"
|
||||
"\"add\": { \"description\":\"Wash the dog\",\n"
|
||||
" \"project\":\"home\",\n"
|
||||
" \"due\":\"20101101T000000Z\" }\n"
|
||||
"}",
|
||||
|
||||
"{"
|
||||
"\"ticket\":{"
|
||||
"\"type\":\"synch\","
|
||||
"\"client\":\"taskd-test-suite 1.0\""
|
||||
"},"
|
||||
"\"synch\":{"
|
||||
"\"user\":{"
|
||||
"\"data\":["
|
||||
"{"
|
||||
"\"uuid\":\"11111111-1111-1111-1111-111111111111\","
|
||||
"\"status\":\"pending\","
|
||||
"\"description\":\"This is a test\","
|
||||
"\"entry\":\"20110111T124000Z\""
|
||||
"}"
|
||||
"],"
|
||||
"\"synch\":\"key\""
|
||||
"}"
|
||||
"},"
|
||||
"\"auth\":{"
|
||||
"\"org\":\"gbf\","
|
||||
"\"user\":\"Paul Beckingham\","
|
||||
"\"key\":\"K\","
|
||||
"\"locale\":\"en-US\""
|
||||
"}"
|
||||
"}"
|
||||
};
|
||||
|
||||
#define NUM_POSITIVE_TESTS (sizeof (positive_tests) / sizeof (positive_tests[0]))
|
||||
|
||||
const char *negative_tests[] =
|
||||
{
|
||||
"",
|
||||
"{",
|
||||
"}",
|
||||
"[",
|
||||
"]",
|
||||
"foo",
|
||||
"[?]"
|
||||
};
|
||||
|
||||
#define NUM_NEGATIVE_TESTS (sizeof (negative_tests) / sizeof (negative_tests[0]))
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
UnitTest t (19);
|
||||
UnitTest t (NUM_POSITIVE_TESTS + NUM_NEGATIVE_TESTS + 14);
|
||||
|
||||
// Positive tests.
|
||||
for (int i = 0; i < NUM_POSITIVE_TESTS; ++i)
|
||||
{
|
||||
try
|
||||
{
|
||||
json::value* root = json::parse (positive_tests[i]);
|
||||
t.ok (root, std::string ("positive: ") + positive_tests[i]);
|
||||
if (root)
|
||||
{
|
||||
t.diag (root->dump ());
|
||||
delete root;
|
||||
}
|
||||
}
|
||||
|
||||
catch (const std::string& e) { t.diag (e); }
|
||||
catch (...) { t.diag ("Unknown error"); }
|
||||
}
|
||||
|
||||
// Negative tests.
|
||||
for (int i = 0; i < NUM_NEGATIVE_TESTS; ++i)
|
||||
{
|
||||
try
|
||||
{
|
||||
json::value* root = json::parse (negative_tests[i]);
|
||||
t.is (root, NULL, std::string ("negative: ") + negative_tests[i]);
|
||||
}
|
||||
|
||||
catch (const std::string& e) { t.pass (e); }
|
||||
catch (...) { t.fail ("Unknown error"); }
|
||||
}
|
||||
|
||||
// Other tests.
|
||||
try
|
||||
{
|
||||
// j1
|
||||
std::string input = "{}";
|
||||
std::cout << "# -- j1 -------------------\n"
|
||||
<< "# input: " << input << "\n";
|
||||
json::value* root = json::parse (input);
|
||||
t.ok (root, "j1 parse ok");
|
||||
if (root)
|
||||
{
|
||||
t.diag ("output: " + root->dump ());
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
t.fail ("j1 parse error");
|
||||
|
||||
// j2
|
||||
input = "{\"name\":123}";
|
||||
std::cout << "# -- j2 -------------------\n"
|
||||
<< "# input: " << input << "\n";
|
||||
root = json::parse (input);
|
||||
t.ok (root, "j2 parse ok");
|
||||
if (root)
|
||||
{
|
||||
t.diag ("output: " + root->dump ());
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
t.fail ("j2 parse error");
|
||||
|
||||
// j3
|
||||
input = "{\"name\":123, \"array\":[1,2,3.4], \"map\":{\"m1\":\"v1\", \"m2\":\"v2\"}}";
|
||||
std::cout << "# -- j3 -------------------\n"
|
||||
<< "# input: " << input << "\n";
|
||||
root = json::parse (input);
|
||||
t.ok (root, "j3 parse ok");
|
||||
if (root)
|
||||
{
|
||||
t.diag ("output: " + root->dump ());
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
t.fail ("j3 parse error");
|
||||
|
||||
// j4
|
||||
input = "{\n"
|
||||
"\"ticket\": { \"type\":\"add\", \"client\":\"taskwarrior 2.x\"},\n"
|
||||
"\"auth\": { \"user\":\"paul\", \"org\":\"gbf\", \"key\":\".........\",\n"
|
||||
" \"locale\":\"en-US\" },\n"
|
||||
"\n"
|
||||
"\"add\": { \"description\":\"Wash the dog\",\n"
|
||||
" \"project\":\"home\",\n"
|
||||
" \"due\":\"20101101T000000Z\" }\n"
|
||||
"}";
|
||||
std::cout << "# -- j4 -------------------\n"
|
||||
<< "# input: " << input << "\n";
|
||||
root = json::parse (input);
|
||||
t.ok (root, "j4 parse ok");
|
||||
if (root)
|
||||
{
|
||||
t.diag ("output: " + root->dump ());
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
t.fail ("j4 parse error");
|
||||
|
||||
// Regular unit tests.
|
||||
t.is (json::encode ("1\b2"), "1\\b2", "json::encode \\b -> \\\\b");
|
||||
t.is (json::decode ("1\\b2"), "1\b2", "json::decode \\\\b -> \\b");
|
||||
|
@ -124,53 +160,6 @@ int main (int argc, char** argv)
|
|||
|
||||
t.is (json::encode ("1€2"), "1€2", "json::encode € -> €");
|
||||
t.is (json::decode ("1\\u20ac2"), "1€2", "json::decode \\u20ac -> €");
|
||||
|
||||
/*
|
||||
{
|
||||
"ticket":
|
||||
{
|
||||
"type":"synch",
|
||||
"client":"taskd-test-suite 1.0"
|
||||
},
|
||||
|
||||
"synch":
|
||||
{
|
||||
"user":
|
||||
{
|
||||
"data":
|
||||
[
|
||||
{
|
||||
"uuid":"11111111-1111-1111-1111-111111111111",
|
||||
"status":"pending",
|
||||
"description":"This is a test",
|
||||
"entry":"20110111T124000Z"
|
||||
}
|
||||
],
|
||||
"synch":"key"
|
||||
}
|
||||
},
|
||||
|
||||
"auth":
|
||||
{
|
||||
"org":"gbf",
|
||||
"user":"Paul Beckingham",
|
||||
"key":"K",
|
||||
"locale":"en-US"
|
||||
}
|
||||
}
|
||||
*/
|
||||
input = "{\"ticket\":{\"type\":\"synch\",\"client\":\"taskd-test-suite 1.0\"},\"synch\":{\"user\":{\"data\":[{\"uuid\":\"11111111-1111-1111-1111-111111111111\",\"status\":\"pending\",\"description\":\"This is a test\",\"entry\":\"20110111T124000Z\"}],\"synch\":\"key\"}},\"auth\":{\"org\":\"gbf\",\"user\":\"Paul Beckingham\",\"key\":\"K\",\"locale\":\"en-US\"}}";
|
||||
std::cout << "# -- j5 -------------------\n"
|
||||
<< "# input: " << input << "\n";
|
||||
root = json::parse (input);
|
||||
t.ok (root, "j5 parse ok");
|
||||
if (root)
|
||||
{
|
||||
t.diag ("output: " + root->dump ());
|
||||
delete root;
|
||||
}
|
||||
else
|
||||
t.fail ("j5 parse error");
|
||||
}
|
||||
|
||||
catch (std::string& e) {t.diag (e);}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue