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
|
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 - 2018, 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.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Interval.h>
#include <test.h>
////////////////////////////////////////////////////////////////////////////////
int main (int, char**)
{
UnitTest t (40);
// bool is_started () const;
// bool is_ended () const;
Interval i1;
t.is (i1.empty (), true, "Interval().empty -> true");
t.is (i1.range.is_started (), false, "Interval().is_started -> false");
t.is (i1.range.is_ended (), false, "Interval().is_ended -> false");
// void start (Datetime);
i1.range.start = Datetime ();
t.is (i1.empty (), false, "Interval().empty -> false");
t.is (i1.range.is_started (), true, "Interval(start=now).is_started -> true");
t.is (i1.range.is_ended (), false, "Interval(start=now).is_ended -> false");
// void end (Datetime);
i1.range.end = Datetime ();
t.is (i1.range.is_started (), true, "Interval(start=now,end=now).is_started -> true");
t.is (i1.range.is_ended (), true, "Interval(start=now,end=now).is_ended -> true");
// std::set <std::string> tags () const;
// void tag (const std::string&);
Interval i2;
t.ok (i2.tags () == std::set <std::string> {}, "Interval(tag=) -> {}");
i2.tag ("foo");
t.ok (i2.tags () == std::set <std::string> {"foo"}, "Interval(tag=foo) -> {foo}");
i2.tag ("foo"); // Duplicate
t.ok (i2.tags () == std::set <std::string> {"foo"}, "Interval(tag=foo,foo) -> {foo}");
i2.tag ("bar");
t.ok (i2.tags () == std::set <std::string> {"foo", "bar"}, "Interval(tag=foo,bar) -> {foo,bar}");
i2.tag ("baz");
t.ok (i2.tags () == std::set <std::string> {"foo", "bar", "baz"}, "Interval(tag=foo,bar,baz) -> {foo,bar,baz}");
i2.untag ("foo");
t.ok (i2.tags () == std::set <std::string> {"bar", "baz"}, "Interval(tag=bar,baz) -> {bar,baz}");
// std::string serialize () const;
Interval i3;
t.is (i3.serialize (), "inc", "Interval().serialize -> 'inc'");
i3.tag ("foo");
t.is (i3.serialize (), "inc # foo", "Interval().serialize -> 'inc # foo'");
i3.tag ("bar");
t.is (i3.serialize (), "inc # bar foo", "Interval().serialize -> 'inc # bar foo'");
i3.range.start = Datetime(1);
t.is (i3.serialize (), "inc 19700101T000001Z # bar foo", "Interval(Datetime(1)).serialize -> 'inc 19700101T000001Z # bar foo'");
i3.range.end = Datetime(2);
t.is (i3.serialize (), "inc 19700101T000001Z - 19700101T000002Z # bar foo", "Interval(Datetime(1)).serialize -> 'inc 19700101T000001Z - 19700101T000002Z # bar foo'");
i3.tag ("Trans-Europe Express");
t.is (i3.serialize (), "inc 19700101T000001Z - 19700101T000002Z # \"Trans-Europe Express\" bar foo", "Interval(Datetime(1)).serialize -> 'inc 19700101T000001Z - 19700101T000002Z # \"Trans-Europe Express\" bar foo'");
// Round-trip parsing.
Interval i4;
i4.initialize ( "inc");
t.is (i4.serialize (), "inc",
"Round-trip 'inc'");
Interval i5;
i5.initialize ( "inc # foo");
t.is (i5.serialize (), "inc # foo",
"Round-trip 'inc # foo'");
Interval i6;
i6.initialize ( "inc # bar foo");
t.is (i6.serialize (), "inc # bar foo",
"Round-trip 'inc # bar foo'");
Interval i7;
i7.initialize ( "inc 19700101T000001Z");
t.is (i7.serialize (), "inc 19700101T000001Z",
"Round-trip 'inc 19700101T000001Z'");
Interval i8;
i8.initialize ( "inc 19700101T000001Z - 19700101T000002Z");
t.is (i8.serialize (), "inc 19700101T000001Z - 19700101T000002Z",
"Round-trip 'inc 19700101T000001Z - 19700101T000002Z'");
Interval i9;
i9.initialize ( "inc 19700101T000001Z # bar foo");
t.is (i9.serialize (), "inc 19700101T000001Z # bar foo",
"Round-trip 'inc 19700101T000001Z # bar foo'");
Interval i10;
i10.initialize ( "inc 19700101T000001Z - 19700101T000002Z # bar foo");
t.is (i10.serialize (), "inc 19700101T000001Z - 19700101T000002Z # bar foo",
"Round-trip 'inc 19700101T000001Z - 19700101T000002Z # bar foo'");
Interval i11;
i11.initialize ( "inc 19700101T000001Z - 19700101T000002Z # \"Trans-Europe Express\" bar foo");
t.is (i11.serialize (), "inc 19700101T000001Z - 19700101T000002Z # \"Trans-Europe Express\" bar foo",
"Round-trip 'inc 19700101T000001Z - 19700101T000002Z # \"Trans-Europe Express\" bar foo'");
// std::string json () const;
Interval i12;
i12.initialize ( "inc");
t.is (i12.json (), "{}",
"JSON '{}'");
Interval i13;
i13.initialize ( "inc # foo");
t.is (i13.json (), "{\"tags\":[\"foo\"]}",
"JSON '{\"tags\":[\"foo\"]}'");
Interval i14;
i14.initialize ( "inc # bar foo");
t.is (i14.json (), "{\"tags\":[\"bar\",\"foo\"]}",
"JSON '{\"tags\":[\"bar\",\"foo\"]}'");
Interval i15;
i15.initialize ( "inc 19700101T000001Z");
t.is (i15.json (), "{\"start\":\"19700101T000001Z\"}",
"JSON '{\"start\":\"19700101T000001Z\"}'");
Interval i16;
i16.initialize ( "inc 19700101T000001Z - 19700101T000002Z");
t.is (i16.json (), "{\"start\":\"19700101T000001Z\",\"end\":\"19700101T000002Z\"}",
"JSON '{\"start\":\"19700101T000001Z\",\"end\":\"19700101T000002Z\"}'");
Interval i17;
i17.initialize ( "inc 19700101T000001Z # bar foo");
t.is (i17.json (), "{\"start\":\"19700101T000001Z\",\"tags\":[\"bar\",\"foo\"]}",
"JSON '{\"start\":\"19700101T000001Z\",\"tags\":[\"bar\",\"foo\"]}'");
Interval i18;
i18.initialize ( "inc 19700101T000001Z - 19700101T000002Z # bar foo");
t.is (i18.json (), "{\"start\":\"19700101T000001Z\",\"end\":\"19700101T000002Z\",\"tags\":[\"bar\",\"foo\"]}",
"JSON '{\"start\":\"19700101T000001Z\",\"end\":\"19700101T000002Z\",\"tags\":[\"bar\",\"foo\"]}'");
Interval i19;
i19.initialize ( "inc 19700101T000001Z - 19700101T000002Z # \"Trans-Europe Express\" bar foo");
t.is (i19.json (), "{\"start\":\"19700101T000001Z\",\"end\":\"19700101T000002Z\",\"tags\":[\"Trans-Europe Express\",\"bar\",\"foo\"]}",
"JSON '{\"start\":\"19700101T000001Z\",\"end\":\"19700101T000002Z\",\"tags\":[\"Trans-Europe Express\",\"bar\",\"foo\"]}'");
Interval i20;
i20.tag ("foo");
t.ok (i20.hasTag ("foo"), "hasTag positive");
t.notok (i20.hasTag ("bar"), "hasTag negative");
Interval i21;
i21.tag ("one-two");
i21.tag ("three");
t.is (i21.json (), "{\"tags\":[\"one-two\",\"three\"]}",
"JSON '{\"tags\":[\"one-two\",\"three\"]}'");
// make sure that underscores are escaped
Interval i22;
i22.tag ("foo_bar");
t.is (i22.serialize (), "inc # \"foo_bar\"", "Interval().serialize -> 'inc # \"foo_bar\"'");
return 0;
}
////////////////////////////////////////////////////////////////////////////////
|