Code Migration

- Migrated taskd JSON parser into task, to provide encode/decode
  capability to Task::composeJSON.
- Migrated taskd utf8 code, replacing old unused code.
- Added unit tests or JSON.
- Migrated Tree updates from taskd.
This commit is contained in:
Paul Beckingham
2011-01-22 23:33:47 -05:00
parent 2f4e0d9e17
commit 57c1983e07
13 changed files with 717 additions and 607 deletions

1
test/.gitignore vendored
View File

@@ -11,6 +11,7 @@ duration.t
file.t
filt.t
grid.t
json.t
lisp.t
list.t
nibbler.t

View File

@@ -6,7 +6,7 @@ include_directories (${CMAKE_SOURCE_DIR}/src
set (test_SRCS date.t t.t tdb.t duration.t t.benchmark.t text.t autocomplete.t seq.t
record.t att.t stringtable.t subst.t nibbler.t filt.t cmd.t config.t
util.t color.t list.t path.t file.t grid.t directory.t rx.t taskmod.t
lisp.t rectangle.t sensor.t tree.t tree2.t uri.t)
lisp.t rectangle.t sensor.t tree.t tree2.t uri.t json.t)
add_custom_target (test ./run_all DEPENDS ${test_SRCS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test)

145
test/json.t.cpp Normal file
View File

@@ -0,0 +1,145 @@
////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006 - 2011, Paul Beckingham.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the
//
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA
// 02110-1301
// USA
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <JSON.h>
#include <test.h>
#include <Context.h>
Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (14);
try
{
// Basic parsing tests.
std::string input = "{}";
std::cout << "-- j1 -------------------\n"
<< "input: " << input << "\n";
JSON j1 (input);
j1.tree ()->dump ();
input = "{\"name\":123}";
std::cout << "-- j2 -------------------\n"
<< "input: " << input << "\n";
JSON j2 (input);
j2.tree ()->dump ();
input = "{\"name\":123, \"array\":[1,2,3.4], \"map\":{\"m1\":\"v1\", \"m2\":\"v2\"}}";
std::cout << "-- j3 -------------------\n"
<< "input: " << input << "\n";
JSON j3 (input);
j3.tree ()->dump ();
// Sample ticket as a parsing test.
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";
JSON j4 (input);
j4.tree ()->dump ();
std::cout << "-------------------------\n";
// 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");
t.is (JSON::encode ("1\n2"), "1\\n2", "JSON::encode \\n -> \\\\n");
t.is (JSON::decode ("1\\n2"), "1\n2", "JSON::decode \\\\n -> \\n");
t.is (JSON::encode ("1\r2"), "1\\r2", "JSON::encode \\r -> \\\\r");
t.is (JSON::decode ("1\\r2"), "1\r2", "JSON::decode \\\\r -> \\r");
t.is (JSON::encode ("1\t2"), "1\\t2", "JSON::encode \\t -> \\\\t");
t.is (JSON::decode ("1\\t2"), "1\t2", "JSON::decode \\\\t -> \\t");
t.is (JSON::encode ("1\\2"), "1\\\\2", "JSON::encode \\ -> \\\\");
t.is (JSON::decode ("1\\\\2"), "1\\2", "JSON::decode \\\\ -> \\");
t.is (JSON::encode ("1\x2"), "1\x2", "JSON::encode \\x -> \\x (NOP)");
t.is (JSON::decode ("1\x2"), "1\x2", "JSON::decode \\x -> \\x (NOP)");
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 << "-- j4 -------------------\n"
<< "input: " << input << "\n";
JSON j5 (input);
j5.tree ()->dump ();
}
catch (std::string& e) {t.diag (e);}
return 0;
}
////////////////////////////////////////////////////////////////////////////////

View File

@@ -244,7 +244,7 @@ int main (int argc, char** argv)
t.diag ("Nibbler::getNumber");
n = Nibbler ("-1.234 2.3e4");
t.ok (n.getNumber (d), "'-1.234 2.3e4' : getNumber () -> true");
t.is (d, -1.234, "'-1.234 2.3e4' : getNumber () -> '-1.234'");
t.is (d, -1.234, 0.000001, "'-1.234 2.3e4' : getNumber () -> '-1.234'");
t.ok (n.skip (' '), " ' 2.3e4' : skip (' ') -> true");
t.ok (n.getNumber (d), " '2.3e4' : getNumber () -> true");
t.is (d, 2.3e4, " '2.3e4' : getNumber () -> '2.3e4'");