Skip to content

Commit de135c9

Browse files
committed
Add regression tests for HLSLParser parenthesized expressions
Covers the fix for issue #940 with tests for: - Parenthesized constructor with binary operators - Double-nested parentheses - Both operands parenthesized - Chained operators after parens
1 parent bbcd5f8 commit de135c9

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

tests/libprojectM/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test_api_headers(TestMainAPIHeaders
2727
)
2828

2929
add_executable(projectM-unittest
30+
HLSLParserTest.cpp
3031
LoggingTest.cpp
3132
PresetFileParserTest.cpp
3233
WaveformAlignerTest.cpp
@@ -50,6 +51,7 @@ target_include_directories(projectM-unittest
5051
PRIVATE
5152
"${PROJECTM_SOURCE_DIR}/src/libprojectM"
5253
"${PROJECTM_SOURCE_DIR}"
54+
"${PROJECTM_SOURCE_DIR}/vendor/hlslparser/src"
5355
)
5456

5557
target_link_libraries(projectM-unittest
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <HLSLParser.h>
4+
#include <HLSLTree.h>
5+
6+
#include <cstring>
7+
8+
namespace {
9+
10+
bool ParseHLSL(const char* code)
11+
{
12+
M4::Allocator allocator;
13+
M4::HLSLTree tree(&allocator);
14+
M4::HLSLParser parser(&allocator, &tree);
15+
return parser.Parse("test.hlsl", code, strlen(code));
16+
}
17+
18+
} // namespace
19+
20+
// Regression test for issue #940: parenthesized constructor with binary operator
21+
// was rejected with "expected ';'" error.
22+
TEST(HLSLParser, ParenthesizedConstructorWithMultiply)
23+
{
24+
EXPECT_TRUE(ParseHLSL(
25+
"float scalar = 2.0;\n"
26+
"float2 var = (float2(1.0, 2.0)) * scalar;\n"
27+
));
28+
}
29+
30+
TEST(HLSLParser, ParenthesizedConstructorWithAdd)
31+
{
32+
EXPECT_TRUE(ParseHLSL(
33+
"float2 var = (float2(1.0, 2.0)) + float2(3.0, 4.0);\n"
34+
));
35+
}
36+
37+
TEST(HLSLParser, DoubleNestedParensWithOperator)
38+
{
39+
EXPECT_TRUE(ParseHLSL(
40+
"float2 var = ((float2(1.0, 2.0))) * 2.0;\n"
41+
));
42+
}
43+
44+
TEST(HLSLParser, BothOperandsParenthesized)
45+
{
46+
EXPECT_TRUE(ParseHLSL(
47+
"float2 var = (float2(1.0, 2.0)) * (float2(3.0, 4.0));\n"
48+
));
49+
}
50+
51+
TEST(HLSLParser, ChainedOperatorsAfterParens)
52+
{
53+
EXPECT_TRUE(ParseHLSL(
54+
"float a = 1.0; float b = 2.0; float c = 3.0;\n"
55+
"float x = (a) * b + c;\n"
56+
));
57+
}
58+
59+
TEST(HLSLParser, ConstructorWithoutParensStillWorks)
60+
{
61+
EXPECT_TRUE(ParseHLSL(
62+
"float2 var = float2(1.0, 2.0) * 2.0;\n"
63+
));
64+
}

0 commit comments

Comments
 (0)