123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <initializer_list>
- #include <unordered_set>
-
- #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<std::string> &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<std::pair<
- std::string, std::vector<std::string>>> list) {
- INFO("Source: " << str);
- bufio::ISStream stream(str);
- BXParser parser(stream);
- BXVariables vars;
- parser.parse(vars);
-
- std::unordered_set<std::string> 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" } },
- });
- }
|