• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GothenburgBitFactory / taskwarrior / 12343201393

15 Dec 2024 11:30PM UTC coverage: 84.419% (-1.1%) from 85.522%
12343201393

Pull #3724

github

web-flow
Merge 532931b9f into ddae5c4ba
Pull Request #3724: Support importing Taskwarrior v2.x data files

15 of 145 new or added lines in 4 files covered. (10.34%)

183 existing lines in 48 files now uncovered.

19289 of 22849 relevant lines covered (84.42%)

23168.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.87
/test/test.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included
13
// in all copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
//
23
// https://www.opensource.org/licenses/mit-license.php
24
//
25
////////////////////////////////////////////////////////////////////////////////
26

27
#include <math.h>
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <test.h>
32
#include <unistd.h>
33

34
#include <iomanip>
35
#include <iostream>
36

37
///////////////////////////////////////////////////////////////////////////////
38
UnitTest::UnitTest() : _planned(0), _counter(0), _passed(0), _failed(0), _skipped(0) {}
1✔
39

40
///////////////////////////////////////////////////////////////////////////////
41
UnitTest::UnitTest(int planned)
30✔
42
    : _planned(planned), _counter(0), _passed(0), _failed(0), _skipped(0) {
30✔
43
  std::cout << "1.." << _planned << '\n';
30✔
44
}
30✔
45

46
///////////////////////////////////////////////////////////////////////////////
47
UnitTest::~UnitTest() {
31✔
48
  float percentPassed = 0.0;
31✔
49
  if (_planned > 0)
31✔
50
    percentPassed = (100.0 * _passed) / std::max(_planned, _passed + _failed + _skipped);
30✔
51

52
  if (_counter < _planned) {
31✔
53
    std::cout << "# Only " << _counter << " tests, out of a planned " << _planned << " were run.\n";
2✔
54
    _skipped += _planned - _counter;
2✔
55
  }
56

57
  else if (_counter > _planned)
29✔
58
    std::cout << "# " << _counter << " tests were run, but only " << _planned << " were planned.\n";
7✔
59

60
  std::cout << "# " << _passed << " passed, " << _failed << " failed, " << _skipped << " skipped. "
31✔
61
            << std::setprecision(3) << percentPassed << "% passed.\n";
31✔
62
  exit(_failed > 0);
31✔
63
}
64

65
///////////////////////////////////////////////////////////////////////////////
66
void UnitTest::plan(int planned) {
×
67
  _planned = planned;
×
68
  _counter = 0;
×
69
  _passed = 0;
×
70
  _failed = 0;
×
71
  _skipped = 0;
×
72

73
  std::cout << "1.." << _planned << '\n';
×
UNCOV
74
}
×
75

76
///////////////////////////////////////////////////////////////////////////////
77
void UnitTest::planMore(int extra) {
×
78
  _planned += extra;
×
79
  std::cout << "1.." << _planned << '\n';
×
UNCOV
80
}
×
81

82
///////////////////////////////////////////////////////////////////////////////
83
void UnitTest::ok(bool expression, const std::string& name, bool expfail /* = false */) {
457✔
84
  ++_counter;
457✔
85

86
  bool success = expression;
457✔
87

88
  if (success and !expfail) {
457✔
89
    ++_passed;
457✔
90
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
914✔
91
  } else {
92
    if (success == expfail) ++_failed;
×
93
    std::cout << red("not ok") << " " << _counter << " - " << name
×
94
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << '\n';
×
95
  }
96
}
457✔
97

98
///////////////////////////////////////////////////////////////////////////////
99
void UnitTest::notok(bool expression, const std::string& name, bool expfail /* = false */) {
18✔
100
  ++_counter;
18✔
101

102
  bool success = not expression;
18✔
103

104
  if (success and !expfail) {
18✔
105
    ++_passed;
18✔
106
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
36✔
107
  } else {
108
    if (success == expfail) ++_failed;
×
109
    std::cout << red("not ok") << " " << _counter << " - " << name
×
110
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << '\n';
×
111
  }
112
}
18✔
113

