Browse Source

test suite

master
Martin Dørum 3 years ago
parent
commit
7abd923c58
5 changed files with 17942 additions and 0 deletions
  1. 3
    0
      test/.gitignore
  2. 5
    0
      test/build.bx
  3. 17825
    0
      test/catch2/catch.hpp
  4. 107
    0
      test/src/BXParser.t.cc
  5. 2
    0
      test/src/main.cc

+ 3
- 0
test/.gitignore View File

@@ -0,0 +1,3 @@
/bx-out
/.cache
/compile_commands.json

+ 5
- 0
test/build.bx View File

@@ -0,0 +1,5 @@
target := test
files := src ../lib
includes := catch2 ../lib
ldlibs := pthread
cxxflags := -g

+ 17825
- 0
test/catch2/catch.hpp
File diff suppressed because it is too large
View File


+ 107
- 0
test/src/BXParser.t.cc View File

@@ -0,0 +1,107 @@
#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" } },
});
}

+ 2
- 0
test/src/main.cc View File

@@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

Loading…
Cancel
Save