#include "stdafx.h" #include "doctest.h" #include "scratch.h" #include "utilities/parser.h" #include "utilities/parsertape.h" #include "utilities/Globals.h" #include #include #include // the tape of a scenery parser (utilities/parsertape.h): what the outermost parser gave its reader, replayed to the same reader with // the same answers and without the files (docs/world-format-eu07.md, section 7) namespace { // a reader that asks the parser the way the scenery loader does: several tokens at once, single ones in their case, a vector, a // look ahead, and the header text; it writes down every answer, and the state of the parser after it std::vector read_all(cParser &Input) { std::vector transcript; auto const state{[&Input, &transcript](std::string const &What) { transcript.push_back(std::format("{} | {} {} {} {} {} {}", What, Input.Name(), Input.Line(), Input.LineMain(), Input.TokenBegin(), Input.TokenEnd(), Input.InLayerFile())); }}; while (true) { auto const header{std::string{Input.headerText(4)}}; auto const keyword{Input.getToken()}; if (keyword.empty()) break; state(std::format("keyword {} [{}]", keyword, header)); if (keyword != "node") continue; Input.getTokens(4); std::string name; std::string type; double range_max{0.0}; double range_min{0.0}; Input >> range_max >> range_min >> name >> type; state(std::format("node {} {} {} {}", range_max, range_min, name, type)); auto const position{Input.getToken()}; state(std::format("at {} {} {}", position.x, position.y, position.z)); auto const texture{Input.getToken(false)}; state("texture " + texture); Input.getTokens(1); state("peek " + Input.peek()); Input.getTokens(1); std::string end; Input >> end; state("end " + end); } return transcript; } std::string const injected{"node 1 0 Injected model 0 0 0 none 4 endmodel"}; struct recording { std::vector transcript; std::string bytes; std::vector files; }; // reads the scenery with recording, and checks that the reader got what it gets with no tape and gets from the replayed tape recording record_and_replay(test::scratch_folder const &Folder, std::string const &File, bool const Loadtraction) { recording result; parser_tape::writer tape; { cParser input{File, cParser::buffer_FILE, Folder.path(), Loadtraction}; input.injectString(injected); input.startRecording(tape); result.transcript = read_all(input); } result.files = tape.files(); result.bytes = tape.finish(); std::vector plain; { cParser input{File, cParser::buffer_FILE, Folder.path(), Loadtraction}; input.injectString(injected); plain = read_all(input); } CHECK(result.transcript == plain); std::vector files; std::size_t entries{0}; CHECK(parser_tape::check(result.bytes, files, entries)); CHECK(files == result.files); std::vector replayed; { parser_tape::reader reader{result.bytes}; cParser input{reader}; // the loader injects its text again; the tape holds it already input.injectString(injected); replayed = read_all(input); } CHECK(replayed == result.transcript); return result; } std::string joined(std::vector const &Lines) { std::string all; for (auto const &line : Lines) all += line + "\n"; return all; } } // namespace TEST_CASE("a parser replaying its tape gives the reader the same tokens and the same state, with no file opened") { test::scratch_folder const folder{"parser_tape"}; folder.write("main.scn", "// the main file\n" "node 1000 0 Signal_A model 1.5 2.0 -3.25 Textures/Sem.DDS 7 endmodel\n" "include tpl/pole.inc Pole_1 12.5 endtrack end\n" "include tr/wire.scm end\n" "node 500 0 none track 0 0 0 Rail none endtrack\n"); folder.write("tpl/pole.inc", "node 300 0 (p1) model (p2) 0 0 Pole.dds 1 (p3)\n" "include tpl/inner.inc end\n"); folder.write("tpl/inner.inc", "node 100 0 Inner model 0 0 0 none 2 endmodel\n"); folder.write("tr/wire.scm", "node 900 0 Wire traction 0 0 0 none 3 endtraction\n"); auto const with_traction{record_and_replay(folder, "main.scn", true)}; CHECK(with_traction.transcript.size() > 30); // the parameters of the template are in the tokens, the nested include expanded CHECK(joined(with_traction.transcript).find("node 300 0 pole_1 model") != std::string::npos); CHECK(joined(with_traction.transcript).find("node 100 0 inner model") != std::string::npos); CHECK(joined(with_traction.transcript).find("wire traction") != std::string::npos); // the two templates opened and closed their node groups, and the tape holds that int creates{0}; int closes{0}; parser_tape::reader reader{with_traction.bytes}; while (reader.next_kind() != parser_tape::entry_kind::end) { creates += reader.next_kind() == parser_tape::entry_kind::group_create ? 1 : 0; closes += reader.next_kind() == parser_tape::entry_kind::group_close ? 1 : 0; reader.pop(); } CHECK_FALSE(reader.broken()); CHECK(creates == 2); CHECK(closes == 2); // with the traction left out, its include is skipped in the tape as in the files auto const without_traction{record_and_replay(folder, "main.scn", false)}; CHECK(joined(without_traction.transcript).find("wire traction") == std::string::npos); SUBCASE("a terrain file the binary terrain stands in for is counted on replay as on the first reading") { folder.write("terrain.scn", "include x_ter.scm end\nnode 1 0 After model 0 0 0 none 1 endmodel\n"); auto const previous_state{Global.file_binary_terrain_state}; Global.file_binary_terrain_state = true; auto const before{Global.file_binary_terrain_skipped}; parser_tape::writer terrain; { cParser input{"terrain.scn", cParser::buffer_FILE, folder.path()}; input.startRecording(terrain); read_all(input); } CHECK(Global.file_binary_terrain_skipped == before + 1); auto const bytes{terrain.finish()}; { parser_tape::reader replay{bytes}; cParser input{replay}; read_all(input); } CHECK(Global.file_binary_terrain_skipped == before + 2); Global.file_binary_terrain_state = previous_state; } } TEST_CASE("a cut or foreign tape doesn't read") { parser_tape::writer tape; parser_tape::state start; start.line = 1; tape.begin("a.scn", start); parser_tape::state state; state.line = 3; state.begin = 10; state.end = 14; tape.token("node", "a.scn", state); tape.side_effect(parser_tape::entry_kind::group_create); tape.token("track", "b.inc", state); tape.text("node 1 0 x"); auto const whole{tape.finish()}; std::vector files; std::size_t entries{0}; REQUIRE(parser_tape::check(whole, files, entries)); CHECK(files == std::vector{"a.scn", "b.inc"}); CHECK(entries == 4); for (std::size_t length = 0; length < whole.size(); ++length) CHECK_FALSE(parser_tape::check(whole.substr(0, length), files, entries)); CHECK_FALSE(parser_tape::check(whole + "x", files, entries)); auto wrong{whole}; wrong[0] = 9; // another version CHECK_FALSE(parser_tape::check(wrong, files, entries)); }