Index: source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h =================================================================== --- source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h +++ source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h @@ -20,6 +20,18 @@ #include "graphics/PreprocessorWrapper.h" #include "third_party/ogre3d_preprocessor/OgreGLSLPreprocessor.h" +#include + +namespace +{ + // Removes leading and trailing white spaces. + std::string Trim(std::string text) + { + boost::algorithm::trim(text); + return text; + } +} + class TestPreprocessor : public CxxTest::TestSuite { public: @@ -28,33 +40,88 @@ Ogre::CPreprocessor::ErrorHandler = CPreprocessorWrapper::PyrogenesisShaderError; } - void test_basic() + struct PreprocessorResult + { + std::string output; + std::string loggerOutput; + }; + + PreprocessorResult Parse(const char* in) { + PreprocessorResult result; + TestLogger logger; Ogre::CPreprocessor preproc; - const char* in = "#define TEST 2\n1+1=TEST\n"; size_t len = 0; char* out = preproc.Parse(in, strlen(in), len); - TS_ASSERT_EQUALS(std::string(out, len), "\n1+1=2\n"); + result.output = std::string(out, len); + result.loggerOutput = logger.GetOutput(); // Free output if it's not inside the source string if (!(out >= in && out < in + strlen(in))) free(out); + + return result; + } + + void test_basic() + { + PreprocessorResult result = Parse("#define TEST 2\n1+1=TEST\n"); + TS_ASSERT_EQUALS(result.output, "\n1+1=2\n"); } void test_error() { - TestLogger logger; + PreprocessorResult result = Parse("foo\n#if ()\nbar\n"); + TS_ASSERT_EQUALS(result.output, ""); + TS_ASSERT_STR_CONTAINS(result.loggerOutput, "ERROR: Preprocessor error: line 2: Unclosed parenthesis in #if expression\n"); + } - Ogre::CPreprocessor preproc; - const char* in = "foo\n#if ()\nbar\n"; - size_t len = 0; - char* out = preproc.Parse(in, strlen(in), len); - TS_ASSERT_EQUALS(std::string(out, len), ""); + void test_else() + { + PreprocessorResult result = Parse(R"( + #define FOO 0 + #if FOO + #define BAR 0 + #else + #define BAR 42 + #endif + BAR + )"); + TS_ASSERT_EQUALS(Trim(result.output), "42"); + } - TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: Preprocessor error: line 2: Unclosed parenthesis in #if expression\n"); + void test_elif() + { + PreprocessorResult result = Parse(R"( + #define FOO 0 + #if FOO + #define BAR 0 + #elif 1 + #define BAR 42 + #endif + BAR + )"); + TS_ASSERT_EQUALS(Trim(result.output), "42"); + } - // Free output if it's not inside the source string - if (!(out >= in && out < in + strlen(in))) - free(out); + void test_nested_macro() + { + PreprocessorResult result = Parse(R"( + #define FOO(a, b, c, d) func1(a, b + (c * r), 0) + #define BAR func2 + FOO(x, y, BAR(u, v), w) + )"); + TS_ASSERT_EQUALS(Trim(result.output), "func1(x, y + (func2(u, v) * r), 0)"); + } + + void test_division_by_zero_error() + { + PreprocessorResult result = Parse(R"( + #if 2 / 0 + 42 + #endif + )"); + TS_ASSERT_EQUALS(Trim(result.output), ""); + TS_ASSERT_STR_CONTAINS(result.loggerOutput, "ERROR: Preprocessor error: line 2: Division by zero"); } };