tests/test_properties.c

changeset 1562
f2b63cad2142
parent 1561
fcebf53de51c
equal deleted inserted replaced
1561:fcebf53de51c 1562:f2b63cad2142
39 39
40 CX_TEST_ASSERT(prop.config.delimiter == '='); 40 CX_TEST_ASSERT(prop.config.delimiter == '=');
41 CX_TEST_ASSERT(prop.config.comment1 == '#'); 41 CX_TEST_ASSERT(prop.config.comment1 == '#');
42 CX_TEST_ASSERT(prop.config.comment2 == 0); 42 CX_TEST_ASSERT(prop.config.comment2 == 0);
43 CX_TEST_ASSERT(prop.config.comment3 == 0); 43 CX_TEST_ASSERT(prop.config.comment3 == 0);
44 CX_TEST_ASSERT(prop.config.continuation == '\\');
44 CX_TEST_ASSERT(prop.input.space == NULL); 45 CX_TEST_ASSERT(prop.input.space == NULL);
45 CX_TEST_ASSERT(prop.buffer.space == NULL); 46 CX_TEST_ASSERT(prop.buffer.space == NULL);
46 47
47 cxPropertiesDestroy(&prop); 48 cxPropertiesDestroy(&prop);
48 } 49 }
381 382
382 free(long_key); 383 free(long_key);
383 free(long_value); 384 free(long_value);
384 } 385 }
385 386
387 CX_TEST(test_properties_next_line_continuation) {
388 const char *str =
389 "key1 = multiline \\\nvalue\n"
390 "key2 = normal\n"
391 "key3 = multiline \\\n trim \n"
392 "key4 = m1\\\nm2\\\n m3\\\nm4 \n"
393 "key5 = no\\continuation\n";
394
395 CxProperties prop;
396 cxPropertiesInitDefault(&prop);
397
398 cxstring key;
399 cxstring value;
400
401 CX_TEST_DO {
402 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
403
404 CX_TEST_ASSERT(cxPropertiesNext(&prop, &key, &value) == CX_PROPERTIES_NO_ERROR);
405 CX_TEST_ASSERT(!cx_strcmp(key, "key1"));
406 CX_TEST_ASSERT(!cx_strcmp(value, "multiline value"));
407
408 CX_TEST_ASSERT(cxPropertiesNext(&prop, &key, &value) == CX_PROPERTIES_NO_ERROR);
409 CX_TEST_ASSERT(!cx_strcmp(key, "key2"));
410 CX_TEST_ASSERT(!cx_strcmp(value, "normal"));
411
412 CX_TEST_ASSERT(cxPropertiesNext(&prop, &key, &value) == CX_PROPERTIES_NO_ERROR);
413 CX_TEST_ASSERT(!cx_strcmp(key, "key3"));
414 CX_TEST_ASSERT(!cx_strcmp(value, "multiline trim"));
415
416 CX_TEST_ASSERT(cxPropertiesNext(&prop, &key, &value) == CX_PROPERTIES_NO_ERROR);
417 CX_TEST_ASSERT(!cx_strcmp(key, "key4"));
418 CX_TEST_ASSERT(!cx_strcmp(value, "m1m2m3m4"));
419
420 CX_TEST_ASSERT(cxPropertiesNext(&prop, &key, &value) == CX_PROPERTIES_NO_ERROR);
421 CX_TEST_ASSERT(!cx_strcmp(key, "key5"));
422 CX_TEST_ASSERT(!cx_strcmp(value, "no\\continuation"));
423 }
424
425 cxPropertiesDestroy(&prop);
426 }
427
428 CX_TEST(test_properties_next_line_continuation_part) {
429 CxProperties prop;
430 cxPropertiesInitDefault(&prop);
431
432 cxstring key;
433 cxstring value;
434 CxPropertiesStatus result;
435 const char *str;
436
437 CX_TEST_DO {
438 // key1 = continue continue ...line
439 str = "key1 ";
440 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
441 result = cxPropertiesNext(&prop, &key, &value);
442 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
443
444 str = "= continue \\";
445 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
446 result = cxPropertiesNext(&prop, &key, &value);
447 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
448
449 str = "\ncontinue \\\n";
450 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
451 result = cxPropertiesNext(&prop, &key, &value);
452 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
453
454 str = "...";
455 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
456 result = cxPropertiesNext(&prop, &key, &value);
457 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
458
459 str = "line\nkey2 = value2\n";
460 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
461 result = cxPropertiesNext(&prop, &key, &value);
462 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
463 CX_TEST_ASSERT(!cx_strcmp(key, "key1"));
464 CX_TEST_ASSERT(!cx_strcmp(value, "continue continue ...line"));
465
466 // key2 = value2
467 result = cxPropertiesNext(&prop, &key, &value);
468 CX_TEST_ASSERT(!cx_strcmp(key, "key2"));
469 CX_TEST_ASSERT(!cx_strcmp(value, "value2"));
470
471 // key3 = continue-line
472 str = "key3=\\\ncontinue-\\\n line";
473 size_t len = strlen(str);
474 for(size_t i=0;i<len;i++) {
475 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, cx_strn(str+i, 1)));
476 result = cxPropertiesNext(&prop, &key, &value);
477 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
478 }
479 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, "\n"));
480 result = cxPropertiesNext(&prop, &key, &value);
481 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
482 CX_TEST_ASSERT(!cx_strcmp(key, "key3"));
483 CX_TEST_ASSERT(!cx_strcmp(value, "continue-line"));
484 }
485
486 cxPropertiesDestroy(&prop);
487 }
488
386 CX_TEST(test_properties_load) { 489 CX_TEST(test_properties_load) {
387 CxTestingAllocator talloc; 490 CxTestingAllocator talloc;
388 cx_testing_allocator_init(&talloc); 491 cx_testing_allocator_init(&talloc);
389 char fname[16] = "ucxtestXXXXXX"; 492 char fname[16] = "ucxtestXXXXXX";
390 int tmpfd = mkstemp(fname); 493 int tmpfd = mkstemp(fname);
708 cx_test_register(suite, test_properties_init); 811 cx_test_register(suite, test_properties_init);
709 cx_test_register(suite, test_properties_next); 812 cx_test_register(suite, test_properties_next);
710 cx_test_register(suite, test_properties_next_multi); 813 cx_test_register(suite, test_properties_next_multi);
711 cx_test_register(suite, test_properties_next_part); 814 cx_test_register(suite, test_properties_next_part);
712 cx_test_register(suite, test_properties_next_long_lines); 815 cx_test_register(suite, test_properties_next_long_lines);
816 cx_test_register(suite, test_properties_next_line_continuation);
817 cx_test_register(suite, test_properties_next_line_continuation_part);
713 cx_test_register(suite, test_properties_load); 818 cx_test_register(suite, test_properties_load);
714 cx_test_register(suite, test_properties_load_empty_file); 819 cx_test_register(suite, test_properties_load_empty_file);
715 cx_test_register(suite, test_properties_load_only_comments); 820 cx_test_register(suite, test_properties_load_only_comments);
716 cx_test_register(suite, test_properties_load_error); 821 cx_test_register(suite, test_properties_load_error);
717 cx_test_register(suite, test_properties_load_file_not_exists); 822 cx_test_register(suite, test_properties_load_file_not_exists);

mercurial