#include "catch.hpp" #include #include #include #include using namespace ink::runtime; SCENARIO("run story with global variable", "[global variables]") { GIVEN ("a story with global variables") { auto ink = story::from_file(INK_TEST_RESOURCE_DIR "GlobalStory.bin"); globals globStore = ink->new_globals(); runner thread = ink->new_runner(globStore); WHEN( "just runs") { THEN("variables should contain values as in inkScript") { REQUIRE(thread->getall() == "My name is Jean Passepartout, but my friend's call me Jackie. I'm 23 years old.\nFoo:23\n"); REQUIRE(*globStore->get("age") == 23); REQUIRE(*globStore->get("friendly_name_of_player") == std::string{"Jackie"}); } } WHEN ("edit number") { bool resi = globStore->set("age", 30); bool resc = globStore->set("friendly_name_of_player", "Freddy"); THEN("execution should success") { REQUIRE(resi == true); REQUIRE(resc == true); } THEN("variable should contain new value") { REQUIRE(thread->getall() == "My name is Jean Passepartout, but my friend's call me Freddy. I'm 30 years old.\nFoo:30\n"); REQUIRE(*globStore->get("age") == 30); REQUIRE(*globStore->get("friendly_name_of_player") == std::string{"Freddy"}); } WHEN ("something added to string") { // concat in GlobalsStory.ink thread->getall(); THEN("get should return the whole string") { REQUIRE(*globStore->get("concat") == std::string{"Foo:30"}); } } } WHEN ("name or type not exist") { auto wrongType = globStore->get("age"); auto notExistingName = globStore->get("foo"); THEN("should return nullptr") { REQUIRE(wrongType.has_value() == false); REQUIRE(notExistingName.has_value() == false); } bool rest = globStore->set("age", 3); bool resn = globStore->set("foo", 3); THEN("should return false") { REQUIRE(rest == false); REQUIRE(resn == false); } } } }