summaryrefslogtreecommitdiffstats
path: root/tests/BlockTypeRegistry/BlockStateTest.cpp
blob: d3ce92455eb7ee470a5f9921959cd05349663da3 (plain) (blame)
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
#include "Globals.h"
#include "BlockState.h"
#include "../TestHelpers.h"




/** Tests the class constructors with static (hard-coded) data. */
static void testStaticCreation()
{
	LOGD("Testing BlockState creation from static data...");

	// Create a few BlockStates using the static-data constructors:
	BlockState bs1v1;
	BlockState bs2v1("property", "value");
	BlockState bs3v1({{"property1", "value1"}, {"property2", "value2"}});
	BlockState bs1v2(bs1v1);
	BlockState bs2v2(bs2v1);
	BlockState bs3v2(bs3v1);
	BlockState bs1v3(bs1v2, {{"added property", "value1"}});
	BlockState bs2v3(bs2v2, {{"added property", "value2"}});
	BlockState bs3v3(bs3v2, {{"added property", "value3"}});

	// Test (in-)equality (v1 = v2 != v3):
	TEST_EQUAL(bs1v1, bs1v2);
	TEST_EQUAL(bs2v1, bs2v2);
	TEST_EQUAL(bs3v1, bs3v2);
	TEST_NOTEQUAL(bs1v1, bs1v3);
	TEST_NOTEQUAL(bs2v1, bs2v3);
	TEST_NOTEQUAL(bs3v1, bs3v3);
	TEST_NOTEQUAL(bs1v2, bs1v3);
	TEST_NOTEQUAL(bs2v2, bs2v3);
	TEST_NOTEQUAL(bs3v2, bs3v3);

	// Test that the values are actually stored:
	TEST_EQUAL(bs1v1.value("property"), "");
	TEST_EQUAL(bs2v1.value("property"), "value");
	TEST_EQUAL(bs2v1.value("property1"), "");
	TEST_EQUAL(bs3v1.value("property1"), "value1");
	TEST_EQUAL(bs3v1.value("property2"), "value2");
	TEST_EQUAL(bs1v3.value("added property"), "value1");
	TEST_EQUAL(bs2v3.value("added property"), "value2");
	TEST_EQUAL(bs3v3.value("added property"), "value3");
}





/** Tests the dynamic-data constructors (map param, deep-copy). */
static void testDynamicCreation()
{
	LOGD("Testing BlockState creation from dynamic data...");

	using Map = std::map<AString, AString>;

	// Construct from scratch:
	{
		BlockState bs1a({{"property", "value"}});
		Map map1({{"property", "value"}});
		BlockState bs1b(map1);
		TEST_EQUAL(bs1a, bs1b);  // Creation works
		map1.clear();
		TEST_EQUAL(bs1a, bs1b);  // Created a copy independent of map1
	}

	// Construct by moving:
	{
		BlockState bs2a({{"property", "value"}});
		Map map2({{"property", "value"}});
		BlockState bs2b(std::move(map2));
		TEST_EQUAL(bs2a, bs2b);  // Creation works
	}

	// Construct by modifying:
	{
		BlockState bsSrc("property1", "value1");
		BlockState bs3a(bsSrc, {{"property2", "value2"}});
		Map map3({{"property2", "value2"}});
		BlockState bs3b(bsSrc, map3);
		TEST_EQUAL(bs3a, bs3b);
		map3.clear();
		TEST_EQUAL(bs3a, bs3b);
	}
}





/** Tests replacing the properties in the copy-and-modify constructors. */
static void testReplacing()
{
	LOGD("Testing replacing / removing properties in BlockState copies...");

	// Test replacing:
	BlockState bs1("property1", "value1v1");
	BlockState bs2(bs1, {{"property1", "value1v2"}});
	TEST_EQUAL(bs2.value("property1"), "value1v2");  // Stored the new one
	TEST_EQUAL(bs1.value("property1"), "value1v1");  // Didn't replace in the original

	// Test removing:
	BlockState bs3(bs1, {{"property1", ""}});
	BlockState bsEmpty;
	TEST_EQUAL(bs3, bsEmpty);
}





int main()
{
	LOGD("BlockStateTest started");

	try
	{
		testStaticCreation();
		testDynamicCreation();
		testReplacing();
	}
	catch (const TestException & exc)
	{
		LOGERROR("BlockStateTest has failed explicitly: %s", exc.mMessage.c_str());
		return 1;
	}
	catch (const std::runtime_error & exc)
	{
		LOGERROR("BlockStateTest has failed, an unhandled exception was thrown: %s", exc.what());
		return 1;
	}
	catch (...)
	{
		LOGERROR("BlockStateTest has failed, an unknown exception was thrown.");
		return 1;
	}

	LOGD("BlockStateTest finished");

	return 0;
}