testc_ini.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "faux/str.h"
  4. #include "faux/ini.h"
  5. #include "faux/testc_helpers.h"
  6. int testc_faux_ini_parse_file(void) {
  7. // Source INI file
  8. const char *src_file =
  9. "# Comment\n"
  10. "DISTRIB_ID=Ubuntu\n"
  11. "DISTRIB_RELEASE=18.04\n"
  12. "DISTRIB_CODENAME=bionic\n"
  13. "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
  14. "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
  15. "WO_QUOTES_VAR = qwerty\n"
  16. "WO_QUOTES_VAR2 = qwerty 98989898\n"
  17. "EMPTY_VAR3 = \n"
  18. "EMPTY_VAR4 =\n"
  19. " EMPTY_VAR5 = \"\"\t \n"
  20. " ANOTHER_VAR6 = \"Normal var\"\t \n"
  21. "\tTABBED_VAR = \"Normal tabbed var\"\t \n"
  22. "# Another comment\n"
  23. " # Yet another comment\n"
  24. "\t# Tabbed comment\n"
  25. "VAR_WITHOUT_EOL=zxcvbnm"
  26. ;
  27. // Etalon file
  28. const char *etalon_file =
  29. "ANOTHER_VAR6=\"Normal var\"\n"
  30. "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
  31. "DISTRIB_CODENAME=bionic\n"
  32. "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
  33. "DISTRIB_ID=Ubuntu\n"
  34. "DISTRIB_RELEASE=18.04\n"
  35. "TABBED_VAR=\"Normal tabbed var\"\n"
  36. "VAR_WITHOUT_EOL=zxcvbnm\n"
  37. "WO_QUOTES_VAR=qwerty\n"
  38. "WO_QUOTES_VAR2=qwerty\n"
  39. "\"test space\"=\"lk lk lk \"\n"
  40. ;
  41. int ret = -1; // Pessimistic return value
  42. faux_ini_t *ini = NULL;
  43. faux_ini_node_t *iter = NULL;
  44. const faux_pair_t *pair = NULL;
  45. char *src_fn = NULL;
  46. char *dst_fn = NULL;
  47. char *etalon_fn = NULL;
  48. // Prepare files
  49. src_fn = faux_testc_tmpfile_deploy(src_file);
  50. etalon_fn = faux_testc_tmpfile_deploy(etalon_file);
  51. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  52. ini = faux_ini_new();
  53. if (faux_ini_parse_file(ini, src_fn) < 0) {
  54. fprintf(stderr, "Can't parse INI file %s\n", src_fn);
  55. goto parse_error;
  56. }
  57. iter = faux_ini_iter(ini);
  58. while ((pair = faux_ini_each(&iter))) {
  59. printf("[%s] = [%s]\n", faux_pair_name(pair), faux_pair_value(pair));
  60. }
  61. faux_ini_set(ini, "test space", "lk lk lk ");
  62. if (faux_ini_write_file(ini, dst_fn) < 0) {
  63. fprintf(stderr, "Can't write INI file %s\n", dst_fn);
  64. goto parse_error;
  65. }
  66. if (faux_testc_file_cmp(dst_fn, etalon_fn) != 0) {
  67. fprintf(stderr, "Generated file %s is not equal to etalon %s\n",
  68. dst_fn, etalon_fn);
  69. goto parse_error;
  70. }
  71. ret = 0; // success
  72. parse_error:
  73. faux_ini_free(ini);
  74. faux_str_free(dst_fn);
  75. faux_str_free(src_fn);
  76. faux_str_free(etalon_fn);
  77. return ret;
  78. }