forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionalBoolTest.h
144 lines (116 loc) · 4.19 KB
/
OptionalBoolTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidKernel/Exception.h"
#include "MantidKernel/OptionalBool.h"
#include <json/value.h>
#include <cxxtest/TestSuite.h>
#include <sstream>
using namespace Mantid::Kernel;
class OptionalBoolTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static OptionalBoolTest *createSuite() { return new OptionalBoolTest(); }
static void destroySuite(OptionalBoolTest *suite) { delete suite; }
void test_construction_by_bool() {
OptionalBool arg1(true);
TS_ASSERT_EQUALS(OptionalBool::True, arg1.getValue());
OptionalBool arg2(false);
TS_ASSERT_EQUALS(OptionalBool::False, arg2.getValue());
}
void test_defaults_to_unset() {
OptionalBool arg;
TS_ASSERT_EQUALS(OptionalBool::Unset, arg.getValue());
}
void test_construction_by_value() {
auto value = OptionalBool::True;
OptionalBool arg(value);
TS_ASSERT_EQUALS(value, arg.getValue());
}
void test_comparison_overload() {
OptionalBool a(OptionalBool::True);
OptionalBool b(OptionalBool::True);
TS_ASSERT_EQUALS(a, b);
OptionalBool c(OptionalBool::False);
TS_ASSERT_DIFFERS(a, c);
OptionalBool d(OptionalBool::Unset);
TS_ASSERT_DIFFERS(a, d);
}
void test_copy_constructor() {
OptionalBool arg(OptionalBool::False);
OptionalBool copy(arg);
TS_ASSERT_EQUALS(OptionalBool::False, copy.getValue());
}
void test_assignment() {
OptionalBool arg(OptionalBool::False);
arg = OptionalBool(OptionalBool::True);
TS_ASSERT_EQUALS(OptionalBool::True, arg.getValue());
}
void test_ostream_false() {
std::stringstream buffer;
OptionalBool notTrue(OptionalBool::False);
buffer << notTrue;
TS_ASSERT_EQUALS("False", buffer.str());
}
void test_ostream_true() {
std::stringstream buffer;
OptionalBool isTrue(OptionalBool::True);
buffer << isTrue;
TS_ASSERT_EQUALS("True", buffer.str());
}
void test_ostream_unset() {
std::stringstream buffer;
OptionalBool notTrue(OptionalBool::Unset);
buffer << notTrue;
TS_ASSERT_EQUALS("Unset", buffer.str());
}
void test_istream_to_false() {
OptionalBool target;
std::stringstream buffer;
buffer << "False";
buffer >> target;
TS_ASSERT_EQUALS(target, OptionalBool(OptionalBool::False));
}
void test_istream_to_true() {
OptionalBool target;
std::stringstream buffer;
buffer << "True";
buffer >> target;
TS_ASSERT_EQUALS(target, OptionalBool(OptionalBool::True));
}
void test_istream_to_unset() {
OptionalBool target;
std::stringstream buffer;
buffer << "Unset";
buffer >> target;
TS_ASSERT_EQUALS(target, OptionalBool(OptionalBool::Unset));
}
void test_str_map() {
auto map = OptionalBool::strToEmumMap();
TS_ASSERT_EQUALS(3, map.size());
TS_ASSERT_EQUALS(map[OptionalBool::StrUnset], OptionalBool::Unset);
TS_ASSERT_EQUALS(map[OptionalBool::StrFalse], OptionalBool::False);
TS_ASSERT_EQUALS(map[OptionalBool::StrTrue], OptionalBool::True);
}
void test_value_map() {
auto map = OptionalBool::enumToStrMap();
TS_ASSERT_EQUALS(3, map.size());
TS_ASSERT_EQUALS(OptionalBool::StrUnset, map[OptionalBool::Unset]);
TS_ASSERT_EQUALS(OptionalBool::StrFalse, map[OptionalBool::False]);
TS_ASSERT_EQUALS(OptionalBool::StrTrue, map[OptionalBool::True]);
}
void testEncodeOptionalBoolPropertyEncodesSetValue() {
using Mantid::Kernel::OptionalBool;
TS_ASSERT_EQUALS(Json::Value(true), encodeAsJson(OptionalBool{OptionalBool::True}));
TS_ASSERT_EQUALS(Json::Value(false), encodeAsJson(OptionalBool{OptionalBool::False}));
}
void testEncodeOptionalBoolPropertyReturnsNullJsonforUnsetValue() {
using Mantid::Kernel::OptionalBool;
TS_ASSERT_EQUALS(Json::Value(), encodeAsJson(OptionalBool{OptionalBool::Unset}));
}
};