ScriptFixture.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * ScriptFixture.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 "ScriptFixture.h"
  12. namespace test
  13. {
  14. using namespace ::testing;
  15. using namespace ::scripting;
  16. ScriptFixture::ScriptFixture() = default;
  17. ScriptFixture::~ScriptFixture() = default;
  18. void ScriptFixture::loadScriptFromFile(const std::string & path)
  19. {
  20. JsonNode scriptConfig(JsonNode::JsonType::DATA_STRUCT);
  21. scriptConfig["source"].String() = path;
  22. loadScript(scriptConfig);
  23. }
  24. void ScriptFixture::loadScript(const JsonNode & scriptConfig)
  25. {
  26. subject = VLC->scriptHandler->loadFromJson(&loggerMock, CModHandler::scopeBuiltin(), scriptConfig, "test");
  27. GTEST_ASSERT_NE(subject, nullptr);
  28. context = subject->createContext(&environmentMock);
  29. EXPECT_CALL(*pool, getContext(Eq(subject.get()))).WillRepeatedly(Return(context));
  30. }
  31. void ScriptFixture::loadScript(ModulePtr module, const std::string & scriptSource)
  32. {
  33. subject = std::make_shared<ScriptImpl>(VLC->scriptHandler);
  34. subject->host = module;
  35. subject->sourceText = scriptSource;
  36. subject->identifier = "test";
  37. subject->compile(&loggerMock);
  38. context = subject->createContext(&environmentMock);
  39. EXPECT_CALL(*pool, getContext(Eq(subject.get()))).WillRepeatedly(Return(context));
  40. }
  41. void ScriptFixture::loadScript(ModulePtr module, const std::vector<std::string> & scriptSource)
  42. {
  43. std::string source = boost::algorithm::join(scriptSource, "\n");
  44. loadScript(module, source);
  45. }
  46. void ScriptFixture::setUp()
  47. {
  48. pool = std::make_shared<PoolMock>();
  49. EXPECT_CALL(environmentMock, battle()).WillRepeatedly(Return(&binfoMock));
  50. EXPECT_CALL(environmentMock, game()).WillRepeatedly(Return(&infoMock));
  51. EXPECT_CALL(environmentMock, logger()).WillRepeatedly(Return(&loggerMock));
  52. EXPECT_CALL(environmentMock, eventBus()).WillRepeatedly(Return(&eventBus));
  53. EXPECT_CALL(environmentMock, services()).WillRepeatedly(Return(&servicesMock));
  54. }
  55. JsonNode ScriptFixture::runClientServer(const JsonNode & scriptState)
  56. {
  57. context->run(scriptState);
  58. return context->saveState();
  59. }
  60. JsonNode ScriptFixture::runServer(const JsonNode & scriptState)
  61. {
  62. context->run(&serverMock, scriptState);
  63. return context->saveState();
  64. }
  65. JsonNode ScriptFixture::runScript(ModulePtr module, const std::string & scriptSource, const JsonNode & scriptState)
  66. {
  67. loadScript(module, scriptSource);
  68. return runClientServer(scriptState);
  69. }
  70. }