#include #include #include "BXParser.h" #include "catch.hpp" static BXVariables parse(const char *str) { bufio::ISStream stream(str); BXParser parser(stream); BXVariables vars; parser.parse(vars); return vars; } std::string stringify(std::vector &vec) { if (vec.size() == 0) { return "(empty)"; } std::string s; for (auto &val: vec) { if (s.size() == 0) { s += '\'' + val + '\''; } else { s += " '" + val + '\''; } } return s; } static void expecteq( const char *str, const std::initializer_list>> list) { INFO("Source: " << str); bufio::ISStream stream(str); BXParser parser(stream); BXVariables vars; parser.parse(vars); std::unordered_set keyset; for (auto &expected: list) { keyset.insert(expected.first); auto it = vars.find(expected.first); if (it == vars.end()) { FAIL("Missing key '" << expected.first << "'!"); } if (it->second != expected.second) { INFO("Value: " << stringify(it->second)); FAIL("Value of key '" << expected.first << "' doesn't match!"); } } for (auto &pair: vars) { if (keyset.find(pair.first) == keyset.end()) { FAIL("Unexpected key '" << pair.first << "'!"); } } } TEST_CASE("BXParser parsing", "[BXParser][parse]") { expecteq("hello := 10 world := 20", { { "hello", { "10" } }, { "world", { "20" } }, }); expecteq("foo := hello world bar := 10 20 30 baz := 0", { { "foo", { "hello", "world" } }, { "bar", { "10", "20", "30" } }, { "baz", { "0" } }, }); } TEST_CASE("BXParser parsing variables", "[BXParser][parse]") { expecteq("foo := \"hello world\" bar := $foo", { { "foo", { "hello world" } }, { "bar", { "hello world" } }, }); expecteq("foo := hello world bar := hey $foo what's up", { { "foo", { "hello", "world" } }, { "bar", { "hey", "hello", "world", "what's", "up" } }, }); } TEST_CASE("BXParser append/prepend", "[BXParser][parse]") { expecteq("foo := 10 foo += 20 foo += 30", { { "foo", { "10", "20", "30" } }, }); expecteq("foo := 10 foo =+ 20 foo =+ 30", { { "foo", { "30", "20", "10" } }, }); } TEST_CASE("BXParser newline separation", "[BXParser][parse]") { expecteq( "foo := 10 20\n" "bar := 30 40\n", { { "foo", { "10", "20" } }, { "bar", { "30", "40" } }, }); }