1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#include "RadiantTest.h"
#include "parser/CodeTokeniser.h"
#include "parser/GuiTokeniser.h"
#include "testutil/TemporaryFile.h"
namespace test
{
using GuiTokeniser = RadiantTest;
inline void expectTokenSequence(parser::CodeTokeniser& tokeniser, std::vector<std::string> expectedTokens)
{
for (auto expectedToken = expectedTokens.begin(); expectedToken != expectedTokens.end(); ++expectedToken)
{
EXPECT_TRUE(tokeniser.hasMoreTokens()) << "Tokeniser ran out of tokens";
EXPECT_EQ(tokeniser.nextToken(), *expectedToken);
}
EXPECT_FALSE(tokeniser.hasMoreTokens()) << "Tokeniser should have no more tokens now";
}
constexpr const char* const TEMPORARY_GUI_FILE = "guis/temporary.gui";
inline void expectTokenSequence(const radiant::TestContext& context, const std::string& contents, std::vector<std::string> expectedTokens)
{
auto path = context.getTestProjectPath() + TEMPORARY_GUI_FILE;
TemporaryFile tempFile(path, contents);
auto file = GlobalFileSystem().openTextFile(TEMPORARY_GUI_FILE);
parser::GuiTokeniser tokeniser(file);
expectTokenSequence(tokeniser, expectedTokens);
}
TEST_F(GuiTokeniser, ParseWindowDef)
{
std::string contents = R"(
windowDef rightPageBackground {
rect -100, -10, 500, 400
background "guis/assets/background_right_01"
matcolor 1, 1, 1, 0
})";
expectTokenSequence(_context, contents,
{
"windowDef", "rightPageBackground",
"{",
"rect", "-","100",",","-", "10",",","500",",","400",
"background", "guis/assets/background_right_01",
"matcolor", "1",",","1",",","1",",","0",
"}",
});
}
// The game's GUI parser supports reading rect-100 values without whitespace separation
TEST_F(GuiTokeniser, ParseRectExpression)
{
std::string contents = R"(
windowDef rightPageBackground {
rect-100,-10, 500, 400
})";
expectTokenSequence(_context, contents,
{
"windowDef", "rightPageBackground",
"{",
"rect", "-","100",",","-","10",",","500",",","400",
"}",
});
}
TEST_F(GuiTokeniser, ParseOnTimeExpression)
{
std::string contents = R"(
onTime 0 {
if ("gui::worldDisplay" == 1)
{
set "title::forecolor" "0 0 0 1";
set "body::forecolor" "0 0 0 1";
}
set "title::text" "$gui::title";
})";
expectTokenSequence(_context, contents,
{
"onTime", "0",
"{",
"if", "(", "gui::worldDisplay","==","1",")",
"{",
"set", "title::forecolor", "0 0 0 1", ";",
"set", "body::forecolor", "0 0 0 1", ";",
"}",
"set", "title::text", "$gui::title", ";",
"}",
});
}
TEST_F(GuiTokeniser, ParseQuotes)
{
std::string contents = R"(
// Escaped double quotes
if ("gui::\"worldDisplay\"" == 1)
{
// Single quotes
set 'title::forecolor' "0 0 0 1";
// Single quotes containing double quotes
set 'body::"forecolor"' "0 0 0 1";
}
// Single quotes with escaped single quotes
set 'that\'s something' "$gui::title";
)";
expectTokenSequence(_context, contents,
{
"if", "(", "gui::\"worldDisplay\"","==","1",")",
"{",
"set", "title::forecolor", "0 0 0 1", ";",
"set", "body::\"forecolor\"", "0 0 0 1", ";",
"}",
"set", "that's something", "$gui::title", ";",
});
}
TEST_F(GuiTokeniser, ParseComparisonOperators)
{
expectTokenSequence(_context, R"("visible" > 1)", { "visible", ">", "1" });
expectTokenSequence(_context, R"("visible" < 1)", { "visible", "<", "1" });
expectTokenSequence(_context, R"("visible" >= 1)", { "visible", ">=", "1" });
expectTokenSequence(_context, R"("visible" <= 1)", { "visible", "<=", "1" });
expectTokenSequence(_context, R"("visible" != 1)", { "visible", "!=", "1" });
expectTokenSequence(_context, R"("visible" == 1)", { "visible", "==", "1" });
// Without whitespace
expectTokenSequence(_context, R"("visible">1)", { "visible", ">", "1" });
expectTokenSequence(_context, R"("visible"<1)", { "visible", "<", "1" });
expectTokenSequence(_context, R"("visible">=1)", { "visible", ">=", "1" });
expectTokenSequence(_context, R"("visible"<=1)", { "visible", "<=", "1" });
expectTokenSequence(_context, R"("visible"!=1)", { "visible", "!=", "1" });
expectTokenSequence(_context, R"("visible"==1)", { "visible", "==", "1" });
// This is semantic garbage but the parser should pick something up
expectTokenSequence(_context, R"("visible">>1)", { "visible", ">", ">", "1" });
expectTokenSequence(_context, R"("visible" > >1)", { "visible", ">", ">", "1" });
expectTokenSequence(_context, R"("visible" > > 1)", { "visible", ">", ">", "1" });
expectTokenSequence(_context, R"("visible" <>= 1)", { "visible", "<", ">=", "1" });
expectTokenSequence(_context, R"("visible"<>=1)", { "visible", "<", ">=", "1" });
// With some parentheses around it
expectTokenSequence(_context, R"(("visible"==1))", { "(", "visible", "==", "1", ")" });
}
TEST_F(GuiTokeniser, ParseMathOperators)
{
expectTokenSequence(_context, R"("width"*3)", { "width", "*", "3" });
expectTokenSequence(_context, R"("width"*-3)", { "width", "*", "-", "3" });
expectTokenSequence(_context, R"(3*width+4)", { "3", "*", "width", "+", "4" });
expectTokenSequence(_context, R"(3%width+4)", { "3", "%", "width", "+", "4" });
expectTokenSequence(_context, R"("width" - -3)", { "width", "-", "-", "3" });
expectTokenSequence(_context, R"('gui::test' - (-3+7.1))", { "gui::test", "-", "(", "-", "3", "+", "7.1", ")" });
expectTokenSequence(_context, R"(3||6+4)", { "3", "||", "6", "+", "4" });
expectTokenSequence(_context, R"(3||6&&4)", { "3", "||", "6", "&&", "4" });
expectTokenSequence(_context, R"(3 || -6 && 4)", { "3", "||", "-", "6", "&&", "4" });
expectTokenSequence(_context, R"(3 < 6.4)", { "3", "<", "6.4" });
expectTokenSequence(_context, R"(3<6.4)", { "3", "<", "6.4" });
expectTokenSequence(_context, R"(3/5)", { "3", "/", "5" });
expectTokenSequence(_context, R"(3/-5)", { "3", "/", "-", "5" });
expectTokenSequence(_context, R"(3/5)", { "3", "/", "5" });
expectTokenSequence(_context, R"("visible" = 3 > 0)", { "visible", "=", "3", ">", "0" });
expectTokenSequence(_context, R"(rect ("gui::ingame" ? 0 : 50), -75, 640, 640)",
{ "rect","(","gui::ingame","?","0",":","50",")",",","-", "75",",","640",",","640" });
expectTokenSequence(_context, R"(rect ("gui::ingame"?0:50), -75, 640, 640)",
{ "rect","(","gui::ingame","?","0",":","50",")",",","-","75",",","640",",","640" });
expectTokenSequence(_context, R"("gui::av_in_progress" == 0 && "exit" == 0)",
{ "gui::av_in_progress","==","0","&&","exit","==","0" });
}
TEST_F(GuiTokeniser, ParseMacroExpansion)
{
std::string contents = R"(
#define POS_START_X 40
#define SIZE_MULTIPLIER 1
#define SUB_WINDOW(Name, Type, VisibleCheck) Type Name \
{\
rect POS_START_X*SIZE_MULTIPLIER,50,60,70\
visible VisibleCheck\
}
#define fullWindow(parent, start, objvisible, box, objtext)\
windowDef parent\
{\
rect POS_START_X, start, 490, 16\
visible objvisible\
SUB_WINDOW(box, windowDef, objvisible)\
text objtext\
}
#define exampleText "#str_02925"
fullWindow(Obj_t1_parent, 10, "gui::NumObjectivesPerPage" >= 1, Objbox_t1, exampleText)
)";
expectTokenSequence(_context, contents,
{
"windowDef", "Obj_t1_parent",
"{",
"rect", "40",",","10",",","490",",","16",
"visible","gui::NumObjectivesPerPage",">=", "1",
"windowDef", "Objbox_t1",
"{",
"rect","40","*","1",",","50",",","60",",","70",
"visible","gui::NumObjectivesPerPage",">=", "1",
"}",
"text","#str_02925",
"}",
});
}
TEST_F(GuiTokeniser, ParseObjectivesMacroExpansion)
{
// Taken from TDM's objectives menu
std::string contents = R"(
#define POS_START_X 40
#define SIZE_MULTIPLIER 1
#define POS_OBJ1_START_Y 10
#define objectiveExample(parent, start, objvisible, box, obj, objtext)\
windowDef parent\
{\
rect POS_START_X, start, 490, 16\
visible objvisible\
windowDef box\
{\
rect 0, -5, 32*SIZE_MULTIPLIER, 32*SIZE_MULTIPLIER\
bordercolor 1,1,1,1\
visible 1}\
windowDef obj\
{\
rect 25*SIZE_MULTIPLIER, 0, 430, 60*SIZE_MULTIPLIER*SIZE_MULTIPLIER\
bordercolor 1,1,1,1\
text objtext\
visible 1\
}\
}
#define exampleText "#str_02925" // This is an example text to illustrate the size of the objectives text.
objectiveExample(Obj_t1_parent, POS_OBJ1_START_Y, "gui::NumObjectivesPerPage" >= 1, Objbox_t1, Obj_t1, exampleText)
)";
expectTokenSequence(_context, contents,
{
"windowDef", "Obj_t1_parent",
"{",
"rect", "40",",","10",",","490",",","16",
"visible","gui::NumObjectivesPerPage",">=", "1",
"windowDef", "Objbox_t1",
"{",
"rect","0",",","-","5",",","32","*","1",",","32","*","1",
"bordercolor","1",",","1",",","1",",","1",
"visible","1",
"}",
"windowDef", "Obj_t1",
"{",
"rect","25","*","1",",","0",",","430",",","60","*","1","*","1",
"bordercolor","1",",","1",",","1",",","1",
"text","#str_02925",
"visible","1",
"}",
"}",
});
}
TEST_F(GuiTokeniser, PreprocessorDirectives)
{
// The file contains a couple of preprocessor expressions
auto file = GlobalFileSystem().openTextFile("guis/parse_test2.gui");
parser::GuiTokeniser tokeniser(file);
expectTokenSequence(tokeniser,
{
"windowDef", "Contents",
"{",
"forceaspectwidth", "633",
"forceaspectheight", "211",
"windowDef", "IncludedWindow",
"{",
"rect", "0",",","0",",","640",",","480",
"}",
"}",
"windowDef", "ThisShouldAppear", "{", "}",
"windowDef", "ThisShouldAppearAsWell", "{", "}"
});
}
}
|