Skip to content

Commit dfff9f7

Browse files
committed
More tests
1 parent ff4d3de commit dfff9f7

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

modules/yup_core/containers/yup_PropertySet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class YUP_API PropertySet
176176
/** Returns the keys/value pair array containing all the properties. */
177177
StringPairArray& getAllProperties() noexcept { return properties; }
178178

179+
/** Returns the keys/value pair array containing all the properties. */
180+
const StringPairArray& getAllProperties() const noexcept { return properties; }
181+
179182
/** Returns the lock used when reading or writing to this set */
180183
const CriticalSection& getLock() const noexcept { return lock; }
181184

tests/yup_core/yup_PropertySet.cpp

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,32 @@
2323

2424
#include <yup_core/yup_core.h>
2525

26+
namespace
27+
{
28+
29+
struct InvocablePropertySet : yup::PropertySet
30+
{
31+
InvocablePropertySet (std::function<void()> callback)
32+
: callback (callback)
33+
{
34+
}
35+
36+
void propertyChanged() override
37+
{
38+
callback();
39+
}
40+
41+
private:
42+
std::function<void()> callback;
43+
};
44+
45+
} // namespace
46+
2647
class PropertySetTests : public ::testing::Test
2748
{
2849
protected:
2950
yup::PropertySet propertySet;
51+
yup::PropertySet propertySetInsensitive { true };
3052

3153
void SetUp() override
3254
{
@@ -35,18 +57,59 @@ class PropertySetTests : public ::testing::Test
3557
propertySet.setValue ("doubleKey", 45.67);
3658
propertySet.setValue ("boolKey", true);
3759

60+
propertySetInsensitive.setValue ("stringKey", "stringValue");
61+
propertySetInsensitive.setValue ("intKey", 123);
62+
propertySetInsensitive.setValue ("doubleKey", 45.67);
63+
propertySetInsensitive.setValue ("boolKey", true);
64+
3865
auto xml = std::make_unique<yup::XmlElement> ("root");
3966
xml->setAttribute ("attribute", "value");
4067
propertySet.setValue ("xmlKey", xml.get());
4168
}
4269
};
4370

71+
TEST_F (PropertySetTests, Empty)
72+
{
73+
yup::PropertySet emptyPropertySet (false);
74+
EXPECT_EQ (emptyPropertySet.getAllProperties().size(), 0);
75+
76+
yup::PropertySet emptyPropertySet2 (true);
77+
EXPECT_EQ (emptyPropertySet2.getAllProperties().size(), 0);
78+
}
79+
80+
TEST_F (PropertySetTests, Copy)
81+
{
82+
yup::PropertySet anotherPropertySet (propertySet);
83+
EXPECT_EQ (anotherPropertySet.getAllProperties(), propertySet.getAllProperties());
84+
85+
yup::PropertySet anotherPropertySet2;
86+
anotherPropertySet2 = anotherPropertySet;
87+
EXPECT_EQ (anotherPropertySet2.getAllProperties(), anotherPropertySet.getAllProperties());
88+
}
89+
90+
TEST_F (PropertySetTests, Move)
91+
{
92+
auto allProperties = propertySet.getAllProperties();
93+
94+
yup::PropertySet anotherPropertySet (std::move (propertySet));
95+
EXPECT_EQ (anotherPropertySet.getAllProperties(), allProperties);
96+
97+
yup::PropertySet anotherPropertySet2;
98+
anotherPropertySet2 = std::move (anotherPropertySet);
99+
EXPECT_EQ (anotherPropertySet2.getAllProperties(), allProperties);
100+
}
101+
44102
TEST_F (PropertySetTests, GetValue)
45103
{
46104
EXPECT_EQ (propertySet.getValue ("stringKey"), "stringValue");
47105
EXPECT_EQ (propertySet.getIntValue ("intKey"), 123);
48106
EXPECT_DOUBLE_EQ (propertySet.getDoubleValue ("doubleKey"), 45.67);
49107
EXPECT_TRUE (propertySet.getBoolValue ("boolKey"));
108+
109+
EXPECT_EQ (propertySetInsensitive.getValue ("stringkey"), "stringValue");
110+
EXPECT_EQ (propertySetInsensitive.getIntValue ("intkey"), 123);
111+
EXPECT_DOUBLE_EQ (propertySetInsensitive.getDoubleValue ("doublekey"), 45.67);
112+
EXPECT_TRUE (propertySetInsensitive.getBoolValue ("boolkey"));
50113
}
51114

