1 
2 #include "TestData.h"
3 
4 #include <algorithm>
5 #include <fstream>
6 #include <gtest/gtest.h>
7 #include <iterator>
8 #include <openrct2/CmdlineSprite.h>
9 #include <openrct2/core/Path.hpp>
10 
11 class CommandLineTests : public testing::Test
12 {
13 public:
SpriteTestDataPath()14     static std::string SpriteTestDataPath()
15     {
16         return Path::Combine(TestData::GetBasePath(), "sprites");
17     }
18 
ManifestFilePath()19     static std::string ManifestFilePath()
20     {
21         return Path::Combine(SpriteTestDataPath(), "manifest.json");
22     }
23 
BadManifestFilePath()24     static std::string BadManifestFilePath()
25     {
26         return Path::Combine(SpriteTestDataPath(), "badManifest.json");
27     }
28 
ExampleSpriteFilePath()29     static std::string ExampleSpriteFilePath()
30     {
31         return Path::Combine(SpriteTestDataPath(), "example.dat");
32     }
33 
BuildOutputfilePath()34     static std::string BuildOutputfilePath()
35     {
36         return Path::Combine(SpriteTestDataPath(), "result.dat");
37     }
38 
CompareSpriteFiles(std::string original,std::string generated)39     static bool CompareSpriteFiles(std::string original, std::string generated)
40     {
41         std::ifstream originalFile(original, std::ios::binary | std::ifstream::in);
42         std::ifstream generatedFile(generated, std::ios::binary | std::ifstream::in);
43         if (!(originalFile.is_open() && generatedFile.is_open()))
44         {
45             return false;
46         }
47         if (originalFile.tellg() != generatedFile.tellg())
48         {
49             return false;
50         }
51         return std::equal(
52             std::istreambuf_iterator<char>(originalFile.rdbuf()), std::istreambuf_iterator<char>(),
53             std::istreambuf_iterator<char>(generatedFile.rdbuf()));
54     }
55 };
56 
TEST_F(CommandLineTests,cmdline_cmdline_for_sprite_details)57 TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_details)
58 {
59     std::string exampleFilePath = ExampleSpriteFilePath();
60     const char* detailsCmd[3] = { "details", exampleFilePath.c_str() };
61 
62     int32_t result = cmdline_for_sprite(detailsCmd, 2);
63     // need to come up with some way to extract stdout/stderr stream if we want to
64     // fully test this module
65     ASSERT_EQ(result, 1);
66 }
67 
TEST_F(CommandLineTests,cmdline_cmdline_for_sprite_build)68 TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_build)
69 {
70     std::string manifestFilePath = ManifestFilePath();
71     std::string outputfilePath = BuildOutputfilePath();
72     const char* detailsCmd[3] = { "build", outputfilePath.c_str(), manifestFilePath.c_str() };
73 
74     int32_t result = cmdline_for_sprite(detailsCmd, 3);
75     ASSERT_EQ(result, 1);
76     // compare the resulting output file and assert its identical to expected
77     ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath));
78 }
79 
TEST_F(CommandLineTests,cmdline_cmdline_for_sprite_failed_build)80 TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_failed_build)
81 {
82     // run on correct manifest file
83     std::string manifestFilePath = ManifestFilePath();
84     std::string outputfilePath = BuildOutputfilePath();
85     const char* detailsCmd[3] = { "build", outputfilePath.c_str(), manifestFilePath.c_str() };
86     int32_t result = cmdline_for_sprite(detailsCmd, 3);
87     ASSERT_EQ(result, 1);
88     ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath));
89 
90     // now use bad manifest and make sure output file is not edited
91     std::string badManifestFilePath = BadManifestFilePath();
92     detailsCmd[2] = badManifestFilePath.c_str();
93     result = cmdline_for_sprite(detailsCmd, 3);
94     // check the command failed
95     ASSERT_EQ(result, -1);
96     // validate the target file was unchanged
97     ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath));
98 }
99