From 7453a9fbe120f345625a24bbea18c35cd3c26a6a Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 2 Dec 2019 16:45:55 +0100 Subject: Moved ProtocolBlockTypePalette functionality into BlockTypePalette. --- tests/BlockTypeRegistry/BlockTypePaletteTest.cpp | 148 +++++++++++++++++++++++ tests/BlockTypeRegistry/CMakeLists.txt | 12 +- tests/BlockTypeRegistry/test.btp.json | 146 ++++++++++++++++++++++ 3 files changed, 304 insertions(+), 2 deletions(-) create mode 100644 tests/BlockTypeRegistry/test.btp.json (limited to 'tests/BlockTypeRegistry') diff --git a/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp b/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp index 1337c7dc3..dddf80348 100644 --- a/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp +++ b/tests/BlockTypeRegistry/BlockTypePaletteTest.cpp @@ -123,10 +123,158 @@ static void testTransformWithFallback() +/** Tests that loading a simple JSON palette succeeds. */ +static void testLoadSimpleSuccess(void) +{ + LOG("Testing loading a simple JSON palette"); + + BlockTypePalette palette; + + auto example = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ + \"props\": {\ + \"foo\": \"bar\"\ + }, \ + \"name\": \"b\", \ + \"id\": \"0\"\ + }]}"; + + palette.loadFromString(example); + TEST_EQUAL(palette.maybeIndex("b", BlockState({{"foo", "bar"}})), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("b", BlockState({{"foo", "baz"}})), (std::make_pair(0, false))); + TEST_EQUAL(palette.maybeIndex("a", BlockState({{"foo", "bar"}})), (std::make_pair(0, false))); +} + + + + + +static void testLoadErrors(void) +{ + LOG("Testing palette load error reporting."); + + BlockTypePalette palette; + TEST_THROWS(palette.loadFromString(""), BlockTypePalette::LoadFailedException); + TEST_THROWS(palette.loadFromString("[]"), BlockTypePalette::LoadFailedException); + TEST_THROWS(palette.loadFromString("a = {}"), BlockTypePalette::LoadFailedException); + TEST_THROWS(palette.loadFromString("{x = 1}"), BlockTypePalette::LoadFailedException); // Lua style + TEST_THROWS(palette.loadFromString("$#^%&"), BlockTypePalette::LoadFailedException); +} + + + + + +static void testLoadComplex1(void) +{ + LOG("Testing loading a complex palette (1)"); + BlockTypePalette 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\"\ + }]}"; + // Note: The palette has a duplicate entry with differrent IDs, the latter ID wins + palette.loadFromString(str); + TEST_EQUAL(palette.maybeIndex("b", {{"foo", "bar"}}).second, false); + TEST_EQUAL(palette.maybeIndex("b", {{"foo", "bar"}, {"moo", "baz"}}), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("b", {{"foo", "baz"}, {"moo", "bar"}}), (std::make_pair(1001, true))); + TEST_EQUAL(palette.maybeIndex("c", {{"foo", "baz"}, {"moo", "bar"}}).second, false); +} + + + + + +static void testLoadComplex2(void) +{ + LOG("Testing loading a complex palette (2)"); + BlockTypePalette palette; + auto str = "{\"Metadata\":{\"ProtocolBlockTypePaletteVersion\":1}, \"Palette\":[{\ + \"id\": \"0\", \ + \"name\": \"a\"\ + }, {\ + \"id\": \"1\", \ + \"name\": \"b\"\ + }]}"; + palette.loadFromString(str); + TEST_EQUAL(palette.maybeIndex("a", BlockState()), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("b", BlockState()), (std::make_pair(1, true))); +} + + + + + +static void testLoadFromFile1(void) +{ + LOG("Testing loading a palette from file \"test.btp.json\""); + BlockTypePalette palette; + palette.loadFromString(cFile::ReadWholeFile("test.btp.json")); + + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair(1, true))); + TEST_EQUAL( + palette.maybeIndex( + "minecraft:dark_oak_leaves", + BlockState({{"persistent", "false"}, {"distance", "6"}}) + ), + (std::make_pair(225, true)) + ); + TEST_EQUAL( + palette.maybeIndex( + "minecraft:dark_oak_leaves", + BlockState({{"persistent", "false"}}) + ).second, + false + ); +} + + + + + +static void testLoadFromFile2(void) +{ + LOG("Testing loading a palette from file \"base.btp.json\" (version 1.13)"); + BlockTypePalette palette; + palette.loadFromString(cFile::ReadWholeFile("base.btp.json")); + + TEST_EQUAL(palette.maybeIndex("minecraft:air", BlockState()), (std::make_pair(0, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:stone", BlockState()), (std::make_pair(1, true))); + TEST_EQUAL(palette.maybeIndex("minecraft:dirt", BlockState()), (std::make_pair(10, true))); +} + + + + + IMPLEMENT_TEST_MAIN("BlockTypePalette", testBasic(); testTransformAddMissing(); testTransformWithFallback(); + testLoadSimpleSuccess(); + testLoadErrors(); + testLoadComplex1(); + testLoadComplex2(); + testLoadFromFile1(); + testLoadFromFile2(); ) diff --git a/tests/BlockTypeRegistry/CMakeLists.txt b/tests/BlockTypeRegistry/CMakeLists.txt index 379158d5c..beadc8af3 100644 --- a/tests/BlockTypeRegistry/CMakeLists.txt +++ b/tests/BlockTypeRegistry/CMakeLists.txt @@ -28,8 +28,9 @@ add_executable(BlockTypePaletteTest ${CMAKE_SOURCE_DIR}/src/BlockTypePalette.cpp ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp ${CMAKE_SOURCE_DIR}/src/OSSupport/CriticalSection.cpp + ${CMAKE_SOURCE_DIR}/src/OSSupport/File.cpp ) -target_link_libraries(BlockTypePaletteTest fmt::fmt) +target_link_libraries(BlockTypePaletteTest fmt::fmt jsoncpp_lib_static) # BlockTypeRegistryTest: Verify that the BlockTypeRegistry class works as intended: add_executable(BlockTypeRegistryTest @@ -53,7 +54,14 @@ add_executable(PalettedBlockAreaTest ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp ${CMAKE_SOURCE_DIR}/src/OSSupport/CriticalSection.cpp ) -target_link_libraries(PalettedBlockAreaTest fmt::fmt) +target_link_libraries(PalettedBlockAreaTest fmt::fmt jsoncpp_lib_static) + +# Extra files for BlockTypePalette test: +file (COPY + test.btp.json + ../../Server/Protocol/1.13/base.btp.json + DESTINATION ./ +) diff --git a/tests/BlockTypeRegistry/test.btp.json b/tests/BlockTypeRegistry/test.btp.json new file mode 100644 index 000000000..264e56185 --- /dev/null +++ b/tests/BlockTypeRegistry/test.btp.json @@ -0,0 +1,146 @@ +{ + "Metadata": { + "ProtocolBlockTypePaletteVersion": 1 + }, + "Palette": [{ + "id": 0, + "name": "minecraft:air" + }, { + "id": 1, + "name": "minecraft:stone" + }, { + "id": 221, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "4" + } + }, { + "id": 222, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "true", + "distance": "5" + } + }, { + "id": 223, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "5" + } + }, { + "id": 224, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "true", + "distance": "6" + } + }, { + "id": 225, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "6" + } + }, { + "id": 226, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "true", + "distance": "7" + } + }, { + "id": 227, + "name": "minecraft:dark_oak_leaves", + "props": { + "persistent": "false", + "distance": "7" + } + }, { + "id": 9988, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "north_south" + } + }, { + "id": 9989, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "east_west" + } + }, { + "id": 9990, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_east" + } + }, { + "id": 9991, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_west" + } + }, { + "id": 9992, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_north" + } + }, { + "id": 9993, + "name": "minecraft:powered_rail", + "props": { + "powered": "true", + "shape": "ascending_south" + } + }, { + "id": 9994, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "north_south" + } + }, { + "id": 9995, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "east_west" + } + }, { + "id": 9996, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_east" + } + }, { + "id": 9997, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_west" + } + }, { + "id": 9998, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_north" + } + }, { + "id": 9999, + "name": "minecraft:powered_rail", + "props": { + "powered": "false", + "shape": "ascending_south" + } + } + ] +} -- cgit v1.2.3