Refactors Preprocessor testing and adds more tests

Reviewed By: Stan
Differential Revision: https://code.wildfiregames.com/D2574
This was SVN commit r23405.
This commit is contained in:
Vladislav Belov 2020-01-16 00:02:22 +00:00
parent 215447a761
commit df314f2974

View File

@ -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");
}
};