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
|
/* Copyright (C) 2009 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/self_test.h"
#include "ps/Parser.h"
class TestParser : public CxxTest::TestSuite
{
public:
void test_basic()
{
CParser Parser;
Parser.InputTaskType("test", "_$ident_=_$value_");
std::string str;
int i;
CParserLine Line;
TS_ASSERT(Line.ParseString(Parser, "value=23"));
TS_ASSERT(Line.GetArgString(0, str) && str == "value");
TS_ASSERT(Line.GetArgInt(1, i) && i == 23);
}
void test_hotkey()
{
CParser Parser;
Parser.InputTaskType( "multikey", "<[~$arg(_negate)]$value_+_>_[~$arg(_negate)]$value" );
std::string str;
CParserLine Line;
TS_ASSERT(Line.ParseString(Parser, "x+yzzy+~w"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 4);
TS_ASSERT(Line.GetArgString(0, str) && str == "x");
TS_ASSERT(Line.GetArgString(1, str) && str == "yzzy");
TS_ASSERT(Line.GetArgString(2, str) && str == "_negate");
TS_ASSERT(Line.GetArgString(3, str) && str == "w");
// This fails because '[' isn't a value character (per _IsValueChar).
// I don't know whether that's a feature or a bug.
// TS_ASSERT(Line.ParseString(Parser, "["));
// TS_ASSERT_EQUALS((int)Line.GetArgCount(), 1);
// TS_ASSERT(Line.GetArgString(0, str) && str == "[");
}
void test_optional()
{
CParser Parser;
Parser.InputTaskType("test", "_$value_[$value]_");
std::string str;
CParserLine Line;
TS_ASSERT(Line.ParseString(Parser, "12 34"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 2);
TS_ASSERT(Line.GetArgString(0, str) && str == "12");
TS_ASSERT(Line.GetArgString(1, str) && str == "34");
TS_ASSERT(Line.ParseString(Parser, "56"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 1);
TS_ASSERT(Line.GetArgString(0, str) && str == "56");
TS_ASSERT(! Line.ParseString(Parser, " "));
}
void test_multi_optional()
{
CParser Parser;
Parser.InputTaskType("test", "_[$value]_[$value]_[$value]_");
std::string str;
CParserLine Line;
TS_ASSERT(Line.ParseString(Parser, "12 34 56"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 3);
TS_ASSERT(Line.GetArgString(0, str) && str == "12");
TS_ASSERT(Line.GetArgString(1, str) && str == "34");
TS_ASSERT(Line.GetArgString(2, str) && str == "56");
TS_ASSERT(Line.ParseString(Parser, "78 90"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 2);
TS_ASSERT(Line.GetArgString(0, str) && str == "78");
TS_ASSERT(Line.GetArgString(1, str) && str == "90");
TS_ASSERT(Line.ParseString(Parser, "ab"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 1);
TS_ASSERT(Line.GetArgString(0, str) && str == "ab");
TS_ASSERT(Line.ParseString(Parser, " "));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 0);
}
void test_optional_repeat()
{
CParser Parser;
Parser.InputTaskType("test", "<[_a_][_b_]_x_>");
std::string str;
CParserLine Line;
TS_ASSERT(Line.ParseString(Parser, "a b x a b x"));
TS_ASSERT(Line.ParseString(Parser, "a x b x"));
TS_ASSERT(Line.ParseString(Parser, "a x"));
TS_ASSERT(Line.ParseString(Parser, "b x"));
TS_ASSERT(Line.ParseString(Parser, "x"));
TS_ASSERT(! Line.ParseString(Parser, "a x c x"));
TS_ASSERT(! Line.ParseString(Parser, "a b a x"));
TS_ASSERT(! Line.ParseString(Parser, "a"));
TS_ASSERT(! Line.ParseString(Parser, "a a x"));
TS_ASSERT(Line.ParseString(Parser, "a x a b x a x b x b x b x b x a x a x a b x a b x b x a x"));
}
void test_rest()
{
CParser Parser;
Parser.InputTaskType("assignment", "_$ident_=_$value[[;]$rest]");
std::string str;
CParserLine Line;
TS_ASSERT(Line.ParseString(Parser, "12 = 34 ; comment"));
TS_ASSERT_EQUALS((int)Line.GetArgCount(), 2);
TS_ASSERT(Line.GetArgString(0, str) && str == "12");
TS_ASSERT(Line.GetArgString(1, str) && str == "34");
}
};
|