#include #include "../src/Interpreter.hpp" #define TEST_RUN(SRC, ...) \ test_run(SRC, {__VA_ARGS__}) using namespace sn; struct StateMock: public State { std::vector modified_files; explicit StateMock(std::vector const& p_modified_files) : modified_files { p_modified_files } { } virtual void init(std::vector const&) override { } virtual void update(std::filesystem::path) override { } virtual std::vector get_modified_files(std::vector const&) override { return modified_files; } }; struct LoaderMock: public Loader { std::string snakefile; explicit LoaderMock(std::string const& p_snakefile) : snakefile { p_snakefile } { } virtual std::filesystem::path find_snakefile() override { return ""; } virtual std::string load_snakefile() override { return snakefile; } }; struct ExecutorMock: public Executor { std::vector history; virtual std::string execute(std::string const& command) override { history.push_back(command); return ""; } }; class InterpreterTest { public: explicit InterpreterTest() {} virtual ~InterpreterTest() {} std::vector test_run(std::string const& snakefile, std::vector const& modified) { auto state = std::make_shared(modified); auto loader = std::make_shared(snakefile); auto executor = std::make_shared(); std::stringstream ss; Interpreter interpreter {state, loader, executor, ss}; interpreter.run(); return executor->history; } void test_contains(std::string const& str, std::vector vec) { INFO("'" << str << "' not found"); REQUIRE(std::find(std::begin(vec), std::end(vec), str) != std::end(vec)); } protected: }; TEST_CASE_METHOD(InterpreterTest, "Interpreter_simple_var") { std::stringstream ss; ss << "$X = a" << std::endl; ss << "b -> $X {" << std::endl; ss << "executed" << std::endl; ss << "}" << std::endl; auto res = TEST_RUN(ss.str(), std::filesystem::path("a")); test_contains("executed", res); } TEST_CASE_METHOD(InterpreterTest, "Interpreter_array_var") { SECTION("value") { std::stringstream ss; ss << "$X = (a b c)" << std::endl; ss << "res -> a {" << std::endl; ss << "$X" << std::endl; ss << "}" << std::endl; auto res = TEST_RUN(ss.str(), std::filesystem::path("a")); test_contains("a b c", res); } SECTION("as dep") { std::stringstream ss; ss << "$X = (a b c)" << std::endl; ss << "z -> $X {" << std::endl; ss << "executed" << std::endl; ss << "}" << std::endl; SECTION("a") { auto res = TEST_RUN(ss.str(), std::filesystem::path("a")); test_contains("executed", res); } SECTION("b") { auto res = TEST_RUN(ss.str(), std::filesystem::path("b")); test_contains("executed", res); } SECTION("c") { auto res = TEST_RUN(ss.str(), std::filesystem::path("c")); test_contains("executed", res); } } SECTION("direct use of array") { std::stringstream ss; ss << "z -> (a b c) {" << std::endl; ss << "executed" << std::endl; ss << "}" << std::endl; SECTION("a") { auto res = TEST_RUN(ss.str(), std::filesystem::path("a")); test_contains("executed", res); } SECTION("b") { auto res = TEST_RUN(ss.str(), std::filesystem::path("b")); test_contains("executed", res); } SECTION("c") { auto res = TEST_RUN(ss.str(), std::filesystem::path("c")); test_contains("executed", res); } } }