| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "tests/test_common.h" |
| 22 |
#include "lib/util/string_array.h" |
| 23 |
#include "lib/util/template.h" |
| 24 |
|
| 25 |
void test_template_if(void **state) |
| 26 |
{ |
| 27 |
const char *myfeatures[] = { |
| 28 |
"true", |
| 29 |
NULL |
| 30 |
}; |
| 31 |
|
| 32 |
const char *template = |
| 33 |
"line 01 {if \"true\":yes} \n" |
| 34 |
"line 02 {if \"false\":yes} \n" |
| 35 |
"line 03 {if \"true\":yes|no} \n" |
| 36 |
"line 04 {if \"false\":yes|no} \n" |
| 37 |
"line 05 {if \"false\" or \"true\":yes|no} \n" |
| 38 |
"line 06 {if \"false\" and \"true\":yes|no} \n" |
| 39 |
"line 07 {if \"true\" and (\"true\" or \"false\"):yes|no} \n" |
| 40 |
"line 08 {if \"false\" or (\"true\" and \"false\"):yes|no} \n" |
| 41 |
""; |
| 42 |
const char *expected = |
| 43 |
"line 01 yes\n" |
| 44 |
"line 02\n" |
| 45 |
"line 03 yes\n" |
| 46 |
"line 04 no\n" |
| 47 |
"line 05 yes\n" |
| 48 |
"line 06 no\n" |
| 49 |
"line 07 yes\n" |
| 50 |
"line 08 no\n" |
| 51 |
""; |
| 52 |
|
| 53 |
char *result = template_generate(template, myfeatures); |
| 54 |
assert_string_equal(expected, result); |
| 55 |
free(result); |
| 56 |
} |
| 57 |
|
| 58 |
void test_template_if_not(void **state) |
| 59 |
{ |
| 60 |
const char *myfeatures[] = { |
| 61 |
"true", |
| 62 |
NULL |
| 63 |
}; |
| 64 |
|
| 65 |
const char *template = |
| 66 |
"line 01 {if not \"true\":yes} \n" |
| 67 |
"line 02 {if not \"false\":yes} \n" |
| 68 |
"line 03 {if not \"true\":yes|no} \n" |
| 69 |
"line 04 {if not \"false\":yes|no} \n" |
| 70 |
"line 05 {if not \"false\" and \"true\":yes|no} \n" |
| 71 |
"line 06 {if not \"true\" and \"false\":yes|no} \n" |
| 72 |
"line 07 {if not (\"false\" and \"true\"):yes|no} \n" |
| 73 |
""; |
| 74 |
const char *expected = |
| 75 |
"line 01\n" |
| 76 |
"line 02 yes\n" |
| 77 |
"line 03 no\n" |
| 78 |
"line 04 yes\n" |
| 79 |
"line 05 yes\n" |
| 80 |
"line 06 no\n" |
| 81 |
"line 07 yes\n" |
| 82 |
""; |
| 83 |
|
| 84 |
char *result = template_generate(template, myfeatures); |
| 85 |
assert_string_equal(expected, result); |
| 86 |
free(result); |
| 87 |
} |
| 88 |
|
| 89 |
void test_template_exclude_if(void **state) |
| 90 |
{ |
| 91 |
const char *myfeatures[] = { |
| 92 |
"true", |
| 93 |
NULL |
| 94 |
}; |
| 95 |
|
| 96 |
const char *template = |
| 97 |
"line 01 {exclude if \"true\"}\n" |
| 98 |
"line 02 {exclude if \"false\"}\n" |
| 99 |
"line 03 {exclude if \"false\" or \"true\"}\n" |
| 100 |
"line 04 {exclude if \"false\" and \"true\"}\n" |
| 101 |
""; |
| 102 |
const char *expected = |
| 103 |
"line 02\n" |
| 104 |
"line 04\n" |
| 105 |
""; |
| 106 |
|
| 107 |
char *result = template_generate(template, myfeatures); |
| 108 |
assert_string_equal(expected, result); |
| 109 |
free(result); |
| 110 |
} |
| 111 |
|
| 112 |
void test_template_include_if(void **state) |
| 113 |
{ |
| 114 |
const char *myfeatures[] = { |
| 115 |
"true", |
| 116 |
NULL |
| 117 |
}; |
| 118 |
|
| 119 |
const char *template = |
| 120 |
"line 01 {include if \"true\"}\n" |
| 121 |
"line 02 {include if \"false\"}\n" |
| 122 |
"line 03 {include if \"false\" or \"true\"}\n" |
| 123 |
"line 04 {include if \"false\" and \"true\"}\n" |
| 124 |
""; |
| 125 |
const char *expected = |
| 126 |
"line 01\n" |
| 127 |
"line 03\n" |
| 128 |
""; |
| 129 |
|
| 130 |
char *result = template_generate(template, myfeatures); |
| 131 |
assert_string_equal(expected, result); |
| 132 |
free(result); |
| 133 |
} |
| 134 |
|
| 135 |
void test_template_stop_if(void **state) |
| 136 |
{ |
| 137 |
const char *myfeatures[] = { |
| 138 |
"true", |
| 139 |
NULL |
| 140 |
}; |
| 141 |
|
| 142 |
const char *template = |
| 143 |
"line 01\n" |
| 144 |
"{stop if \"false\"}\n" |
| 145 |
"line 02\n" |
| 146 |
"{stop if \"true\"}\n" |
| 147 |
"line 03\n" |
| 148 |
""; |
| 149 |
const char *expected = |
| 150 |
"line 01\n" |
| 151 |
"line 02\n" |
| 152 |
""; |
| 153 |
|
| 154 |
const char *template2 = |
| 155 |
"line 01\n" |
| 156 |
"{stop if \"false\" and \"true\"}\n" |
| 157 |
"line 02\n" |
| 158 |
"{stop if \"true\" or \"false\"}\n" |
| 159 |
"line 03\n" |
| 160 |
""; |
| 161 |
const char *expected2 = |
| 162 |
"line 01\n" |
| 163 |
"line 02\n" |
| 164 |
""; |
| 165 |
|
| 166 |
char *result = template_generate(template, myfeatures); |
| 167 |
assert_string_equal(expected, result); |
| 168 |
free(result); |
| 169 |
|
| 170 |
result = template_generate(template2, myfeatures); |
| 171 |
assert_string_equal(expected2, result); |
| 172 |
free(result); |
| 173 |
} |
| 174 |
|
| 175 |
void test_template_continue_if(void **state) |
| 176 |
{ |
| 177 |
const char *myfeatures[] = { |
| 178 |
"true", |
| 179 |
NULL |
| 180 |
}; |
| 181 |
|
| 182 |
const char *template = |
| 183 |
"line 01\n" |
| 184 |
"{continue if \"true\"}\n" |
| 185 |
"line 02\n" |
| 186 |
"{continue if \"false\"}\n" |
| 187 |
"line 03\n" |
| 188 |
""; |
| 189 |
const char *expected = |
| 190 |
"line 01\n" |
| 191 |
"line 02\n" |
| 192 |
""; |
| 193 |
|
| 194 |
const char *template2 = |
| 195 |
"line 01\n" |
| 196 |
"{continue if \"true\" or \"false\"}\n" |
| 197 |
"line 02\n" |
| 198 |
"{continue if \"true\" and \"false\"}\n" |
| 199 |
"line 03\n" |
| 200 |
""; |
| 201 |
const char *expected2 = |
| 202 |
"line 01\n" |
| 203 |
"line 02\n" |
| 204 |
""; |
| 205 |
|
| 206 |
char *result = template_generate(template, myfeatures); |
| 207 |
assert_string_equal(expected, result); |
| 208 |
free(result); |
| 209 |
|
| 210 |
result = template_generate(template2, myfeatures); |
| 211 |
assert_string_equal(expected2, result); |
| 212 |
free(result); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
void test_template_list_features(void **state) |
| 217 |
{ |
| 218 |
int i; |
| 219 |
const char *template = |
| 220 |
"line 01 {if \"feature1\":yes} \n" |
| 221 |
"line 02 {if \"feature2\":yes|no}\n" |
| 222 |
"line 03 {if \"feature3\" or \"feature1\":yes} \n" |
| 223 |
"line 04 {if not \"feature4\" or \"feature1\":yes|no}\n" |
| 224 |
"{stop if \"feature5\" or \"feature6\"}\n" |
| 225 |
"{continue if \"feature1\" and (\"feature2\" or \"feature7\")}\n" |
| 226 |
""; |
| 227 |
const char *expected[] = { |
| 228 |
"feature1", |
| 229 |
"feature2", |
| 230 |
"feature3", |
| 231 |
"feature4", |
| 232 |
"feature5", |
| 233 |
"feature6", |
| 234 |
"feature7", |
| 235 |
NULL |
| 236 |
}; |
| 237 |
char **flist; |
| 238 |
|
| 239 |
flist = template_list_features(template); |
| 240 |
assert_non_null(flist); |
| 241 |
|
| 242 |
assert_int_equal(string_array_count(flist), |
| 243 |
string_array_count((char **)expected)); |
| 244 |
|
| 245 |
string_array_sort(flist); |
| 246 |
for (i = 0; i < string_array_count(flist); ++i) { |
| 247 |
assert_string_equal(expected[i], flist[i]); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void test_template_imply_if(void **state) |
| 252 |
{ |
| 253 |
const char *myfeatures[] = { |
| 254 |
"true", |
| 255 |
NULL |
| 256 |
}; |
| 257 |
|
| 258 |
const char *template = |
| 259 |
"line 01 {include if \"enabled\"}\n" |
| 260 |
"{imply \"enabled\" if \"true\"}\n" |
| 261 |
"line 03 {include if \"enabled\"}\n" |
| 262 |
""; |
| 263 |
const char *expected = |
| 264 |
"line 03\n" |
| 265 |
""; |
| 266 |
|
| 267 |
char *result = template_generate(template, myfeatures); |
| 268 |
assert_string_equal(expected, result); |
| 269 |
free(result); |
| 270 |
} |
| 271 |
|
| 272 |
void test_template_if_and_include__true(void **state) |
| 273 |
{ |
| 274 |
const char *myfeatures[] = { |
| 275 |
"true", |
| 276 |
NULL |
| 277 |
}; |
| 278 |
|
| 279 |
const char *template = |
| 280 |
"L1 {if \"f1\":T1|T2} T3 {include if \"true\"} \n" |
| 281 |
"L2 \n" |
| 282 |
""; |
| 283 |
const char *expected = |
| 284 |
"L1 T2 T3\n" |
| 285 |
"L2\n" |
| 286 |
""; |
| 287 |
|
| 288 |
char *result = template_generate(template, myfeatures); |
| 289 |
assert_string_equal(expected, result); |
| 290 |
free(result); |
| 291 |
} |
| 292 |
|
| 293 |
void test_template_if_and_include__false(void **state) |
| 294 |
{ |
| 295 |
const char *myfeatures[] = { |
| 296 |
NULL |
| 297 |
}; |
| 298 |
|
| 299 |
const char *template = |
| 300 |
"L1 {if \"f1\":T1|T2} T3 {include if \"true\"} \n" |
| 301 |
"L2 \n" |
| 302 |
""; |
| 303 |
const char *expected = |
| 304 |
"L2\n" |
| 305 |
""; |
| 306 |
|
| 307 |
char *result = template_generate(template, myfeatures); |
| 308 |
assert_string_equal(expected, result); |
| 309 |
free(result); |
| 310 |
} |
| 311 |
|
| 312 |
void test_template_if_and_exclude__true(void **state) |
| 313 |
{ |
| 314 |
const char *myfeatures[] = { |
| 315 |
"true", |
| 316 |
NULL |
| 317 |
}; |
| 318 |
|
| 319 |
const char *template = |
| 320 |
"L1 {if \"f1\":T1|T2} T3 {exclude if \"true\"} \n" |
| 321 |
"L2 \n" |
| 322 |
""; |
| 323 |
const char *expected = |
| 324 |
"L2\n" |
| 325 |
""; |
| 326 |
|
| 327 |
char *result = template_generate(template, myfeatures); |
| 328 |
assert_string_equal(expected, result); |
| 329 |
free(result); |
| 330 |
} |
| 331 |
|
| 332 |
void test_template_if_and_exclude__false(void **state) |
| 333 |
{ |
| 334 |
const char *myfeatures[] = { |
| 335 |
NULL |
| 336 |
}; |
| 337 |
|
| 338 |
const char *template = |
| 339 |
"L1 {if \"f1\":T1|T2} T3 {exclude if \"true\"} \n" |
| 340 |
"L2 \n" |
| 341 |
""; |
| 342 |
const char *expected = |
| 343 |
"L1 T2 T3\n" |
| 344 |
"L2\n" |
| 345 |
""; |
| 346 |
|
| 347 |
char *result = template_generate(template, myfeatures); |
| 348 |
assert_string_equal(expected, result); |
| 349 |
free(result); |
| 350 |
} |
| 351 |
|
| 352 |
int main(int argc, const char *argv[]) |
| 353 |
{ |
| 354 |
|
| 355 |
const struct CMUnitTest tests[] = { |
| 356 |
cmocka_unit_test(test_template_if), |
| 357 |
cmocka_unit_test(test_template_if_not), |
| 358 |
cmocka_unit_test(test_template_include_if), |
| 359 |
cmocka_unit_test(test_template_exclude_if), |
| 360 |
cmocka_unit_test(test_template_stop_if), |
| 361 |
cmocka_unit_test(test_template_continue_if), |
| 362 |
cmocka_unit_test(test_template_list_features), |
| 363 |
cmocka_unit_test(test_template_imply_if), |
| 364 |
cmocka_unit_test(test_template_if_and_include__true), |
| 365 |
cmocka_unit_test(test_template_if_and_include__false), |
| 366 |
cmocka_unit_test(test_template_if_and_exclude__true), |
| 367 |
cmocka_unit_test(test_template_if_and_exclude__false), |
| 368 |
}; |
| 369 |
|
| 370 |
return cmocka_run_group_tests(tests, NULL, NULL); |
| 371 |
} |