Skip to content

Commit 791fc31

Browse files
committed
Add zip test
1 parent 7305d18 commit 791fc31

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
1414
enable_testing()
1515

1616
include(GoogleTest)
17+
add_subdirectory(zip)

test/zip/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(
2+
zip_test
3+
zip_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
zip_test
8+
GTest::gtest_main
9+
zip
10+
)
11+
12+
gtest_discover_tests(zip_test)

test/zip/zip_test.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <gtest/gtest.h>
2+
#include <zip.h>
3+
#include <filesystem>
4+
#include "../common.h"
5+
6+
std::string readSb3Json(const std::string &fileName)
7+
{
8+
// TODO: Move this to a class and use it in Scratch3Reader
9+
// Open the zip file
10+
unsigned char *buf;
11+
size_t bufsize;
12+
struct zip_t *zip = zip_open(fileName.c_str(), 0, 'r');
13+
14+
// Extract project.json
15+
zip_entry_open(zip, "project.json");
16+
bufsize = zip_entry_size(zip);
17+
buf = (unsigned char *)calloc(sizeof(unsigned char), bufsize);
18+
zip_entry_noallocread(zip, (void *)buf, bufsize);
19+
zip_entry_close(zip);
20+
std::string str(reinterpret_cast<char const *>(buf));
21+
free(buf);
22+
23+
// Remove garbage after the JSON
24+
int end;
25+
for (end = str.size(); end >= 0; end--) {
26+
char ch = str[end];
27+
if (ch == '}') {
28+
break;
29+
}
30+
}
31+
return str.substr(0, end + 1);
32+
}
33+
34+
TEST(ZipTest, EmptyProject)
35+
{
36+
ASSERT_EQ(readSb3Json("empty_project.sb3"), readFileStr("empty_project.json"));
37+
}
38+
39+
TEST(ZipTest, DefaultProject)
40+
{
41+
ASSERT_EQ(readSb3Json("default_project.sb3"), readFileStr("default_project.json"));
42+
}
43+
44+
TEST(ZipTest, BubbleSort)
45+
{
46+
ASSERT_EQ(readSb3Json("bubble_sort.sb3"), readFileStr("bubble_sort.json"));
47+
}
48+
49+
TEST(ZipTest, FileManager)
50+
{
51+
ASSERT_EQ(readSb3Json("file_manager.sb3"), readFileStr("file_manager.json"));
52+
}

0 commit comments

Comments
 (0)