| 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 |
#pragma once |
| 32 |
|
| 33 |
#include "tests/test_macros.h" |
| 34 |
#include "tests/test_utils.h" |
| 35 |
|
| 36 |
#include "../zip_packer.h" |
| 37 |
#include "../zip_reader.h" |
| 38 |
|
| 39 |
namespace TestZip { |
| 40 |
|
| 41 |
void check_file_size(const String &p_path, int p_expected_size); |
| 42 |
|
| 43 |
TEST_CASE("[ZIPPacker] default compression") { |
| 44 |
const String path = TestUtils::get_temp_path("compressed.zip"); |
| 45 |
Ref<ZIPPacker> packer; |
| 46 |
packer.instantiate(); |
| 47 |
Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE); |
| 48 |
CHECK(open_result == OK); |
| 49 |
Error start_file_result = packer->start_file("demo.txt"); |
| 50 |
CHECK(start_file_result == OK); |
| 51 |
String text = "hello world!"; |
| 52 |
Error write_file_result = packer->write_file(text.to_utf8_buffer()); |
| 53 |
CHECK(write_file_result == OK); |
| 54 |
Error close_file_result = packer->close_file(); |
| 55 |
CHECK(close_file_result == OK); |
| 56 |
Error close_result = packer->close(); |
| 57 |
CHECK(close_result == OK); |
| 58 |
check_file_size(path, 128); |
| 59 |
} |
| 60 |
|
| 61 |
TEST_CASE("[ZIPPacker] no compression") { |
| 62 |
const String path = TestUtils::get_temp_path("uncompressed.zip"); |
| 63 |
Ref<ZIPPacker> packer; |
| 64 |
packer.instantiate(); |
| 65 |
Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE); |
| 66 |
CHECK(open_result == OK); |
| 67 |
packer->set_compression_level(ZIPPacker::COMPRESSION_NONE); |
| 68 |
Error start_file_result = packer->start_file("demo.txt"); |
| 69 |
CHECK(start_file_result == OK); |
| 70 |
String text = "hello world!"; |
| 71 |
Error write_file_result = packer->write_file(text.to_utf8_buffer()); |
| 72 |
CHECK(write_file_result == OK); |
| 73 |
Error close_file_result = packer->close_file(); |
| 74 |
CHECK(close_file_result == OK); |
| 75 |
Error close_result = packer->close(); |
| 76 |
CHECK(close_result == OK); |
| 77 |
check_file_size(path, 131); |
| 78 |
} |
| 79 |
|
| 80 |
TEST_CASE("[ZIPReader] read files") { |
| 81 |
String test_data = String("modules/zip/tests/data/").path_join("test.zip"); |
| 82 |
Ref<ZIPReader> reader; |
| 83 |
reader.instantiate(); |
| 84 |
Error open_result = reader->open(test_data); |
| 85 |
CHECK(open_result == OK); |
| 86 |
|
| 87 |
const String hello_path = "hello.txt"; |
| 88 |
const String world_path = "world.txt"; |
| 89 |
PackedStringArray expected_files; |
| 90 |
expected_files.push_back(hello_path); |
| 91 |
expected_files.push_back(world_path); |
| 92 |
CHECK(reader->get_files() == expected_files); |
| 93 |
|
| 94 |
const String expected_hello_text = "hello world!"; |
| 95 |
const String expected_world_text = "game over!"; |
| 96 |
PackedByteArray hello_bytes = reader->read_file(hello_path, false); |
| 97 |
PackedByteArray world_bytes = reader->read_file(world_path, true); |
| 98 |
CHECK(hello_bytes == expected_hello_text.to_utf8_buffer()); |
| 99 |
CHECK(world_bytes == expected_world_text.to_utf8_buffer()); |
| 100 |
|
| 101 |
CHECK(reader->get_compression_level(hello_path, true) == 6); |
| 102 |
CHECK(reader->get_compression_level(world_path, false) == 9); |
| 103 |
} |
| 104 |
|
| 105 |
} |