From df314f2974c45e9cc633a937c501ae2591183acc Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Thu, 16 Jan 2020 00:02:22 +0000 Subject: [PATCH] Refactors Preprocessor testing and adds more tests Reviewed By: Stan Differential Revision: https://code.wildfiregames.com/D2574 This was SVN commit r23405. --- .../tests/test_Preprocessor.h | 82 ++++++++++++++++--- 1 file changed, 69 insertions(+), 13 deletions(-) diff --git a/source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h b/source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h index d3608ad387..029d92dfd8 100644 --- a/source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h +++ b/source/third_party/ogre3d_preprocessor/tests/test_Preprocessor.h @@ -18,6 +18,7 @@ #include "lib/self_test.h" #include "graphics/PreprocessorWrapper.h" +#include "ps/CStr.h" #include "third_party/ogre3d_preprocessor/OgreGLSLPreprocessor.h" class TestPreprocessor : public CxxTest::TestSuite @@ -28,33 +29,88 @@ public: Ogre::CPreprocessor::ErrorHandler = CPreprocessorWrapper::PyrogenesisShaderError; } - void test_basic() + struct PreprocessorResult { + CStr8 output; + CStr8 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(result.output.Trim(PS_TRIM_BOTH), "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(result.output.Trim(PS_TRIM_BOTH), "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(result.output.Trim(PS_TRIM_BOTH), "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(result.output.Trim(PS_TRIM_BOTH), ""); + TS_ASSERT_STR_CONTAINS(result.loggerOutput, "ERROR: Preprocessor error: line 2: Division by zero"); } };