|
| 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