Make assertion stringizer methods for std containers work for any value types

This commit is contained in:
Max Brunsfeld 2013-12-30 19:30:48 -08:00
parent 30315a78d2
commit c6699a4c30
3 changed files with 31 additions and 41 deletions

View file

@ -6,7 +6,7 @@
START_TEST
describe("code_generation", []() {
describe("code generation", []() {
string test_parser_dir = src_dir() + "/spec/fixtures/parsers";
it("works for the arithmetic grammar", [&]() {

View file

@ -1,37 +1,5 @@
#include "spec_helper.h"
namespace tree_sitter {
namespace lr {
template<typename TKey, typename TValue>
std::ostream & stream_map_of_sets(std::ostream &stream, const unordered_map<TKey, unordered_set<TValue>> &map) {
stream << string("{");
bool started = false;
for (auto pair : map) {
if (started) stream << string(", ");
stream << pair.first;
stream << string(" => [");
bool started_set = false;
for (TValue action : pair.second) {
if (started_set) stream << ", ";
stream << action;
started_set = true;
}
stream << string("]}");
started = true;
}
return stream;
}
std::ostream& operator<<(std::ostream &stream, const unordered_map<string, unordered_set<ParseAction>> &map) {
return stream_map_of_sets(stream, map);
}
std::ostream& operator<<(std::ostream &stream, const unordered_map<CharMatch, unordered_set<LexAction>> &map) {
return stream_map_of_sets(stream, map);
}
}
}
string src_dir() {
return string(getenv("TREESITTER_SRC_DIR"));
}

View file

@ -18,6 +18,34 @@ using namespace bandit;
#define START_TEST go_bandit([]() {
#define END_TEST });
namespace std {
template<typename T>
inline std::ostream& operator<<(std::ostream &stream, const unordered_set<T> &set) {
stream << string("#<set: ");
bool started = false;
for (auto item : set) {
if (started) stream << string(", ");
stream << item;
started = true;
}
return stream << ">";
}
template<typename TKey, typename TValue>
inline std::ostream& operator<<(std::ostream &stream, const unordered_map<TKey, TValue> &map) {
stream << string("#<map: ");
bool started = false;
for (auto pair : map) {
if (started) stream << string(", ");
stream << pair.first;
stream << string(" => ");
stream << pair.second;
started = true;
}
return stream << ">";
}
}
namespace snowhouse {
template<typename ExpectedType>
struct EqualsPointerConstraint : Expression<EqualsPointerConstraint<ExpectedType>>
@ -37,7 +65,7 @@ namespace snowhouse {
{
static std::string ToString(const EqualsPointerConstraint<ExpectedType>& constraint) {
std::ostringstream builder;
builder << "equal to pointer " << snowhouse::Stringize(constraint.expected);
builder << "pointer to " << snowhouse::Stringize(constraint.expected);
return builder.str();
}
};
@ -46,13 +74,7 @@ namespace snowhouse {
inline EqualsPointerConstraint<ExpectedType> EqualsPointer(const ExpectedType& expected) {
return EqualsPointerConstraint<ExpectedType>(expected);
}
}
namespace tree_sitter {
namespace lr {
std::ostream& operator<<(std::ostream &stream, const unordered_map<string, unordered_set<lr::ParseAction>> &map);
std::ostream& operator<<(std::ostream &stream, const unordered_map<CharMatch, unordered_set<lr::LexAction>> &map);
}
}
string src_dir();