libfyaml_ut.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <limits.h>
  6. #include <span>
  7. #include <contrib/libs/libfyaml/include/libfyaml.h>
  8. #include <library/cpp/testing/unittest/registar.h>
  9. /*
  10. These tests are ported from https://github.com/pantoniou/libfyaml/blob/master/test/libfyaml-test-core.c
  11. To check windows compatibility and avoid possible internal patch issues
  12. */
  13. Y_UNIT_TEST_SUITE(LibFyamlCore) {
  14. Y_UNIT_TEST(doc_build_simple) {
  15. struct fy_document *fyd;
  16. /* setup */
  17. fyd = fy_document_create(NULL);
  18. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  19. /* cleanup */
  20. fy_document_destroy(fyd);
  21. }
  22. Y_UNIT_TEST(doc_build_parse_check) {
  23. struct fy_document *fyd;
  24. char *buf;
  25. /* build document (with comments, newlines etc) */
  26. fyd = fy_document_build_from_string(NULL, "#comment\n[ 42, \n 12 ] # comment\n", FY_NT);
  27. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  28. /* convert to string */
  29. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  30. UNIT_ASSERT_UNEQUAL(buf, NULL);
  31. /* compare with expected result */
  32. UNIT_ASSERT_VALUES_EQUAL(buf, TString("[42, 12]\n"));
  33. free(buf);
  34. fy_document_destroy(fyd);
  35. }
  36. Y_UNIT_TEST(doc_build_scalar) {
  37. struct fy_document *fyd;
  38. /* build document */
  39. fyd = fy_document_build_from_string(NULL, "plain scalar # comment", FY_NT);
  40. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  41. /* compare with expected result */
  42. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_document_root(fyd)), TString("plain scalar"));
  43. fy_document_destroy(fyd);
  44. }
  45. Y_UNIT_TEST(doc_build_sequence) {
  46. struct fy_document *fyd;
  47. struct fy_node *fyn;
  48. int count;
  49. void *iter;
  50. /* build document */
  51. fyd = fy_document_build_from_string(NULL, "[ 10, 11, foo ] # comment", FY_NT);
  52. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  53. /* check for correct count value */
  54. count = fy_node_sequence_item_count(fy_document_root(fyd));
  55. UNIT_ASSERT_EQUAL(count, 3);
  56. /* try forward iterator first */
  57. iter = NULL;
  58. /* 0 */
  59. fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
  60. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  61. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
  62. /* 1 */
  63. fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
  64. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  65. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
  66. /* 2 */
  67. fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
  68. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  69. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
  70. /* final, iterator must return NULL */
  71. fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
  72. UNIT_ASSERT_EQUAL(fyn, NULL);
  73. /* reverse iterator */
  74. iter = NULL;
  75. /* 2 */
  76. fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
  77. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  78. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
  79. /* 1 */
  80. fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
  81. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  82. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
  83. /* 0 */
  84. fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
  85. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  86. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
  87. /* final, iterator must return NULL */
  88. fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
  89. UNIT_ASSERT_EQUAL(fyn, NULL);
  90. /* do forward index based accesses */
  91. /* 0 */
  92. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 0);
  93. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  94. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
  95. /* 1 */
  96. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 1);
  97. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  98. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
  99. /* 2 */
  100. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 2);
  101. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  102. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
  103. /* 3, it must not exist */
  104. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 3);
  105. UNIT_ASSERT_EQUAL(fyn, NULL);
  106. /* do backward index based accesses */
  107. /* 2 */
  108. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -1);
  109. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  110. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
  111. /* 1 */
  112. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -2);
  113. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  114. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
  115. /* 0 */
  116. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -3);
  117. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  118. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
  119. /* -1, it must not exist */
  120. fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -4);
  121. UNIT_ASSERT_EQUAL(fyn, NULL);
  122. fy_document_destroy(fyd);
  123. }
  124. Y_UNIT_TEST(doc_build_mapping) {
  125. struct fy_document *fyd;
  126. struct fy_node_pair *fynp;
  127. int count;
  128. void *iter;
  129. fyd = fy_document_build_from_string(NULL, "{ foo: 10, bar : 20, baz: [100, 101], [frob, 1]: boo }", FY_NT);
  130. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  131. /* check for correct count value */
  132. count = fy_node_mapping_item_count(fy_document_root(fyd));
  133. UNIT_ASSERT_EQUAL(count, 4);
  134. /* forward iterator first */
  135. iter = NULL;
  136. /* 0 */
  137. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  138. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  139. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("foo"));
  140. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("10"));
  141. /* 1 */
  142. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  143. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  144. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("bar"));
  145. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("20"));
  146. /* 2 */
  147. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  148. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  149. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("baz"));
  150. UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_value(fynp)), 2);
  151. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 0)), TString("100"));
  152. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 1)), TString("101"));
  153. /* 3 */
  154. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  155. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  156. UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_key(fynp)), 2);
  157. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 0)), TString("frob"));
  158. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 1)), TString("1"));
  159. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("boo"));
  160. /* 4, it must not exist */
  161. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  162. UNIT_ASSERT_EQUAL(fynp, NULL);
  163. /* reverse iterator */
  164. iter = NULL;
  165. /* 3 */
  166. fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
  167. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  168. UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_key(fynp)), 2);
  169. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 0)), TString("frob"));
  170. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 1)), TString("1"));
  171. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("boo"));
  172. /* 2 */
  173. fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
  174. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  175. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("baz"));
  176. UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_value(fynp)), 2);
  177. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 0)), TString("100"));
  178. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 1)), TString("101"));
  179. /* 1 */
  180. fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
  181. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  182. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("bar"));
  183. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("20"));
  184. /* 0 */
  185. fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
  186. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  187. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("foo"));
  188. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("10"));
  189. /* -1, it must not exist */
  190. fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
  191. UNIT_ASSERT_EQUAL(fynp, NULL);
  192. /* key lookups (note how only the contents are compared) */
  193. UNIT_ASSERT(fy_node_compare_string(fy_node_mapping_lookup_by_string(fy_document_root(fyd), "foo", FY_NT), "10", FY_NT) == true);
  194. UNIT_ASSERT(fy_node_compare_string(fy_node_mapping_lookup_by_string(fy_document_root(fyd), "bar", FY_NT), "20", FY_NT) == true);
  195. UNIT_ASSERT(fy_node_compare_string(fy_node_mapping_lookup_by_string(fy_document_root(fyd), "baz", FY_NT), "- 100\n- 101", FY_NT) == true);
  196. UNIT_ASSERT(fy_node_compare_string(fy_node_mapping_lookup_by_string(fy_document_root(fyd), "- 'frob'\n- \"\x31\"", FY_NT), "boo", FY_NT) == true);
  197. fy_document_destroy(fyd);
  198. fyd = NULL;
  199. }
  200. Y_UNIT_TEST(doc_path_access) {
  201. struct fy_document *fyd;
  202. struct fy_node *fyn;
  203. /* build document */
  204. fyd = fy_document_build_from_string(NULL, "{ "
  205. "foo: 10, bar : 20, baz:{ frob: boo }, "
  206. "frooz: [ seq1, { key: value} ], \"zero\\0zero\" : 0, "
  207. "{ key2: value2 }: { key3: value3 } "
  208. "}", FY_NT);
  209. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  210. /* check that getting root node works */
  211. fyn = fy_node_by_path(fy_document_root(fyd), "/", FY_NT, FYNWF_DONT_FOLLOW);
  212. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  213. UNIT_ASSERT_EQUAL(fyn, fy_document_root(fyd));
  214. /* check access to scalars */
  215. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW), "10", FY_NT) == true);
  216. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "bar", FY_NT, FYNWF_DONT_FOLLOW), "20", FY_NT) == true);
  217. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "baz/frob", FY_NT, FYNWF_DONT_FOLLOW), "boo", FY_NT) == true);
  218. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/frooz/0", FY_NT, FYNWF_DONT_FOLLOW), "seq1", FY_NT) == true);
  219. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/frooz/1/key", FY_NT, FYNWF_DONT_FOLLOW), "value", FY_NT) == true);
  220. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "\"zero\\0zero\"", FY_NT, FYNWF_DONT_FOLLOW), "0", FY_NT) == true);
  221. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/{ key2: value2 }/key3", FY_NT, FYNWF_DONT_FOLLOW), "value3", FY_NT) == true);
  222. fy_document_destroy(fyd);
  223. }
  224. Y_UNIT_TEST(doc_path_node) {
  225. struct fy_document *fyd;
  226. char *path;
  227. /* build document */
  228. fyd = fy_document_build_from_string(NULL, "{ "
  229. "foo: 10, bar : 20, baz:{ frob: boo }, "
  230. "frooz: [ seq1, { key: value} ], \"zero\\0zero\" : 0, "
  231. "{ key2: value2 }: { key3: value3 } "
  232. "}", FY_NT);
  233. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  234. path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/", FY_NT, FYNWF_DONT_FOLLOW));
  235. UNIT_ASSERT_VALUES_EQUAL(path, TString("/"));
  236. free(path);
  237. path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/frooz", FY_NT, FYNWF_DONT_FOLLOW));
  238. UNIT_ASSERT_VALUES_EQUAL(path, TString("/frooz"));
  239. free(path);
  240. path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/frooz/0", FY_NT, FYNWF_DONT_FOLLOW));
  241. UNIT_ASSERT_VALUES_EQUAL(path, TString("/frooz/0"));
  242. free(path);
  243. path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/{ key2: value2 }/key3", FY_NT, FYNWF_DONT_FOLLOW));
  244. UNIT_ASSERT_VALUES_EQUAL(path, TString("/{key2: value2}/key3"));
  245. free(path);
  246. fy_document_destroy(fyd);
  247. }
  248. Y_UNIT_TEST(doc_path_parent) {
  249. struct fy_document *fyd;
  250. struct fy_node *fyn_root, *fyn_foo, *fyn_bar, *fyn_baz, *fyn_frob, *fyn_ten;
  251. struct fy_node *fyn_deep, *fyn_deeper;
  252. char *path;
  253. /* build document */
  254. fyd = fy_document_build_from_string(NULL, "{ "
  255. "foo: 10, bar : [ ten, 20 ], baz:{ frob: boo, deep: { deeper: yet } }, "
  256. "}", FY_NT);
  257. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  258. fyn_root = fy_document_root(fyd);
  259. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  260. fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
  261. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  262. fyn_bar = fy_node_by_path(fyn_root, "/bar", FY_NT, FYNWF_DONT_FOLLOW);
  263. UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
  264. fyn_baz = fy_node_by_path(fyn_root, "/baz", FY_NT, FYNWF_DONT_FOLLOW);
  265. UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
  266. fyn_frob = fy_node_by_path(fyn_root, "/baz/frob", FY_NT, FYNWF_DONT_FOLLOW);
  267. UNIT_ASSERT_UNEQUAL(fyn_frob, NULL);
  268. fyn_ten = fy_node_by_path(fyn_root, "/bar/0", FY_NT, FYNWF_DONT_FOLLOW);
  269. UNIT_ASSERT_UNEQUAL(fyn_ten, NULL);
  270. fyn_deep = fy_node_by_path(fyn_root, "/baz/deep", FY_NT, FYNWF_DONT_FOLLOW);
  271. UNIT_ASSERT_UNEQUAL(fyn_deep, NULL);
  272. fyn_deeper = fy_node_by_path(fyn_root, "/baz/deep/deeper", FY_NT, FYNWF_DONT_FOLLOW);
  273. UNIT_ASSERT_UNEQUAL(fyn_deeper, NULL);
  274. /* check parent paths of foo, frob, ten */
  275. path = fy_node_get_parent_address(fyn_foo);
  276. UNIT_ASSERT_UNEQUAL(path, NULL);
  277. UNIT_ASSERT_VALUES_EQUAL(path, TString("foo"));
  278. free(path);
  279. path = fy_node_get_parent_address(fyn_frob);
  280. UNIT_ASSERT_UNEQUAL(path, NULL);
  281. UNIT_ASSERT_VALUES_EQUAL(path, TString("frob"));
  282. free(path);
  283. path = fy_node_get_parent_address(fyn_ten);
  284. UNIT_ASSERT_UNEQUAL(path, NULL);
  285. UNIT_ASSERT_VALUES_EQUAL(path, TString("0"));
  286. free(path);
  287. /* check relative paths to root */
  288. path = fy_node_get_path_relative_to(NULL, fyn_foo);
  289. UNIT_ASSERT_UNEQUAL(path, NULL);
  290. UNIT_ASSERT_VALUES_EQUAL(path, TString("foo"));
  291. free(path);
  292. path = fy_node_get_path_relative_to(fyn_root, fyn_foo);
  293. UNIT_ASSERT_UNEQUAL(path, NULL);
  294. UNIT_ASSERT_VALUES_EQUAL(path, TString("foo"));
  295. free(path);
  296. path = fy_node_get_path_relative_to(fyn_root, fyn_frob);
  297. UNIT_ASSERT_UNEQUAL(path, NULL);
  298. UNIT_ASSERT_VALUES_EQUAL(path, TString("baz/frob"));
  299. free(path);
  300. path = fy_node_get_path_relative_to(fyn_root, fyn_ten);
  301. UNIT_ASSERT_UNEQUAL(path, NULL);
  302. UNIT_ASSERT_VALUES_EQUAL(path, TString("bar/0"));
  303. free(path);
  304. /* check relative paths to other parents */
  305. path = fy_node_get_path_relative_to(fyn_baz, fyn_frob);
  306. UNIT_ASSERT_UNEQUAL(path, NULL);
  307. UNIT_ASSERT_VALUES_EQUAL(path, TString("frob"));
  308. free(path);
  309. path = fy_node_get_path_relative_to(fyn_bar, fyn_ten);
  310. UNIT_ASSERT_UNEQUAL(path, NULL);
  311. UNIT_ASSERT_VALUES_EQUAL(path, TString("0"));
  312. free(path);
  313. path = fy_node_get_path_relative_to(fyn_baz, fyn_deeper);
  314. UNIT_ASSERT_UNEQUAL(path, NULL);
  315. UNIT_ASSERT_VALUES_EQUAL(path, TString("deep/deeper"));
  316. free(path);
  317. fy_document_destroy(fyd);
  318. }
  319. Y_UNIT_TEST(doc_short_path) {
  320. struct fy_document *fyd;
  321. struct fy_node *fyn_root, *fyn_foo, *fyn_notfoo, *fyn_bar, *fyn_baz;
  322. const char *str;
  323. /* build document */
  324. fyd = fy_document_build_from_string(NULL,
  325. "--- &r\n"
  326. " foo: &f\n"
  327. " bar: [ 0, two, baz: what ]\n"
  328. " frob: true\n"
  329. " notfoo: false\n"
  330. , FY_NT);
  331. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  332. fyn_root = fy_document_root(fyd);
  333. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  334. fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
  335. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  336. fyn_notfoo = fy_node_by_path(fyn_root, "/notfoo", FY_NT, FYNWF_DONT_FOLLOW);
  337. UNIT_ASSERT_UNEQUAL(fyn_notfoo, NULL);
  338. fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
  339. UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
  340. fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/2/baz", FY_NT, FYNWF_DONT_FOLLOW);
  341. UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
  342. str = fy_node_get_short_path(fyn_root);
  343. UNIT_ASSERT_UNEQUAL(str, NULL);
  344. UNIT_ASSERT_VALUES_EQUAL(str, "*r");
  345. UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_root);
  346. free((void*)str);
  347. str = fy_node_get_short_path(fyn_foo);
  348. UNIT_ASSERT_UNEQUAL(str, NULL);
  349. UNIT_ASSERT_VALUES_EQUAL(str, "*f");
  350. UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_foo);
  351. free((void*)str);
  352. str = fy_node_get_short_path(fyn_notfoo);
  353. UNIT_ASSERT_UNEQUAL(str, NULL);
  354. UNIT_ASSERT_VALUES_EQUAL(str, "*r/notfoo");
  355. UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_notfoo);
  356. free((void*)str);
  357. str = fy_node_get_short_path(fyn_bar);
  358. UNIT_ASSERT_UNEQUAL(str, NULL);
  359. UNIT_ASSERT_VALUES_EQUAL(str, "*f/bar");
  360. UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_bar);
  361. free((void*)str);
  362. str = fy_node_get_short_path(fyn_baz);
  363. UNIT_ASSERT_UNEQUAL(str, NULL);
  364. UNIT_ASSERT_VALUES_EQUAL(str, "*f/bar/2/baz");
  365. UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_baz);
  366. free((void*)str);
  367. fy_document_destroy(fyd);
  368. }
  369. Y_UNIT_TEST(doc_scalar_path) {
  370. struct fy_document *fyd;
  371. struct fy_node *fyn_root, *fyn_foo;
  372. /* build document */
  373. fyd = fy_document_build_from_string(NULL, "--- foo\n", FY_NT);
  374. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  375. fyn_root = fy_document_root(fyd);
  376. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  377. /* get the scalar root and verify */
  378. fyn_foo = fy_node_by_path(fyn_root, "/", FY_NT, FYNWF_DONT_FOLLOW);
  379. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  380. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn_foo), "foo");
  381. fy_document_destroy(fyd);
  382. }
  383. Y_UNIT_TEST(doc_scalar_path_array) {
  384. struct fy_document *fyd;
  385. struct fy_node *fyn_root, *fynt;
  386. /* build document */
  387. fyd = fy_document_build_from_string(NULL, "--- [ foo, bar, baz ]\n", FY_NT);
  388. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  389. fyn_root = fy_document_root(fyd);
  390. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  391. /* get the scalars in the array and verify */
  392. fynt = fy_node_by_path(fyn_root, "/0", FY_NT, FYNWF_DONT_FOLLOW);
  393. UNIT_ASSERT_UNEQUAL(fynt, NULL);
  394. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fynt), "foo");
  395. fynt = fy_node_by_path(fyn_root, "/1", FY_NT, FYNWF_DONT_FOLLOW);
  396. UNIT_ASSERT_UNEQUAL(fynt, NULL);
  397. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fynt), "bar");
  398. fynt = fy_node_by_path(fyn_root, "/2", FY_NT, FYNWF_DONT_FOLLOW);
  399. UNIT_ASSERT_UNEQUAL(fynt, NULL);
  400. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fynt), "baz");
  401. fy_document_destroy(fyd);
  402. }
  403. Y_UNIT_TEST(doc_nearest_anchor) {
  404. struct fy_document *fyd;
  405. struct fy_node *fyn, *fyn_root, *fyn_foo, *fyn_notfoo, *fyn_bar, *fyn_baz;
  406. /* build document */
  407. fyd = fy_document_build_from_string(NULL,
  408. "--- &r\n"
  409. " foo: &f\n"
  410. " bar: [ 0, two, baz: what ]\n"
  411. " frob: true\n"
  412. " notfoo: false\n"
  413. , FY_NT);
  414. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  415. fyn_root = fy_document_root(fyd);
  416. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  417. fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
  418. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  419. fyn_notfoo = fy_node_by_path(fyn_root, "/notfoo", FY_NT, FYNWF_DONT_FOLLOW);
  420. UNIT_ASSERT_UNEQUAL(fyn_notfoo, NULL);
  421. fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
  422. UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
  423. fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/2/baz", FY_NT, FYNWF_DONT_FOLLOW);
  424. UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
  425. /* get nearest anchor of root (is root) */
  426. fyn = fy_anchor_node(fy_node_get_nearest_anchor(fyn_root));
  427. UNIT_ASSERT_EQUAL(fyn, fyn_root);
  428. /* get nearest anchor of notfoo (is root) */
  429. fyn = fy_anchor_node(fy_node_get_nearest_anchor(fyn_notfoo));
  430. UNIT_ASSERT_EQUAL(fyn, fyn_root);
  431. /* get nearest anchor of baz (is foo) */
  432. fyn = fy_anchor_node(fy_node_get_nearest_anchor(fyn_baz));
  433. UNIT_ASSERT_EQUAL(fyn, fyn_foo);
  434. fy_document_destroy(fyd);
  435. }
  436. Y_UNIT_TEST(doc_references) {
  437. struct fy_document *fyd;
  438. struct fy_node *fyn, *fyn_ref, *fyn_root, *fyn_foo, *fyn_notfoo, *fyn_bar, *fyn_baz;
  439. char *path;
  440. /* build document */
  441. fyd = fy_document_build_from_string(NULL,
  442. "---\n"
  443. " foo: &f\n"
  444. " bar: [ 0, two, baz: what ]\n"
  445. " frob: true\n"
  446. " notfoo: false\n"
  447. , FY_NT);
  448. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  449. fyn_root = fy_document_root(fyd);
  450. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  451. fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
  452. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  453. fyn_notfoo = fy_node_by_path(fyn_root, "/notfoo", FY_NT, FYNWF_DONT_FOLLOW);
  454. UNIT_ASSERT_UNEQUAL(fyn_notfoo, NULL);
  455. fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
  456. UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
  457. fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/2/baz", FY_NT, FYNWF_DONT_FOLLOW);
  458. UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
  459. /* get reference to root */
  460. path = fy_node_get_reference(fyn_root);
  461. UNIT_ASSERT_UNEQUAL(path, NULL);
  462. UNIT_ASSERT_VALUES_EQUAL(path, TString("*/"));
  463. free(path);
  464. /* get reference to /foo */
  465. path = fy_node_get_reference(fyn_foo);
  466. UNIT_ASSERT_UNEQUAL(path, NULL);
  467. UNIT_ASSERT_VALUES_EQUAL(path, TString("*f"));
  468. free(path);
  469. /* get reference to /notfoo */
  470. path = fy_node_get_reference(fyn_notfoo);
  471. UNIT_ASSERT_UNEQUAL(path, NULL);
  472. UNIT_ASSERT_VALUES_EQUAL(path, TString("*/notfoo"));
  473. free(path);
  474. /* get reference to /foo/bar/2/baz */
  475. path = fy_node_get_reference(fyn_baz);
  476. UNIT_ASSERT_UNEQUAL(path, NULL);
  477. UNIT_ASSERT_VALUES_EQUAL(path, TString("*/foo/bar/2/baz"));
  478. free(path);
  479. /* create reference to root and verify it points there */
  480. fyn_ref = fy_node_create_reference(fyn_root);
  481. UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
  482. fyn = fy_node_resolve_alias(fyn_ref);
  483. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  484. UNIT_ASSERT_EQUAL(fyn, fyn_root);
  485. fy_node_free(fyn_ref);
  486. /* get reference to /foo */
  487. fyn_ref = fy_node_create_reference(fyn_foo);
  488. UNIT_ASSERT_UNEQUAL(path, NULL);
  489. fyn = fy_node_resolve_alias(fyn_ref);
  490. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  491. UNIT_ASSERT_EQUAL(fyn, fyn_foo);
  492. fy_node_free(fyn_ref);
  493. /* get reference to /notfoo */
  494. fyn_ref = fy_node_create_reference(fyn_notfoo);
  495. UNIT_ASSERT_UNEQUAL(path, NULL);
  496. fyn = fy_node_resolve_alias(fyn_ref);
  497. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  498. UNIT_ASSERT_EQUAL(fyn, fyn_notfoo);
  499. fy_node_free(fyn_ref);
  500. /* get reference to /bar */
  501. fyn_ref = fy_node_create_reference(fyn_bar);
  502. UNIT_ASSERT_UNEQUAL(path, NULL);
  503. fyn = fy_node_resolve_alias(fyn_ref);
  504. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  505. UNIT_ASSERT_EQUAL(fyn, fyn_bar);
  506. fy_node_free(fyn_ref);
  507. /* get reference to /baz */
  508. fyn_ref = fy_node_create_reference(fyn_baz);
  509. UNIT_ASSERT_UNEQUAL(path, NULL);
  510. fyn = fy_node_resolve_alias(fyn_ref);
  511. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  512. UNIT_ASSERT_EQUAL(fyn, fyn_baz);
  513. fy_node_free(fyn_ref);
  514. /* get relative reference to /foo starting at / */
  515. path = fy_node_get_relative_reference(fyn_root, fyn_foo);
  516. UNIT_ASSERT_UNEQUAL(path, NULL);
  517. UNIT_ASSERT_VALUES_EQUAL(path, TString("*/foo"));
  518. free(path);
  519. /* get relative reference to /foo/bar/2/baz starting at / */
  520. path = fy_node_get_relative_reference(fyn_root, fyn_baz);
  521. UNIT_ASSERT_UNEQUAL(path, NULL);
  522. UNIT_ASSERT_VALUES_EQUAL(path, TString("*/foo/bar/2/baz"));
  523. free(path);
  524. /* get relative reference to /foo/bar/2/baz starting at /foo */
  525. path = fy_node_get_relative_reference(fyn_foo, fyn_baz);
  526. UNIT_ASSERT_UNEQUAL(path, NULL);
  527. UNIT_ASSERT_VALUES_EQUAL(path, TString("*f/bar/2/baz"));
  528. free(path);
  529. /* get relative reference to /notfoo at /foo (will return absolute) */
  530. path = fy_node_get_relative_reference(fyn_foo, fyn_notfoo);
  531. UNIT_ASSERT_UNEQUAL(path, NULL);
  532. UNIT_ASSERT_VALUES_EQUAL(path, TString("*/notfoo"));
  533. free(path);
  534. /* create relative reference to /foo starting at / */
  535. fyn_ref = fy_node_create_relative_reference(fyn_root, fyn_foo);
  536. UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
  537. fyn = fy_node_resolve_alias(fyn_ref);
  538. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  539. UNIT_ASSERT_EQUAL(fyn, fyn_foo);
  540. fy_node_free(fyn_ref);
  541. /* create relative reference to /foo/bar/2/baz starting at / */
  542. fyn_ref = fy_node_create_relative_reference(fyn_root, fyn_baz);
  543. UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
  544. fyn = fy_node_resolve_alias(fyn_ref);
  545. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  546. UNIT_ASSERT_EQUAL(fyn, fyn_baz);
  547. fy_node_free(fyn_ref);
  548. /* create relative reference to /foo/bar/2/baz starting at /foo */
  549. fyn_ref = fy_node_create_relative_reference(fyn_foo, fyn_baz);
  550. UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
  551. fyn = fy_node_resolve_alias(fyn_ref);
  552. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  553. UNIT_ASSERT_EQUAL(fyn, fyn_baz);
  554. fy_node_free(fyn_ref);
  555. /* create relative reference to /notfoo starting at /foo (will use absolute) */
  556. fyn_ref = fy_node_create_relative_reference(fyn_foo, fyn_notfoo);
  557. UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
  558. fyn = fy_node_resolve_alias(fyn_ref);
  559. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  560. UNIT_ASSERT_EQUAL(fyn, fyn_notfoo);
  561. fy_node_free(fyn_ref);
  562. fy_document_destroy(fyd);
  563. }
  564. Y_UNIT_TEST(doc_nearest_child_of) {
  565. struct fy_document *fyd;
  566. struct fy_node *fyn, *fyn_root, *fyn_foo, *fyn_bar, *fyn_baz;
  567. /* build document */
  568. fyd = fy_document_build_from_string(NULL,
  569. "foo:\n"
  570. " bar:\n"
  571. " barz: [ zero, baz: true ]\n"
  572. " frooz: notfoo\n"
  573. , FY_NT);
  574. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  575. fyn_root = fy_document_root(fyd);
  576. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  577. fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
  578. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  579. fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
  580. UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
  581. fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/barz/1/baz", FY_NT, FYNWF_DONT_FOLLOW);
  582. UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
  583. /* nearest child to the root of /foo is /foo */
  584. fyn = fy_node_get_nearest_child_of(fyn_root, fyn_foo);
  585. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  586. UNIT_ASSERT_EQUAL(fyn, fyn_foo);
  587. /* nearest child to the root of /foo/bar/barz/1/baz is /foo */
  588. fyn = fy_node_get_nearest_child_of(fyn_root, fyn_baz);
  589. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  590. UNIT_ASSERT_EQUAL(fyn, fyn_foo);
  591. /* nearest child to foo of /foo/bar/barz/1/baz is /foo/bar */
  592. fyn = fy_node_get_nearest_child_of(fyn_foo, fyn_baz);
  593. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  594. UNIT_ASSERT_EQUAL(fyn, fyn_bar);
  595. fy_document_destroy(fyd);
  596. }
  597. Y_UNIT_TEST(doc_create_empty_seq1) {
  598. struct fy_document *fyd;
  599. struct fy_node *fyn;
  600. char *buf;
  601. fyd = fy_document_create(NULL);
  602. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  603. fyn = fy_node_build_from_string(fyd, "[ ]", FY_NT);
  604. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  605. fy_document_set_root(fyd, fyn);
  606. /* convert to string */
  607. buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
  608. UNIT_ASSERT_UNEQUAL(buf, NULL);
  609. /* compare with expected result */
  610. UNIT_ASSERT_VALUES_EQUAL(buf, TString("[]"));
  611. free(buf);
  612. fy_document_destroy(fyd);
  613. }
  614. Y_UNIT_TEST(doc_create_empty_seq2) {
  615. struct fy_document *fyd;
  616. struct fy_node *fyn;
  617. char *buf;
  618. fyd = fy_document_create(NULL);
  619. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  620. fyn = fy_node_create_sequence(fyd);
  621. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  622. fy_document_set_root(fyd, fyn);
  623. /* convert to string */
  624. buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
  625. UNIT_ASSERT_UNEQUAL(buf, NULL);
  626. /* compare with expected result */
  627. UNIT_ASSERT_VALUES_EQUAL(buf, TString("[]"));
  628. free(buf);
  629. fy_document_destroy(fyd);
  630. }
  631. Y_UNIT_TEST(doc_create_empty_map1) {
  632. struct fy_document *fyd;
  633. struct fy_node *fyn;
  634. char *buf;
  635. fyd = fy_document_create(NULL);
  636. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  637. fyn = fy_node_build_from_string(fyd, "{ }", FY_NT);
  638. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  639. fy_document_set_root(fyd, fyn);
  640. /* convert to string */
  641. buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
  642. UNIT_ASSERT_UNEQUAL(buf, NULL);
  643. /* compare with expected result */
  644. UNIT_ASSERT_VALUES_EQUAL(buf, TString("{}"));
  645. free(buf);
  646. fy_document_destroy(fyd);
  647. }
  648. Y_UNIT_TEST(doc_create_empty_map2) {
  649. struct fy_document *fyd;
  650. struct fy_node *fyn;
  651. char *buf;
  652. fyd = fy_document_create(NULL);
  653. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  654. fyn = fy_node_create_mapping(fyd);
  655. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  656. fy_document_set_root(fyd, fyn);
  657. /* convert to string */
  658. buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
  659. UNIT_ASSERT_UNEQUAL(buf, NULL);
  660. /* compare with expected result */
  661. UNIT_ASSERT_VALUES_EQUAL(buf, TString("{}"));
  662. free(buf);
  663. fy_document_destroy(fyd);
  664. }
  665. Y_UNIT_TEST(doc_create_test_seq1) {
  666. struct fy_document *fyd;
  667. struct fy_node *fyn;
  668. int ret;
  669. fyd = fy_document_create(NULL);
  670. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  671. fyn = fy_node_create_sequence(fyd);
  672. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  673. ret = fy_node_sequence_append(fyn, fy_node_create_scalar(fyd, "foo", FY_NT));
  674. UNIT_ASSERT_EQUAL(ret, 0);
  675. ret = fy_node_sequence_append(fyn, fy_node_create_scalar(fyd, "bar", FY_NT));
  676. UNIT_ASSERT_EQUAL(ret, 0);
  677. ret = fy_node_sequence_append(fyn, fy_node_build_from_string(fyd, "{ baz: frooz }", FY_NT));
  678. UNIT_ASSERT_EQUAL(ret, 0);
  679. fy_document_set_root(fyd, fyn);
  680. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/0", FY_NT, FYNWF_DONT_FOLLOW), "foo", FY_NT) == true);
  681. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/1", FY_NT, FYNWF_DONT_FOLLOW), "bar", FY_NT) == true);
  682. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/2/baz", FY_NT, FYNWF_DONT_FOLLOW), "frooz", FY_NT) == true);
  683. fy_document_destroy(fyd);
  684. }
  685. Y_UNIT_TEST(doc_create_test_map1) {
  686. struct fy_document *fyd;
  687. struct fy_node *fyn, *fyn1, *fyn2, *fyn3;
  688. int ret;
  689. fyd = fy_document_create(NULL);
  690. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  691. fyn = fy_node_create_mapping(fyd);
  692. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  693. ret = fy_node_mapping_append(fyn,
  694. fy_node_build_from_string(fyd, "seq", FY_NT),
  695. fy_node_build_from_string(fyd, "[ zero, one ]", FY_NT));
  696. UNIT_ASSERT_EQUAL(ret, 0);
  697. ret = fy_node_mapping_append(fyn, NULL,
  698. fy_node_build_from_string(fyd, "value-of-null-key", FY_NT));
  699. UNIT_ASSERT_EQUAL(ret, 0);
  700. ret = fy_node_mapping_append(fyn,
  701. fy_node_build_from_string(fyd, "key-of-null-value", FY_NT), NULL);
  702. UNIT_ASSERT_EQUAL(ret, 0);
  703. fy_document_set_root(fyd, fyn);
  704. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/seq/0", FY_NT, FYNWF_DONT_FOLLOW), "zero", FY_NT) == true);
  705. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/seq/1", FY_NT, FYNWF_DONT_FOLLOW), "one", FY_NT) == true);
  706. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/''", FY_NT, FYNWF_DONT_FOLLOW), "value-of-null-key", FY_NT) == true);
  707. fyn1 = fy_node_by_path(fy_document_root(fyd), "/key-of-null-value", FY_NT, FYNWF_DONT_FOLLOW);
  708. UNIT_ASSERT_EQUAL(fyn1, NULL);
  709. /* try to append duplicate key (it should fail) */
  710. fyn2 = fy_node_build_from_string(fyd, "seq", FY_NT);
  711. UNIT_ASSERT_UNEQUAL(fyn2, NULL);
  712. fyn3 = fy_node_create_scalar(fyd, "dupl", FY_NT);
  713. UNIT_ASSERT_UNEQUAL(fyn3, NULL);
  714. ret = fy_node_mapping_append(fyn, fyn2, fyn3);
  715. UNIT_ASSERT_UNEQUAL(ret, 0);
  716. fy_node_free(fyn3);
  717. fy_node_free(fyn2);
  718. fy_document_destroy(fyd);
  719. }
  720. Y_UNIT_TEST(doc_insert_remove_seq) {
  721. struct fy_document *fyd;
  722. struct fy_node *fyn;
  723. int ret;
  724. fyd = fy_document_create(NULL);
  725. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  726. fy_document_set_root(fyd, fy_node_build_from_string(fyd, "[ one, two, four ]", FY_NT));
  727. /* check that the order is correct */
  728. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/0", FY_NT, FYNWF_DONT_FOLLOW), "one", FY_NT) == true);
  729. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/1", FY_NT, FYNWF_DONT_FOLLOW), "two", FY_NT) == true);
  730. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/2", FY_NT, FYNWF_DONT_FOLLOW), "four", FY_NT) == true);
  731. ret = fy_node_sequence_append(fy_document_root(fyd), fy_node_build_from_string(fyd, "five", FY_NT));
  732. UNIT_ASSERT_EQUAL(ret, 0);
  733. ret = fy_node_sequence_prepend(fy_document_root(fyd), fy_node_build_from_string(fyd, "zero", FY_NT));
  734. UNIT_ASSERT_EQUAL(ret, 0);
  735. ret = fy_node_sequence_insert_after(fy_document_root(fyd),
  736. fy_node_by_path(fy_document_root(fyd), "/2", FY_NT, FYNWF_DONT_FOLLOW),
  737. fy_node_build_from_string(fyd, "three", FY_NT));
  738. UNIT_ASSERT_EQUAL(ret, 0);
  739. ret = fy_node_sequence_insert_before(fy_document_root(fyd),
  740. fy_node_by_path(fy_document_root(fyd), "/3", FY_NT, FYNWF_DONT_FOLLOW),
  741. fy_node_build_from_string(fyd, "two-and-a-half", FY_NT));
  742. UNIT_ASSERT_EQUAL(ret, 0);
  743. fyn = fy_node_sequence_remove(fy_document_root(fyd),
  744. fy_node_by_path(fy_document_root(fyd), "/3", FY_NT, FYNWF_DONT_FOLLOW));
  745. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  746. fy_node_free(fyn);
  747. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/0", FY_NT, FYNWF_DONT_FOLLOW), "zero", FY_NT) == true);
  748. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/1", FY_NT, FYNWF_DONT_FOLLOW), "one", FY_NT) == true);
  749. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/2", FY_NT, FYNWF_DONT_FOLLOW), "two", FY_NT) == true);
  750. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/3", FY_NT, FYNWF_DONT_FOLLOW), "three", FY_NT) == true);
  751. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/4", FY_NT, FYNWF_DONT_FOLLOW), "four", FY_NT) == true);
  752. fy_document_destroy(fyd);
  753. }
  754. Y_UNIT_TEST(doc_insert_remove_map) {
  755. struct fy_document *fyd;
  756. struct fy_node *fyn;
  757. int ret;
  758. fyd = fy_document_build_from_string(NULL, "{ one: 1, two: 2, four: 4 }", FY_NT);
  759. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  760. /* check that the order is correct */
  761. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/one", FY_NT, FYNWF_DONT_FOLLOW), "1", FY_NT) == true);
  762. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/two", FY_NT, FYNWF_DONT_FOLLOW), "2", FY_NT) == true);
  763. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/four", FY_NT, FYNWF_DONT_FOLLOW), "4", FY_NT) == true);
  764. ret = fy_node_mapping_append(fy_document_root(fyd),
  765. fy_node_build_from_string(fyd, "three", FY_NT),
  766. fy_node_build_from_string(fyd, "3", FY_NT));
  767. UNIT_ASSERT_EQUAL(ret, 0);
  768. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/three", FY_NT, FYNWF_DONT_FOLLOW), "3", FY_NT) == true);
  769. ret = fy_node_mapping_prepend(fy_document_root(fyd),
  770. fy_node_build_from_string(fyd, "zero", FY_NT),
  771. fy_node_build_from_string(fyd, "0", FY_NT));
  772. UNIT_ASSERT_EQUAL(ret, 0);
  773. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/zero", FY_NT, FYNWF_DONT_FOLLOW), "0", FY_NT) == true);
  774. ret = fy_node_mapping_append(fy_document_root(fyd),
  775. fy_node_build_from_string(fyd, "two-and-a-half", FY_NT),
  776. fy_node_build_from_string(fyd, "2.5", FY_NT));
  777. UNIT_ASSERT_EQUAL(ret, 0);
  778. UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/two-and-a-half", FY_NT, FYNWF_DONT_FOLLOW), "2.5", FY_NT) == true);
  779. fyn = fy_node_mapping_remove_by_key(fy_document_root(fyd),
  780. fy_node_build_from_string(fyd, "two-and-a-half", FY_NT));
  781. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  782. fy_node_free(fyn);
  783. /* it must be removed */
  784. fyn = fy_node_by_path(fy_document_root(fyd), "/two-and-a-half", FY_NT, FYNWF_DONT_FOLLOW);
  785. UNIT_ASSERT_EQUAL(fyn, NULL);
  786. fy_document_destroy(fyd);
  787. }
  788. Y_UNIT_TEST(doc_sort) {
  789. struct fy_document *fyd;
  790. fy_node_pair *fynp;
  791. void *iter;
  792. int ret, count;
  793. fyd = fy_document_build_from_string(NULL, "{ a: 5, { z: bar }: 1, z: 7, "
  794. "[ a, b, c] : 3, { a: whee } : 2 , "
  795. "b: 6, [ z ]: 4 }", FY_NT);
  796. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  797. ret = fy_node_sort(fy_document_root(fyd), NULL, NULL);
  798. UNIT_ASSERT_EQUAL(ret, 0);
  799. /* check for correct count value */
  800. count = fy_node_mapping_item_count(fy_document_root(fyd));
  801. UNIT_ASSERT_EQUAL(count, 7);
  802. /* forward iterator first */
  803. iter = NULL;
  804. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  805. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  806. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "1");
  807. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  808. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  809. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "2");
  810. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  811. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  812. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "3");
  813. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  814. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  815. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "4");
  816. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  817. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  818. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "5");
  819. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  820. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  821. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "6");
  822. fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
  823. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  824. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "7");
  825. fy_document_destroy(fyd);
  826. }
  827. static char *join_docs(const char *tgt_text, const char *tgt_path,
  828. const char *src_text, const char *src_path,
  829. const char *emit_path)
  830. {
  831. struct fy_document *fyd_tgt, *fyd_src;
  832. struct fy_node *fyn_tgt, *fyn_src, *fyn_emit;
  833. char *output;
  834. int ret;
  835. /* insert which overwrites root ( <map> <- <scalar> ) */
  836. fyd_tgt = fy_document_build_from_string(NULL, tgt_text, FY_NT);
  837. UNIT_ASSERT_UNEQUAL(fyd_tgt, NULL);
  838. fyd_src = fy_document_build_from_string(NULL, src_text, FY_NT);
  839. UNIT_ASSERT_UNEQUAL(fyd_src, NULL);
  840. fyn_tgt = fy_node_by_path(fy_document_root(fyd_tgt), tgt_path, FY_NT, FYNWF_DONT_FOLLOW);
  841. UNIT_ASSERT_UNEQUAL(fyn_tgt, NULL);
  842. fyn_src = fy_node_by_path(fy_document_root(fyd_src), src_path, FY_NT, FYNWF_DONT_FOLLOW);
  843. UNIT_ASSERT_UNEQUAL(fyn_src, NULL);
  844. ret = fy_node_insert(fyn_tgt, fyn_src);
  845. UNIT_ASSERT_EQUAL(ret, 0);
  846. ret = fy_document_set_parent(fyd_tgt, fyd_src);
  847. UNIT_ASSERT_EQUAL(ret, 0);
  848. fyn_emit = fy_node_by_path(fy_document_root(fyd_tgt), emit_path, FY_NT, FYNWF_DONT_FOLLOW);
  849. UNIT_ASSERT_UNEQUAL(fyn_emit, NULL);
  850. output = fy_emit_node_to_string(fyn_emit, (fy_emitter_cfg_flags) (FYECF_MODE_FLOW_ONELINE | FYECF_WIDTH_INF));
  851. UNIT_ASSERT_UNEQUAL(output, NULL);
  852. fy_document_destroy(fyd_tgt);
  853. return output;
  854. }
  855. Y_UNIT_TEST(doc_join_scalar_to_scalar) {
  856. char *output;
  857. output = join_docs(
  858. "foo", "/", /* target */
  859. "bar", "/", /* source */
  860. "/"); /* emit path */
  861. UNIT_ASSERT_VALUES_EQUAL(output, TString("bar"));
  862. free(output);
  863. }
  864. Y_UNIT_TEST(doc_join_scalar_to_map) {
  865. char *output;
  866. output = join_docs(
  867. "{ foo: baz }", "/", /* target */
  868. "bar", "/", /* source */
  869. "/"); /* emit path */
  870. UNIT_ASSERT_VALUES_EQUAL(output, TString("bar"));
  871. free(output);
  872. }
  873. Y_UNIT_TEST(doc_join_scalar_to_seq) {
  874. char *output;
  875. output = join_docs(
  876. "[ foo, baz ]", "/", /* target */
  877. "bar", "/", /* source */
  878. "/"); /* emit path */
  879. UNIT_ASSERT_VALUES_EQUAL(output, TString("bar"));
  880. free(output);
  881. }
  882. Y_UNIT_TEST(doc_join_map_to_scalar) {
  883. char *output;
  884. output = join_docs(
  885. "foo", "/", /* target */
  886. "{bar: baz}", "/", /* source */
  887. "/"); /* emit path */
  888. UNIT_ASSERT_VALUES_EQUAL(output, TString("{bar: baz}"));
  889. free(output);
  890. }
  891. Y_UNIT_TEST(doc_join_map_to_seq) {
  892. char *output;
  893. output = join_docs(
  894. "[foo, frooz]", "/", /* target */
  895. "{bar: baz}", "/", /* source */
  896. "/"); /* emit path */
  897. UNIT_ASSERT_VALUES_EQUAL(output, TString("{bar: baz}"));
  898. free(output);
  899. }
  900. Y_UNIT_TEST(doc_join_map_to_map) {
  901. char *output;
  902. output = join_docs(
  903. "{foo: frooz}", "/", /* target */
  904. "{bar: baz}", "/", /* source */
  905. "/"); /* emit path */
  906. UNIT_ASSERT_VALUES_EQUAL(output, TString("{foo: frooz, bar: baz}"));
  907. free(output);
  908. }
  909. Y_UNIT_TEST(doc_join_seq_to_scalar) {
  910. char *output;
  911. output = join_docs(
  912. "foo", "/", /* target */
  913. "[bar, baz]", "/", /* source */
  914. "/"); /* emit path */
  915. UNIT_ASSERT_VALUES_EQUAL(output, TString("[bar, baz]"));
  916. free(output);
  917. }
  918. Y_UNIT_TEST(doc_join_seq_to_seq) {
  919. char *output;
  920. output = join_docs(
  921. "[foo, frooz]", "/", /* target */
  922. "[bar, baz]", "/", /* source */
  923. "/"); /* emit path */
  924. UNIT_ASSERT_VALUES_EQUAL(output, TString("[foo, frooz, bar, baz]"));
  925. free(output);
  926. }
  927. Y_UNIT_TEST(doc_join_seq_to_map) {
  928. char *output;
  929. output = join_docs(
  930. "{foo: frooz}", "/", /* target */
  931. "[bar, baz]", "/", /* source */
  932. "/"); /* emit path */
  933. UNIT_ASSERT_VALUES_EQUAL(output, TString("[bar, baz]"));
  934. free(output);
  935. }
  936. Y_UNIT_TEST(doc_join_tags) {
  937. char *output;
  938. output = join_docs(
  939. "%TAG !a! tag:a.com,2019:\n"
  940. "---\n"
  941. "- !a!foo\n"
  942. " foo: bar\n", "/",
  943. "%TAG !b! tag:b.com,2019:\n"
  944. "---\n"
  945. "- !b!bar\n"
  946. " something: other\n", "/",
  947. "/");
  948. UNIT_ASSERT_VALUES_EQUAL(output, TString("[!a!foo {foo: bar}, !b!bar {something: other}]"));
  949. free(output);
  950. }
  951. Y_UNIT_TEST(doc_build_with_tags) {
  952. struct fy_document *fyd;
  953. struct fy_node *fyn;
  954. struct fy_token *fyt;
  955. char *buf;
  956. int rc;
  957. /* build document */
  958. fyd = fy_document_create(NULL);
  959. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  960. /* create a sequence and set it as root */
  961. fyn = fy_node_create_sequence(fyd);
  962. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  963. fy_document_set_root(fyd, fyn);
  964. fyn = NULL;
  965. /* create a node, containing a new tag */
  966. fyn = fy_node_build_from_string(fyd, "%TAG !e! tag:example.com,2000:app/\n---\n- foo\n- !e!foo bar\n", FY_NT);
  967. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  968. /* append it to the root of the document */
  969. rc = fy_node_sequence_append(fy_document_root(fyd), fyn);
  970. UNIT_ASSERT_EQUAL(rc, 0);
  971. fyn = NULL;
  972. /* there must be a new tag */
  973. fyt = fy_document_tag_directive_lookup(fyd, "!e!");
  974. UNIT_ASSERT_UNEQUAL(fyt, NULL);
  975. /* try to build another, but with a different !e! prefix, it must fail */
  976. fyn = fy_node_build_from_string(fyd, "%TAG !e! tag:example.com,2019:app/\n---\n- foo\n- !e!foo bar\n", FY_NT);
  977. UNIT_ASSERT_EQUAL(fyn, NULL);
  978. /* manually add a tag */
  979. rc = fy_document_tag_directive_add(fyd, "!f!", "tag:example.com,2019:f/");
  980. UNIT_ASSERT_EQUAL(rc, 0);
  981. /* build a node with a tag that's already in the document */
  982. fyn = fy_node_build_from_string(fyd, "!f!whiz frooz\n", FY_NT);
  983. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  984. /* append it to the root of the document */
  985. rc = fy_node_sequence_append(fy_document_root(fyd), fyn);
  986. UNIT_ASSERT_EQUAL(rc, 0);
  987. fyn = NULL;
  988. /* convert to string */
  989. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  990. UNIT_ASSERT_UNEQUAL(buf, NULL);
  991. free(buf);
  992. fy_document_destroy(fyd);
  993. }
  994. Y_UNIT_TEST(doc_attach_check) {
  995. struct fy_document *fyd;
  996. struct fy_node *fyn, *fyn_seq, *fyn_map;
  997. struct fy_node *fyn_foo, *fyn_foo2, *fyn_bar, *fyn_baz;
  998. struct fy_node_pair *fynp;
  999. int rc;
  1000. /* build document */
  1001. fyd = fy_document_create(NULL);
  1002. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1003. /* create a sequence */
  1004. fyn_seq = fy_node_create_sequence(fyd);
  1005. UNIT_ASSERT_UNEQUAL(fyn_seq, NULL);
  1006. /* create a mapping */
  1007. fyn_map = fy_node_create_mapping(fyd);
  1008. UNIT_ASSERT_UNEQUAL(fyn_map, NULL);
  1009. /* create a simple scalar node foo */
  1010. fyn_foo = fy_node_build_from_string(fyd, "foo", FY_NT);
  1011. UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
  1012. /* create another simple scalar node bar */
  1013. fyn_bar = fy_node_build_from_string(fyd, "bar", FY_NT);
  1014. UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
  1015. /* create another simple scalar node baz */
  1016. fyn_baz = fy_node_build_from_string(fyd, "baz", FY_NT);
  1017. UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
  1018. /* create a scalar node with the same content as foo */
  1019. fyn_foo2 = fy_node_build_from_string(fyd, "foo", FY_NT);
  1020. UNIT_ASSERT_UNEQUAL(fyn_foo2, NULL);
  1021. /* set the root as the sequence */
  1022. rc = fy_document_set_root(fyd, fyn_seq);
  1023. UNIT_ASSERT_EQUAL(rc, 0);
  1024. /* should fail since fyn_seq is now attached */
  1025. rc = fy_document_set_root(fyd, fyn_seq);
  1026. UNIT_ASSERT_UNEQUAL(rc, 0);
  1027. /* freeing should fail, since it's attached too */
  1028. rc = fy_node_free(fyn_seq);
  1029. UNIT_ASSERT_UNEQUAL(rc, 0);
  1030. /* append it to the sequence */
  1031. rc = fy_node_sequence_append(fyn_seq, fyn_foo);
  1032. UNIT_ASSERT_EQUAL(rc, 0);
  1033. /* freeing should fail, since it's attached to the seq */
  1034. rc = fy_node_free(fyn_foo);
  1035. UNIT_ASSERT_UNEQUAL(rc, 0);
  1036. /* trying to append it to the sequence again should fail */
  1037. rc = fy_node_sequence_append(fyn_seq, fyn_foo);
  1038. UNIT_ASSERT_UNEQUAL(rc, 0);
  1039. /* append the mapping to the sequence */
  1040. rc = fy_node_sequence_append(fyn_seq, fyn_map);
  1041. UNIT_ASSERT_EQUAL(rc, 0);
  1042. /* this should fail, since foo is attached to the sequence */
  1043. rc = fy_node_mapping_append(fyn_map, fyn_foo, fyn_bar);
  1044. UNIT_ASSERT_UNEQUAL(rc, 0);
  1045. /* this should be OK, since foo2 is not attached */
  1046. rc = fy_node_mapping_append(fyn_map, fyn_foo2, fyn_bar);
  1047. UNIT_ASSERT_EQUAL(rc, 0);
  1048. /* remove foo from the sequence */
  1049. fyn = fy_node_sequence_remove(fyn_seq, fyn_foo);
  1050. UNIT_ASSERT_EQUAL(fyn, fyn_foo);
  1051. /* trying to append the same key should fail */
  1052. rc = fy_node_mapping_append(fyn_map, fyn_foo, NULL);
  1053. UNIT_ASSERT_UNEQUAL(rc, 0);
  1054. /* append the baz: NULL mapping */
  1055. rc = fy_node_mapping_append(fyn_map, fyn_baz, NULL);
  1056. UNIT_ASSERT_EQUAL(rc, 0);
  1057. /* get the baz: null node pair */
  1058. fynp = fy_node_mapping_lookup_pair(fyn_map, fyn_baz);
  1059. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  1060. UNIT_ASSERT_EQUAL(fy_node_pair_key(fynp), fyn_baz);
  1061. UNIT_ASSERT_EQUAL(fy_node_pair_value(fynp), NULL);
  1062. /* trying to set the same key in the mapping should fail */
  1063. rc = fy_node_pair_set_key(fynp, fyn_foo);
  1064. UNIT_ASSERT_UNEQUAL(rc, 0);
  1065. /* get the foo: bar node pair */
  1066. fynp = fy_node_mapping_lookup_pair(fyn_map, fyn_foo);
  1067. UNIT_ASSERT_UNEQUAL(fynp, NULL);
  1068. UNIT_ASSERT_EQUAL(fy_node_pair_key(fynp), fyn_foo2);
  1069. UNIT_ASSERT_EQUAL(fy_node_pair_value(fynp), fyn_bar);
  1070. /* we're setting the same key to the mapping, but that's OK
  1071. * since the key is replaced */
  1072. rc = fy_node_pair_set_key(fynp, fyn_foo);
  1073. UNIT_ASSERT_EQUAL(rc, 0);
  1074. /* fyn_foo has been freed */
  1075. fyn_foo = NULL;
  1076. /* convert to string */
  1077. rc = fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stderr);
  1078. UNIT_ASSERT_EQUAL(rc, 0);
  1079. fy_document_destroy(fyd);
  1080. }
  1081. Y_UNIT_TEST(manual_scalar_esc) {
  1082. // FIXME removed \e because it works incorrectly in MS C
  1083. const std::span MANUAL_SCALAR_ESC = "\\\"\0\a\b\t\v\f\r\xc2\x85\xc2\xa0\xe2\x80\xa8\xe2\x80\xa9";
  1084. const TStringBuf MANUAL_SCALAR_ESC_TXT = "\"\\\\\\\"\\0\\a\\b\\t\\v\\f\\r\\N\\_\\L\\P\"";
  1085. const char *what = MANUAL_SCALAR_ESC.data();
  1086. size_t what_sz = MANUAL_SCALAR_ESC.size() - 1;
  1087. struct fy_document *fyd;
  1088. struct fy_node *fyn;
  1089. char *buf;
  1090. const char *buf2;
  1091. size_t sz2;
  1092. int rc;
  1093. /* build document */
  1094. fyd = fy_document_create(NULL);
  1095. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1096. /* create a manual scalar with all the escapes */
  1097. fyn = fy_node_create_scalar(fyd, what, what_sz);
  1098. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1099. fy_document_set_root(fyd, fyn);
  1100. fyn = NULL;
  1101. /* emit to a buffer */
  1102. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  1103. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1104. /* destroy the old document */
  1105. fy_document_destroy(fyd);
  1106. fyd = NULL;
  1107. /* verify that the resulting document is in the escaped form */
  1108. UNIT_ASSERT_VALUES_EQUAL(buf, TString(MANUAL_SCALAR_ESC_TXT) + "\n");
  1109. /* now load the result back and verify that it contains exactly the same */
  1110. fyd = fy_document_build_from_string(NULL, buf, FY_NT);
  1111. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1112. /* get the scalar content */
  1113. buf2 = fy_node_get_scalar(fy_document_root(fyd), &sz2);
  1114. UNIT_ASSERT_UNEQUAL(buf2, NULL);
  1115. /* sizes must match */
  1116. UNIT_ASSERT_EQUAL(what_sz, sz2);
  1117. /* and the strings too */
  1118. rc = memcmp(what, buf2, what_sz);
  1119. UNIT_ASSERT_EQUAL(rc, 0);
  1120. /* free the document */
  1121. fy_document_destroy(fyd);
  1122. fyd = NULL;
  1123. free(buf);
  1124. }
  1125. Y_UNIT_TEST(manual_scalar_quoted) {
  1126. const TStringBuf MANUAL_SCALAR_QUOTED = "&foo";
  1127. const TStringBuf MANUAL_SCALAR_QUOTED_TXT = "\"&foo\"";
  1128. const char *what = MANUAL_SCALAR_QUOTED.data();
  1129. size_t what_sz = MANUAL_SCALAR_QUOTED.length();
  1130. struct fy_document *fyd;
  1131. struct fy_node *fyn;
  1132. char *buf;
  1133. const char *buf2;
  1134. size_t sz2;
  1135. int rc;
  1136. /* build document */
  1137. fyd = fy_document_create(NULL);
  1138. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1139. /* create a manual scalar with all the escapes */
  1140. fyn = fy_node_create_scalar(fyd, what, what_sz);
  1141. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1142. fy_document_set_root(fyd, fyn);
  1143. fyn = NULL;
  1144. /* emit to a buffer */
  1145. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  1146. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1147. /* destroy the old document */
  1148. fy_document_destroy(fyd);
  1149. fyd = NULL;
  1150. /* verify that the resulting document is in the escaped form */
  1151. UNIT_ASSERT_VALUES_EQUAL(buf, TString(MANUAL_SCALAR_QUOTED_TXT) + "\n");
  1152. /* now load the result back and verify that it contains exactly the same */
  1153. fyd = fy_document_build_from_string(NULL, buf, FY_NT);
  1154. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1155. /* get the scalar content */
  1156. buf2 = fy_node_get_scalar(fy_document_root(fyd), &sz2);
  1157. UNIT_ASSERT_UNEQUAL(buf2, NULL);
  1158. /* sizes must match */
  1159. UNIT_ASSERT_EQUAL(what_sz, sz2);
  1160. /* and the strings too */
  1161. rc = memcmp(what, buf2, what_sz);
  1162. UNIT_ASSERT_EQUAL(rc, 0);
  1163. /* free the document */
  1164. fy_document_destroy(fyd);
  1165. fyd = NULL;
  1166. free(buf);
  1167. }
  1168. Y_UNIT_TEST(manual_scalar_copy) {
  1169. const TStringBuf MANUAL_SCALAR_COPY = "foo";
  1170. const char *what = MANUAL_SCALAR_COPY.data();
  1171. size_t what_sz = MANUAL_SCALAR_COPY.length();
  1172. char *what_copy;
  1173. struct fy_document *fyd;
  1174. struct fy_node *fyn;
  1175. char *buf;
  1176. /* build document */
  1177. fyd = fy_document_create(NULL);
  1178. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1179. what_copy = (char*)malloc(what_sz);
  1180. UNIT_ASSERT_UNEQUAL(what_copy, NULL);
  1181. memcpy(what_copy, what, what_sz);
  1182. /* create a manual scalar with all the escapes */
  1183. fyn = fy_node_create_scalar_copy(fyd, what_copy, what_sz);
  1184. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1185. /* free the data */
  1186. free(what_copy);
  1187. fy_document_set_root(fyd, fyn);
  1188. fyn = NULL;
  1189. /* emit to a buffer */
  1190. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  1191. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1192. /* verify that the resulting document is the one we used + '\n' */
  1193. UNIT_ASSERT_VALUES_EQUAL(buf, TString(MANUAL_SCALAR_COPY) + "\n");
  1194. /* destroy the old document */
  1195. fy_document_destroy(fyd);
  1196. fyd = NULL;
  1197. free(buf);
  1198. }
  1199. Y_UNIT_TEST(manual_scalarf) {
  1200. struct fy_document *fyd;
  1201. struct fy_node *fyn;
  1202. char *buf;
  1203. /* build document */
  1204. fyd = fy_document_create(NULL);
  1205. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1206. /* create a manual scalar using the printf interface */
  1207. fyn = fy_node_create_scalarf(fyd, "foo%d", 13);
  1208. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1209. fy_document_set_root(fyd, fyn);
  1210. fyn = NULL;
  1211. /* emit to a buffer */
  1212. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  1213. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1214. /* verify that the resulting document is the one we used + '\n' */
  1215. UNIT_ASSERT_VALUES_EQUAL(buf, TString("foo13\n"));
  1216. /* destroy the old document */
  1217. fy_document_destroy(fyd);
  1218. fyd = NULL;
  1219. free(buf);
  1220. }
  1221. Y_UNIT_TEST(manual_valid_anchor) {
  1222. struct fy_document *fyd;
  1223. struct fy_node *fyn;
  1224. int ret;
  1225. fyd = fy_document_create(NULL);
  1226. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1227. fyn = fy_node_create_sequence(fyd);
  1228. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1229. fy_document_set_root(fyd, fyn);
  1230. fyn = fy_node_create_scalar(fyd, "foo", FY_NT);
  1231. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1232. ret = fy_node_sequence_append(fy_document_root(fyd), fyn);
  1233. UNIT_ASSERT_EQUAL(ret, 0);
  1234. /* create a valid anchor */
  1235. ret = fy_node_set_anchor(fyn, "foo", FY_NT);
  1236. UNIT_ASSERT_EQUAL(ret, 0);
  1237. fy_document_destroy(fyd);
  1238. }
  1239. Y_UNIT_TEST(manual_invalid_anchor) {
  1240. struct fy_document *fyd;
  1241. struct fy_node *fyn;
  1242. int ret;
  1243. fyd = fy_document_create(NULL);
  1244. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1245. fyn = fy_node_create_sequence(fyd);
  1246. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1247. fy_document_set_root(fyd, fyn);
  1248. fyn = fy_node_create_scalar(fyd, "foo", FY_NT);
  1249. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1250. ret = fy_node_sequence_append(fy_document_root(fyd), fyn);
  1251. UNIT_ASSERT_EQUAL(ret, 0);
  1252. /* create an invalid anchor */
  1253. ret = fy_node_set_anchor(fyn, "*foo", FY_NT);
  1254. UNIT_ASSERT_UNEQUAL(ret, 0);
  1255. fy_document_destroy(fyd);
  1256. }
  1257. Y_UNIT_TEST(manual_anchor_removal) {
  1258. struct fy_document *fyd;
  1259. struct fy_node *fyn;
  1260. int ret;
  1261. fyd = fy_document_create(NULL);
  1262. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1263. fyn = fy_node_create_sequence(fyd);
  1264. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1265. fy_document_set_root(fyd, fyn);
  1266. fyn = fy_node_create_scalar(fyd, "foo", FY_NT);
  1267. UNIT_ASSERT_UNEQUAL(fyn, NULL);
  1268. ret = fy_node_sequence_append(fy_document_root(fyd), fyn);
  1269. UNIT_ASSERT_EQUAL(ret, 0);
  1270. /* create a valid anchor */
  1271. ret = fy_node_set_anchor(fyn, "foo", FY_NT);
  1272. UNIT_ASSERT_EQUAL(ret, 0);
  1273. fprintf(stderr, "---\n# with anchor\n");
  1274. fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stderr);
  1275. /* should fail (an anchor already exists) */
  1276. ret = fy_node_set_anchor(fyn, "bar", FY_NT);
  1277. UNIT_ASSERT_UNEQUAL(ret, 0);
  1278. /* should succeed */
  1279. ret = fy_node_remove_anchor(fyn);
  1280. UNIT_ASSERT_EQUAL(ret, 0);
  1281. fprintf(stderr, "---\n# without anchor\n");
  1282. fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stderr);
  1283. fy_document_destroy(fyd);
  1284. }
  1285. Y_UNIT_TEST(manual_block_flow_mix) {
  1286. struct fy_document *fyd;
  1287. struct fy_node *fyn_mapping, *fyn_key, *fyn_value;
  1288. char *buf;
  1289. int ret;
  1290. fyd = fy_document_build_from_string(NULL, "--- &root\n{\n}\n", FY_NT);
  1291. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1292. fyn_mapping = fy_document_root(fyd);
  1293. UNIT_ASSERT_UNEQUAL(fyn_mapping, NULL);
  1294. UNIT_ASSERT(fy_node_is_mapping(fyn_mapping) == true);
  1295. fyn_key = fy_node_create_scalar(fyd, "key", FY_NT);
  1296. UNIT_ASSERT_UNEQUAL(fyn_key, NULL);
  1297. fyn_value = fy_node_build_from_string(fyd, "|\n literal\n", FY_NT);
  1298. UNIT_ASSERT_UNEQUAL(fyn_value, NULL);
  1299. ret = fy_node_mapping_append(fyn_mapping, fyn_key, fyn_value);
  1300. UNIT_ASSERT_EQUAL(ret, 0);
  1301. /* emit document to buffer */
  1302. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  1303. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1304. /* destroy the first document */
  1305. fy_document_destroy(fyd);
  1306. fyd = NULL;
  1307. /* read the emitted document back */
  1308. fyd = fy_document_build_from_string(NULL, buf, FY_NT);
  1309. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1310. /* compare with expected result */
  1311. UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_by_path(fy_document_root(fyd), "/key", FY_NT, FYNWF_DONT_FOLLOW)), "literal\n");
  1312. /* destroy the second document */
  1313. fy_document_destroy(fyd);
  1314. fyd = NULL;
  1315. free(buf);
  1316. }
  1317. /* FIXME
  1318. * This test is disabled because we can't make compatible
  1319. * alloca-based API on windows because original library
  1320. * uses gcc inline block extensions it will be uncommented
  1321. * when new API will be introduced
  1322. */
  1323. #if false
  1324. Y_UNIT_TEST(alloca_check) {
  1325. struct fy_document *fyd;
  1326. char *buf;
  1327. const char *abuf;
  1328. /* build document */
  1329. fyd = fy_document_build_from_string(NULL, "{ "
  1330. "foo: 10, bar : [ ten, 20 ], baz:{ frob: boo, deep: { deeper: yet } }, "
  1331. "}", FY_NT);
  1332. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1333. /* fy_emit_document_to_string*() */
  1334. buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
  1335. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1336. abuf = fy_emit_document_to_string_alloca(fyd, FYECF_MODE_FLOW_ONELINE);
  1337. UNIT_ASSERT_UNEQUAL(abuf, NULL);
  1338. UNIT_ASSERT_VALUES_EQUAL(buf, abuf);
  1339. free(buf);
  1340. /* fy_emit_node_to_string*() */
  1341. buf = fy_emit_node_to_string(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW), FYECF_MODE_FLOW_ONELINE);
  1342. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1343. abuf = fy_emit_node_to_string_alloca(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW), FYECF_MODE_FLOW_ONELINE);
  1344. UNIT_ASSERT_UNEQUAL(abuf, NULL);
  1345. UNIT_ASSERT_VALUES_EQUAL(buf, abuf);
  1346. free(buf);
  1347. /* path check eq */
  1348. buf = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW));
  1349. UNIT_ASSERT_UNEQUAL(buf, NULL);
  1350. UNIT_ASSERT_VALUES_EQUAL(buf, "/foo");
  1351. abuf = fy_node_get_path_alloca(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW));
  1352. UNIT_ASSERT_UNEQUAL(abuf, NULL);
  1353. UNIT_ASSERT_VALUES_EQUAL(abuf, "/foo");
  1354. UNIT_ASSERT_VALUES_EQUAL(buf, abuf);
  1355. free(buf);
  1356. /* check that a bad path is "" */
  1357. abuf = fy_node_get_path_alloca(NULL);
  1358. UNIT_ASSERT_UNEQUAL(abuf, NULL);
  1359. UNIT_ASSERT_VALUES_EQUAL(abuf, "");
  1360. fy_document_destroy(fyd);
  1361. }
  1362. #endif
  1363. Y_UNIT_TEST(scanf_check) {
  1364. struct fy_document *fyd;
  1365. struct fy_node *fyn_root;
  1366. int ret, ival;
  1367. char sval[256];
  1368. /* build document */
  1369. fyd = fy_document_build_from_string(NULL, "{ "
  1370. "foo: 10, bar : 20, baz:{ frob: boo }, "
  1371. "frooz: [ 1, { key: value }, three ]"
  1372. "}", FY_NT);
  1373. UNIT_ASSERT_UNEQUAL(fyd, NULL);
  1374. fyn_root = fy_document_root(fyd);
  1375. UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
  1376. /* check scanf accesses to scalars */
  1377. ret = fy_node_scanf(fyn_root, "/foo %d", &ival);
  1378. UNIT_ASSERT_EQUAL(ret, 1);
  1379. UNIT_ASSERT_EQUAL(ival, 10);
  1380. ret = fy_node_scanf(fyn_root, "/bar %d", &ival);
  1381. UNIT_ASSERT_EQUAL(ret, 1);
  1382. UNIT_ASSERT_EQUAL(ival, 20);
  1383. ret = fy_node_scanf(fyn_root, "/baz/frob %s", sval);
  1384. UNIT_ASSERT_EQUAL(ret, 1);
  1385. UNIT_ASSERT_VALUES_EQUAL(sval, "boo");
  1386. ret = fy_node_scanf(fyn_root, "/frooz/0 %d", &ival);
  1387. UNIT_ASSERT_EQUAL(ret, 1);
  1388. UNIT_ASSERT_EQUAL(ival, 1);
  1389. ret = fy_node_scanf(fyn_root, "/frooz/1/key %s", sval);
  1390. UNIT_ASSERT_EQUAL(ret, 1);
  1391. UNIT_ASSERT_VALUES_EQUAL(sval, "value");
  1392. ret = fy_node_scanf(fyn_root, "/frooz/2 %s", sval);
  1393. UNIT_ASSERT_EQUAL(ret, 1);
  1394. UNIT_ASSERT_VALUES_EQUAL(sval, "three");
  1395. fy_document_destroy(fyd);
  1396. }
  1397. }