ExamplesTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ExamplesTest.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "../scripting/ScriptFixture.h"
  12. #include "../../lib/VCMI_Lib.h"
  13. #include "../../lib/ScriptHandler.h"
  14. #include "../../lib/NetPacks.h"
  15. #include "../../lib/serializer/Cast.h"
  16. #include "../../lib/VCMIDirs.h"
  17. #include "../../lib/filesystem/Filesystem.h"
  18. #include "../../lib/filesystem/FileInfo.h"
  19. ///All unsorted ERM tests goes here
  20. namespace test
  21. {
  22. using namespace ::testing;
  23. using namespace ::scripting;
  24. class ExamplesTest : public Test, public ScriptFixture
  25. {
  26. public:
  27. std::vector<std::string> actualMessages;
  28. ExamplesTest()
  29. : ScriptFixture()
  30. {
  31. }
  32. void setDefaultExpectations()
  33. {
  34. EXPECT_CALL(infoMock, getLocalPlayer()).WillRepeatedly(Return(PlayerColor(3)));
  35. EXPECT_CALL(serverMock, apply(Matcher<CPackForClient *>(_))).WillRepeatedly(Invoke(this, &ExamplesTest::onCommit));
  36. }
  37. void onCommit(CPackForClient * pack)
  38. {
  39. InfoWindow * iw = dynamic_ptr_cast<InfoWindow>(pack);
  40. if(iw)
  41. {
  42. actualMessages.push_back(iw->text.toString());
  43. EXPECT_EQ(iw->player, PlayerColor(3));
  44. }
  45. else
  46. {
  47. GTEST_FAIL() << "Invalid NetPack";
  48. }
  49. }
  50. void saveScript(const std::string & name)
  51. {
  52. #ifdef VCMI_DUMP_TEST_SCRIPTS
  53. auto path = VCMIDirs::get().userDataPath() / name;
  54. boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::trunc);
  55. tmp.write(subject->code.c_str(), subject->code.size());
  56. tmp.flush();
  57. tmp.close();
  58. #endif
  59. }
  60. protected:
  61. void SetUp() override
  62. {
  63. ScriptFixture::setUp();
  64. }
  65. };
  66. TEST_F(ExamplesTest, ALL)
  67. {
  68. setDefaultExpectations();
  69. auto sources = CResourceHandler::get()->getFilteredFiles([](const ResourceID & ident)
  70. {
  71. return ident.getType() == EResType::ERM && boost::algorithm::starts_with(ident.getName(), "SCRIPTS/TEST/ERM/");
  72. });
  73. for(const ResourceID & file : sources)
  74. {
  75. actualMessages.clear();
  76. boost::filesystem::path scriptPath(file.getName() + ".VERM");
  77. boost::filesystem::path baseName = scriptPath.stem();
  78. boost::filesystem::path basePath = boost::filesystem::path("TEST/ERM/") / scriptPath.stem();
  79. std::string dataName = basePath.string()+".JSON";
  80. std::string scriptName = basePath.string()+".VERM";
  81. ResourceID dataPath(dataName);
  82. if(!CResourceHandler::get()->existsResource(dataPath))
  83. {
  84. GTEST_FAIL() << dataName << " does not exists";
  85. return;
  86. }
  87. const JsonNode expectedState(dataPath);
  88. loadScriptFromFile(scriptName);
  89. saveScript(baseName.string()+".lua");
  90. const JsonNode actualState = runServer();
  91. SCOPED_TRACE("\n"+actualState.toJson(true));
  92. JsonComparer c(false);
  93. c.compare(baseName.string(), actualState["ERM"], expectedState["ERM"]);
  94. auto expectedMessages = expectedState["messages"].convertTo<std::vector<std::string>>();
  95. EXPECT_THAT(actualMessages, Eq(expectedMessages));
  96. }
  97. }
  98. }