12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <limits.h>
- #include <span>
- #include <contrib/libs/libfyaml/include/libfyaml.h>
- #include <library/cpp/testing/unittest/registar.h>
- /*
- These tests are ported from https://github.com/pantoniou/libfyaml/blob/master/test/libfyaml-test-core.c
- To check windows compatibility and avoid possible internal patch issues
- */
- Y_UNIT_TEST_SUITE(LibFyamlCore) {
- Y_UNIT_TEST(doc_build_simple) {
- struct fy_document *fyd;
- /* setup */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* cleanup */
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_build_parse_check) {
- struct fy_document *fyd;
- char *buf;
- /* build document (with comments, newlines etc) */
- fyd = fy_document_build_from_string(NULL, "#comment\n[ 42, \n 12 ] # comment\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* convert to string */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString("[42, 12]\n"));
- free(buf);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_build_scalar) {
- struct fy_document *fyd;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "plain scalar # comment", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_document_root(fyd)), TString("plain scalar"));
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_build_sequence) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int count;
- void *iter;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "[ 10, 11, foo ] # comment", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* check for correct count value */
- count = fy_node_sequence_item_count(fy_document_root(fyd));
- UNIT_ASSERT_EQUAL(count, 3);
- /* try forward iterator first */
- iter = NULL;
- /* 0 */
- fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
- /* 1 */
- fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
- /* 2 */
- fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
- /* final, iterator must return NULL */
- fyn = fy_node_sequence_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_EQUAL(fyn, NULL);
- /* reverse iterator */
- iter = NULL;
- /* 2 */
- fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
- /* 1 */
- fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
- /* 0 */
- fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
- /* final, iterator must return NULL */
- fyn = fy_node_sequence_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_EQUAL(fyn, NULL);
- /* do forward index based accesses */
- /* 0 */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 0);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
- /* 1 */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 1);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
- /* 2 */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 2);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
- /* 3, it must not exist */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), 3);
- UNIT_ASSERT_EQUAL(fyn, NULL);
- /* do backward index based accesses */
- /* 2 */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -1);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("foo"));
- /* 1 */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -2);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("11"));
- /* 0 */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -3);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn), TString("10"));
- /* -1, it must not exist */
- fyn = fy_node_sequence_get_by_index(fy_document_root(fyd), -4);
- UNIT_ASSERT_EQUAL(fyn, NULL);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_build_mapping) {
- struct fy_document *fyd;
- struct fy_node_pair *fynp;
- int count;
- void *iter;
- fyd = fy_document_build_from_string(NULL, "{ foo: 10, bar : 20, baz: [100, 101], [frob, 1]: boo }", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* check for correct count value */
- count = fy_node_mapping_item_count(fy_document_root(fyd));
- UNIT_ASSERT_EQUAL(count, 4);
- /* forward iterator first */
- iter = NULL;
- /* 0 */
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("foo"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("10"));
- /* 1 */
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("bar"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("20"));
- /* 2 */
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("baz"));
- UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_value(fynp)), 2);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 0)), TString("100"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 1)), TString("101"));
- /* 3 */
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_key(fynp)), 2);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 0)), TString("frob"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 1)), TString("1"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("boo"));
- /* 4, it must not exist */
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_EQUAL(fynp, NULL);
- /* reverse iterator */
- iter = NULL;
- /* 3 */
- fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_key(fynp)), 2);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 0)), TString("frob"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_key(fynp), 1)), TString("1"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("boo"));
- /* 2 */
- fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("baz"));
- UNIT_ASSERT_EQUAL(fy_node_sequence_item_count(fy_node_pair_value(fynp)), 2);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 0)), TString("100"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_sequence_get_by_index(fy_node_pair_value(fynp), 1)), TString("101"));
- /* 1 */
- fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("bar"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("20"));
- /* 0 */
- fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_key(fynp)), TString("foo"));
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), TString("10"));
- /* -1, it must not exist */
- fynp = fy_node_mapping_reverse_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_EQUAL(fynp, NULL);
- /* key lookups (note how only the contents are compared) */
- UNIT_ASSERT(fy_node_compare_string(fy_node_mapping_lookup_by_string(fy_document_root(fyd), "foo", FY_NT), "10", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_mapping_lookup_by_string(fy_document_root(fyd), "bar", FY_NT), "20", FY_NT) == true);
- 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);
- 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);
- fy_document_destroy(fyd);
- fyd = NULL;
- }
- Y_UNIT_TEST(doc_path_access) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "{ "
- "foo: 10, bar : 20, baz:{ frob: boo }, "
- "frooz: [ seq1, { key: value} ], \"zero\\0zero\" : 0, "
- "{ key2: value2 }: { key3: value3 } "
- "}", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* check that getting root node works */
- fyn = fy_node_by_path(fy_document_root(fyd), "/", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fy_document_root(fyd));
- /* check access to scalars */
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW), "10", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "bar", FY_NT, FYNWF_DONT_FOLLOW), "20", FY_NT) == true);
- 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);
- 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);
- 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);
- 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);
- 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);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_path_node) {
- struct fy_document *fyd;
- char *path;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "{ "
- "foo: 10, bar : 20, baz:{ frob: boo }, "
- "frooz: [ seq1, { key: value} ], \"zero\\0zero\" : 0, "
- "{ key2: value2 }: { key3: value3 } "
- "}", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_VALUES_EQUAL(path, TString("/"));
- free(path);
- path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/frooz", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_VALUES_EQUAL(path, TString("/frooz"));
- free(path);
- path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/frooz/0", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_VALUES_EQUAL(path, TString("/frooz/0"));
- free(path);
- path = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/{ key2: value2 }/key3", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_VALUES_EQUAL(path, TString("/{key2: value2}/key3"));
- free(path);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_path_parent) {
- struct fy_document *fyd;
- struct fy_node *fyn_root, *fyn_foo, *fyn_bar, *fyn_baz, *fyn_frob, *fyn_ten;
- struct fy_node *fyn_deep, *fyn_deeper;
- char *path;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "{ "
- "foo: 10, bar : [ ten, 20 ], baz:{ frob: boo, deep: { deeper: yet } }, "
- "}", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- fyn_bar = fy_node_by_path(fyn_root, "/bar", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
- fyn_baz = fy_node_by_path(fyn_root, "/baz", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
- fyn_frob = fy_node_by_path(fyn_root, "/baz/frob", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_frob, NULL);
- fyn_ten = fy_node_by_path(fyn_root, "/bar/0", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_ten, NULL);
- fyn_deep = fy_node_by_path(fyn_root, "/baz/deep", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_deep, NULL);
- fyn_deeper = fy_node_by_path(fyn_root, "/baz/deep/deeper", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_deeper, NULL);
- /* check parent paths of foo, frob, ten */
- path = fy_node_get_parent_address(fyn_foo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("foo"));
- free(path);
- path = fy_node_get_parent_address(fyn_frob);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("frob"));
- free(path);
- path = fy_node_get_parent_address(fyn_ten);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("0"));
- free(path);
- /* check relative paths to root */
- path = fy_node_get_path_relative_to(NULL, fyn_foo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("foo"));
- free(path);
- path = fy_node_get_path_relative_to(fyn_root, fyn_foo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("foo"));
- free(path);
- path = fy_node_get_path_relative_to(fyn_root, fyn_frob);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("baz/frob"));
- free(path);
- path = fy_node_get_path_relative_to(fyn_root, fyn_ten);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("bar/0"));
- free(path);
- /* check relative paths to other parents */
- path = fy_node_get_path_relative_to(fyn_baz, fyn_frob);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("frob"));
- free(path);
- path = fy_node_get_path_relative_to(fyn_bar, fyn_ten);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("0"));
- free(path);
- path = fy_node_get_path_relative_to(fyn_baz, fyn_deeper);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("deep/deeper"));
- free(path);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_short_path) {
- struct fy_document *fyd;
- struct fy_node *fyn_root, *fyn_foo, *fyn_notfoo, *fyn_bar, *fyn_baz;
- const char *str;
- /* build document */
- fyd = fy_document_build_from_string(NULL,
- "--- &r\n"
- " foo: &f\n"
- " bar: [ 0, two, baz: what ]\n"
- " frob: true\n"
- " notfoo: false\n"
- , FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- fyn_notfoo = fy_node_by_path(fyn_root, "/notfoo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_notfoo, NULL);
- fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
- fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/2/baz", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
- str = fy_node_get_short_path(fyn_root);
- UNIT_ASSERT_UNEQUAL(str, NULL);
- UNIT_ASSERT_VALUES_EQUAL(str, "*r");
- UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_root);
- free((void*)str);
- str = fy_node_get_short_path(fyn_foo);
- UNIT_ASSERT_UNEQUAL(str, NULL);
- UNIT_ASSERT_VALUES_EQUAL(str, "*f");
- UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_foo);
- free((void*)str);
- str = fy_node_get_short_path(fyn_notfoo);
- UNIT_ASSERT_UNEQUAL(str, NULL);
- UNIT_ASSERT_VALUES_EQUAL(str, "*r/notfoo");
- UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_notfoo);
- free((void*)str);
- str = fy_node_get_short_path(fyn_bar);
- UNIT_ASSERT_UNEQUAL(str, NULL);
- UNIT_ASSERT_VALUES_EQUAL(str, "*f/bar");
- UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_bar);
- free((void*)str);
- str = fy_node_get_short_path(fyn_baz);
- UNIT_ASSERT_UNEQUAL(str, NULL);
- UNIT_ASSERT_VALUES_EQUAL(str, "*f/bar/2/baz");
- UNIT_ASSERT_EQUAL(fy_node_by_path(fy_document_root(fyd), str, FY_NT, FYNWF_FOLLOW), fyn_baz);
- free((void*)str);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_scalar_path) {
- struct fy_document *fyd;
- struct fy_node *fyn_root, *fyn_foo;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "--- foo\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- /* get the scalar root and verify */
- fyn_foo = fy_node_by_path(fyn_root, "/", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fyn_foo), "foo");
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_scalar_path_array) {
- struct fy_document *fyd;
- struct fy_node *fyn_root, *fynt;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "--- [ foo, bar, baz ]\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- /* get the scalars in the array and verify */
- fynt = fy_node_by_path(fyn_root, "/0", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fynt, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fynt), "foo");
- fynt = fy_node_by_path(fyn_root, "/1", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fynt, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fynt), "bar");
- fynt = fy_node_by_path(fyn_root, "/2", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fynt, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fynt), "baz");
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_nearest_anchor) {
- struct fy_document *fyd;
- struct fy_node *fyn, *fyn_root, *fyn_foo, *fyn_notfoo, *fyn_bar, *fyn_baz;
- /* build document */
- fyd = fy_document_build_from_string(NULL,
- "--- &r\n"
- " foo: &f\n"
- " bar: [ 0, two, baz: what ]\n"
- " frob: true\n"
- " notfoo: false\n"
- , FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- fyn_notfoo = fy_node_by_path(fyn_root, "/notfoo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_notfoo, NULL);
- fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
- fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/2/baz", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
- /* get nearest anchor of root (is root) */
- fyn = fy_anchor_node(fy_node_get_nearest_anchor(fyn_root));
- UNIT_ASSERT_EQUAL(fyn, fyn_root);
- /* get nearest anchor of notfoo (is root) */
- fyn = fy_anchor_node(fy_node_get_nearest_anchor(fyn_notfoo));
- UNIT_ASSERT_EQUAL(fyn, fyn_root);
- /* get nearest anchor of baz (is foo) */
- fyn = fy_anchor_node(fy_node_get_nearest_anchor(fyn_baz));
- UNIT_ASSERT_EQUAL(fyn, fyn_foo);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_references) {
- struct fy_document *fyd;
- struct fy_node *fyn, *fyn_ref, *fyn_root, *fyn_foo, *fyn_notfoo, *fyn_bar, *fyn_baz;
- char *path;
- /* build document */
- fyd = fy_document_build_from_string(NULL,
- "---\n"
- " foo: &f\n"
- " bar: [ 0, two, baz: what ]\n"
- " frob: true\n"
- " notfoo: false\n"
- , FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- fyn_notfoo = fy_node_by_path(fyn_root, "/notfoo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_notfoo, NULL);
- fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
- fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/2/baz", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
- /* get reference to root */
- path = fy_node_get_reference(fyn_root);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*/"));
- free(path);
- /* get reference to /foo */
- path = fy_node_get_reference(fyn_foo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*f"));
- free(path);
- /* get reference to /notfoo */
- path = fy_node_get_reference(fyn_notfoo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*/notfoo"));
- free(path);
- /* get reference to /foo/bar/2/baz */
- path = fy_node_get_reference(fyn_baz);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*/foo/bar/2/baz"));
- free(path);
- /* create reference to root and verify it points there */
- fyn_ref = fy_node_create_reference(fyn_root);
- UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_root);
- fy_node_free(fyn_ref);
- /* get reference to /foo */
- fyn_ref = fy_node_create_reference(fyn_foo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_foo);
- fy_node_free(fyn_ref);
- /* get reference to /notfoo */
- fyn_ref = fy_node_create_reference(fyn_notfoo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_notfoo);
- fy_node_free(fyn_ref);
- /* get reference to /bar */
- fyn_ref = fy_node_create_reference(fyn_bar);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_bar);
- fy_node_free(fyn_ref);
- /* get reference to /baz */
- fyn_ref = fy_node_create_reference(fyn_baz);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_baz);
- fy_node_free(fyn_ref);
- /* get relative reference to /foo starting at / */
- path = fy_node_get_relative_reference(fyn_root, fyn_foo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*/foo"));
- free(path);
- /* get relative reference to /foo/bar/2/baz starting at / */
- path = fy_node_get_relative_reference(fyn_root, fyn_baz);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*/foo/bar/2/baz"));
- free(path);
- /* get relative reference to /foo/bar/2/baz starting at /foo */
- path = fy_node_get_relative_reference(fyn_foo, fyn_baz);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*f/bar/2/baz"));
- free(path);
- /* get relative reference to /notfoo at /foo (will return absolute) */
- path = fy_node_get_relative_reference(fyn_foo, fyn_notfoo);
- UNIT_ASSERT_UNEQUAL(path, NULL);
- UNIT_ASSERT_VALUES_EQUAL(path, TString("*/notfoo"));
- free(path);
- /* create relative reference to /foo starting at / */
- fyn_ref = fy_node_create_relative_reference(fyn_root, fyn_foo);
- UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_foo);
- fy_node_free(fyn_ref);
- /* create relative reference to /foo/bar/2/baz starting at / */
- fyn_ref = fy_node_create_relative_reference(fyn_root, fyn_baz);
- UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_baz);
- fy_node_free(fyn_ref);
- /* create relative reference to /foo/bar/2/baz starting at /foo */
- fyn_ref = fy_node_create_relative_reference(fyn_foo, fyn_baz);
- UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_baz);
- fy_node_free(fyn_ref);
- /* create relative reference to /notfoo starting at /foo (will use absolute) */
- fyn_ref = fy_node_create_relative_reference(fyn_foo, fyn_notfoo);
- UNIT_ASSERT_UNEQUAL(fyn_ref, NULL);
- fyn = fy_node_resolve_alias(fyn_ref);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_notfoo);
- fy_node_free(fyn_ref);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_nearest_child_of) {
- struct fy_document *fyd;
- struct fy_node *fyn, *fyn_root, *fyn_foo, *fyn_bar, *fyn_baz;
- /* build document */
- fyd = fy_document_build_from_string(NULL,
- "foo:\n"
- " bar:\n"
- " barz: [ zero, baz: true ]\n"
- " frooz: notfoo\n"
- , FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- fyn_foo = fy_node_by_path(fyn_root, "/foo", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- fyn_bar = fy_node_by_path(fyn_root, "/foo/bar", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
- fyn_baz = fy_node_by_path(fyn_root, "/foo/bar/barz/1/baz", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
- /* nearest child to the root of /foo is /foo */
- fyn = fy_node_get_nearest_child_of(fyn_root, fyn_foo);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_foo);
- /* nearest child to the root of /foo/bar/barz/1/baz is /foo */
- fyn = fy_node_get_nearest_child_of(fyn_root, fyn_baz);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_foo);
- /* nearest child to foo of /foo/bar/barz/1/baz is /foo/bar */
- fyn = fy_node_get_nearest_child_of(fyn_foo, fyn_baz);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- UNIT_ASSERT_EQUAL(fyn, fyn_bar);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_create_empty_seq1) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_build_from_string(fyd, "[ ]", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- /* convert to string */
- buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString("[]"));
- free(buf);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_create_empty_seq2) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- /* convert to string */
- buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString("[]"));
- free(buf);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_create_empty_map1) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_build_from_string(fyd, "{ }", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- /* convert to string */
- buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString("{}"));
- free(buf);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_create_empty_map2) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_mapping(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- /* convert to string */
- buf = fy_emit_node_to_string(fy_document_root(fyd), FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString("{}"));
- free(buf);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_create_test_seq1) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int ret;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- ret = fy_node_sequence_append(fyn, fy_node_create_scalar(fyd, "foo", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_sequence_append(fyn, fy_node_create_scalar(fyd, "bar", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_sequence_append(fyn, fy_node_build_from_string(fyd, "{ baz: frooz }", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- fy_document_set_root(fyd, fyn);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/0", FY_NT, FYNWF_DONT_FOLLOW), "foo", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/1", FY_NT, FYNWF_DONT_FOLLOW), "bar", FY_NT) == true);
- 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);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_create_test_map1) {
- struct fy_document *fyd;
- struct fy_node *fyn, *fyn1, *fyn2, *fyn3;
- int ret;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_mapping(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- ret = fy_node_mapping_append(fyn,
- fy_node_build_from_string(fyd, "seq", FY_NT),
- fy_node_build_from_string(fyd, "[ zero, one ]", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_mapping_append(fyn, NULL,
- fy_node_build_from_string(fyd, "value-of-null-key", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_mapping_append(fyn,
- fy_node_build_from_string(fyd, "key-of-null-value", FY_NT), NULL);
- UNIT_ASSERT_EQUAL(ret, 0);
- fy_document_set_root(fyd, fyn);
- 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);
- 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);
- 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);
- fyn1 = fy_node_by_path(fy_document_root(fyd), "/key-of-null-value", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_EQUAL(fyn1, NULL);
- /* try to append duplicate key (it should fail) */
- fyn2 = fy_node_build_from_string(fyd, "seq", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn2, NULL);
- fyn3 = fy_node_create_scalar(fyd, "dupl", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn3, NULL);
- ret = fy_node_mapping_append(fyn, fyn2, fyn3);
- UNIT_ASSERT_UNEQUAL(ret, 0);
- fy_node_free(fyn3);
- fy_node_free(fyn2);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_insert_remove_seq) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int ret;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fy_document_set_root(fyd, fy_node_build_from_string(fyd, "[ one, two, four ]", FY_NT));
- /* check that the order is correct */
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/0", FY_NT, FYNWF_DONT_FOLLOW), "one", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/1", FY_NT, FYNWF_DONT_FOLLOW), "two", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/2", FY_NT, FYNWF_DONT_FOLLOW), "four", FY_NT) == true);
- ret = fy_node_sequence_append(fy_document_root(fyd), fy_node_build_from_string(fyd, "five", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_sequence_prepend(fy_document_root(fyd), fy_node_build_from_string(fyd, "zero", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_sequence_insert_after(fy_document_root(fyd),
- fy_node_by_path(fy_document_root(fyd), "/2", FY_NT, FYNWF_DONT_FOLLOW),
- fy_node_build_from_string(fyd, "three", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_node_sequence_insert_before(fy_document_root(fyd),
- fy_node_by_path(fy_document_root(fyd), "/3", FY_NT, FYNWF_DONT_FOLLOW),
- fy_node_build_from_string(fyd, "two-and-a-half", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- fyn = fy_node_sequence_remove(fy_document_root(fyd),
- fy_node_by_path(fy_document_root(fyd), "/3", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_node_free(fyn);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/0", FY_NT, FYNWF_DONT_FOLLOW), "zero", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/1", FY_NT, FYNWF_DONT_FOLLOW), "one", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/2", FY_NT, FYNWF_DONT_FOLLOW), "two", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/3", FY_NT, FYNWF_DONT_FOLLOW), "three", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/4", FY_NT, FYNWF_DONT_FOLLOW), "four", FY_NT) == true);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_insert_remove_map) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int ret;
- fyd = fy_document_build_from_string(NULL, "{ one: 1, two: 2, four: 4 }", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* check that the order is correct */
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/one", FY_NT, FYNWF_DONT_FOLLOW), "1", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/two", FY_NT, FYNWF_DONT_FOLLOW), "2", FY_NT) == true);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/four", FY_NT, FYNWF_DONT_FOLLOW), "4", FY_NT) == true);
- ret = fy_node_mapping_append(fy_document_root(fyd),
- fy_node_build_from_string(fyd, "three", FY_NT),
- fy_node_build_from_string(fyd, "3", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/three", FY_NT, FYNWF_DONT_FOLLOW), "3", FY_NT) == true);
- ret = fy_node_mapping_prepend(fy_document_root(fyd),
- fy_node_build_from_string(fyd, "zero", FY_NT),
- fy_node_build_from_string(fyd, "0", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- UNIT_ASSERT(fy_node_compare_string(fy_node_by_path(fy_document_root(fyd), "/zero", FY_NT, FYNWF_DONT_FOLLOW), "0", FY_NT) == true);
- ret = fy_node_mapping_append(fy_document_root(fyd),
- fy_node_build_from_string(fyd, "two-and-a-half", FY_NT),
- fy_node_build_from_string(fyd, "2.5", FY_NT));
- UNIT_ASSERT_EQUAL(ret, 0);
- 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);
- fyn = fy_node_mapping_remove_by_key(fy_document_root(fyd),
- fy_node_build_from_string(fyd, "two-and-a-half", FY_NT));
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_node_free(fyn);
- /* it must be removed */
- fyn = fy_node_by_path(fy_document_root(fyd), "/two-and-a-half", FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_EQUAL(fyn, NULL);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_sort) {
- struct fy_document *fyd;
- fy_node_pair *fynp;
- void *iter;
- int ret, count;
- fyd = fy_document_build_from_string(NULL, "{ a: 5, { z: bar }: 1, z: 7, "
- "[ a, b, c] : 3, { a: whee } : 2 , "
- "b: 6, [ z ]: 4 }", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- ret = fy_node_sort(fy_document_root(fyd), NULL, NULL);
- UNIT_ASSERT_EQUAL(ret, 0);
- /* check for correct count value */
- count = fy_node_mapping_item_count(fy_document_root(fyd));
- UNIT_ASSERT_EQUAL(count, 7);
- /* forward iterator first */
- iter = NULL;
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "1");
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "2");
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "3");
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "4");
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "5");
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "6");
- fynp = fy_node_mapping_iterate(fy_document_root(fyd), &iter);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_pair_value(fynp)), "7");
- fy_document_destroy(fyd);
- }
- static char *join_docs(const char *tgt_text, const char *tgt_path,
- const char *src_text, const char *src_path,
- const char *emit_path)
- {
- struct fy_document *fyd_tgt, *fyd_src;
- struct fy_node *fyn_tgt, *fyn_src, *fyn_emit;
- char *output;
- int ret;
- /* insert which overwrites root ( <map> <- <scalar> ) */
- fyd_tgt = fy_document_build_from_string(NULL, tgt_text, FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd_tgt, NULL);
- fyd_src = fy_document_build_from_string(NULL, src_text, FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd_src, NULL);
- fyn_tgt = fy_node_by_path(fy_document_root(fyd_tgt), tgt_path, FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_tgt, NULL);
- fyn_src = fy_node_by_path(fy_document_root(fyd_src), src_path, FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_src, NULL);
- ret = fy_node_insert(fyn_tgt, fyn_src);
- UNIT_ASSERT_EQUAL(ret, 0);
- ret = fy_document_set_parent(fyd_tgt, fyd_src);
- UNIT_ASSERT_EQUAL(ret, 0);
- fyn_emit = fy_node_by_path(fy_document_root(fyd_tgt), emit_path, FY_NT, FYNWF_DONT_FOLLOW);
- UNIT_ASSERT_UNEQUAL(fyn_emit, NULL);
- output = fy_emit_node_to_string(fyn_emit, (fy_emitter_cfg_flags) (FYECF_MODE_FLOW_ONELINE | FYECF_WIDTH_INF));
- UNIT_ASSERT_UNEQUAL(output, NULL);
- fy_document_destroy(fyd_tgt);
- return output;
- }
- Y_UNIT_TEST(doc_join_scalar_to_scalar) {
- char *output;
- output = join_docs(
- "foo", "/", /* target */
- "bar", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("bar"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_scalar_to_map) {
- char *output;
- output = join_docs(
- "{ foo: baz }", "/", /* target */
- "bar", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("bar"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_scalar_to_seq) {
- char *output;
- output = join_docs(
- "[ foo, baz ]", "/", /* target */
- "bar", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("bar"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_map_to_scalar) {
- char *output;
- output = join_docs(
- "foo", "/", /* target */
- "{bar: baz}", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("{bar: baz}"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_map_to_seq) {
- char *output;
- output = join_docs(
- "[foo, frooz]", "/", /* target */
- "{bar: baz}", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("{bar: baz}"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_map_to_map) {
- char *output;
- output = join_docs(
- "{foo: frooz}", "/", /* target */
- "{bar: baz}", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("{foo: frooz, bar: baz}"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_seq_to_scalar) {
- char *output;
- output = join_docs(
- "foo", "/", /* target */
- "[bar, baz]", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("[bar, baz]"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_seq_to_seq) {
- char *output;
- output = join_docs(
- "[foo, frooz]", "/", /* target */
- "[bar, baz]", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("[foo, frooz, bar, baz]"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_seq_to_map) {
- char *output;
- output = join_docs(
- "{foo: frooz}", "/", /* target */
- "[bar, baz]", "/", /* source */
- "/"); /* emit path */
- UNIT_ASSERT_VALUES_EQUAL(output, TString("[bar, baz]"));
- free(output);
- }
- Y_UNIT_TEST(doc_join_tags) {
- char *output;
- output = join_docs(
- "%TAG !a! tag:a.com,2019:\n"
- "---\n"
- "- !a!foo\n"
- " foo: bar\n", "/",
- "%TAG !b! tag:b.com,2019:\n"
- "---\n"
- "- !b!bar\n"
- " something: other\n", "/",
- "/");
- UNIT_ASSERT_VALUES_EQUAL(output, TString("[!a!foo {foo: bar}, !b!bar {something: other}]"));
- free(output);
- }
- Y_UNIT_TEST(doc_build_with_tags) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- struct fy_token *fyt;
- char *buf;
- int rc;
- /* build document */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* create a sequence and set it as root */
- fyn = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = NULL;
- /* create a node, containing a new tag */
- fyn = fy_node_build_from_string(fyd, "%TAG !e! tag:example.com,2000:app/\n---\n- foo\n- !e!foo bar\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- /* append it to the root of the document */
- rc = fy_node_sequence_append(fy_document_root(fyd), fyn);
- UNIT_ASSERT_EQUAL(rc, 0);
- fyn = NULL;
- /* there must be a new tag */
- fyt = fy_document_tag_directive_lookup(fyd, "!e!");
- UNIT_ASSERT_UNEQUAL(fyt, NULL);
- /* try to build another, but with a different !e! prefix, it must fail */
- fyn = fy_node_build_from_string(fyd, "%TAG !e! tag:example.com,2019:app/\n---\n- foo\n- !e!foo bar\n", FY_NT);
- UNIT_ASSERT_EQUAL(fyn, NULL);
- /* manually add a tag */
- rc = fy_document_tag_directive_add(fyd, "!f!", "tag:example.com,2019:f/");
- UNIT_ASSERT_EQUAL(rc, 0);
- /* build a node with a tag that's already in the document */
- fyn = fy_node_build_from_string(fyd, "!f!whiz frooz\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- /* append it to the root of the document */
- rc = fy_node_sequence_append(fy_document_root(fyd), fyn);
- UNIT_ASSERT_EQUAL(rc, 0);
- fyn = NULL;
- /* convert to string */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- free(buf);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(doc_attach_check) {
- struct fy_document *fyd;
- struct fy_node *fyn, *fyn_seq, *fyn_map;
- struct fy_node *fyn_foo, *fyn_foo2, *fyn_bar, *fyn_baz;
- struct fy_node_pair *fynp;
- int rc;
- /* build document */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* create a sequence */
- fyn_seq = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_seq, NULL);
- /* create a mapping */
- fyn_map = fy_node_create_mapping(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_map, NULL);
- /* create a simple scalar node foo */
- fyn_foo = fy_node_build_from_string(fyd, "foo", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn_foo, NULL);
- /* create another simple scalar node bar */
- fyn_bar = fy_node_build_from_string(fyd, "bar", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn_bar, NULL);
- /* create another simple scalar node baz */
- fyn_baz = fy_node_build_from_string(fyd, "baz", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn_baz, NULL);
- /* create a scalar node with the same content as foo */
- fyn_foo2 = fy_node_build_from_string(fyd, "foo", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn_foo2, NULL);
- /* set the root as the sequence */
- rc = fy_document_set_root(fyd, fyn_seq);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* should fail since fyn_seq is now attached */
- rc = fy_document_set_root(fyd, fyn_seq);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* freeing should fail, since it's attached too */
- rc = fy_node_free(fyn_seq);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* append it to the sequence */
- rc = fy_node_sequence_append(fyn_seq, fyn_foo);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* freeing should fail, since it's attached to the seq */
- rc = fy_node_free(fyn_foo);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* trying to append it to the sequence again should fail */
- rc = fy_node_sequence_append(fyn_seq, fyn_foo);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* append the mapping to the sequence */
- rc = fy_node_sequence_append(fyn_seq, fyn_map);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* this should fail, since foo is attached to the sequence */
- rc = fy_node_mapping_append(fyn_map, fyn_foo, fyn_bar);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* this should be OK, since foo2 is not attached */
- rc = fy_node_mapping_append(fyn_map, fyn_foo2, fyn_bar);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* remove foo from the sequence */
- fyn = fy_node_sequence_remove(fyn_seq, fyn_foo);
- UNIT_ASSERT_EQUAL(fyn, fyn_foo);
- /* trying to append the same key should fail */
- rc = fy_node_mapping_append(fyn_map, fyn_foo, NULL);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* append the baz: NULL mapping */
- rc = fy_node_mapping_append(fyn_map, fyn_baz, NULL);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* get the baz: null node pair */
- fynp = fy_node_mapping_lookup_pair(fyn_map, fyn_baz);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_EQUAL(fy_node_pair_key(fynp), fyn_baz);
- UNIT_ASSERT_EQUAL(fy_node_pair_value(fynp), NULL);
- /* trying to set the same key in the mapping should fail */
- rc = fy_node_pair_set_key(fynp, fyn_foo);
- UNIT_ASSERT_UNEQUAL(rc, 0);
- /* get the foo: bar node pair */
- fynp = fy_node_mapping_lookup_pair(fyn_map, fyn_foo);
- UNIT_ASSERT_UNEQUAL(fynp, NULL);
- UNIT_ASSERT_EQUAL(fy_node_pair_key(fynp), fyn_foo2);
- UNIT_ASSERT_EQUAL(fy_node_pair_value(fynp), fyn_bar);
- /* we're setting the same key to the mapping, but that's OK
- * since the key is replaced */
- rc = fy_node_pair_set_key(fynp, fyn_foo);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* fyn_foo has been freed */
- fyn_foo = NULL;
- /* convert to string */
- rc = fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stderr);
- UNIT_ASSERT_EQUAL(rc, 0);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(manual_scalar_esc) {
- // FIXME removed \e because it works incorrectly in MS C
- const std::span MANUAL_SCALAR_ESC = "\\\"\0\a\b\t\v\f\r\xc2\x85\xc2\xa0\xe2\x80\xa8\xe2\x80\xa9";
- const TStringBuf MANUAL_SCALAR_ESC_TXT = "\"\\\\\\\"\\0\\a\\b\\t\\v\\f\\r\\N\\_\\L\\P\"";
- const char *what = MANUAL_SCALAR_ESC.data();
- size_t what_sz = MANUAL_SCALAR_ESC.size() - 1;
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- const char *buf2;
- size_t sz2;
- int rc;
- /* build document */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* create a manual scalar with all the escapes */
- fyn = fy_node_create_scalar(fyd, what, what_sz);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = NULL;
- /* emit to a buffer */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* destroy the old document */
- fy_document_destroy(fyd);
- fyd = NULL;
- /* verify that the resulting document is in the escaped form */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString(MANUAL_SCALAR_ESC_TXT) + "\n");
- /* now load the result back and verify that it contains exactly the same */
- fyd = fy_document_build_from_string(NULL, buf, FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* get the scalar content */
- buf2 = fy_node_get_scalar(fy_document_root(fyd), &sz2);
- UNIT_ASSERT_UNEQUAL(buf2, NULL);
- /* sizes must match */
- UNIT_ASSERT_EQUAL(what_sz, sz2);
- /* and the strings too */
- rc = memcmp(what, buf2, what_sz);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* free the document */
- fy_document_destroy(fyd);
- fyd = NULL;
- free(buf);
- }
- Y_UNIT_TEST(manual_scalar_quoted) {
- const TStringBuf MANUAL_SCALAR_QUOTED = "&foo";
- const TStringBuf MANUAL_SCALAR_QUOTED_TXT = "\"&foo\"";
- const char *what = MANUAL_SCALAR_QUOTED.data();
- size_t what_sz = MANUAL_SCALAR_QUOTED.length();
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- const char *buf2;
- size_t sz2;
- int rc;
- /* build document */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* create a manual scalar with all the escapes */
- fyn = fy_node_create_scalar(fyd, what, what_sz);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = NULL;
- /* emit to a buffer */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* destroy the old document */
- fy_document_destroy(fyd);
- fyd = NULL;
- /* verify that the resulting document is in the escaped form */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString(MANUAL_SCALAR_QUOTED_TXT) + "\n");
- /* now load the result back and verify that it contains exactly the same */
- fyd = fy_document_build_from_string(NULL, buf, FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* get the scalar content */
- buf2 = fy_node_get_scalar(fy_document_root(fyd), &sz2);
- UNIT_ASSERT_UNEQUAL(buf2, NULL);
- /* sizes must match */
- UNIT_ASSERT_EQUAL(what_sz, sz2);
- /* and the strings too */
- rc = memcmp(what, buf2, what_sz);
- UNIT_ASSERT_EQUAL(rc, 0);
- /* free the document */
- fy_document_destroy(fyd);
- fyd = NULL;
- free(buf);
- }
- Y_UNIT_TEST(manual_scalar_copy) {
- const TStringBuf MANUAL_SCALAR_COPY = "foo";
- const char *what = MANUAL_SCALAR_COPY.data();
- size_t what_sz = MANUAL_SCALAR_COPY.length();
- char *what_copy;
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- /* build document */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- what_copy = (char*)malloc(what_sz);
- UNIT_ASSERT_UNEQUAL(what_copy, NULL);
- memcpy(what_copy, what, what_sz);
- /* create a manual scalar with all the escapes */
- fyn = fy_node_create_scalar_copy(fyd, what_copy, what_sz);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- /* free the data */
- free(what_copy);
- fy_document_set_root(fyd, fyn);
- fyn = NULL;
- /* emit to a buffer */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* verify that the resulting document is the one we used + '\n' */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString(MANUAL_SCALAR_COPY) + "\n");
- /* destroy the old document */
- fy_document_destroy(fyd);
- fyd = NULL;
- free(buf);
- }
- Y_UNIT_TEST(manual_scalarf) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- char *buf;
- /* build document */
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* create a manual scalar using the printf interface */
- fyn = fy_node_create_scalarf(fyd, "foo%d", 13);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = NULL;
- /* emit to a buffer */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* verify that the resulting document is the one we used + '\n' */
- UNIT_ASSERT_VALUES_EQUAL(buf, TString("foo13\n"));
- /* destroy the old document */
- fy_document_destroy(fyd);
- fyd = NULL;
- free(buf);
- }
- Y_UNIT_TEST(manual_valid_anchor) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int ret;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = fy_node_create_scalar(fyd, "foo", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- ret = fy_node_sequence_append(fy_document_root(fyd), fyn);
- UNIT_ASSERT_EQUAL(ret, 0);
- /* create a valid anchor */
- ret = fy_node_set_anchor(fyn, "foo", FY_NT);
- UNIT_ASSERT_EQUAL(ret, 0);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(manual_invalid_anchor) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int ret;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = fy_node_create_scalar(fyd, "foo", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- ret = fy_node_sequence_append(fy_document_root(fyd), fyn);
- UNIT_ASSERT_EQUAL(ret, 0);
- /* create an invalid anchor */
- ret = fy_node_set_anchor(fyn, "*foo", FY_NT);
- UNIT_ASSERT_UNEQUAL(ret, 0);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(manual_anchor_removal) {
- struct fy_document *fyd;
- struct fy_node *fyn;
- int ret;
- fyd = fy_document_create(NULL);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn = fy_node_create_sequence(fyd);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- fy_document_set_root(fyd, fyn);
- fyn = fy_node_create_scalar(fyd, "foo", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn, NULL);
- ret = fy_node_sequence_append(fy_document_root(fyd), fyn);
- UNIT_ASSERT_EQUAL(ret, 0);
- /* create a valid anchor */
- ret = fy_node_set_anchor(fyn, "foo", FY_NT);
- UNIT_ASSERT_EQUAL(ret, 0);
- fprintf(stderr, "---\n# with anchor\n");
- fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stderr);
- /* should fail (an anchor already exists) */
- ret = fy_node_set_anchor(fyn, "bar", FY_NT);
- UNIT_ASSERT_UNEQUAL(ret, 0);
- /* should succeed */
- ret = fy_node_remove_anchor(fyn);
- UNIT_ASSERT_EQUAL(ret, 0);
- fprintf(stderr, "---\n# without anchor\n");
- fy_emit_document_to_fp(fyd, FYECF_MODE_FLOW_ONELINE, stderr);
- fy_document_destroy(fyd);
- }
- Y_UNIT_TEST(manual_block_flow_mix) {
- struct fy_document *fyd;
- struct fy_node *fyn_mapping, *fyn_key, *fyn_value;
- char *buf;
- int ret;
- fyd = fy_document_build_from_string(NULL, "--- &root\n{\n}\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_mapping = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_mapping, NULL);
- UNIT_ASSERT(fy_node_is_mapping(fyn_mapping) == true);
- fyn_key = fy_node_create_scalar(fyd, "key", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn_key, NULL);
- fyn_value = fy_node_build_from_string(fyd, "|\n literal\n", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyn_value, NULL);
- ret = fy_node_mapping_append(fyn_mapping, fyn_key, fyn_value);
- UNIT_ASSERT_EQUAL(ret, 0);
- /* emit document to buffer */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- /* destroy the first document */
- fy_document_destroy(fyd);
- fyd = NULL;
- /* read the emitted document back */
- fyd = fy_document_build_from_string(NULL, buf, FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* compare with expected result */
- UNIT_ASSERT_VALUES_EQUAL(fy_node_get_scalar0(fy_node_by_path(fy_document_root(fyd), "/key", FY_NT, FYNWF_DONT_FOLLOW)), "literal\n");
- /* destroy the second document */
- fy_document_destroy(fyd);
- fyd = NULL;
- free(buf);
- }
- /* FIXME
- * This test is disabled because we can't make compatible
- * alloca-based API on windows because original library
- * uses gcc inline block extensions it will be uncommented
- * when new API will be introduced
- */
- #if false
- Y_UNIT_TEST(alloca_check) {
- struct fy_document *fyd;
- char *buf;
- const char *abuf;
- /* build document */
- fyd = fy_document_build_from_string(NULL, "{ "
- "foo: 10, bar : [ ten, 20 ], baz:{ frob: boo, deep: { deeper: yet } }, "
- "}", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- /* fy_emit_document_to_string*() */
- buf = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- abuf = fy_emit_document_to_string_alloca(fyd, FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(abuf, NULL);
- UNIT_ASSERT_VALUES_EQUAL(buf, abuf);
- free(buf);
- /* fy_emit_node_to_string*() */
- buf = fy_emit_node_to_string(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW), FYECF_MODE_FLOW_ONELINE);
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- 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);
- UNIT_ASSERT_UNEQUAL(abuf, NULL);
- UNIT_ASSERT_VALUES_EQUAL(buf, abuf);
- free(buf);
- /* path check eq */
- buf = fy_node_get_path(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_UNEQUAL(buf, NULL);
- UNIT_ASSERT_VALUES_EQUAL(buf, "/foo");
- abuf = fy_node_get_path_alloca(fy_node_by_path(fy_document_root(fyd), "/foo", FY_NT, FYNWF_DONT_FOLLOW));
- UNIT_ASSERT_UNEQUAL(abuf, NULL);
- UNIT_ASSERT_VALUES_EQUAL(abuf, "/foo");
- UNIT_ASSERT_VALUES_EQUAL(buf, abuf);
- free(buf);
- /* check that a bad path is "" */
- abuf = fy_node_get_path_alloca(NULL);
- UNIT_ASSERT_UNEQUAL(abuf, NULL);
- UNIT_ASSERT_VALUES_EQUAL(abuf, "");
- fy_document_destroy(fyd);
- }
- #endif
- Y_UNIT_TEST(scanf_check) {
- struct fy_document *fyd;
- struct fy_node *fyn_root;
- int ret, ival;
- char sval[256];
- /* build document */
- fyd = fy_document_build_from_string(NULL, "{ "
- "foo: 10, bar : 20, baz:{ frob: boo }, "
- "frooz: [ 1, { key: value }, three ]"
- "}", FY_NT);
- UNIT_ASSERT_UNEQUAL(fyd, NULL);
- fyn_root = fy_document_root(fyd);
- UNIT_ASSERT_UNEQUAL(fyn_root, NULL);
- /* check scanf accesses to scalars */
- ret = fy_node_scanf(fyn_root, "/foo %d", &ival);
- UNIT_ASSERT_EQUAL(ret, 1);
- UNIT_ASSERT_EQUAL(ival, 10);
- ret = fy_node_scanf(fyn_root, "/bar %d", &ival);
- UNIT_ASSERT_EQUAL(ret, 1);
- UNIT_ASSERT_EQUAL(ival, 20);
- ret = fy_node_scanf(fyn_root, "/baz/frob %s", sval);
- UNIT_ASSERT_EQUAL(ret, 1);
- UNIT_ASSERT_VALUES_EQUAL(sval, "boo");
- ret = fy_node_scanf(fyn_root, "/frooz/0 %d", &ival);
- UNIT_ASSERT_EQUAL(ret, 1);
- UNIT_ASSERT_EQUAL(ival, 1);
- ret = fy_node_scanf(fyn_root, "/frooz/1/key %s", sval);
- UNIT_ASSERT_EQUAL(ret, 1);
- UNIT_ASSERT_VALUES_EQUAL(sval, "value");
- ret = fy_node_scanf(fyn_root, "/frooz/2 %s", sval);
- UNIT_ASSERT_EQUAL(ret, 1);
- UNIT_ASSERT_VALUES_EQUAL(sval, "three");
- fy_document_destroy(fyd);
- }
- }
|