52115
TEST_F (PropertySetTests, GetFallbackValue)
@@ -76,6 +139,8 @@ TEST_F (PropertySetTests, NonExistingKey)
76139
{
77140
EXPECT_EQ (propertySet.getValue ("nonExistingKey", "default"), "default");
78141
EXPECT_EQ (propertySet.getIntValue ("nonExistingKey", 0), 0);
142+
EXPECT_DOUBLE_EQ (propertySet.getDoubleValue ("nonExistingKey", 45.67), 45.67);
143+
EXPECT_TRUE (propertySet.getBoolValue ("nonExistingKey", true));
79144
}
80145

81146
TEST_F (PropertySetTests, RemoveAndClearValues)
@@ -92,14 +157,21 @@ TEST_F (PropertySetTests, RemoveAndClearValues)
92157
EXPECT_FALSE (propertySet.containsKey ("boolKey"));
93158
}
94159

95-
TEST_F (PropertySetTests, CopyAndAssignment)
160+
TEST_F (PropertySetTests, AddAllPropertiesFrom)
96161
{
97-
yup::PropertySet anotherPropertySet (propertySet);
98-
EXPECT_EQ (anotherPropertySet.getValue ("stringKey"), "stringValue");
162+
yup::PropertySet anotherPropertySet;
163+
anotherPropertySet.setValue ("stringKey", "stringValue2");
164+
anotherPropertySet.setValue ("intKey", 456);
165+
anotherPropertySet.setValue ("double2Key", 45.67);
166+
anotherPropertySet.setValue ("bool2Key", true);
167+
anotherPropertySet.addAllPropertiesFrom (propertySet);
99168

100-
yup::PropertySet yetAnotherPropertySet;
101-
yetAnotherPropertySet = propertySet;
102-
EXPECT_EQ (yetAnotherPropertySet.getValue ("stringKey"), "stringValue");
169+
EXPECT_EQ (anotherPropertySet.getValue ("stringKey"), "stringValue");
170+
EXPECT_EQ (anotherPropertySet.getIntValue ("intKey"), 123);
171+
EXPECT_DOUBLE_EQ (anotherPropertySet.getDoubleValue ("doubleKey"), 45.67);
172+
EXPECT_TRUE (anotherPropertySet.getBoolValue ("boolKey"));
173+
EXPECT_DOUBLE_EQ (anotherPropertySet.getDoubleValue ("double2Key"), 45.67);
174+
EXPECT_TRUE (anotherPropertySet.getBoolValue ("bool2Key"));
103175
}
104176

105177
TEST_F (PropertySetTests, CreateAndRestoreXml)
@@ -112,3 +184,17 @@ TEST_F (PropertySetTests, CreateAndRestoreXml)
112184
restoredSet.restoreFromXml (*xml);
113185
EXPECT_EQ (restoredSet.getValue ("stringKey"), "stringValue");
114186
}
187+
188+
TEST_F (PropertySetTests, PropertyChanged)
189+
{
190+
bool changed = false;
191+
192+
InvocablePropertySet anotherPropertySet ([&changed]
193+
{
194+
changed = true;
195+
});
196+
197+
EXPECT_FALSE (changed);
198+
anotherPropertySet.setValue ("abc", 1);
199+
EXPECT_TRUE (changed);
200+
}

0 commit comments

Comments
 (0)