forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicFactoryTest.h
179 lines (144 loc) · 6.86 KB
/
DynamicFactoryTest.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
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
// 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/DynamicFactory.h"
#include <cxxtest/TestSuite.h>
#include <Poco/NObserver.h>
#include <memory>
#include <string>
#include <vector>
using namespace Mantid::Kernel;
// Helper class
class IntFactory : public DynamicFactory<int> {};
// Helper class
class CaseSensitiveIntFactory : public DynamicFactory<int, CaseSensitiveStringComparator> {};
class DynamicFactoryTest : public CxxTest::TestSuite {
using int_ptr = std::shared_ptr<int>;
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static DynamicFactoryTest *createSuite() { return new DynamicFactoryTest(); }
static void destroySuite(DynamicFactoryTest *suite) { delete suite; }
DynamicFactoryTest()
: CxxTest::TestSuite(), factory(), m_notificationObserver(*this, &DynamicFactoryTest::handleFactoryUpdate),
m_updateNoticeReceived(false) {
factory.notificationCenter.addObserver(m_notificationObserver);
}
~DynamicFactoryTest() override { factory.notificationCenter.removeObserver(m_notificationObserver); }
void testCreate() {
TS_ASSERT_THROWS(factory.create("testEntry"), const std::runtime_error &)
factory.subscribe<int>("testEntry");
TS_ASSERT_THROWS_NOTHING(int_ptr i = factory.create("testEntry"));
TS_ASSERT_THROWS_NOTHING(int_ptr i = factory.create("TESTENTRY"));
factory.unsubscribe("testEntry");
}
void testCreateCaseSensitive() {
TS_ASSERT_THROWS(caseSensitiveFactory.create("testEntryCaseSensitive"), const std::runtime_error &)
caseSensitiveFactory.subscribe<int>("testEntryCaseSensitive");
TS_ASSERT_THROWS(int_ptr i = caseSensitiveFactory.create("testEntryCaseSENSITIVE"),
const std::runtime_error &); // case error on a case sensitive dynamic factory
TS_ASSERT_THROWS_NOTHING(int_ptr i2 = caseSensitiveFactory.create("testEntryCaseSensitive"));
caseSensitiveFactory.unsubscribe("testEntryCaseSensitive");
}
void testCreateUnwrapped() {
TS_ASSERT_THROWS(factory.createUnwrapped("testUnrappedEntry"), const std::runtime_error &)
factory.subscribe<int>("testUnwrappedEntry");
int *i = nullptr;
TS_ASSERT_THROWS_NOTHING(i = factory.createUnwrapped("testUnwrappedEntry"));
delete i;
int *j = nullptr;
TS_ASSERT_THROWS_NOTHING(j = factory.createUnwrapped("TESTUnwrappedEntry"));
delete j;
factory.unsubscribe("testUnwrappedEntry");
}
void testCreateUnwrappedCaseSensitive() {
TS_ASSERT_THROWS(caseSensitiveFactory.create("testUnrappedEntryCaseSensitive"), const std::runtime_error &)
caseSensitiveFactory.subscribe<int>("testUnrappedEntryCaseSensitive");
int *i = nullptr;
TS_ASSERT_THROWS(i = caseSensitiveFactory.createUnwrapped("testUnrappedentrycaseSENSITIVE"),
const std::runtime_error &); // case error on a case sensitive dynamic factory
TS_ASSERT_THROWS_NOTHING(i = caseSensitiveFactory.createUnwrapped("testUnrappedEntryCaseSensitive"));
delete i;
caseSensitiveFactory.unsubscribe("testUnrappedEntryCaseSensitive");
}
void testSubscribeWithEmptyNameThrowsInvalidArgument() {
TS_ASSERT_THROWS(factory.subscribe<int>(""), const std::invalid_argument &);
}
void testSubscribeWithReplaceEqualsErrorIfExistsThrowsRegisteringMatchingClass() {
TS_ASSERT_THROWS_NOTHING(factory.subscribe("int", std::make_unique<Instantiator<int, int>>()));
TS_ASSERT_THROWS(factory.subscribe("int", std::make_unique<Instantiator<int, int>>(), IntFactory::ErrorIfExists),
const std::runtime_error &);
factory.unsubscribe("int");
}
void testSubscribeWithReplaceEqualsOverwriteCurrentReplacesMatchingClass() {
TS_ASSERT_THROWS_NOTHING(factory.subscribe("int", std::make_unique<Instantiator<int, int>>()));
TS_ASSERT_THROWS_NOTHING(
factory.subscribe("int", std::make_unique<Instantiator<int, int>>(), IntFactory::OverwriteCurrent));
factory.unsubscribe("int");
}
void testSubscribeByDefaultDoesNotNotify() {
m_updateNoticeReceived = false;
TS_ASSERT_THROWS_NOTHING(factory.subscribe<int>("int"));
TS_ASSERT_EQUALS(m_updateNoticeReceived, false)
factory.unsubscribe("int");
}
void testSubscribeNotifiesIfTheyAreSwitchedOn() {
m_updateNoticeReceived = false;
factory.enableNotifications();
TS_ASSERT_THROWS_NOTHING(factory.subscribe<int>("intWithNotice"));
TS_ASSERT_EQUALS(m_updateNoticeReceived, true)
factory.disableNotifications();
TS_ASSERT_THROWS_NOTHING(factory.unsubscribe("intWithNotice"));
}
void testUnsubscribeByDefaultDoesNotNotify() {
TS_ASSERT_THROWS(factory.unsubscribe("tester"), const std::runtime_error &);
factory.subscribe<int>("tester");
m_updateNoticeReceived = false;
TS_ASSERT_THROWS_NOTHING(factory.unsubscribe("tester"));
TS_ASSERT_EQUALS(m_updateNoticeReceived, false)
}
void testUnsubscribeNotifiesIfTheyAreSwitchedOn() {
TS_ASSERT_THROWS_NOTHING(factory.subscribe<int>("intWithNotice"));
factory.enableNotifications();
m_updateNoticeReceived = false;
TS_ASSERT_THROWS_NOTHING(factory.unsubscribe("intWithNotice"));
TS_ASSERT_EQUALS(m_updateNoticeReceived, true)
factory.disableNotifications();
m_updateNoticeReceived = false;
}
void testExists() {
TS_ASSERT(!factory.exists("testing"));
factory.subscribe<int>("testing");
TS_ASSERT(factory.exists("testing"));
TS_ASSERT(factory.exists("TESTING"));
}
void testGetKeys() {
std::string testKey = "testGetKeys";
// check it is not already present
TS_ASSERT_THROWS(factory.create(testKey), const std::runtime_error &)
factory.subscribe<int>(testKey);
std::vector<std::string> keys = factory.getKeys();
TSM_ASSERT("Could not find the test key in the returned vector.",
std::find(keys.begin(), keys.end(), testKey) != keys.end()) // check the case is correct
TS_ASSERT(!keys.empty());
factory.unsubscribe(testKey);
}
void testGetKeysRetainsCase() {
std::string testKey = "testGetKeysRetainsCase";
// check it is not already present
TS_ASSERT_THROWS(factory.create(testKey), const std::runtime_error &)
factory.subscribe<int>(testKey);
std::vector<std::string> keys = factory.getKeys();
factory.unsubscribe(testKey);
}
private:
void handleFactoryUpdate(const Poco::AutoPtr<IntFactory::UpdateNotification> &) { m_updateNoticeReceived = true; }
IntFactory factory;
CaseSensitiveIntFactory caseSensitiveFactory;
Poco::NObserver<DynamicFactoryTest, IntFactory::UpdateNotification> m_notificationObserver;
bool m_updateNoticeReceived;
};