Newer
Older
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef PROPERTYWITHVALUEJSONDECODERTEST_H
#define PROPERTYWITHVALUEJSONDECODERTEST_H
#include <cxxtest/TestSuite.h>
#include <jsoncpp/json/value.h>
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/PropertyWithValueJSONDecoder.h"
class PropertyWithValueJSONDecoderTest : public CxxTest::TestSuite {
public:
static PropertyWithValueJSONDecoderTest *createSuite() {
return new PropertyWithValueJSONDecoderTest;
}
static void destroySuite(PropertyWithValueJSONDecoderTest *suite) {
return delete suite;
}
void testDecodeSingleJSONIntAsProperty() {
doSimpleObjectDecodeTest("IntProperty", 10);
}
void testDecodeSingleJSONDoubleAsProperty() {
doSimpleObjectDecodeTest("DoubleProperty", 10.5);
}
void testDecodeSingleJSONStringAsProperty() {
doSimpleObjectDecodeTest("StringProperty", std::string("My value"));
}
void testDecodeSingleJSONBoolAsProperty() {
doSimpleObjectDecodeTest("BoolProperty", false);
}
// ----------------------- Failure tests -----------------------
void testDecodeThrowsWithEmptyValue() {
using Mantid::Kernel::decode;
Json::Value root;
TSM_ASSERT_THROWS("Expected decode to throw for empty value", decode(root),
std::invalid_argument);
}
void testDecodeThrowsWithGreaterThanOneMember() {
using Mantid::Kernel::decode;
Json::Value root;
root["one"] = 1;
root["two"] = 2;
TSM_ASSERT_THROWS("Expected decode to throw with more than 1 member",
decode(root), std::invalid_argument);
}
void testDecodeThrowsWithNonObjectValue() {
using Mantid::Kernel::decode;
TSM_ASSERT_THROWS("Expected decode to throw with non-object type",
decode(Json::Value(10)), std::invalid_argument);
}
private:
template <typename ValueType>
void doSimpleObjectDecodeTest(const std::string &propName,
const ValueType &propValue) {
Json::Value root;
root[propName] = propValue;
using Mantid::Kernel::decode;
auto property = decode(root);
TSM_ASSERT("Decode failed to create a Property. ", property);
using Mantid::Kernel::PropertyWithValue;
auto typedProperty =
dynamic_cast<PropertyWithValue<ValueType> *>(property.get());
TSM_ASSERT("Property has unexpected type ", typedProperty);
TS_ASSERT_EQUALS(propName, typedProperty->name());
TS_ASSERT_EQUALS(propValue, (*typedProperty)());
}
};
#endif // PROPERTYWITHVALUEJSONDECODERTEST_H