From 0fb59c18dd2fc4090c6b3c7510f0fb29489efaa3 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 22 May 2012 22:10:47 -0500 Subject: [PATCH] Split test struct and handler macros out for the emitter/spec tests --- test/create-emitter-tests.py | 12 ++++++++++-- test/emittertests.cpp | 33 +++++++++++++++++++++++++++++++++ test/handlermacros.h | 3 +-- test/spectests.h | 11 +---------- test/teststruct.h | 18 ++++++++++++++++++ 5 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 test/teststruct.h diff --git a/test/create-emitter-tests.py b/test/create-emitter-tests.py index 6bc0096..237d013 100644 --- a/test/create-emitter-tests.py +++ b/test/create-emitter-tests.py @@ -61,8 +61,10 @@ def gen_tests(): def create_emitter_tests(out): out.write('namespace %s {\n' % NS) - for test in gen_tests(): - out.write('inline TEST %s(YAML::Emitter& out)\n' % test['name']) + tests = list(gen_tests()) + + for test in tests: + out.write('TEST %s(YAML::Emitter& out)\n' % test['name']) out.write('{\n') for event in test['events']: emit = event['emit'] @@ -82,5 +84,11 @@ def create_emitter_tests(out): out.write('}\n') + out.write('void RunGenEmitterTests(int& passed, int& total)\n') + out.write('{\n') + for test in tests: + out.write(' RunGenEmitterTest(&Emitter::%s, %s, passed, total);\n' % (test['name'], encode(test['name']))) + out.write('}\n') + if __name__ == '__main__': create_emitter_tests(sys.stdout) diff --git a/test/emittertests.cpp b/test/emittertests.cpp index af819b5..690de05 100644 --- a/test/emittertests.cpp +++ b/test/emittertests.cpp @@ -1,4 +1,5 @@ #include "tests.h" +#include "handlermacros.h" #include "yaml-cpp/yaml.h" #include "yaml-cpp/eventhandler.h" #include @@ -1051,7 +1052,37 @@ namespace Test } total++; } + + void RunGenEmitterTest(TEST (*test)(YAML::Emitter&), const std::string& name, int& passed, int& total) { + YAML::Emitter out; + TEST ret; + + try { + ret = test(out); + } catch(const YAML::Exception& e) { + ret.ok = false; + ret.error = std::string(" Exception caught: ") + e.what(); + } + + if(!out.good()) { + ret.ok = false; + ret.error = out.GetLastError(); + } + + if(!ret.ok) { + std::cout << "Generated emitter test failed: " << name << "\n"; + std::cout << "Output:\n"; + std::cout << out.c_str() << "<<<\n"; + std::cout << ret.error << "\n"; + } + + if(ret.ok) + passed++; + total++; + } } + +#include "genemittertests.h" bool RunEmitterTests() { @@ -1142,6 +1173,8 @@ namespace Test RunEmitterErrorTest(&Emitter::InvalidAnchor, "invalid anchor", passed, total); RunEmitterErrorTest(&Emitter::InvalidAlias, "invalid alias", passed, total); RunEmitterErrorTest(&Emitter::BadLocalTag, "bad local tag", passed, total); + + RunGenEmitterTests(passed, total); std::cout << "Emitter tests: " << passed << "/" << total << " passed\n"; return passed == total; diff --git a/test/handlermacros.h b/test/handlermacros.h index c36d502..5bdaf69 100644 --- a/test/handlermacros.h +++ b/test/handlermacros.h @@ -1,3 +1,4 @@ +#include "teststruct.h" #pragma once #include "yaml-cpp/yaml.h" @@ -5,8 +6,6 @@ #include #include -#define YAML_ASSERT(cond) do { if(!(cond)) return " Assert failed: " #cond; } while(false) - namespace Test { std::string Quote(const std::string& text) { YAML::Emitter out; diff --git a/test/spectests.h b/test/spectests.h index 5246df5..611f3c0 100644 --- a/test/spectests.h +++ b/test/spectests.h @@ -5,18 +5,9 @@ #pragma once #endif -#include +#include "teststruct.h" namespace Test { - struct TEST { - TEST(): ok(false) {} - TEST(bool ok_): ok(ok_) {} - TEST(const char *error_): ok(false), error(error_) {} - - bool ok; - std::string error; - }; - namespace Spec { // 2.1 TEST SeqScalars(); diff --git a/test/teststruct.h b/test/teststruct.h new file mode 100644 index 0000000..2d563e5 --- /dev/null +++ b/test/teststruct.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#define YAML_ASSERT(cond) do { if(!(cond)) return " Assert failed: " #cond; } while(false) + +namespace Test +{ + struct TEST { + TEST(): ok(false) {} + TEST(bool ok_): ok(ok_) {} + TEST(const char *error_): ok(false), error(error_) {} + + bool ok; + std::string error; + }; + +}