• 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

97.63
/test/variant_cast_test.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2013 - 2021, Göteborg Bit Factory.
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 <cmake.h>
28
// cmake.h include header must come first
29

30
#include <Variant.h>
31
#include <test.h>
32

33
#include <iostream>
34

35
#define EPSILON 0.001
36

37
////////////////////////////////////////////////////////////////////////////////
38
int TEST_NAME(int, char**) {
1✔
39
  UnitTest t(81);
1✔
40

41
  time_t now = time(nullptr);
1✔
42

43
  try {
44
    // Variant::type_boolean --> *
45
    Variant v00(true);
1✔
46
    v00.cast(Variant::type_boolean);
1✔
47
    t.ok(v00.type() == Variant::type_boolean, "cast boolean --> boolean");
2✔
48
    t.ok(v00.get_bool() == true, "cast boolean --> boolean");
1✔
49

50
    Variant v01(true);
1✔
51
    v01.cast(Variant::type_integer);
1✔
52
    t.ok(v01.type() == Variant::type_integer, "cast boolean --> integer");
2✔
53
    t.ok(v01.get_integer() == 1, "cast boolean --> integer");
1✔
54

55
    Variant v02(true);
1✔
56
    v02.cast(Variant::type_real);
1✔
57
    t.ok(v02.type() == Variant::type_real, "cast boolean --> real");
2✔
58
    t.is(v02.get_real(), 1.0, EPSILON, "cast boolean --> real");
1✔
59

60
    Variant v03(true);
1✔
61
    v03.cast(Variant::type_string);
1✔
62
    t.ok(v03.type() == Variant::type_string, "cast boolean --> string");
2✔
63
    t.ok(v03.get_string() == "true", "cast boolean --> string");
1✔
64

65
    Variant v04(true);
1✔
66
    v04.cast(Variant::type_date);
1✔
67
    t.ok(v04.type() == Variant::type_date, "cast boolean --> date");
2✔
68
    t.is(v04.get_date(), 1, "cast boolean --> date");
1✔
69

70
    Variant v05(true);
1✔
71
    v05.cast(Variant::type_duration);
1✔
72
    t.ok(v05.type() == Variant::type_duration, "cast boolean --> duration");
2✔
73
    t.is(v05.get_duration(), 1, "cast boolean --> duration");
1✔
74

75
    // Variant::type_integer --> *
76
    Variant v10(42);
1✔
77
    v10.cast(Variant::type_boolean);
1✔
78
    t.ok(v10.type() == Variant::type_boolean, "cast integer --> boolean");
2✔
79
    t.ok(v10.get_bool() == true, "cast integer --> boolean");
1✔
80

81
    Variant v11(42);
1✔
82
    v11.cast(Variant::type_integer);
1✔
83
    t.ok(v11.type() == Variant::type_integer, "cast integer --> integer");
2✔
84
    t.ok(v11.get_integer() == 42, "cast integer --> integer");
1✔
85

86
    Variant v12(42);
1✔
87
    v12.cast(Variant::type_real);
1✔
88
    t.ok(v12.type() == Variant::type_real, "cast integer --> real");
3✔
89
    t.is(v12.get_real(), 42.0, EPSILON, "cast integer --> real");
1✔
90

91
    Variant v13(42);
1✔
92
    v13.cast(Variant::type_string);
1✔
93
    t.is(v13.type(), Variant::type_string, "cast integer --> string");
2✔
94
    t.is(v13.get_string(), "42", "cast integer --> string");
3✔
95

96
    Variant v14(42);
1✔
97
    v14.cast(Variant::type_date);
1✔
98
    t.ok(v14.type() == Variant::type_date, "cast integer --> date");
2✔
99
    t.ok(v14.get_date() == 42, "cast integer --> date");
1✔
100

101
    Variant v15(42);
1✔
102
    v15.cast(Variant::type_duration);
1✔
103
    t.is(v15.type(), Variant::type_duration, "cast integer --> duration");
2✔
104
    t.is(v15.get_duration(), 42, "cast integer --> duration");
1✔
105

106
    // Variant::type_real --> *
107
    Variant v20(3.14);
1✔
108
    v20.cast(Variant::type_boolean);
1✔
109
    t.ok(v20.type() == Variant::type_boolean, "cast real --> boolean");
2✔
110
    t.ok(v20.get_bool() == true, "cast real --> boolean");
1✔
111

112
    Variant v21(3.14);
1✔
113
    v21.cast(Variant::type_integer);
1✔
114
    t.ok(v21.type() == Variant::type_integer, "cast real --> integer");
2✔
115
    t.ok(v21.get_integer() == 3, "cast real --> integer");
1✔
116

117
    Variant v22(3.14);
1✔
118
    v22.cast(Variant::type_real);
1✔
119
    t.ok(v22.type() == Variant::type_real, "cast real --> real");
2✔
120
    t.is(v22.get_real(), 3.14, EPSILON, "cast real --> real");
1✔
121

122
    Variant v23(3.14);
1✔
123
    v23.cast(Variant::type_string);
1✔
124
    t.ok(v23.type() == Variant::type_string, "cast real --> string");
2✔
125
    t.ok(v23.get_string() == "3.14", "cast real --> string");
1✔
126

127
    Variant v24(3.14);
1✔
128
    v24.cast(Variant::type_date);
1✔
129
    t.ok(v24.type() == Variant::type_date, "cast real --> date");
2✔
130
    t.ok(v24.get_date() == 3, "cast real --> date");
1✔
131

132
    Variant v25(3.14);
1✔
133
    v25.cast(Variant::type_duration);
1✔
134
    t.ok(v25.type() == Variant::type_duration, "cast real --> duration");
2✔
135
    t.ok(v25.get_duration() == 3, "cast real --> duration");
1✔
136

137
    // Variant::type_string --> *
138
    Variant v30("foo");
1✔
139
    v30.cast(Variant::type_boolean);
1✔
140
    t.ok(v30.type() == Variant::type_boolean, "cast string --> boolean");
2✔
141
    t.ok(v30.get_bool() == true, "cast string --> boolean");
1✔
142

143
    Variant v31("42");
1✔
144
    v31.cast(Variant::type_integer);
1✔
145
    t.ok(v31.type() == Variant::type_integer, "cast string --> integer");
2✔
146
    t.ok(v31.get_integer() == 42, "cast string --> integer");
1✔
147

148
    Variant v31h("0x20");
1✔
149
    v31h.cast(Variant::type_integer);
1✔
150
    t.ok(v31h.type() == Variant::type_integer, "cast string(hex) --> integer");
2✔
151
    t.ok(v31h.get_integer() == 32, "cast string(hex) --> integer");
1✔
152

153
    Variant v32("3.14");
1✔
154
    v32.cast(Variant::type_real);
1✔
155
    t.ok(v32.type() == Variant::type_real, "cast string --> real");
2✔
156
    t.is(v32.get_real(), 3.14, EPSILON, "cast string --> real");
1✔
157

158
    Variant v33("foo");
1✔
159
    v33.cast(Variant::type_string);
1✔
160
    t.ok(v33.type() == Variant::type_string, "cast string --> string");
2✔
161
    t.ok(v33.get_string() == "foo", "cast string --> string");
1✔
162

163
    Variant v34("2013-12-07T16:33:00-05:00");
1✔
164
    v34.cast(Variant::type_date);
1✔
165
    t.ok(v34.type() == Variant::type_date, "cast string --> date");
2✔
166
    t.ok(v34.get_date() == 1386451980, "cast string --> date");
1✔
167

168
    Variant v35("42 days");
1✔
169
    v35.cast(Variant::type_duration);
1✔
170
    t.ok(v35.type() == Variant::type_duration, "cast string --> duration");
2✔
171
    t.is(v35.get_duration(), 3628800, "cast string --> duration");
1✔
172

173
    Variant v35b("P42D");
1✔
174
    v35b.cast(Variant::type_duration);
1✔
175
    t.ok(v35b.type() == Variant::type_duration, "cast string --> duration");
2✔
176
    t.is(v35b.get_duration(), 3628800, "cast string --> duration");
1✔
177

178
    // Variant::type_date --> *
179
    Variant v40((time_t)1234567890, Variant::type_date);
1✔
180
    v40.cast(Variant::type_boolean);
1✔
181
    t.ok(v40.type() == Variant::type_boolean, "cast date --> boolean");
2✔
182
    t.ok(v40.get_bool() == true, "cast date --> boolean");
1✔
183

184
    Variant v41((time_t)1234567890, Variant::type_date);
1✔
185
    v41.cast(Variant::type_integer);
1✔
186
    t.ok(v41.type() == Variant::type_integer, "cast date --> integer");
2✔
187
    t.ok(v41.get_integer() == 1234567890, "cast date --> integer");
1✔
188

189
    Variant v42((time_t)1234567890, Variant::type_date);
1✔
190
    v42.cast(Variant::type_real);
1✔
191
    t.ok(v42.type() == Variant::type_real, "cast date --> real");
2✔
192
    t.is(v42.get_real(), 1234567890.0, EPSILON, "cast date --> real");
1✔
193

194
    // YYYY-MM-DDThh:mm:ss
195
    //     ^  ^  ^  ^  ^
196
    Variant v43((time_t)1234567890, Variant::type_date);
1✔
197
    v43.cast(Variant::type_string);
1✔
198
    t.ok(v43.type() == Variant::type_string, "cast date --> string");
1✔
199
    std::string s = v43.get_string();
1✔
200
    t.is((int)s[4], (int)'-', "cast date --> string");
2✔
201
    t.is((int)s[7], (int)'-', "cast date --> string");
2✔
202
    t.is((int)s[10], (int)'T', "cast date --> string");
2✔
203
    t.is((int)s[13], (int)':', "cast date --> string");
2✔
204
    t.is((int)s[16], (int)':', "cast date --> string");
2✔
205
    t.is((int)s.length(), 19, "cast date --> string");
1✔
206

207
    Variant v44((time_t)1234567890, Variant::type_date);
1✔
208
    v44.cast(Variant::type_date);
1✔
209
    t.ok(v44.type() == Variant::type_date, "cast date --> date");
2✔
210
    t.ok(v44.get_date() == 1234567890, "cast date --> date");
1✔
211

212
    Variant v45((time_t)1234567890, Variant::type_date);
1✔
213
    v45.cast(Variant::type_duration);
1✔
214
    t.ok(v45.type() == Variant::type_duration, "cast date --> duration");
2✔
215
    t.ok(v45.get_duration() <= 1234567890 - now, "cast date --> duration");
2✔
216
    // Assuming this unit test takes less than 10 min to execute
217
    t.ok(v45.get_duration() > 1234567890 - now - 600, "cast date --> duration");
1✔
218

219
    // Variant::type_duration --> *
220
    Variant v50((time_t)12345, Variant::type_duration);
1✔
221
    v50.cast(Variant::type_boolean);
1✔
222
    t.ok(v50.type() == Variant::type_boolean, "cast duration --> boolean");
2✔
223
    t.ok(v50.get_bool() == true, "cast duration --> boolean");
1✔
224

225
    Variant v51((time_t)12345, Variant::type_duration);
1✔
226
    v51.cast(Variant::type_integer);
1✔
227
    t.ok(v51.type() == Variant::type_integer, "cast duration --> integer");
2✔
228
    t.ok(v51.get_integer() == 12345, "cast duration --> integer");
1✔
229

230
    Variant v52((time_t)12345, Variant::type_duration);
1✔
231
    v52.cast(Variant::type_real);
1✔
232
    t.ok(v52.type() == Variant::type_real, "cast duration --> real");
2✔
233
    t.is(v52.get_real(), 12345.0, EPSILON, "cast duration --> real");
1✔
234

235
    Variant v53((time_t)12345, Variant::type_duration);
1✔
236
    v53.cast(Variant::type_string);
1✔
237
    t.ok(v53.type() == Variant::type_string, "cast duration --> string");
2✔
238
    t.is(v53.get_string(), "PT3H25M45S", "cast duration --> string");
3✔
239

240
    Variant v54((time_t)12345, Variant::type_duration);
1✔
241
    v54.cast(Variant::type_date);
1✔
242
    t.ok(v54.type() == Variant::type_date, "cast duration --> date");
2✔
243
    t.ok(v54.get_date() >= 12345 + now, "cast duration --> duration");
2✔
244
    t.ok(v54.get_date() < 12345 + now + 600, "cast duration --> duration");
1✔
245

246
    Variant v55((time_t)12345, Variant::type_duration);
1✔
247
    v55.cast(Variant::type_duration);
1✔
248
    t.ok(v55.type() == Variant::type_duration, "cast duration --> duration");
2✔
249
    t.is(v55.get_duration(), 12345, "cast duration --> duration");
1✔
250
  }
1✔
251

252
  catch (const std::string& e) {
×
253
    t.diag(e);
×
UNCOV
254
  }
×
255

256
  return 0;
×
257
}
1✔
258

259
////////////////////////////////////////////////////////////////////////////////
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