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