114
///////////////////////////////////////////////////////////////////////////////
115
void UnitTest::is(bool actual, bool expected, const std::string& name, bool expfail /* = false */) {
520✔
116
  ++_counter;
520✔
117
  bool success = (actual == expected);
520✔
118

119
  if (success and !expfail) {
520✔
120
    ++_passed;
520✔
121
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
1,040✔
122
  } else {
123
    if (success == expfail) ++_failed;
×
124
    std::cout << red("not ok") << " " << _counter << " - " << name
×
125
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: " << expected
×
126
              << "\n#      got: " << actual << '\n';
×
127
  }
128
}
520✔
129

130
///////////////////////////////////////////////////////////////////////////////
131
void UnitTest::is(size_t actual, size_t expected, const std::string& name,
8✔
132
                  bool expfail /* = false */) {
133
  ++_counter;
8✔
134
  bool success = (actual == expected);
8✔
135

136
  if (success and !expfail) {
8✔
137
    ++_passed;
8✔
138
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
16✔
139
  } else {
140
    if (success == expfail) ++_failed;
×
141
    std::cout << red("not ok") << " " << _counter << " - " << name
×
142
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: " << expected
×
143
              << "\n#      got: " << actual << '\n';
×
144
  }
145
}
8✔
146

147
///////////////////////////////////////////////////////////////////////////////
148
void UnitTest::is(int actual, int expected, const std::string& name, bool expfail /* = false */) {
1,004✔
149
  ++_counter;
1,004✔
150
  bool success = (actual == expected);
1,004✔
151

152
  if (success and !expfail) {
1,004✔
153
    ++_passed;
1,002✔
154
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
2,004✔
155
  } else {
156
    if (success == expfail) ++_failed;
2✔
157
    std::cout << red("not ok") << " " << _counter << " - " << name
6✔
158
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: " << expected
2✔
159
              << "\n#      got: " << actual << '\n';
2✔
160
  }
161
}
1,004✔
162

163
///////////////////////////////////////////////////////////////////////////////
164
void UnitTest::is(double actual, double expected, const std::string& name,
5✔
165
                  bool expfail /* = false */) {
166
  ++_counter;
5✔
167
  bool success = (actual == expected);
5✔
168

169
  if (success and !expfail) {
5✔
170
    ++_passed;
5✔
171
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
10✔
172
  } else {
173
    if (success == expfail) ++_failed;
×
174
    std::cout << red("not ok") << " " << _counter << " - " << name
×
175
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: " << expected
×
176
              << "\n#      got: " << actual << '\n';
×
177
  }
178
}
5✔
179

180
///////////////////////////////////////////////////////////////////////////////
181
void UnitTest::is(double actual, double expected, double tolerance, const std::string& name,
29✔
182
                  bool expfail /* = false */) {
183
  ++_counter;
29✔
184
  bool success = (fabs(actual - expected) <= tolerance);
29✔
185

186
  if (success and !expfail) {
29✔
187
    ++_passed;
29✔
188
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
58✔
189
  } else {
190
    if (success == expfail) ++_failed;
×
191
    std::cout << red("not ok") << " " << _counter << " - " << name
×
192
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: " << expected
×
193
              << "\n#      got: " << actual << '\n';
×
194
  }
195
}
29✔
196

197
///////////////////////////////////////////////////////////////////////////////
198
void UnitTest::is(unsigned char actual, unsigned char expected, const std::string& name,
×
199
                  bool expfail /* = false */) {
200
  ++_counter;
×
201
  bool success = (actual == expected);
×
202

203
  if (success and !expfail) {
×
204
    ++_passed;
×
205
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
×
206
  } else {
207
    if (success == expfail) ++_failed;
×
208
    std::cout << red("not ok") << " " << _counter << " - " << name
×
209
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: " << expected
×
210
              << "\n#      got: " << actual << '\n';
×
211
  }
UNCOV
212
}
×
213

