diff options
Diffstat (limited to 'tests/core/io')
-rw-r--r-- | tests/core/io/test_config_file.h | 2 | ||||
-rw-r--r-- | tests/core/io/test_image.h | 10 | ||||
-rw-r--r-- | tests/core/io/test_json.h | 2 | ||||
-rw-r--r-- | tests/core/io/test_pck_packer.h | 16 | ||||
-rw-r--r-- | tests/core/io/test_resource.h | 4 | ||||
-rw-r--r-- | tests/core/io/test_xml_parser.h | 164 |
6 files changed, 181 insertions, 17 deletions
diff --git a/tests/core/io/test_config_file.h b/tests/core/io/test_config_file.h index 355aca479e..666719febb 100644 --- a/tests/core/io/test_config_file.h +++ b/tests/core/io/test_config_file.h @@ -127,7 +127,7 @@ TEST_CASE("[ConfigFile] Saving file") { config_file.set_value("quoted", "a=b", 7); #ifdef WINDOWS_ENABLED - const String config_path = OS::get_singleton()->get_environment("TEMP").plus_file("config.ini"); + const String config_path = OS::get_singleton()->get_environment("TEMP").path_join("config.ini"); #else const String config_path = "/tmp/config.ini"; #endif diff --git a/tests/core/io/test_image.h b/tests/core/io/test_image.h index 36e6b83bfd..1559c59b5c 100644 --- a/tests/core/io/test_image.h +++ b/tests/core/io/test_image.h @@ -78,8 +78,8 @@ TEST_CASE("[Image] Instantiation") { TEST_CASE("[Image] Saving and loading") { Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8)); - const String save_path_png = OS::get_singleton()->get_cache_path().plus_file("image.png"); - const String save_path_exr = OS::get_singleton()->get_cache_path().plus_file("image.exr"); + const String save_path_png = OS::get_singleton()->get_cache_path().path_join("image.png"); + const String save_path_exr = OS::get_singleton()->get_cache_path().path_join("image.exr"); // Save PNG Error err; @@ -124,7 +124,7 @@ TEST_CASE("[Image] Saving and loading") { image_jpg->load_jpg_from_buffer(data_jpg) == OK, "The JPG image should load successfully."); - // Load WEBP + // Load WebP Ref<Image> image_webp = memnew(Image()); Ref<FileAccess> f_webp = FileAccess::open(TestUtils::get_data_path("images/icon.webp"), FileAccess::READ, &err); PackedByteArray data_webp; @@ -132,7 +132,7 @@ TEST_CASE("[Image] Saving and loading") { f_webp->get_buffer(data_webp.ptrw(), f_webp->get_length()); CHECK_MESSAGE( image_webp->load_webp_from_buffer(data_webp) == OK, - "The WEBP image should load successfully."); + "The WebP image should load successfully."); // Load PNG Ref<Image> image_png = memnew(Image()); @@ -162,7 +162,7 @@ TEST_CASE("[Image] Basic getters") { CHECK(image->get_size() == Vector2(8, 4)); CHECK(image->get_format() == Image::FORMAT_LA8); CHECK(image->get_used_rect() == Rect2i(0, 0, 0, 0)); - Ref<Image> image_get_rect = image->get_rect(Rect2i(0, 0, 2, 1)); + Ref<Image> image_get_rect = image->get_region(Rect2i(0, 0, 2, 1)); CHECK(image_get_rect->get_size() == Vector2(2, 1)); } diff --git a/tests/core/io/test_json.h b/tests/core/io/test_json.h index 478cf1766e..af450da3b8 100644 --- a/tests/core/io/test_json.h +++ b/tests/core/io/test_json.h @@ -83,7 +83,7 @@ TEST_CASE("[JSON] Parsing single data types") { json.get_error_line() == 0, "Parsing a floating-point number as JSON should parse successfully."); CHECK_MESSAGE( - Math::is_equal_approx(double(json.get_data()), 0.123456), + double(json.get_data()) == doctest::Approx(0.123456), "Parsing a floating-point number as JSON should return the expected value."); json.parse("\"hello\""); diff --git a/tests/core/io/test_pck_packer.h b/tests/core/io/test_pck_packer.h index d21fbdaf50..8d0e5c402a 100644 --- a/tests/core/io/test_pck_packer.h +++ b/tests/core/io/test_pck_packer.h @@ -42,7 +42,7 @@ namespace TestPCKPacker { TEST_CASE("[PCKPacker] Pack an empty PCK file") { PCKPacker pck_packer; - const String output_pck_path = OS::get_singleton()->get_cache_path().plus_file("output_empty.pck"); + const String output_pck_path = OS::get_singleton()->get_cache_path().path_join("output_empty.pck"); CHECK_MESSAGE( pck_packer.pck_start(output_pck_path) == OK, "Starting a PCK file should return an OK error code."); @@ -66,7 +66,7 @@ TEST_CASE("[PCKPacker] Pack an empty PCK file") { TEST_CASE("[PCKPacker] Pack empty with zero alignment invalid") { PCKPacker pck_packer; - const String output_pck_path = OS::get_singleton()->get_cache_path().plus_file("output_empty.pck"); + const String output_pck_path = OS::get_singleton()->get_cache_path().path_join("output_empty.pck"); ERR_PRINT_OFF; CHECK_MESSAGE(pck_packer.pck_start(output_pck_path, 0) != OK, "PCK with zero alignment should fail."); ERR_PRINT_ON; @@ -74,7 +74,7 @@ TEST_CASE("[PCKPacker] Pack empty with zero alignment invalid") { TEST_CASE("[PCKPacker] Pack empty with invalid key") { PCKPacker pck_packer; - const String output_pck_path = OS::get_singleton()->get_cache_path().plus_file("output_empty.pck"); + const String output_pck_path = OS::get_singleton()->get_cache_path().path_join("output_empty.pck"); ERR_PRINT_OFF; CHECK_MESSAGE(pck_packer.pck_start(output_pck_path, 32, "") != OK, "PCK with invalid key should fail."); ERR_PRINT_ON; @@ -82,7 +82,7 @@ TEST_CASE("[PCKPacker] Pack empty with invalid key") { TEST_CASE("[PCKPacker] Pack a PCK file with some files and directories") { PCKPacker pck_packer; - const String output_pck_path = OS::get_singleton()->get_cache_path().plus_file("output_with_files.pck"); + const String output_pck_path = OS::get_singleton()->get_cache_path().path_join("output_with_files.pck"); CHECK_MESSAGE( pck_packer.pck_start(output_pck_path) == OK, "Starting a PCK file should return an OK error code."); @@ -90,16 +90,16 @@ TEST_CASE("[PCKPacker] Pack a PCK file with some files and directories") { const String base_dir = OS::get_singleton()->get_executable_path().get_base_dir(); CHECK_MESSAGE( - pck_packer.add_file("version.py", base_dir.plus_file("../version.py"), "version.py") == OK, + pck_packer.add_file("version.py", base_dir.path_join("../version.py"), "version.py") == OK, "Adding a file to the PCK should return an OK error code."); CHECK_MESSAGE( - pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.plus_file("../icon.png")) == OK, + pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.path_join("../icon.png")) == OK, "Adding a file to a new subdirectory in the PCK should return an OK error code."); CHECK_MESSAGE( - pck_packer.add_file("some/directories with spaces/to/create/icon.svg", base_dir.plus_file("../icon.svg")) == OK, + pck_packer.add_file("some/directories with spaces/to/create/icon.svg", base_dir.path_join("../icon.svg")) == OK, "Adding a file to an existing subdirectory in the PCK should return an OK error code."); CHECK_MESSAGE( - pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.plus_file("../logo.png")) == OK, + pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.path_join("../logo.png")) == OK, "Overriding a non-flushed file to an existing subdirectory in the PCK should return an OK error code."); CHECK_MESSAGE( pck_packer.flush() == OK, diff --git a/tests/core/io/test_resource.h b/tests/core/io/test_resource.h index c880ca7d2a..2457e06ade 100644 --- a/tests/core/io/test_resource.h +++ b/tests/core/io/test_resource.h @@ -74,8 +74,8 @@ TEST_CASE("[Resource] Saving and loading") { Ref<Resource> child_resource = memnew(Resource); child_resource->set_name("I'm a child resource"); resource->set_meta("other_resource", child_resource); - const String save_path_binary = OS::get_singleton()->get_cache_path().plus_file("resource.res"); - const String save_path_text = OS::get_singleton()->get_cache_path().plus_file("resource.tres"); + const String save_path_binary = OS::get_singleton()->get_cache_path().path_join("resource.res"); + const String save_path_text = OS::get_singleton()->get_cache_path().path_join("resource.tres"); ResourceSaver::save(resource, save_path_binary); ResourceSaver::save(resource, save_path_text); diff --git a/tests/core/io/test_xml_parser.h b/tests/core/io/test_xml_parser.h index 87592b56ce..35e86d8203 100644 --- a/tests/core/io/test_xml_parser.h +++ b/tests/core/io/test_xml_parser.h @@ -66,6 +66,170 @@ TEST_CASE("[XMLParser] End-to-end") { parser.close(); } + +TEST_CASE("[XMLParser] Comments") { + XMLParser parser; + + SUBCASE("Missing end of comment") { + const String input = "<first></first><!-- foo"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT); + CHECK_EQ(parser.get_node_name(), " foo"); + } + SUBCASE("Bad start of comment") { + const String input = "<first></first><!-"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT); + CHECK_EQ(parser.get_node_name(), "-"); + } + SUBCASE("Unblanced angle brackets in comment") { + const String input = "<!-- example << --><next-tag></next-tag>"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT); + CHECK_EQ(parser.get_node_name(), " example << "); + } + SUBCASE("Doctype") { + const String input = "<!DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]>"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT); + CHECK_EQ(parser.get_node_name(), "DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]"); + } +} + +TEST_CASE("[XMLParser] Premature endings") { + SUBCASE("Simple cases") { + String input; + String expected_name; + XMLParser::NodeType expected_type; + + SUBCASE("Incomplete Unknown") { + input = "<first></first><?xml"; + expected_type = XMLParser::NodeType::NODE_UNKNOWN; + expected_name = "?xml"; + } + SUBCASE("Incomplete CDStart") { + input = "<first></first><![CD"; + expected_type = XMLParser::NodeType::NODE_CDATA; + expected_name = ""; + } + SUBCASE("Incomplete CData") { + input = "<first></first><![CDATA[example"; + expected_type = XMLParser::NodeType::NODE_CDATA; + expected_name = "example"; + } + SUBCASE("Incomplete CDEnd") { + input = "<first></first><![CDATA[example]]"; + expected_type = XMLParser::NodeType::NODE_CDATA; + expected_name = "example]]"; + } + SUBCASE("Incomplete start-tag name") { + input = "<first></first><second"; + expected_type = XMLParser::NodeType::NODE_ELEMENT; + expected_name = "second"; + } + + XMLParser parser; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), expected_type); + CHECK_EQ(parser.get_node_name(), expected_name); + } + + SUBCASE("With attributes and texts") { + XMLParser parser; + + SUBCASE("Incomplete start-tag attribute name") { + const String input = "<first></first><second attr1=\"foo\" attr2"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + CHECK_EQ(parser.get_node_name(), "second"); + CHECK_EQ(parser.get_attribute_count(), 1); + CHECK_EQ(parser.get_attribute_name(0), "attr1"); + CHECK_EQ(parser.get_attribute_value(0), "foo"); + } + + SUBCASE("Incomplete start-tag attribute unquoted value") { + const String input = "<first></first><second attr1=\"foo\" attr2=bar"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + CHECK_EQ(parser.get_node_name(), "second"); + CHECK_EQ(parser.get_attribute_count(), 1); + CHECK_EQ(parser.get_attribute_name(0), "attr1"); + CHECK_EQ(parser.get_attribute_value(0), "foo"); + } + + SUBCASE("Incomplete start-tag attribute quoted value") { + const String input = "<first></first><second attr1=\"foo\" attr2=\"bar"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + CHECK_EQ(parser.get_node_name(), "second"); + CHECK_EQ(parser.get_attribute_count(), 2); + CHECK_EQ(parser.get_attribute_name(0), "attr1"); + CHECK_EQ(parser.get_attribute_value(0), "foo"); + CHECK_EQ(parser.get_attribute_name(1), "attr2"); + CHECK_EQ(parser.get_attribute_value(1), "bar"); + } + + SUBCASE("Incomplete end-tag name") { + const String input = "<first></fir"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END); + CHECK_EQ(parser.get_node_name(), "fir"); + } + + SUBCASE("Trailing text") { + const String input = "<first></first>example"; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_TEXT); + CHECK_EQ(parser.get_node_data(), "example"); + } + } +} + +TEST_CASE("[XMLParser] CDATA") { + const String input = "<a><![CDATA[my cdata content goes here]]></a>"; + XMLParser parser; + REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT); + CHECK_EQ(parser.get_node_name(), "a"); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_CDATA); + CHECK_EQ(parser.get_node_name(), "my cdata content goes here"); + REQUIRE_EQ(parser.read(), OK); + CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END); + CHECK_EQ(parser.get_node_name(), "a"); +} } // namespace TestXMLParser #endif // TEST_XML_PARSER_H |