File: t.t.cpp

package info (click to toggle)
task 2.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 7,076 kB
  • sloc: cpp: 45,964; python: 12,713; sh: 785; perl: 189; makefile: 21
file content (257 lines) | stat: -rw-r--r-- 9,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include <cmake.h>
#include <stdlib.h>
#include <main.h>
#include <test.h>

////////////////////////////////////////////////////////////////////////////////
int main (int, char**)
{
  UnitTest test (49);

  // Ensure environment has no influence.
  unsetenv ("TASKDATA");
  unsetenv ("TASKRC");

  test.is ((int)Task::textToStatus ("pending"),   (int)Task::pending,   "textToStatus pending");
  test.is ((int)Task::textToStatus ("completed"), (int)Task::completed, "textToStatus completed");
  test.is ((int)Task::textToStatus ("deleted"),   (int)Task::deleted,   "textToStatus deleted");
  test.is ((int)Task::textToStatus ("recurring"), (int)Task::recurring, "textToStatus recurring");

  test.is (Task::statusToText (Task::pending),   "pending",   "statusToText pending");
  test.is (Task::statusToText (Task::completed), "completed", "statusToText completed");
  test.is (Task::statusToText (Task::deleted),   "deleted",   "statusToText deleted");
  test.is (Task::statusToText (Task::recurring), "recurring", "statusToText recurring");

  // Round-trip testing.
  Task t3;
  t3.set ("name", "value");
  std::string before = t3.composeF4 ();
  t3.parse (before);
  std::string after = t3.composeF4 ();
  t3.parse (after);
  after = t3.composeF4 ();
  t3.parse (after);
  after = t3.composeF4 ();
  test.is (before, after, "Task::composeF4 -> parse round trip 4 iterations");

  // Legacy Format 1 (no longer supported)
  //   [tags] [attributes] description\n
  //   X [tags] [attributes] description\n
  std::string sample = "[tag1 tag2] [att1:value1 att2:value2] Description";
  sample = "X "
           "[one two] "
           "[att1:value1 att2:value2] "
           "Description";
  bool good = true;
  try { Task ff1 (sample); } catch (...) { good = false; }
  test.notok (good, "Support for ff1 removed");

  // Legacy Format 2 (no longer supported)
  //   uuid status [tags] [attributes] description\n
  sample = "00000000-0000-0000-0000-000000000000 "
           "- "
           "[tag1 tag2] "
           "[att1:value1 att2:value2] "
           "Description";
  good = true;
  try { Task ff2 (sample); } catch (...) { good = false; }
  test.notok (good, "Support for ff2 removed");

  // Legacy Format 3
  //   uuid status [tags] [attributes] [annotations] description\n
  sample = "00000000-0000-0000-0000-000000000000 "
           "- "
           "[tag1 tag2] "
           "[att1:value1 att2:value2] "
           "[123:ann1 456:ann2] Description";
  good = true;
  try { Task ff3 (sample); } catch (...) { good = false; }
  test.notok (good, "Support for ff3 removed");

  // Current Format 4
  //   [name:"value" ...]\n
  sample = "["
           "uuid:\"00000000-0000-0000-0000-000000000000\" "
           "status:\"pending\" "
           "tags:\"tag1,tag2\" "
           "att1:\"value1\" "
           "att2:\"value2\" "
           "description:\"Description\""
           "]";
  Task ff4 (sample);
  std::string value = ff4.get ("uuid");
  test.is (value, "00000000-0000-0000-0000-000000000000", "ff4 uuid");
  value = ff4.get ("status");
  test.is (value, "pending", "ff4 status");
  test.ok (ff4.hasTag ("tag1"), "ff4 tag1");
  test.ok (ff4.hasTag ("tag2"), "ff4 tag2");
  test.is (ff4.getTagCount (), 2, "ff4 # tags");
  value = ff4.get ("att1");
  test.is (value, "value1", "ff4 att1");
  value = ff4.get ("att2");
  test.is (value, "value2", "ff4 att2");
  value = ff4.get ("description");
  test.is (value, "Description", "ff4 description");

/*

TODO Task::composeCSV
TODO Task::composeYAML
TODO Task::id
TODO Task::*Status
TODO Task::*Tag*
TODO Task::*Annotation*

TODO Task::addDependency
TODO Task::addDependency
TODO Task::removeDependency
TODO Task::removeDependency
TODO Task::getDependencies
TODO Task::getDependencies

TODO Task::urgency

TODO Task::encode
TODO Task::decode

*/

  // Task::operator==
  Task left ("[one:1 two:2 three:3]");
  Task right (left);
  test.ok (left == right, "left == right -> true");
  left.set ("one", "1.0");
  test.notok (left == right, "left == right -> false");

  ////////////////////////////////////////////////////////////////////////////////
  Task task;

  // (blank)
  good = true;
  try {task = Task ("");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.notok (good, "Task::Task ('')");

  // []
  good = true;
  try {task = Task ("[]");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.notok (good, "Task::Task ('[]')");

  // [name:"value"]
  good = true;
  try {task = Task ("[name:\"value\"]");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.ok (good, "Task::Task ('[name:\"value\"]')");
  test.is (task.get ("name"), "value", "name=value");

  // [name:"one two"]
  good = true;
  try {task = Task ("[name:\"one two\"]");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.ok (good, "Task::Task ('[name:\"one two\"]')");
  test.is (task.get ("name"), "one two", "name=one two");

  // [one:two three:four]
  good = true;
  try {task = Task (R"([one:"two" three:"four"])");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.ok (good, R"(Task::Task ('[one:"two" three:"four"]'))");
  test.is (task.get ("one"), "two", "one=two");
  test.is (task.get ("three"), "four", "three=four");

  // Task::set
  task = Task();
  task.set ("name", "value");
  test.is (task.composeF4 (), "[name:\"value\"]", "Task::set");

  // Task::has
  test.ok    (task.has ("name"), "Task::has");
  test.notok (task.has ("woof"), "Task::has not");

  // Task::get_int
  task.set ("one", 1);
  test.is (task.composeF4 (), R"([name:"value" one:"1"])", "Task::set");
  test.is (task.get_int ("one"), 1, "Task::get_int");

  // Task::get_ulong
  task.set ("two", "4294967295");
  test.is (task.composeF4 (), R"([name:"value" one:"1" two:"4294967295"])", "Task::set");
  test.is ((size_t)task.get_ulong ("two"), (size_t)4294967295UL, "Task::get_ulong");

  // Task::remove
  task.remove ("one");
  task.remove ("two");
  test.is (task.composeF4 (), "[name:\"value\"]", "Task::remove");

  // Task::all
  test.is (task.all ().size (), (size_t)1, "Task::all size");

  ////////////////////////////////////////////////////////////////////////////////

  Task::attributes["description"] = "string";
  Task::attributes["entry"] = "date";
  Task::attributes["tags"] = "string";
  Task::attributes["uuid"] = "string";

  good = true;
  try {Task t4 ("{}");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.ok (good, "Task::Task ('{}')");

  good = true;
  try {Task t5 (R"({"uuid":"00000000-0000-0000-000000000001","description":"foo","entry":"1234567890"})");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.ok (good, "Task::Task ('{<minimal>}')");

  // Verify tag handling is correct between F4 and JSON.
  Task t6;
  t6.set ("entry", "20130602T224000Z");
  t6.set ("description", "DESC");
  t6.addTag ("tag1");
  test.is (t6.composeF4 (), R"([description:"DESC" entry:"20130602T224000Z" tags:"tag1"])", "F4 good");
  test.is (t6.composeJSON (), R"({"description":"DESC","entry":"20130602T224000Z","tags":["tag1"]})", "JSON good");

  t6.addTag ("tag2");
  test.is (t6.composeF4 (), R"([description:"DESC" entry:"20130602T224000Z" tags:"tag1,tag2"])", "F4 good");
  test.is (t6.composeJSON (), R"({"description":"DESC","entry":"20130602T224000Z","tags":["tag1","tag2"]})", "JSON good");

  good = true;
  Task t7;
  try {t7 = Task (R"({"description":"DESC","entry":"20130602T224000Z","tags":["tag1","tag2"]})");}
  catch (const std::string& e){test.diag (e); good = false;}
  test.ok (good, "Task::Task ('{two tags}')");
  test.is (t7.composeF4 (), R"([description:"DESC" entry:"1370212800" tags:"tag1,tag2"])", "F4 good");
  test.is (t7.composeJSON (), R"({"description":"DESC","entry":"20130602T224000Z","tags":["tag1","tag2"]})", "JSON good");

  return 0;
}

////////////////////////////////////////////////////////////////////////////////