test_parser.c
Go to the documentation of this file.00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "CUnit/Basic.h"
00027 #include "test_shared.h"
00028 #include "test_parser.h"
00029 #include "../shared/parse.h"
00030
00035 static int UFO_InitSuiteParser (void)
00036 {
00037 TEST_Init();
00038
00039 Com_EnableFunctionScriptToken(qfalse);
00040 return 0;
00041 }
00042
00047 static int UFO_CleanSuiteParser (void)
00048 {
00049 TEST_Shutdown();
00050 return 0;
00051 }
00052
00056 static void testParser (void)
00057 {
00058 const char* string = "aa \t\n aa,({\"bbb(bbb bbb)bbb {\"{a}\n/* foooo { } \n { } */\n// fooooo\naaaa";
00059 const char* cursor = string;
00060 const char *token;
00061
00062 token = Com_Parse(&cursor);
00063 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00064 CU_ASSERT_STRING_EQUAL(token, "aa");
00065
00066 token = Com_Parse(&cursor);
00067 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00068 CU_ASSERT_STRING_EQUAL(token, "aa,(");
00069
00070 token = Com_Parse(&cursor);
00071 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00072 CU_ASSERT_STRING_EQUAL(token, "{");
00073
00074 token = Com_Parse(&cursor);
00075 CU_ASSERT_TRUE(Com_ParsedTokenIsQuoted());
00076 CU_ASSERT_STRING_EQUAL(token, "bbb(bbb bbb)bbb {");
00077
00078 token = Com_Parse(&cursor);
00079 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00080 CU_ASSERT_STRING_EQUAL(token, "{");
00081
00082 token = Com_Parse(&cursor);
00083 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00084 CU_ASSERT_STRING_EQUAL(token, "a");
00085
00086 token = Com_Parse(&cursor);
00087 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00088 CU_ASSERT_STRING_EQUAL(token, "}");
00089
00090 token = Com_Parse(&cursor);
00091 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00092 CU_ASSERT_STRING_EQUAL(token, "aaaa");
00093
00094 token = Com_Parse(&cursor);
00095 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00096 CU_ASSERT_STRING_EQUAL(token, "\0");
00097 }
00098
00102 static void testParserWithEntity (void)
00103 {
00104 const char* string = "\n\taaaa \" \\n \\t \\\" \"";
00105 const char* cursor = string;
00106 const char *token;
00107
00108 token = Com_Parse(&cursor);
00109 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00110 CU_ASSERT_STRING_EQUAL(token, "aaaa");
00111
00112 token = Com_Parse(&cursor);
00113 CU_ASSERT_TRUE(Com_ParsedTokenIsQuoted());
00114 CU_ASSERT_STRING_EQUAL(token, " \n \t \" ");
00115 }
00116
00120 static void testParserWithUnParse (void)
00121 {
00122 const char* string = "aaaaa\n\tbbbbb \"ccccc\"";
00123 const char* cursor = string;
00124 const char *token;
00125
00126 token = Com_Parse(&cursor);
00127 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00128 CU_ASSERT_STRING_EQUAL(token, "aaaaa");
00129
00130 Com_UnParseLastToken();
00131
00132 token = Com_Parse(&cursor);
00133 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00134 CU_ASSERT_STRING_EQUAL(token, "aaaaa");
00135
00136 Com_UnParseLastToken();
00137
00138 token = Com_Parse(&cursor);
00139 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00140 CU_ASSERT_STRING_EQUAL(token, "aaaaa");
00141
00142 token = Com_Parse(&cursor);
00143 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00144 CU_ASSERT_STRING_EQUAL(token, "bbbbb");
00145
00146 Com_UnParseLastToken();
00147
00148 token = Com_Parse(&cursor);
00149 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00150 CU_ASSERT_STRING_EQUAL(token, "bbbbb");
00151
00152 Com_UnParseLastToken();
00153
00154 token = Com_Parse(&cursor);
00155 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00156 CU_ASSERT_STRING_EQUAL(token, "bbbbb");
00157
00158 token = Com_Parse(&cursor);
00159 CU_ASSERT_TRUE(Com_ParsedTokenIsQuoted());
00160 CU_ASSERT_STRING_EQUAL(token, "ccccc");
00161
00162 Com_UnParseLastToken();
00163
00164 token = Com_Parse(&cursor);
00165 CU_ASSERT_TRUE(Com_ParsedTokenIsQuoted());
00166 CU_ASSERT_STRING_EQUAL(token, "ccccc");
00167
00168 Com_UnParseLastToken();
00169
00170 token = Com_Parse(&cursor);
00171 CU_ASSERT_TRUE(Com_ParsedTokenIsQuoted());
00172 CU_ASSERT_STRING_EQUAL(token, "ccccc");
00173 }
00174
00178 static void testParserWithFunctionScriptToken (void)
00179 {
00180 const char* string = "aa \t\n aa,({\"bbb(bbb bbb)bbb {\"{a}\n/* foooo { } \n { } */\n// fooooo\naaaa)";
00181 const char* cursor = string;
00182 const char *token;
00183
00184 Com_EnableFunctionScriptToken(qtrue);
00185
00186 token = Com_Parse(&cursor);
00187 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00188 CU_ASSERT_STRING_EQUAL(token, "aa");
00189
00190 token = Com_Parse(&cursor);
00191 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00192 CU_ASSERT_STRING_EQUAL(token, "aa");
00193
00194 token = Com_Parse(&cursor);
00195 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00196 CU_ASSERT_STRING_EQUAL(token, ",");
00197
00198 token = Com_Parse(&cursor);
00199 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00200 CU_ASSERT_STRING_EQUAL(token, "(");
00201
00202 token = Com_Parse(&cursor);
00203 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00204 CU_ASSERT_STRING_EQUAL(token, "{");
00205
00206 token = Com_Parse(&cursor);
00207 CU_ASSERT_TRUE(Com_ParsedTokenIsQuoted());
00208 CU_ASSERT_STRING_EQUAL(token, "bbb(bbb bbb)bbb {");
00209
00210 token = Com_Parse(&cursor);
00211 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00212 CU_ASSERT_STRING_EQUAL(token, "{");
00213
00214 token = Com_Parse(&cursor);
00215 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00216 CU_ASSERT_STRING_EQUAL(token, "a");
00217
00218 token = Com_Parse(&cursor);
00219 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00220 CU_ASSERT_STRING_EQUAL(token, "}");
00221
00222 token = Com_Parse(&cursor);
00223 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00224 CU_ASSERT_STRING_EQUAL(token, "aaaa");
00225
00226 token = Com_Parse(&cursor);
00227 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00228 CU_ASSERT_STRING_EQUAL(token, ")");
00229
00230 token = Com_Parse(&cursor);
00231 CU_ASSERT_FALSE(Com_ParsedTokenIsQuoted());
00232 CU_ASSERT_STRING_EQUAL(token, "\0");
00233 }
00234
00235 int UFO_AddParserTests (void)
00236 {
00237
00238 CU_pSuite ParserSuite = CU_add_suite("ParserTests", UFO_InitSuiteParser, UFO_CleanSuiteParser);
00239
00240 if (ParserSuite == NULL)
00241 return CU_get_error();
00242
00243
00244 if (CU_ADD_TEST(ParserSuite, testParser) == NULL)
00245 return CU_get_error();
00246 if (CU_ADD_TEST(ParserSuite, testParserWithFunctionScriptToken) == NULL)
00247 return CU_get_error();
00248 if (CU_ADD_TEST(ParserSuite, testParserWithUnParse) == NULL)
00249 return CU_get_error();
00250 if (CU_ADD_TEST(ParserSuite, testParserWithEntity) == NULL)
00251 return CU_get_error();
00252
00253 return CUE_SUCCESS;
00254 }