214
///////////////////////////////////////////////////////////////////////////////
215
void UnitTest::is(const std::string& actual, const std::string& expected, const std::string& name,
661✔
216
                  bool expfail /* = false */) {
217
  ++_counter;
661✔
218
  bool success = (actual == expected);
661✔
219

220
  if (success and !expfail) {
661✔
221
    ++_passed;
659✔
222
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
1,318✔
223
  } else {
224
    if (success == expfail) ++_failed;
2✔
225
    std::cout << red("not ok") << " " << _counter << " - " << name
6✔
226
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: '"
2✔
227
              << expected << "'"
228
              << "\n#      got: '" << actual << "'\n";
4✔
229
  }
230
}
661✔
231

232
///////////////////////////////////////////////////////////////////////////////
233
void UnitTest::is(const char* actual, const char* expected, const std::string& name,
1✔
234
                  bool expfail /* = false */) {
235
  ++_counter;
1✔
236
  bool success = (!strcmp(actual, expected));
1✔
237

238
  if (success and !expfail) {
1✔
239
    ++_passed;
1✔
240
    std::cout << green("ok") << " " << _counter << " - " << name << '\n';
2✔
241
  } else {
242
    if (success == expfail) ++_failed;
×
243
    std::cout << red("not ok") << " " << _counter << " - " << name
×
244
              << (expfail ? (success ? " # FIXED" : " # TODO") : "") << "\n# expected: '"
×
245
              << expected << "'"
246
              << "\n#      got: '" << actual << "'\n";
×
247
  }
248
}
1✔
249

250
///////////////////////////////////////////////////////////////////////////////
251
void UnitTest::diag(const std::string& text) {
2✔
252
  auto start = text.find_first_not_of(" \t\n\r\f");
2✔
253
  auto end = text.find_last_not_of(" \t\n\r\f");
2✔
254
  if (start != std::string::npos && end != std::string::npos)
2✔
255
    std::cout << "# " << text.substr(start, end - start + 1) << '\n';
2✔
256
}
2✔
257

258
///////////////////////////////////////////////////////////////////////////////
259
void UnitTest::pass(const std::string& text) {
130✔
260
  ++_counter;
130✔
261
  ++_passed;
130✔
262
  std::cout << green("ok") << " " << _counter << " - " << text << '\n';
130✔
263
}
130✔
264

265
///////////////////////////////////////////////////////////////////////////////
266
void UnitTest::fail(const std::string& text) {
×
267
  ++_counter;
×
268
  ++_failed;
×
269
  std::cout << red("not ok") << " " << _counter << " - " << text << '\n';
×
UNCOV
270
}
×
271

272
///////////////////////////////////////////////////////////////////////////////
273
void UnitTest::skip(const std::string& text) {
×
274
  ++_counter;
×
275
  ++_skipped;
×
276
  std::cout << yellow("ok") << " " << _counter << " - " << text << " # skip" << '\n';
×
UNCOV
277
}
×
278

279
///////////////////////////////////////////////////////////////////////////////
280
std::string UnitTest::red(const std::string& input) {
4✔
281
  if (isatty(fileno(stdout))) return std::string("\033[31m" + input + "\033[0m");
4✔
282

283
  return input;
4✔
284
}
285

286
///////////////////////////////////////////////////////////////////////////////
287
std::string UnitTest::green(const std::string& input) {
2,829✔
288
  if (isatty(fileno(stdout))) return std::string("\033[32m" + input + "\033[0m");
2,829✔
289

290
  return input;
2,829✔
291
}
292

293
///////////////////////////////////////////////////////////////////////////////
294
std::string UnitTest::yellow(const std::string& input) {
×
295
  if (isatty(fileno(stdout))) return std::string("\033[33m" + input + "\033[0m");
×
296

297
  return input;
×
298
}
299

300
///////////////////////////////////////////////////////////////////////////////
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc