summaryrefslogtreecommitdiffstats
path: root/tests/ProtocolBlockTypePalette/ProtocolBlockTypePaletteTest.cpp
blob: 948fd22194644861923a14dee614832df2cbeed1 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// ProtocolBlockTypePaletteTest.cpp

#include <string>
#include <fstream>
#include <streambuf>
#include "Globals.h"

#include "Protocol/ProtocolBlockTypePalette.h"

#include "../TestHelpers.h"





static void TestSuccess(void)
{
	LOG("Test TestSuccess");
	ProtocolBlockTypePalette palette;

	auto example = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\
		\"props\": {\
			\"foo\": \"bar\"\
		}, \
		\"name\": \"b\", \
		\"id\": \"0\"\
	}]}";

	palette.clear();
	TEST_TRUE(palette.loadFromString(example));
	TEST_EQUAL(palette.index("b", BlockState({{"foo", "bar"}})), 0);
	TEST_EQUAL(palette.index("b", BlockState({{"foo", "baz"}})), ProtocolBlockTypePalette::NOT_FOUND);
	TEST_EQUAL(palette.index("a", BlockState({{"foo", "bar"}})), ProtocolBlockTypePalette::NOT_FOUND);
}





static void TestErrors(void)
{
	LOG("Test TestErrors");
	ProtocolBlockTypePalette palette;
	TEST_FALSE(palette.loadFromString(""));

	palette.clear();
	TEST_FALSE(palette.loadFromString("[]"));

	palette.clear();
	TEST_FALSE(palette.loadFromString("a = {}"));

	palette.clear();
	TEST_FALSE(palette.loadFromString("{x = 1}"));  // Lua style

	palette.clear();
	TEST_FALSE(palette.loadFromString("$#^%&"));
}





static void TestComplex1(void)
{
	LOG("Test TestComplex1");
	ProtocolBlockTypePalette palette;
	auto str = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\
		\"props\": {\
			\"foo\": \"bar\", \
			\"moo\": \"baz\"\
		}, \
		\"id\": \"0\", \
		\"name\": \"b\"\
	}, {\
		\"props\": {\
			\"foo\": \"baz\", \
			\"moo\": \"bar\"\
		}, \
		\"id\": \"1\", \
		\"name\": \"b\"\
	}, {\
		\"props\": {\
			\"foo\": \"baz\", \
			\"moo\": \"bar\"\
		}, \
		\"id\": \"1001\", \
		\"name\": \"b\"\
	}]}";
	TEST_TRUE(palette.loadFromString(str));  // This should print info message about duplicate ID
	TEST_EQUAL(palette.index("b", BlockState({{"foo","bar"}})), ProtocolBlockTypePalette::NOT_FOUND);
	TEST_EQUAL(palette.index("b", BlockState({{"foo","bar"}, {"moo","baz"}})), 0);
	TEST_EQUAL(palette.index("b", BlockState({{"foo","baz"}, {"moo","bar"}})), 1);
	TEST_EQUAL(palette.index("c", BlockState({{"foo","baz"}, {"moo","bar"}})), ProtocolBlockTypePalette::NOT_FOUND);
}





static void TestComplex2(void)
{
	LOG("Test TestComplex2");
	ProtocolBlockTypePalette palette;
	auto str = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\
		\"id\": \"0\", \
		\"name\": \"a\"\
	}, {\
		\"id\": \"1\", \
		\"name\": \"b\"\
	}]}";
	TEST_TRUE(palette.loadFromString(str));
	TEST_EQUAL(palette.index("a", BlockState()), 0);
	TEST_EQUAL(palette.index("b", BlockState({})), 1);
}





static void TestFile(void)
{
	LOG("Test TestFile");
	std::ifstream f("base.btp.json");
	ProtocolBlockTypePalette palette;
	TEST_TRUE(palette.loadFromStream(f));

	// This is a bit problematic - the only permanently fixed block Id is air...
	TEST_EQUAL(palette.index("minecraft:air", BlockState()), 0);
	TEST_NOTEQUAL(palette.index("minecraft:stone", BlockState()), ProtocolBlockTypePalette::NOT_FOUND);
	TEST_NOTEQUAL(palette.index("minecraft:dirt", BlockState()), ProtocolBlockTypePalette::NOT_FOUND);
}





static void TestFile2(void)
{
	LOG("Test TestFile2");
	std::ifstream f("test.btp.json");
	ProtocolBlockTypePalette palette;
	TEST_TRUE(palette.loadFromStream(f));

	TEST_EQUAL(palette.index("minecraft:air", BlockState({})), 0);
	TEST_EQUAL(palette.index("minecraft:stone", BlockState()), 1);
	TEST_EQUAL(palette.index("minecraft:stone", BlockState()), 1);
	TEST_EQUAL(palette.index(
		"minecraft:dark_oak_leaves",
		BlockState({{"persistent", "false"}, {"distance", "6"}})
	), 225);
	TEST_EQUAL(palette.index(
		"minecraft:dark_oak_leaves",
		BlockState({{"persistent", "false"}})
	), ProtocolBlockTypePalette::NOT_FOUND);
}





IMPLEMENT_TEST_MAIN("ProtocolBlockTypePaletteTest",
	TestSuccess();
	TestErrors();
	TestComplex1();
	TestComplex2();
	TestFile();
	TestFile2();
)