node_test.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  1. //
  2. // Copyright (c) 2011-2019 Canonical Ltd
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package yaml_test
  16. import (
  17. "bytes"
  18. "fmt"
  19. "os"
  20. . "gopkg.in/check.v1"
  21. "gopkg.in/yaml.v3"
  22. "io"
  23. "strings"
  24. )
  25. var nodeTests = []struct {
  26. yaml string
  27. node yaml.Node
  28. }{
  29. {
  30. "null\n",
  31. yaml.Node{
  32. Kind: yaml.DocumentNode,
  33. Line: 1,
  34. Column: 1,
  35. Content: []*yaml.Node{{
  36. Kind: yaml.ScalarNode,
  37. Value: "null",
  38. Tag: "!!null",
  39. Line: 1,
  40. Column: 1,
  41. }},
  42. },
  43. }, {
  44. "[encode]null\n",
  45. yaml.Node{},
  46. }, {
  47. "foo\n",
  48. yaml.Node{
  49. Kind: yaml.DocumentNode,
  50. Line: 1,
  51. Column: 1,
  52. Content: []*yaml.Node{{
  53. Kind: yaml.ScalarNode,
  54. Value: "foo",
  55. Tag: "!!str",
  56. Line: 1,
  57. Column: 1,
  58. }},
  59. },
  60. }, {
  61. "\"foo\"\n",
  62. yaml.Node{
  63. Kind: yaml.DocumentNode,
  64. Line: 1,
  65. Column: 1,
  66. Content: []*yaml.Node{{
  67. Kind: yaml.ScalarNode,
  68. Style: yaml.DoubleQuotedStyle,
  69. Value: "foo",
  70. Tag: "!!str",
  71. Line: 1,
  72. Column: 1,
  73. }},
  74. },
  75. }, {
  76. "'foo'\n",
  77. yaml.Node{
  78. Kind: yaml.DocumentNode,
  79. Line: 1,
  80. Column: 1,
  81. Content: []*yaml.Node{{
  82. Kind: yaml.ScalarNode,
  83. Style: yaml.SingleQuotedStyle,
  84. Value: "foo",
  85. Tag: "!!str",
  86. Line: 1,
  87. Column: 1,
  88. }},
  89. },
  90. }, {
  91. "!!str 123\n",
  92. yaml.Node{
  93. Kind: yaml.DocumentNode,
  94. Line: 1,
  95. Column: 1,
  96. Content: []*yaml.Node{{
  97. Kind: yaml.ScalarNode,
  98. Style: yaml.TaggedStyle,
  99. Value: "123",
  100. Tag: "!!str",
  101. Line: 1,
  102. Column: 1,
  103. }},
  104. },
  105. }, {
  106. // Although the node isn't TaggedStyle, dropping the tag would change the value.
  107. "[encode]!!binary gIGC\n",
  108. yaml.Node{
  109. Kind: yaml.DocumentNode,
  110. Line: 1,
  111. Column: 1,
  112. Content: []*yaml.Node{{
  113. Kind: yaml.ScalarNode,
  114. Value: "gIGC",
  115. Tag: "!!binary",
  116. Line: 1,
  117. Column: 1,
  118. }},
  119. },
  120. }, {
  121. // Item doesn't have a tag, but needs to be binary encoded due to its content.
  122. "[encode]!!binary gIGC\n",
  123. yaml.Node{
  124. Kind: yaml.DocumentNode,
  125. Line: 1,
  126. Column: 1,
  127. Content: []*yaml.Node{{
  128. Kind: yaml.ScalarNode,
  129. Value: "\x80\x81\x82",
  130. Line: 1,
  131. Column: 1,
  132. }},
  133. },
  134. }, {
  135. // Same, but with strings we can just quote them.
  136. "[encode]\"123\"\n",
  137. yaml.Node{
  138. Kind: yaml.DocumentNode,
  139. Line: 1,
  140. Column: 1,
  141. Content: []*yaml.Node{{
  142. Kind: yaml.ScalarNode,
  143. Value: "123",
  144. Tag: "!!str",
  145. Line: 1,
  146. Column: 1,
  147. }},
  148. },
  149. }, {
  150. "!tag:something 123\n",
  151. yaml.Node{
  152. Kind: yaml.DocumentNode,
  153. Line: 1,
  154. Column: 1,
  155. Content: []*yaml.Node{{
  156. Kind: yaml.ScalarNode,
  157. Style: yaml.TaggedStyle,
  158. Value: "123",
  159. Tag: "!tag:something",
  160. Line: 1,
  161. Column: 1,
  162. }},
  163. },
  164. }, {
  165. "[encode]!tag:something 123\n",
  166. yaml.Node{
  167. Kind: yaml.DocumentNode,
  168. Line: 1,
  169. Column: 1,
  170. Content: []*yaml.Node{{
  171. Kind: yaml.ScalarNode,
  172. Value: "123",
  173. Tag: "!tag:something",
  174. Line: 1,
  175. Column: 1,
  176. }},
  177. },
  178. }, {
  179. "!tag:something {}\n",
  180. yaml.Node{
  181. Kind: yaml.DocumentNode,
  182. Line: 1,
  183. Column: 1,
  184. Content: []*yaml.Node{{
  185. Kind: yaml.MappingNode,
  186. Style: yaml.TaggedStyle | yaml.FlowStyle,
  187. Tag: "!tag:something",
  188. Line: 1,
  189. Column: 1,
  190. }},
  191. },
  192. }, {
  193. "[encode]!tag:something {}\n",
  194. yaml.Node{
  195. Kind: yaml.DocumentNode,
  196. Line: 1,
  197. Column: 1,
  198. Content: []*yaml.Node{{
  199. Kind: yaml.MappingNode,
  200. Style: yaml.FlowStyle,
  201. Tag: "!tag:something",
  202. Line: 1,
  203. Column: 1,
  204. }},
  205. },
  206. }, {
  207. "!tag:something []\n",
  208. yaml.Node{
  209. Kind: yaml.DocumentNode,
  210. Line: 1,
  211. Column: 1,
  212. Content: []*yaml.Node{{
  213. Kind: yaml.SequenceNode,
  214. Style: yaml.TaggedStyle | yaml.FlowStyle,
  215. Tag: "!tag:something",
  216. Line: 1,
  217. Column: 1,
  218. }},
  219. },
  220. }, {
  221. "[encode]!tag:something []\n",
  222. yaml.Node{
  223. Kind: yaml.DocumentNode,
  224. Line: 1,
  225. Column: 1,
  226. Content: []*yaml.Node{{
  227. Kind: yaml.SequenceNode,
  228. Style: yaml.FlowStyle,
  229. Tag: "!tag:something",
  230. Line: 1,
  231. Column: 1,
  232. }},
  233. },
  234. }, {
  235. "''\n",
  236. yaml.Node{
  237. Kind: yaml.DocumentNode,
  238. Line: 1,
  239. Column: 1,
  240. Content: []*yaml.Node{{
  241. Kind: yaml.ScalarNode,
  242. Style: yaml.SingleQuotedStyle,
  243. Value: "",
  244. Tag: "!!str",
  245. Line: 1,
  246. Column: 1,
  247. }},
  248. },
  249. }, {
  250. "|\n foo\n bar\n",
  251. yaml.Node{
  252. Kind: yaml.DocumentNode,
  253. Line: 1,
  254. Column: 1,
  255. Content: []*yaml.Node{{
  256. Kind: yaml.ScalarNode,
  257. Style: yaml.LiteralStyle,
  258. Value: "foo\nbar\n",
  259. Tag: "!!str",
  260. Line: 1,
  261. Column: 1,
  262. }},
  263. },
  264. }, {
  265. "true\n",
  266. yaml.Node{
  267. Kind: yaml.DocumentNode,
  268. Line: 1,
  269. Column: 1,
  270. Content: []*yaml.Node{{
  271. Kind: yaml.ScalarNode,
  272. Value: "true",
  273. Tag: "!!bool",
  274. Line: 1,
  275. Column: 1,
  276. }},
  277. },
  278. }, {
  279. "-10\n",
  280. yaml.Node{
  281. Kind: yaml.DocumentNode,
  282. Line: 1,
  283. Column: 1,
  284. Content: []*yaml.Node{{
  285. Kind: yaml.ScalarNode,
  286. Value: "-10",
  287. Tag: "!!int",
  288. Line: 1,
  289. Column: 1,
  290. }},
  291. },
  292. }, {
  293. "4294967296\n",
  294. yaml.Node{
  295. Kind: yaml.DocumentNode,
  296. Line: 1,
  297. Column: 1,
  298. Content: []*yaml.Node{{
  299. Kind: yaml.ScalarNode,
  300. Value: "4294967296",
  301. Tag: "!!int",
  302. Line: 1,
  303. Column: 1,
  304. }},
  305. },
  306. }, {
  307. "0.1000\n",
  308. yaml.Node{
  309. Kind: yaml.DocumentNode,
  310. Line: 1,
  311. Column: 1,
  312. Content: []*yaml.Node{{
  313. Kind: yaml.ScalarNode,
  314. Value: "0.1000",
  315. Tag: "!!float",
  316. Line: 1,
  317. Column: 1,
  318. }},
  319. },
  320. }, {
  321. "-.inf\n",
  322. yaml.Node{
  323. Kind: yaml.DocumentNode,
  324. Line: 1,
  325. Column: 1,
  326. Content: []*yaml.Node{{
  327. Kind: yaml.ScalarNode,
  328. Value: "-.inf",
  329. Tag: "!!float",
  330. Line: 1,
  331. Column: 1,
  332. }},
  333. },
  334. }, {
  335. ".nan\n",
  336. yaml.Node{
  337. Kind: yaml.DocumentNode,
  338. Line: 1,
  339. Column: 1,
  340. Content: []*yaml.Node{{
  341. Kind: yaml.ScalarNode,
  342. Value: ".nan",
  343. Tag: "!!float",
  344. Line: 1,
  345. Column: 1,
  346. }},
  347. },
  348. }, {
  349. "{}\n",
  350. yaml.Node{
  351. Kind: yaml.DocumentNode,
  352. Line: 1,
  353. Column: 1,
  354. Content: []*yaml.Node{{
  355. Kind: yaml.MappingNode,
  356. Style: yaml.FlowStyle,
  357. Value: "",
  358. Tag: "!!map",
  359. Line: 1,
  360. Column: 1,
  361. }},
  362. },
  363. }, {
  364. "a: b c\n",
  365. yaml.Node{
  366. Kind: yaml.DocumentNode,
  367. Line: 1,
  368. Column: 1,
  369. Content: []*yaml.Node{{
  370. Kind: yaml.MappingNode,
  371. Value: "",
  372. Tag: "!!map",
  373. Line: 1,
  374. Column: 1,
  375. Content: []*yaml.Node{{
  376. Kind: yaml.ScalarNode,
  377. Value: "a",
  378. Tag: "!!str",
  379. Line: 1,
  380. Column: 1,
  381. }, {
  382. Kind: yaml.ScalarNode,
  383. Value: "b c",
  384. Tag: "!!str",
  385. Line: 1,
  386. Column: 4,
  387. }},
  388. }},
  389. },
  390. }, {
  391. "a:\n b: c\n d: e\n",
  392. yaml.Node{
  393. Kind: yaml.DocumentNode,
  394. Line: 1,
  395. Column: 1,
  396. Content: []*yaml.Node{{
  397. Kind: yaml.MappingNode,
  398. Tag: "!!map",
  399. Line: 1,
  400. Column: 1,
  401. Content: []*yaml.Node{{
  402. Kind: yaml.ScalarNode,
  403. Value: "a",
  404. Tag: "!!str",
  405. Line: 1,
  406. Column: 1,
  407. }, {
  408. Kind: yaml.MappingNode,
  409. Tag: "!!map",
  410. Line: 2,
  411. Column: 3,
  412. Content: []*yaml.Node{{
  413. Kind: yaml.ScalarNode,
  414. Value: "b",
  415. Tag: "!!str",
  416. Line: 2,
  417. Column: 3,
  418. }, {
  419. Kind: yaml.ScalarNode,
  420. Value: "c",
  421. Tag: "!!str",
  422. Line: 2,
  423. Column: 6,
  424. }, {
  425. Kind: yaml.ScalarNode,
  426. Value: "d",
  427. Tag: "!!str",
  428. Line: 3,
  429. Column: 3,
  430. }, {
  431. Kind: yaml.ScalarNode,
  432. Value: "e",
  433. Tag: "!!str",
  434. Line: 3,
  435. Column: 6,
  436. }},
  437. }},
  438. }},
  439. },
  440. }, {
  441. "a:\n - b: c\n d: e\n",
  442. yaml.Node{
  443. Kind: yaml.DocumentNode,
  444. Line: 1,
  445. Column: 1,
  446. Content: []*yaml.Node{{
  447. Kind: yaml.MappingNode,
  448. Tag: "!!map",
  449. Line: 1,
  450. Column: 1,
  451. Content: []*yaml.Node{{
  452. Kind: yaml.ScalarNode,
  453. Value: "a",
  454. Tag: "!!str",
  455. Line: 1,
  456. Column: 1,
  457. }, {
  458. Kind: yaml.SequenceNode,
  459. Tag: "!!seq",
  460. Line: 2,
  461. Column: 3,
  462. Content: []*yaml.Node{{
  463. Kind: yaml.MappingNode,
  464. Tag: "!!map",
  465. Line: 2,
  466. Column: 5,
  467. Content: []*yaml.Node{{
  468. Kind: yaml.ScalarNode,
  469. Value: "b",
  470. Tag: "!!str",
  471. Line: 2,
  472. Column: 5,
  473. }, {
  474. Kind: yaml.ScalarNode,
  475. Value: "c",
  476. Tag: "!!str",
  477. Line: 2,
  478. Column: 8,
  479. }, {
  480. Kind: yaml.ScalarNode,
  481. Value: "d",
  482. Tag: "!!str",
  483. Line: 3,
  484. Column: 5,
  485. }, {
  486. Kind: yaml.ScalarNode,
  487. Value: "e",
  488. Tag: "!!str",
  489. Line: 3,
  490. Column: 8,
  491. }},
  492. }},
  493. }},
  494. }},
  495. },
  496. }, {
  497. "a: # AI\n - b\nc:\n - d\n",
  498. yaml.Node{
  499. Kind: yaml.DocumentNode,
  500. Line: 1,
  501. Column: 1,
  502. Content: []*yaml.Node{{
  503. Kind: yaml.MappingNode,
  504. Tag: "!!map",
  505. Line: 1,
  506. Column: 1,
  507. Content: []*yaml.Node{{
  508. Kind: yaml.ScalarNode,
  509. Tag: "!!str",
  510. Value: "a",
  511. LineComment: "# AI",
  512. Line: 1,
  513. Column: 1,
  514. }, {
  515. Kind: yaml.SequenceNode,
  516. Tag: "!!seq",
  517. Content: []*yaml.Node{{
  518. Kind: yaml.ScalarNode,
  519. Tag: "!!str",
  520. Value: "b",
  521. Line: 2,
  522. Column: 5,
  523. }},
  524. Line: 2,
  525. Column: 3,
  526. }, {
  527. Kind: yaml.ScalarNode,
  528. Tag: "!!str",
  529. Value: "c",
  530. Line: 3,
  531. Column: 1,
  532. }, {
  533. Kind: yaml.SequenceNode,
  534. Tag: "!!seq",
  535. Content: []*yaml.Node{{
  536. Kind: yaml.ScalarNode,
  537. Tag: "!!str",
  538. Value: "d",
  539. Line: 4,
  540. Column: 5,
  541. }},
  542. Line: 4,
  543. Column: 3,
  544. }},
  545. }},
  546. },
  547. }, {
  548. "[decode]a:\n # HM\n - # HB1\n # HB2\n b: # IB\n c # IC\n",
  549. yaml.Node{
  550. Kind: yaml.DocumentNode,
  551. Line: 1,
  552. Column: 1,
  553. Content: []*yaml.Node{{
  554. Kind: yaml.MappingNode,
  555. Tag: "!!map",
  556. Line: 1,
  557. Column: 1,
  558. Content: []*yaml.Node{{
  559. Kind: yaml.ScalarNode,
  560. Style: 0x0,
  561. Tag: "!!str",
  562. Value: "a",
  563. Line: 1,
  564. Column: 1,
  565. }, {
  566. Kind: yaml.SequenceNode,
  567. Tag: "!!seq",
  568. Line: 3,
  569. Column: 3,
  570. Content: []*yaml.Node{{
  571. Kind: yaml.MappingNode,
  572. Tag: "!!map",
  573. HeadComment: "# HM",
  574. Line: 5,
  575. Column: 5,
  576. Content: []*yaml.Node{{
  577. Kind: yaml.ScalarNode,
  578. Tag: "!!str",
  579. Value: "b",
  580. HeadComment: "# HB1\n# HB2",
  581. LineComment: "# IB",
  582. Line: 5,
  583. Column: 5,
  584. }, {
  585. Kind: yaml.ScalarNode,
  586. Tag: "!!str",
  587. Value: "c",
  588. LineComment: "# IC",
  589. Line: 6,
  590. Column: 7,
  591. }},
  592. }},
  593. }},
  594. }},
  595. },
  596. }, {
  597. // When encoding the value above, it loses b's inline comment.
  598. "[encode]a:\n # HM\n - # HB1\n # HB2\n b: c # IC\n",
  599. yaml.Node{
  600. Kind: yaml.DocumentNode,
  601. Line: 1,
  602. Column: 1,
  603. Content: []*yaml.Node{{
  604. Kind: yaml.MappingNode,
  605. Tag: "!!map",
  606. Line: 1,
  607. Column: 1,
  608. Content: []*yaml.Node{{
  609. Kind: yaml.ScalarNode,
  610. Style: 0x0,
  611. Tag: "!!str",
  612. Value: "a",
  613. Line: 1,
  614. Column: 1,
  615. }, {
  616. Kind: yaml.SequenceNode,
  617. Tag: "!!seq",
  618. Line: 3,
  619. Column: 3,
  620. Content: []*yaml.Node{{
  621. Kind: yaml.MappingNode,
  622. Tag: "!!map",
  623. HeadComment: "# HM",
  624. Line: 5,
  625. Column: 5,
  626. Content: []*yaml.Node{{
  627. Kind: yaml.ScalarNode,
  628. Tag: "!!str",
  629. Value: "b",
  630. HeadComment: "# HB1\n# HB2",
  631. LineComment: "# IB",
  632. Line: 5,
  633. Column: 5,
  634. }, {
  635. Kind: yaml.ScalarNode,
  636. Tag: "!!str",
  637. Value: "c",
  638. LineComment: "# IC",
  639. Line: 6,
  640. Column: 7,
  641. }},
  642. }},
  643. }},
  644. }},
  645. },
  646. }, {
  647. // Multiple cases of comment inlining next to mapping keys.
  648. "a: | # IA\n str\nb: >- # IB\n str\nc: # IC\n - str\nd: # ID\n str:\n",
  649. yaml.Node{
  650. Kind: yaml.DocumentNode,
  651. Line: 1,
  652. Column: 1,
  653. Content: []*yaml.Node{{
  654. Kind: yaml.MappingNode,
  655. Tag: "!!map",
  656. Line: 1,
  657. Column: 1,
  658. Content: []*yaml.Node{{
  659. Kind: yaml.ScalarNode,
  660. Tag: "!!str",
  661. Value: "a",
  662. Line: 1,
  663. Column: 1,
  664. }, {
  665. Kind: yaml.ScalarNode,
  666. Style: yaml.LiteralStyle,
  667. Tag: "!!str",
  668. Value: "str\n",
  669. LineComment: "# IA",
  670. Line: 1,
  671. Column: 4,
  672. }, {
  673. Kind: yaml.ScalarNode,
  674. Tag: "!!str",
  675. Value: "b",
  676. Line: 3,
  677. Column: 1,
  678. }, {
  679. Kind: yaml.ScalarNode,
  680. Style: yaml.FoldedStyle,
  681. Tag: "!!str",
  682. Value: "str",
  683. LineComment: "# IB",
  684. Line: 3,
  685. Column: 4,
  686. }, {
  687. Kind: yaml.ScalarNode,
  688. Tag: "!!str",
  689. Value: "c",
  690. LineComment: "# IC",
  691. Line: 5,
  692. Column: 1,
  693. }, {
  694. Kind: yaml.SequenceNode,
  695. Tag: "!!seq",
  696. Line: 6,
  697. Column: 3,
  698. Content: []*yaml.Node{{
  699. Kind: yaml.ScalarNode,
  700. Tag: "!!str",
  701. Value: "str",
  702. Line: 6,
  703. Column: 5,
  704. }},
  705. }, {
  706. Kind: yaml.ScalarNode,
  707. Tag: "!!str",
  708. Value: "d",
  709. LineComment: "# ID",
  710. Line: 7,
  711. Column: 1,
  712. }, {
  713. Kind: yaml.MappingNode,
  714. Tag: "!!map",
  715. Line: 8,
  716. Column: 3,
  717. Content: []*yaml.Node{{
  718. Kind: yaml.ScalarNode,
  719. Tag: "!!str",
  720. Value: "str",
  721. Line: 8,
  722. Column: 3,
  723. }, {
  724. Kind: yaml.ScalarNode,
  725. Tag: "!!null",
  726. Line: 8,
  727. Column: 7,
  728. }},
  729. }},
  730. }},
  731. },
  732. }, {
  733. // Indentless sequence.
  734. "[decode]a:\n# HM\n- # HB1\n # HB2\n b: # IB\n c # IC\n",
  735. yaml.Node{
  736. Kind: yaml.DocumentNode,
  737. Line: 1,
  738. Column: 1,
  739. Content: []*yaml.Node{{
  740. Kind: yaml.MappingNode,
  741. Tag: "!!map",
  742. Line: 1,
  743. Column: 1,
  744. Content: []*yaml.Node{{
  745. Kind: yaml.ScalarNode,
  746. Tag: "!!str",
  747. Value: "a",
  748. Line: 1,
  749. Column: 1,
  750. }, {
  751. Kind: yaml.SequenceNode,
  752. Tag: "!!seq",
  753. Line: 3,
  754. Column: 1,
  755. Content: []*yaml.Node{{
  756. Kind: yaml.MappingNode,
  757. Tag: "!!map",
  758. HeadComment: "# HM",
  759. Line: 5,
  760. Column: 3,
  761. Content: []*yaml.Node{{
  762. Kind: yaml.ScalarNode,
  763. Tag: "!!str",
  764. Value: "b",
  765. HeadComment: "# HB1\n# HB2",
  766. LineComment: "# IB",
  767. Line: 5,
  768. Column: 3,
  769. }, {
  770. Kind: yaml.ScalarNode,
  771. Tag: "!!str",
  772. Value: "c",
  773. LineComment: "# IC",
  774. Line: 6,
  775. Column: 5,
  776. }},
  777. }},
  778. }},
  779. }},
  780. },
  781. }, {
  782. "- a\n- b\n",
  783. yaml.Node{
  784. Kind: yaml.DocumentNode,
  785. Line: 1,
  786. Column: 1,
  787. Content: []*yaml.Node{{
  788. Kind: yaml.SequenceNode,
  789. Value: "",
  790. Tag: "!!seq",
  791. Line: 1,
  792. Column: 1,
  793. Content: []*yaml.Node{{
  794. Kind: yaml.ScalarNode,
  795. Value: "a",
  796. Tag: "!!str",
  797. Line: 1,
  798. Column: 3,
  799. }, {
  800. Kind: yaml.ScalarNode,
  801. Value: "b",
  802. Tag: "!!str",
  803. Line: 2,
  804. Column: 3,
  805. }},
  806. }},
  807. },
  808. }, {
  809. "- a\n- - b\n - c\n",
  810. yaml.Node{
  811. Kind: yaml.DocumentNode,
  812. Line: 1,
  813. Column: 1,
  814. Content: []*yaml.Node{{
  815. Kind: yaml.SequenceNode,
  816. Tag: "!!seq",
  817. Line: 1,
  818. Column: 1,
  819. Content: []*yaml.Node{{
  820. Kind: yaml.ScalarNode,
  821. Value: "a",
  822. Tag: "!!str",
  823. Line: 1,
  824. Column: 3,
  825. }, {
  826. Kind: yaml.SequenceNode,
  827. Tag: "!!seq",
  828. Line: 2,
  829. Column: 3,
  830. Content: []*yaml.Node{{
  831. Kind: yaml.ScalarNode,
  832. Value: "b",
  833. Tag: "!!str",
  834. Line: 2,
  835. Column: 5,
  836. }, {
  837. Kind: yaml.ScalarNode,
  838. Value: "c",
  839. Tag: "!!str",
  840. Line: 3,
  841. Column: 5,
  842. }},
  843. }},
  844. }},
  845. },
  846. }, {
  847. "[a, b]\n",
  848. yaml.Node{
  849. Kind: yaml.DocumentNode,
  850. Line: 1,
  851. Column: 1,
  852. Content: []*yaml.Node{{
  853. Kind: yaml.SequenceNode,
  854. Style: yaml.FlowStyle,
  855. Value: "",
  856. Tag: "!!seq",
  857. Line: 1,
  858. Column: 1,
  859. Content: []*yaml.Node{{
  860. Kind: yaml.ScalarNode,
  861. Value: "a",
  862. Tag: "!!str",
  863. Line: 1,
  864. Column: 2,
  865. }, {
  866. Kind: yaml.ScalarNode,
  867. Value: "b",
  868. Tag: "!!str",
  869. Line: 1,
  870. Column: 5,
  871. }},
  872. }},
  873. },
  874. }, {
  875. "- a\n- [b, c]\n",
  876. yaml.Node{
  877. Kind: yaml.DocumentNode,
  878. Line: 1,
  879. Column: 1,
  880. Content: []*yaml.Node{{
  881. Kind: yaml.SequenceNode,
  882. Tag: "!!seq",
  883. Line: 1,
  884. Column: 1,
  885. Content: []*yaml.Node{{
  886. Kind: yaml.ScalarNode,
  887. Value: "a",
  888. Tag: "!!str",
  889. Line: 1,
  890. Column: 3,
  891. }, {
  892. Kind: yaml.SequenceNode,
  893. Tag: "!!seq",
  894. Style: yaml.FlowStyle,
  895. Line: 2,
  896. Column: 3,
  897. Content: []*yaml.Node{{
  898. Kind: yaml.ScalarNode,
  899. Value: "b",
  900. Tag: "!!str",
  901. Line: 2,
  902. Column: 4,
  903. }, {
  904. Kind: yaml.ScalarNode,
  905. Value: "c",
  906. Tag: "!!str",
  907. Line: 2,
  908. Column: 7,
  909. }},
  910. }},
  911. }},
  912. },
  913. }, {
  914. "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
  915. yaml.Node{
  916. Kind: yaml.DocumentNode,
  917. Line: 1,
  918. Column: 1,
  919. Content: []*yaml.Node{{
  920. Kind: yaml.MappingNode,
  921. Line: 1,
  922. Column: 1,
  923. Tag: "!!map",
  924. Content: []*yaml.Node{{
  925. Kind: yaml.ScalarNode,
  926. Value: "a",
  927. Tag: "!!str",
  928. Line: 1,
  929. Column: 1,
  930. },
  931. saveNode("x", &yaml.Node{
  932. Kind: yaml.ScalarNode,
  933. Value: "1",
  934. Tag: "!!int",
  935. Anchor: "x",
  936. Line: 1,
  937. Column: 4,
  938. }),
  939. {
  940. Kind: yaml.ScalarNode,
  941. Value: "b",
  942. Tag: "!!str",
  943. Line: 2,
  944. Column: 1,
  945. },
  946. saveNode("y", &yaml.Node{
  947. Kind: yaml.ScalarNode,
  948. Value: "2",
  949. Tag: "!!int",
  950. Anchor: "y",
  951. Line: 2,
  952. Column: 4,
  953. }),
  954. {
  955. Kind: yaml.ScalarNode,
  956. Value: "c",
  957. Tag: "!!str",
  958. Line: 3,
  959. Column: 1,
  960. }, {
  961. Kind: yaml.AliasNode,
  962. Value: "x",
  963. Alias: dropNode("x"),
  964. Line: 3,
  965. Column: 4,
  966. }, {
  967. Kind: yaml.ScalarNode,
  968. Value: "d",
  969. Tag: "!!str",
  970. Line: 4,
  971. Column: 1,
  972. }, {
  973. Kind: yaml.AliasNode,
  974. Value: "y",
  975. Tag: "",
  976. Alias: dropNode("y"),
  977. Line: 4,
  978. Column: 4,
  979. }},
  980. }},
  981. },
  982. }, {
  983. "# One\n# Two\ntrue # Three\n# Four\n# Five\n",
  984. yaml.Node{
  985. Kind: yaml.DocumentNode,
  986. Line: 3,
  987. Column: 1,
  988. Content: []*yaml.Node{{
  989. Kind: yaml.ScalarNode,
  990. Value: "true",
  991. Tag: "!!bool",
  992. Line: 3,
  993. Column: 1,
  994. HeadComment: "# One\n# Two",
  995. LineComment: "# Three",
  996. FootComment: "# Four\n# Five",
  997. }},
  998. },
  999. }, {
  1000. "# š\ntrue # š\n",
  1001. yaml.Node{
  1002. Kind: yaml.DocumentNode,
  1003. Line: 2,
  1004. Column: 1,
  1005. Content: []*yaml.Node{{
  1006. Kind: yaml.ScalarNode,
  1007. Value: "true",
  1008. Tag: "!!bool",
  1009. Line: 2,
  1010. Column: 1,
  1011. HeadComment: "# š",
  1012. LineComment: "# š",
  1013. }},
  1014. },
  1015. }, {
  1016. "[decode]\n# One\n\n# Two\n\n# Three\ntrue # Four\n# Five\n\n# Six\n\n# Seven\n",
  1017. yaml.Node{
  1018. Kind: yaml.DocumentNode,
  1019. Line: 7,
  1020. Column: 1,
  1021. HeadComment: "# One\n\n# Two",
  1022. FootComment: "# Six\n\n# Seven",
  1023. Content: []*yaml.Node{{
  1024. Kind: yaml.ScalarNode,
  1025. Value: "true",
  1026. Tag: "!!bool",
  1027. Line: 7,
  1028. Column: 1,
  1029. HeadComment: "# Three",
  1030. LineComment: "# Four",
  1031. FootComment: "# Five",
  1032. }},
  1033. },
  1034. }, {
  1035. // Write out the pound character if missing from comments.
  1036. "[encode]# One\n# Two\ntrue # Three\n# Four\n# Five\n",
  1037. yaml.Node{
  1038. Kind: yaml.DocumentNode,
  1039. Line: 3,
  1040. Column: 1,
  1041. Content: []*yaml.Node{{
  1042. Kind: yaml.ScalarNode,
  1043. Value: "true",
  1044. Tag: "!!bool",
  1045. Line: 3,
  1046. Column: 1,
  1047. HeadComment: "One\nTwo\n",
  1048. LineComment: "Three\n",
  1049. FootComment: "Four\nFive\n",
  1050. }},
  1051. },
  1052. }, {
  1053. "[encode]# One\n# Two\ntrue # Three\n# Four\n# Five\n",
  1054. yaml.Node{
  1055. Kind: yaml.DocumentNode,
  1056. Line: 3,
  1057. Column: 1,
  1058. Content: []*yaml.Node{{
  1059. Kind: yaml.ScalarNode,
  1060. Value: "true",
  1061. Tag: "!!bool",
  1062. Line: 3,
  1063. Column: 1,
  1064. HeadComment: " One\n Two",
  1065. LineComment: " Three",
  1066. FootComment: " Four\n Five",
  1067. }},
  1068. },
  1069. }, {
  1070. "# DH1\n\n# DH2\n\n# H1\n# H2\ntrue # I\n# F1\n# F2\n\n# DF1\n\n# DF2\n",
  1071. yaml.Node{
  1072. Kind: yaml.DocumentNode,
  1073. Line: 7,
  1074. Column: 1,
  1075. HeadComment: "# DH1\n\n# DH2",
  1076. FootComment: "# DF1\n\n# DF2",
  1077. Content: []*yaml.Node{{
  1078. Kind: yaml.ScalarNode,
  1079. Value: "true",
  1080. Tag: "!!bool",
  1081. Line: 7,
  1082. Column: 1,
  1083. HeadComment: "# H1\n# H2",
  1084. LineComment: "# I",
  1085. FootComment: "# F1\n# F2",
  1086. }},
  1087. },
  1088. }, {
  1089. "# DH1\n\n# DH2\n\n# HA1\n# HA2\nka: va # IA\n# FA1\n# FA2\n\n# HB1\n# HB2\nkb: vb # IB\n# FB1\n# FB2\n\n# DF1\n\n# DF2\n",
  1090. yaml.Node{
  1091. Kind: yaml.DocumentNode,
  1092. Line: 7,
  1093. Column: 1,
  1094. HeadComment: "# DH1\n\n# DH2",
  1095. FootComment: "# DF1\n\n# DF2",
  1096. Content: []*yaml.Node{{
  1097. Kind: yaml.MappingNode,
  1098. Tag: "!!map",
  1099. Line: 7,
  1100. Column: 1,
  1101. Content: []*yaml.Node{{
  1102. Kind: yaml.ScalarNode,
  1103. Line: 7,
  1104. Column: 1,
  1105. Tag: "!!str",
  1106. Value: "ka",
  1107. HeadComment: "# HA1\n# HA2",
  1108. FootComment: "# FA1\n# FA2",
  1109. }, {
  1110. Kind: yaml.ScalarNode,
  1111. Line: 7,
  1112. Column: 5,
  1113. Tag: "!!str",
  1114. Value: "va",
  1115. LineComment: "# IA",
  1116. }, {
  1117. Kind: yaml.ScalarNode,
  1118. Line: 13,
  1119. Column: 1,
  1120. Tag: "!!str",
  1121. Value: "kb",
  1122. HeadComment: "# HB1\n# HB2",
  1123. FootComment: "# FB1\n# FB2",
  1124. }, {
  1125. Kind: yaml.ScalarNode,
  1126. Line: 13,
  1127. Column: 5,
  1128. Tag: "!!str",
  1129. Value: "vb",
  1130. LineComment: "# IB",
  1131. }},
  1132. }},
  1133. },
  1134. }, {
  1135. "# DH1\n\n# DH2\n\n# HA1\n# HA2\n- la # IA\n# FA1\n# FA2\n\n# HB1\n# HB2\n- lb # IB\n# FB1\n# FB2\n\n# DF1\n\n# DF2\n",
  1136. yaml.Node{
  1137. Kind: yaml.DocumentNode,
  1138. Line: 7,
  1139. Column: 1,
  1140. HeadComment: "# DH1\n\n# DH2",
  1141. FootComment: "# DF1\n\n# DF2",
  1142. Content: []*yaml.Node{{
  1143. Kind: yaml.SequenceNode,
  1144. Tag: "!!seq",
  1145. Line: 7,
  1146. Column: 1,
  1147. Content: []*yaml.Node{{
  1148. Kind: yaml.ScalarNode,
  1149. Tag: "!!str",
  1150. Line: 7,
  1151. Column: 3,
  1152. Value: "la",
  1153. HeadComment: "# HA1\n# HA2",
  1154. LineComment: "# IA",
  1155. FootComment: "# FA1\n# FA2",
  1156. }, {
  1157. Kind: yaml.ScalarNode,
  1158. Tag: "!!str",
  1159. Line: 13,
  1160. Column: 3,
  1161. Value: "lb",
  1162. HeadComment: "# HB1\n# HB2",
  1163. LineComment: "# IB",
  1164. FootComment: "# FB1\n# FB2",
  1165. }},
  1166. }},
  1167. },
  1168. }, {
  1169. "# DH1\n\n- la # IA\n# HB1\n- lb\n",
  1170. yaml.Node{
  1171. Kind: yaml.DocumentNode,
  1172. Line: 3,
  1173. Column: 1,
  1174. HeadComment: "# DH1",
  1175. Content: []*yaml.Node{{
  1176. Kind: yaml.SequenceNode,
  1177. Tag: "!!seq",
  1178. Line: 3,
  1179. Column: 1,
  1180. Content: []*yaml.Node{{
  1181. Kind: yaml.ScalarNode,
  1182. Tag: "!!str",
  1183. Line: 3,
  1184. Column: 3,
  1185. Value: "la",
  1186. LineComment: "# IA",
  1187. }, {
  1188. Kind: yaml.ScalarNode,
  1189. Tag: "!!str",
  1190. Line: 5,
  1191. Column: 3,
  1192. Value: "lb",
  1193. HeadComment: "# HB1",
  1194. }},
  1195. }},
  1196. },
  1197. }, {
  1198. "- la # IA\n- lb # IB\n- lc # IC\n",
  1199. yaml.Node{
  1200. Kind: yaml.DocumentNode,
  1201. Line: 1,
  1202. Column: 1,
  1203. Content: []*yaml.Node{{
  1204. Kind: yaml.SequenceNode,
  1205. Tag: "!!seq",
  1206. Line: 1,
  1207. Column: 1,
  1208. Content: []*yaml.Node{{
  1209. Kind: yaml.ScalarNode,
  1210. Tag: "!!str",
  1211. Line: 1,
  1212. Column: 3,
  1213. Value: "la",
  1214. LineComment: "# IA",
  1215. }, {
  1216. Kind: yaml.ScalarNode,
  1217. Tag: "!!str",
  1218. Line: 2,
  1219. Column: 3,
  1220. Value: "lb",
  1221. LineComment: "# IB",
  1222. }, {
  1223. Kind: yaml.ScalarNode,
  1224. Tag: "!!str",
  1225. Line: 3,
  1226. Column: 3,
  1227. Value: "lc",
  1228. LineComment: "# IC",
  1229. }},
  1230. }},
  1231. },
  1232. }, {
  1233. "# DH1\n\n# HL1\n- - la\n # HB1\n - lb\n",
  1234. yaml.Node{
  1235. Kind: yaml.DocumentNode,
  1236. Line: 4,
  1237. Column: 1,
  1238. HeadComment: "# DH1",
  1239. Content: []*yaml.Node{{
  1240. Kind: yaml.SequenceNode,
  1241. Tag: "!!seq",
  1242. Line: 4,
  1243. Column: 1,
  1244. Content: []*yaml.Node{{
  1245. Kind: yaml.SequenceNode,
  1246. Tag: "!!seq",
  1247. Line: 4,
  1248. Column: 3,
  1249. HeadComment: "# HL1",
  1250. Content: []*yaml.Node{{
  1251. Kind: yaml.ScalarNode,
  1252. Tag: "!!str",
  1253. Line: 4,
  1254. Column: 5,
  1255. Value: "la",
  1256. }, {
  1257. Kind: yaml.ScalarNode,
  1258. Tag: "!!str",
  1259. Line: 6,
  1260. Column: 5,
  1261. Value: "lb",
  1262. HeadComment: "# HB1",
  1263. }},
  1264. }},
  1265. }},
  1266. },
  1267. }, {
  1268. "# DH1\n\n# HL1\n- # HA1\n - la\n # HB1\n - lb\n",
  1269. yaml.Node{
  1270. Kind: yaml.DocumentNode,
  1271. Line: 4,
  1272. Column: 1,
  1273. HeadComment: "# DH1",
  1274. Content: []*yaml.Node{{
  1275. Kind: yaml.SequenceNode,
  1276. Tag: "!!seq",
  1277. Line: 4,
  1278. Column: 1,
  1279. Content: []*yaml.Node{{
  1280. Kind: yaml.SequenceNode,
  1281. Tag: "!!seq",
  1282. Line: 5,
  1283. Column: 3,
  1284. HeadComment: "# HL1",
  1285. Content: []*yaml.Node{{
  1286. Kind: yaml.ScalarNode,
  1287. Tag: "!!str",
  1288. Line: 5,
  1289. Column: 5,
  1290. Value: "la",
  1291. HeadComment: "# HA1",
  1292. }, {
  1293. Kind: yaml.ScalarNode,
  1294. Tag: "!!str",
  1295. Line: 7,
  1296. Column: 5,
  1297. Value: "lb",
  1298. HeadComment: "# HB1",
  1299. }},
  1300. }},
  1301. }},
  1302. },
  1303. }, {
  1304. "[decode]# DH1\n\n# HL1\n- # HA1\n\n - la\n # HB1\n - lb\n",
  1305. yaml.Node{
  1306. Kind: yaml.DocumentNode,
  1307. Line: 4,
  1308. Column: 1,
  1309. HeadComment: "# DH1",
  1310. Content: []*yaml.Node{{
  1311. Kind: yaml.SequenceNode,
  1312. Tag: "!!seq",
  1313. Line: 4,
  1314. Column: 1,
  1315. Content: []*yaml.Node{{
  1316. Kind: yaml.SequenceNode,
  1317. Tag: "!!seq",
  1318. Line: 6,
  1319. Column: 3,
  1320. HeadComment: "# HL1",
  1321. Content: []*yaml.Node{{
  1322. Kind: yaml.ScalarNode,
  1323. Tag: "!!str",
  1324. Line: 6,
  1325. Column: 5,
  1326. Value: "la",
  1327. HeadComment: "# HA1\n",
  1328. }, {
  1329. Kind: yaml.ScalarNode,
  1330. Tag: "!!str",
  1331. Line: 8,
  1332. Column: 5,
  1333. Value: "lb",
  1334. HeadComment: "# HB1",
  1335. }},
  1336. }},
  1337. }},
  1338. },
  1339. }, {
  1340. "# DH1\n\n# HA1\nka:\n # HB1\n kb:\n # HC1\n # HC2\n - lc # IC\n # FC1\n # FC2\n\n # HD1\n - ld # ID\n # FD1\n\n# DF1\n",
  1341. yaml.Node{
  1342. Kind: yaml.DocumentNode,
  1343. Line: 4,
  1344. Column: 1,
  1345. HeadComment: "# DH1",
  1346. FootComment: "# DF1",
  1347. Content: []*yaml.Node{{
  1348. Kind: yaml.MappingNode,
  1349. Tag: "!!map",
  1350. Line: 4,
  1351. Column: 1,
  1352. Content: []*yaml.Node{{
  1353. Kind: yaml.ScalarNode,
  1354. Tag: "!!str",
  1355. Line: 4,
  1356. Column: 1,
  1357. Value: "ka",
  1358. HeadComment: "# HA1",
  1359. }, {
  1360. Kind: yaml.MappingNode,
  1361. Tag: "!!map",
  1362. Line: 6,
  1363. Column: 3,
  1364. Content: []*yaml.Node{{
  1365. Kind: yaml.ScalarNode,
  1366. Tag: "!!str",
  1367. Line: 6,
  1368. Column: 3,
  1369. Value: "kb",
  1370. HeadComment: "# HB1",
  1371. }, {
  1372. Kind: yaml.SequenceNode,
  1373. Line: 9,
  1374. Column: 5,
  1375. Tag: "!!seq",
  1376. Content: []*yaml.Node{{
  1377. Kind: yaml.ScalarNode,
  1378. Tag: "!!str",
  1379. Line: 9,
  1380. Column: 7,
  1381. Value: "lc",
  1382. HeadComment: "# HC1\n# HC2",
  1383. LineComment: "# IC",
  1384. FootComment: "# FC1\n# FC2",
  1385. }, {
  1386. Kind: yaml.ScalarNode,
  1387. Tag: "!!str",
  1388. Line: 14,
  1389. Column: 7,
  1390. Value: "ld",
  1391. HeadComment: "# HD1",
  1392. LineComment: "# ID",
  1393. FootComment: "# FD1",
  1394. }},
  1395. }},
  1396. }},
  1397. }},
  1398. },
  1399. }, {
  1400. "# DH1\n\n# HA1\nka:\n # HB1\n kb:\n # HC1\n # HC2\n - lc # IC\n # FC1\n # FC2\n\n # HD1\n - ld # ID\n # FD1\nke: ve\n\n# DF1\n",
  1401. yaml.Node{
  1402. Kind: yaml.DocumentNode,
  1403. Line: 4,
  1404. Column: 1,
  1405. HeadComment: "# DH1",
  1406. FootComment: "# DF1",
  1407. Content: []*yaml.Node{{
  1408. Kind: yaml.MappingNode,
  1409. Tag: "!!map",
  1410. Line: 4,
  1411. Column: 1,
  1412. Content: []*yaml.Node{{
  1413. Kind: yaml.ScalarNode,
  1414. Tag: "!!str",
  1415. Line: 4,
  1416. Column: 1,
  1417. Value: "ka",
  1418. HeadComment: "# HA1",
  1419. }, {
  1420. Kind: yaml.MappingNode,
  1421. Tag: "!!map",
  1422. Line: 6,
  1423. Column: 3,
  1424. Content: []*yaml.Node{{
  1425. Kind: yaml.ScalarNode,
  1426. Tag: "!!str",
  1427. Line: 6,
  1428. Column: 3,
  1429. Value: "kb",
  1430. HeadComment: "# HB1",
  1431. }, {
  1432. Kind: yaml.SequenceNode,
  1433. Line: 9,
  1434. Column: 5,
  1435. Tag: "!!seq",
  1436. Content: []*yaml.Node{{
  1437. Kind: yaml.ScalarNode,
  1438. Tag: "!!str",
  1439. Line: 9,
  1440. Column: 7,
  1441. Value: "lc",
  1442. HeadComment: "# HC1\n# HC2",
  1443. LineComment: "# IC",
  1444. FootComment: "# FC1\n# FC2",
  1445. }, {
  1446. Kind: yaml.ScalarNode,
  1447. Tag: "!!str",
  1448. Line: 14,
  1449. Column: 7,
  1450. Value: "ld",
  1451. HeadComment: "# HD1",
  1452. LineComment: "# ID",
  1453. FootComment: "# FD1",
  1454. }},
  1455. }},
  1456. }, {
  1457. Kind: yaml.ScalarNode,
  1458. Tag: "!!str",
  1459. Line: 16,
  1460. Column: 1,
  1461. Value: "ke",
  1462. }, {
  1463. Kind: yaml.ScalarNode,
  1464. Tag: "!!str",
  1465. Line: 16,
  1466. Column: 5,
  1467. Value: "ve",
  1468. }},
  1469. }},
  1470. },
  1471. }, {
  1472. "# DH1\n\n# DH2\n\n# HA1\n# HA2\nka:\n # HB1\n # HB2\n kb:\n" +
  1473. " # HC1\n # HC2\n kc:\n # HD1\n # HD2\n kd: vd\n # FD1\n # FD2\n" +
  1474. " # FC1\n # FC2\n # FB1\n # FB2\n# FA1\n# FA2\n\n# HE1\n# HE2\nke: ve\n# FE1\n# FE2\n\n# DF1\n\n# DF2\n",
  1475. yaml.Node{
  1476. Kind: yaml.DocumentNode,
  1477. HeadComment: "# DH1\n\n# DH2",
  1478. FootComment: "# DF1\n\n# DF2",
  1479. Line: 7,
  1480. Column: 1,
  1481. Content: []*yaml.Node{{
  1482. Kind: yaml.MappingNode,
  1483. Tag: "!!map",
  1484. Line: 7,
  1485. Column: 1,
  1486. Content: []*yaml.Node{{
  1487. Kind: yaml.ScalarNode,
  1488. Tag: "!!str",
  1489. Value: "ka",
  1490. HeadComment: "# HA1\n# HA2",
  1491. FootComment: "# FA1\n# FA2",
  1492. Line: 7,
  1493. Column: 1,
  1494. }, {
  1495. Kind: yaml.MappingNode,
  1496. Tag: "!!map",
  1497. Line: 10,
  1498. Column: 3,
  1499. Content: []*yaml.Node{{
  1500. Kind: yaml.ScalarNode,
  1501. Tag: "!!str",
  1502. Value: "kb",
  1503. HeadComment: "# HB1\n# HB2",
  1504. FootComment: "# FB1\n# FB2",
  1505. Line: 10,
  1506. Column: 3,
  1507. }, {
  1508. Kind: yaml.MappingNode,
  1509. Tag: "!!map",
  1510. Line: 13,
  1511. Column: 5,
  1512. Content: []*yaml.Node{{
  1513. Kind: yaml.ScalarNode,
  1514. Tag: "!!str",
  1515. Value: "kc",
  1516. HeadComment: "# HC1\n# HC2",
  1517. FootComment: "# FC1\n# FC2",
  1518. Line: 13,
  1519. Column: 5,
  1520. }, {
  1521. Kind: yaml.MappingNode,
  1522. Tag: "!!map",
  1523. Line: 16,
  1524. Column: 7,
  1525. Content: []*yaml.Node{{
  1526. Kind: yaml.ScalarNode,
  1527. Tag: "!!str",
  1528. Value: "kd",
  1529. HeadComment: "# HD1\n# HD2",
  1530. FootComment: "# FD1\n# FD2",
  1531. Line: 16,
  1532. Column: 7,
  1533. }, {
  1534. Kind: yaml.ScalarNode,
  1535. Tag: "!!str",
  1536. Value: "vd",
  1537. Line: 16,
  1538. Column: 11,
  1539. }},
  1540. }},
  1541. }},
  1542. }, {
  1543. Kind: yaml.ScalarNode,
  1544. Tag: "!!str",
  1545. Value: "ke",
  1546. HeadComment: "# HE1\n# HE2",
  1547. FootComment: "# FE1\n# FE2",
  1548. Line: 28,
  1549. Column: 1,
  1550. }, {
  1551. Kind: yaml.ScalarNode,
  1552. Tag: "!!str",
  1553. Value: "ve",
  1554. Line: 28,
  1555. Column: 5,
  1556. }},
  1557. }},
  1558. },
  1559. }, {
  1560. // Same as above but indenting ke in so it's also part of ka's value.
  1561. "# DH1\n\n# DH2\n\n# HA1\n# HA2\nka:\n # HB1\n # HB2\n kb:\n" +
  1562. " # HC1\n # HC2\n kc:\n # HD1\n # HD2\n kd: vd\n # FD1\n # FD2\n" +
  1563. " # FC1\n # FC2\n # FB1\n # FB2\n\n # HE1\n # HE2\n ke: ve\n # FE1\n # FE2\n# FA1\n# FA2\n\n# DF1\n\n# DF2\n",
  1564. yaml.Node{
  1565. Kind: yaml.DocumentNode,
  1566. HeadComment: "# DH1\n\n# DH2",
  1567. FootComment: "# DF1\n\n# DF2",
  1568. Line: 7,
  1569. Column: 1,
  1570. Content: []*yaml.Node{{
  1571. Kind: yaml.MappingNode,
  1572. Tag: "!!map",
  1573. Line: 7,
  1574. Column: 1,
  1575. Content: []*yaml.Node{{
  1576. Kind: yaml.ScalarNode,
  1577. Tag: "!!str",
  1578. Value: "ka",
  1579. HeadComment: "# HA1\n# HA2",
  1580. FootComment: "# FA1\n# FA2",
  1581. Line: 7,
  1582. Column: 1,
  1583. }, {
  1584. Kind: yaml.MappingNode,
  1585. Tag: "!!map",
  1586. Line: 10,
  1587. Column: 3,
  1588. Content: []*yaml.Node{{
  1589. Kind: yaml.ScalarNode,
  1590. Tag: "!!str",
  1591. Value: "kb",
  1592. HeadComment: "# HB1\n# HB2",
  1593. FootComment: "# FB1\n# FB2",
  1594. Line: 10,
  1595. Column: 3,
  1596. }, {
  1597. Kind: yaml.MappingNode,
  1598. Tag: "!!map",
  1599. Line: 13,
  1600. Column: 5,
  1601. Content: []*yaml.Node{{
  1602. Kind: yaml.ScalarNode,
  1603. Tag: "!!str",
  1604. Value: "kc",
  1605. HeadComment: "# HC1\n# HC2",
  1606. FootComment: "# FC1\n# FC2",
  1607. Line: 13,
  1608. Column: 5,
  1609. }, {
  1610. Kind: yaml.MappingNode,
  1611. Tag: "!!map",
  1612. Line: 16,
  1613. Column: 7,
  1614. Content: []*yaml.Node{{
  1615. Kind: yaml.ScalarNode,
  1616. Tag: "!!str",
  1617. Value: "kd",
  1618. HeadComment: "# HD1\n# HD2",
  1619. FootComment: "# FD1\n# FD2",
  1620. Line: 16,
  1621. Column: 7,
  1622. }, {
  1623. Kind: yaml.ScalarNode,
  1624. Tag: "!!str",
  1625. Value: "vd",
  1626. Line: 16,
  1627. Column: 11,
  1628. }},
  1629. }},
  1630. }, {
  1631. Kind: yaml.ScalarNode,
  1632. Tag: "!!str",
  1633. Value: "ke",
  1634. HeadComment: "# HE1\n# HE2",
  1635. FootComment: "# FE1\n# FE2",
  1636. Line: 26,
  1637. Column: 3,
  1638. }, {
  1639. Kind: yaml.ScalarNode,
  1640. Tag: "!!str",
  1641. Value: "ve",
  1642. Line: 26,
  1643. Column: 7,
  1644. }},
  1645. }},
  1646. }},
  1647. },
  1648. }, {
  1649. // Decode only due to lack of newline at the end.
  1650. "[decode]# HA1\nka:\n # HB1\n kb: vb\n # FB1\n# FA1",
  1651. yaml.Node{
  1652. Kind: yaml.DocumentNode,
  1653. Line: 2,
  1654. Column: 1,
  1655. Content: []*yaml.Node{{
  1656. Kind: yaml.MappingNode,
  1657. Tag: "!!map",
  1658. Line: 2,
  1659. Column: 1,
  1660. Content: []*yaml.Node{{
  1661. Kind: yaml.ScalarNode,
  1662. Tag: "!!str",
  1663. Value: "ka",
  1664. HeadComment: "# HA1",
  1665. FootComment: "# FA1",
  1666. Line: 2,
  1667. Column: 1,
  1668. }, {
  1669. Kind: yaml.MappingNode,
  1670. Tag: "!!map",
  1671. Line: 4,
  1672. Column: 3,
  1673. Content: []*yaml.Node{{
  1674. Kind: yaml.ScalarNode,
  1675. Tag: "!!str",
  1676. Value: "kb",
  1677. HeadComment: "# HB1",
  1678. FootComment: "# FB1",
  1679. Line: 4,
  1680. Column: 3,
  1681. }, {
  1682. Kind: yaml.ScalarNode,
  1683. Tag: "!!str",
  1684. Value: "vb",
  1685. Line: 4,
  1686. Column: 7,
  1687. }},
  1688. }},
  1689. }},
  1690. },
  1691. }, {
  1692. // Same as above, but with newline at the end.
  1693. "# HA1\nka:\n # HB1\n kb: vb\n # FB1\n# FA1\n",
  1694. yaml.Node{
  1695. Kind: yaml.DocumentNode,
  1696. Line: 2,
  1697. Column: 1,
  1698. Content: []*yaml.Node{{
  1699. Kind: yaml.MappingNode,
  1700. Tag: "!!map",
  1701. Line: 2,
  1702. Column: 1,
  1703. Content: []*yaml.Node{{
  1704. Kind: yaml.ScalarNode,
  1705. Tag: "!!str",
  1706. Value: "ka",
  1707. HeadComment: "# HA1",
  1708. FootComment: "# FA1",
  1709. Line: 2,
  1710. Column: 1,
  1711. }, {
  1712. Kind: yaml.MappingNode,
  1713. Tag: "!!map",
  1714. Line: 4,
  1715. Column: 3,
  1716. Content: []*yaml.Node{{
  1717. Kind: yaml.ScalarNode,
  1718. Tag: "!!str",
  1719. Value: "kb",
  1720. HeadComment: "# HB1",
  1721. FootComment: "# FB1",
  1722. Line: 4,
  1723. Column: 3,
  1724. }, {
  1725. Kind: yaml.ScalarNode,
  1726. Tag: "!!str",
  1727. Value: "vb",
  1728. Line: 4,
  1729. Column: 7,
  1730. }},
  1731. }},
  1732. }},
  1733. },
  1734. }, {
  1735. // Same as above, but without FB1.
  1736. "# HA1\nka:\n # HB1\n kb: vb\n# FA1\n",
  1737. yaml.Node{
  1738. Kind: yaml.DocumentNode,
  1739. Line: 2,
  1740. Column: 1,
  1741. Content: []*yaml.Node{{
  1742. Kind: yaml.MappingNode,
  1743. Tag: "!!map",
  1744. Line: 2,
  1745. Column: 1,
  1746. Content: []*yaml.Node{{
  1747. Kind: yaml.ScalarNode,
  1748. Tag: "!!str",
  1749. Value: "ka",
  1750. HeadComment: "# HA1",
  1751. FootComment: "# FA1",
  1752. Line: 2,
  1753. Column: 1,
  1754. }, {
  1755. Kind: yaml.MappingNode,
  1756. Tag: "!!map",
  1757. Line: 4,
  1758. Column: 3,
  1759. Content: []*yaml.Node{{
  1760. Kind: yaml.ScalarNode,
  1761. Tag: "!!str",
  1762. Value: "kb",
  1763. HeadComment: "# HB1",
  1764. Line: 4,
  1765. Column: 3,
  1766. }, {
  1767. Kind: yaml.ScalarNode,
  1768. Tag: "!!str",
  1769. Value: "vb",
  1770. Line: 4,
  1771. Column: 7,
  1772. }},
  1773. }},
  1774. }},
  1775. },
  1776. }, {
  1777. // Same as above, but with two newlines at the end. Decode-only for that.
  1778. "[decode]# HA1\nka:\n # HB1\n kb: vb\n # FB1\n# FA1\n\n",
  1779. yaml.Node{
  1780. Kind: yaml.DocumentNode,
  1781. Line: 2,
  1782. Column: 1,
  1783. Content: []*yaml.Node{{
  1784. Kind: yaml.MappingNode,
  1785. Tag: "!!map",
  1786. Line: 2,
  1787. Column: 1,
  1788. Content: []*yaml.Node{{
  1789. Kind: yaml.ScalarNode,
  1790. Tag: "!!str",
  1791. Value: "ka",
  1792. HeadComment: "# HA1",
  1793. FootComment: "# FA1",
  1794. Line: 2,
  1795. Column: 1,
  1796. }, {
  1797. Kind: yaml.MappingNode,
  1798. Tag: "!!map",
  1799. Line: 4,
  1800. Column: 3,
  1801. Content: []*yaml.Node{{
  1802. Kind: yaml.ScalarNode,
  1803. Tag: "!!str",
  1804. Value: "kb",
  1805. HeadComment: "# HB1",
  1806. FootComment: "# FB1",
  1807. Line: 4,
  1808. Column: 3,
  1809. }, {
  1810. Kind: yaml.ScalarNode,
  1811. Tag: "!!str",
  1812. Value: "vb",
  1813. Line: 4,
  1814. Column: 7,
  1815. }},
  1816. }},
  1817. }},
  1818. },
  1819. }, {
  1820. // Similar to above, but make HB1 look more like a footer of ka.
  1821. "[decode]# HA1\nka:\n# HB1\n\n kb: vb\n# FA1\n",
  1822. yaml.Node{
  1823. Kind: yaml.DocumentNode,
  1824. Line: 2,
  1825. Column: 1,
  1826. Content: []*yaml.Node{{
  1827. Kind: yaml.MappingNode,
  1828. Tag: "!!map",
  1829. Line: 2,
  1830. Column: 1,
  1831. Content: []*yaml.Node{{
  1832. Kind: yaml.ScalarNode,
  1833. Tag: "!!str",
  1834. Value: "ka",
  1835. HeadComment: "# HA1",
  1836. FootComment: "# FA1",
  1837. Line: 2,
  1838. Column: 1,
  1839. }, {
  1840. Kind: yaml.MappingNode,
  1841. Tag: "!!map",
  1842. Line: 5,
  1843. Column: 3,
  1844. Content: []*yaml.Node{{
  1845. Kind: yaml.ScalarNode,
  1846. Tag: "!!str",
  1847. Value: "kb",
  1848. HeadComment: "# HB1\n",
  1849. Line: 5,
  1850. Column: 3,
  1851. }, {
  1852. Kind: yaml.ScalarNode,
  1853. Tag: "!!str",
  1854. Value: "vb",
  1855. Line: 5,
  1856. Column: 7,
  1857. }},
  1858. }},
  1859. }},
  1860. },
  1861. }, {
  1862. "ka:\n kb: vb\n# FA1\n\nkc: vc\n",
  1863. yaml.Node{
  1864. Kind: yaml.DocumentNode,
  1865. Line: 1,
  1866. Column: 1,
  1867. Content: []*yaml.Node{{
  1868. Kind: yaml.MappingNode,
  1869. Tag: "!!map",
  1870. Line: 1,
  1871. Column: 1,
  1872. Content: []*yaml.Node{{
  1873. Kind: yaml.ScalarNode,
  1874. Tag: "!!str",
  1875. Value: "ka",
  1876. Line: 1,
  1877. Column: 1,
  1878. FootComment: "# FA1",
  1879. }, {
  1880. Kind: yaml.MappingNode,
  1881. Tag: "!!map",
  1882. Line: 2,
  1883. Column: 3,
  1884. Content: []*yaml.Node{{
  1885. Kind: yaml.ScalarNode,
  1886. Tag: "!!str",
  1887. Value: "kb",
  1888. Line: 2,
  1889. Column: 3,
  1890. }, {
  1891. Kind: yaml.ScalarNode,
  1892. Tag: "!!str",
  1893. Value: "vb",
  1894. Line: 2,
  1895. Column: 7,
  1896. }},
  1897. }, {
  1898. Kind: yaml.ScalarNode,
  1899. Tag: "!!str",
  1900. Value: "kc",
  1901. Line: 5,
  1902. Column: 1,
  1903. }, {
  1904. Kind: yaml.ScalarNode,
  1905. Tag: "!!str",
  1906. Value: "vc",
  1907. Line: 5,
  1908. Column: 5,
  1909. }},
  1910. }},
  1911. },
  1912. }, {
  1913. "ka:\n kb: vb\n# HC1\nkc: vc\n",
  1914. yaml.Node{
  1915. Kind: yaml.DocumentNode,
  1916. Line: 1,
  1917. Column: 1,
  1918. Content: []*yaml.Node{{
  1919. Kind: yaml.MappingNode,
  1920. Tag: "!!map",
  1921. Line: 1,
  1922. Column: 1,
  1923. Content: []*yaml.Node{{
  1924. Kind: yaml.ScalarNode,
  1925. Tag: "!!str",
  1926. Value: "ka",
  1927. Line: 1,
  1928. Column: 1,
  1929. }, {
  1930. Kind: yaml.MappingNode,
  1931. Tag: "!!map",
  1932. Line: 2,
  1933. Column: 3,
  1934. Content: []*yaml.Node{{
  1935. Kind: yaml.ScalarNode,
  1936. Tag: "!!str",
  1937. Value: "kb",
  1938. Line: 2,
  1939. Column: 3,
  1940. }, {
  1941. Kind: yaml.ScalarNode,
  1942. Tag: "!!str",
  1943. Value: "vb",
  1944. Line: 2,
  1945. Column: 7,
  1946. }},
  1947. }, {
  1948. Kind: yaml.ScalarNode,
  1949. Tag: "!!str",
  1950. Value: "kc",
  1951. HeadComment: "# HC1",
  1952. Line: 4,
  1953. Column: 1,
  1954. }, {
  1955. Kind: yaml.ScalarNode,
  1956. Tag: "!!str",
  1957. Value: "vc",
  1958. Line: 4,
  1959. Column: 5,
  1960. }},
  1961. }},
  1962. },
  1963. }, {
  1964. // Decode only due to empty line before HC1.
  1965. "[decode]ka:\n kb: vb\n\n# HC1\nkc: vc\n",
  1966. yaml.Node{
  1967. Kind: yaml.DocumentNode,
  1968. Line: 1,
  1969. Column: 1,
  1970. Content: []*yaml.Node{{
  1971. Kind: yaml.MappingNode,
  1972. Tag: "!!map",
  1973. Line: 1,
  1974. Column: 1,
  1975. Content: []*yaml.Node{{
  1976. Kind: yaml.ScalarNode,
  1977. Tag: "!!str",
  1978. Value: "ka",
  1979. Line: 1,
  1980. Column: 1,
  1981. }, {
  1982. Kind: yaml.MappingNode,
  1983. Tag: "!!map",
  1984. Line: 2,
  1985. Column: 3,
  1986. Content: []*yaml.Node{{
  1987. Kind: yaml.ScalarNode,
  1988. Tag: "!!str",
  1989. Value: "kb",
  1990. Line: 2,
  1991. Column: 3,
  1992. }, {
  1993. Kind: yaml.ScalarNode,
  1994. Tag: "!!str",
  1995. Value: "vb",
  1996. Line: 2,
  1997. Column: 7,
  1998. }},
  1999. }, {
  2000. Kind: yaml.ScalarNode,
  2001. Tag: "!!str",
  2002. Value: "kc",
  2003. HeadComment: "# HC1",
  2004. Line: 5,
  2005. Column: 1,
  2006. }, {
  2007. Kind: yaml.ScalarNode,
  2008. Tag: "!!str",
  2009. Value: "vc",
  2010. Line: 5,
  2011. Column: 5,
  2012. }},
  2013. }},
  2014. },
  2015. }, {
  2016. // Decode-only due to empty lines around HC1.
  2017. "[decode]ka:\n kb: vb\n\n# HC1\n\nkc: vc\n",
  2018. yaml.Node{
  2019. Kind: yaml.DocumentNode,
  2020. Line: 1,
  2021. Column: 1,
  2022. Content: []*yaml.Node{{
  2023. Kind: yaml.MappingNode,
  2024. Tag: "!!map",
  2025. Line: 1,
  2026. Column: 1,
  2027. Content: []*yaml.Node{{
  2028. Kind: yaml.ScalarNode,
  2029. Tag: "!!str",
  2030. Value: "ka",
  2031. Line: 1,
  2032. Column: 1,
  2033. }, {
  2034. Kind: yaml.MappingNode,
  2035. Tag: "!!map",
  2036. Line: 2,
  2037. Column: 3,
  2038. Content: []*yaml.Node{{
  2039. Kind: yaml.ScalarNode,
  2040. Tag: "!!str",
  2041. Value: "kb",
  2042. Line: 2,
  2043. Column: 3,
  2044. }, {
  2045. Kind: yaml.ScalarNode,
  2046. Tag: "!!str",
  2047. Value: "vb",
  2048. Line: 2,
  2049. Column: 7,
  2050. }},
  2051. }, {
  2052. Kind: yaml.ScalarNode,
  2053. Tag: "!!str",
  2054. Value: "kc",
  2055. HeadComment: "# HC1\n",
  2056. Line: 6,
  2057. Column: 1,
  2058. }, {
  2059. Kind: yaml.ScalarNode,
  2060. Tag: "!!str",
  2061. Value: "vc",
  2062. Line: 6,
  2063. Column: 5,
  2064. }},
  2065. }},
  2066. },
  2067. }, {
  2068. "ka: # IA\n kb: # IB\n",
  2069. yaml.Node{
  2070. Kind: yaml.DocumentNode,
  2071. Line: 1,
  2072. Column: 1,
  2073. Content: []*yaml.Node{{
  2074. Kind: yaml.MappingNode,
  2075. Tag: "!!map",
  2076. Line: 1,
  2077. Column: 1,
  2078. Content: []*yaml.Node{{
  2079. Kind: yaml.ScalarNode,
  2080. Tag: "!!str",
  2081. Value: "ka",
  2082. Line: 1,
  2083. Column: 1,
  2084. LineComment: "# IA",
  2085. }, {
  2086. Kind: yaml.MappingNode,
  2087. Tag: "!!map",
  2088. Line: 2,
  2089. Column: 3,
  2090. Content: []*yaml.Node{{
  2091. Kind: yaml.ScalarNode,
  2092. Tag: "!!str",
  2093. Value: "kb",
  2094. Line: 2,
  2095. Column: 3,
  2096. LineComment: "# IB",
  2097. }, {
  2098. Kind: yaml.ScalarNode,
  2099. Tag: "!!null",
  2100. Line: 2,
  2101. Column: 6,
  2102. }},
  2103. }},
  2104. }},
  2105. },
  2106. }, {
  2107. "# HA1\nka:\n # HB1\n kb: vb\n # FB1\n# HC1\n# HC2\nkc: vc\n# FC1\n# FC2\n",
  2108. yaml.Node{
  2109. Kind: yaml.DocumentNode,
  2110. Line: 2,
  2111. Column: 1,
  2112. Content: []*yaml.Node{{
  2113. Kind: yaml.MappingNode,
  2114. Tag: "!!map",
  2115. Line: 2,
  2116. Column: 1,
  2117. Content: []*yaml.Node{{
  2118. Kind: yaml.ScalarNode,
  2119. Tag: "!!str",
  2120. Value: "ka",
  2121. HeadComment: "# HA1",
  2122. Line: 2,
  2123. Column: 1,
  2124. }, {
  2125. Kind: yaml.MappingNode,
  2126. Tag: "!!map",
  2127. Line: 4,
  2128. Column: 3,
  2129. Content: []*yaml.Node{{
  2130. Kind: yaml.ScalarNode,
  2131. Tag: "!!str",
  2132. Value: "kb",
  2133. HeadComment: "# HB1",
  2134. FootComment: "# FB1",
  2135. Line: 4,
  2136. Column: 3,
  2137. }, {
  2138. Kind: yaml.ScalarNode,
  2139. Tag: "!!str",
  2140. Value: "vb",
  2141. Line: 4,
  2142. Column: 7,
  2143. }},
  2144. }, {
  2145. Kind: yaml.ScalarNode,
  2146. Tag: "!!str",
  2147. Value: "kc",
  2148. HeadComment: "# HC1\n# HC2",
  2149. FootComment: "# FC1\n# FC2",
  2150. Line: 8,
  2151. Column: 1,
  2152. }, {
  2153. Kind: yaml.ScalarNode,
  2154. Tag: "!!str",
  2155. Value: "vc",
  2156. Line: 8,
  2157. Column: 5,
  2158. }},
  2159. }},
  2160. },
  2161. }, {
  2162. // Same as above, but decode only due to empty line between ka's value and kc's headers.
  2163. "[decode]# HA1\nka:\n # HB1\n kb: vb\n # FB1\n\n# HC1\n# HC2\nkc: vc\n# FC1\n# FC2\n",
  2164. yaml.Node{
  2165. Kind: yaml.DocumentNode,
  2166. Line: 2,
  2167. Column: 1,
  2168. Content: []*yaml.Node{{
  2169. Kind: yaml.MappingNode,
  2170. Tag: "!!map",
  2171. Line: 2,
  2172. Column: 1,
  2173. Content: []*yaml.Node{{
  2174. Kind: yaml.ScalarNode,
  2175. Tag: "!!str",
  2176. Value: "ka",
  2177. HeadComment: "# HA1",
  2178. Line: 2,
  2179. Column: 1,
  2180. }, {
  2181. Kind: yaml.MappingNode,
  2182. Tag: "!!map",
  2183. Line: 4,
  2184. Column: 3,
  2185. Content: []*yaml.Node{{
  2186. Kind: yaml.ScalarNode,
  2187. Tag: "!!str",
  2188. Value: "kb",
  2189. HeadComment: "# HB1",
  2190. FootComment: "# FB1",
  2191. Line: 4,
  2192. Column: 3,
  2193. }, {
  2194. Kind: yaml.ScalarNode,
  2195. Tag: "!!str",
  2196. Value: "vb",
  2197. Line: 4,
  2198. Column: 7,
  2199. }},
  2200. }, {
  2201. Kind: yaml.ScalarNode,
  2202. Tag: "!!str",
  2203. Value: "kc",
  2204. HeadComment: "# HC1\n# HC2",
  2205. FootComment: "# FC1\n# FC2",
  2206. Line: 9,
  2207. Column: 1,
  2208. }, {
  2209. Kind: yaml.ScalarNode,
  2210. Tag: "!!str",
  2211. Value: "vc",
  2212. Line: 9,
  2213. Column: 5,
  2214. }},
  2215. }},
  2216. },
  2217. }, {
  2218. "# H1\n[la, lb] # I\n# F1\n",
  2219. yaml.Node{
  2220. Kind: yaml.DocumentNode,
  2221. Line: 2,
  2222. Column: 1,
  2223. Content: []*yaml.Node{{
  2224. Kind: yaml.SequenceNode,
  2225. Tag: "!!seq",
  2226. Style: yaml.FlowStyle,
  2227. Line: 2,
  2228. Column: 1,
  2229. HeadComment: "# H1",
  2230. LineComment: "# I",
  2231. FootComment: "# F1",
  2232. Content: []*yaml.Node{{
  2233. Kind: yaml.ScalarNode,
  2234. Tag: "!!str",
  2235. Line: 2,
  2236. Column: 2,
  2237. Value: "la",
  2238. }, {
  2239. Kind: yaml.ScalarNode,
  2240. Tag: "!!str",
  2241. Line: 2,
  2242. Column: 6,
  2243. Value: "lb",
  2244. }},
  2245. }},
  2246. },
  2247. }, {
  2248. "# DH1\n\n# SH1\n[\n # HA1\n la, # IA\n # FA1\n\n # HB1\n lb, # IB\n # FB1\n]\n# SF1\n\n# DF1\n",
  2249. yaml.Node{
  2250. Kind: yaml.DocumentNode,
  2251. Line: 4,
  2252. Column: 1,
  2253. HeadComment: "# DH1",
  2254. FootComment: "# DF1",
  2255. Content: []*yaml.Node{{
  2256. Kind: yaml.SequenceNode,
  2257. Tag: "!!seq",
  2258. Style: yaml.FlowStyle,
  2259. Line: 4,
  2260. Column: 1,
  2261. HeadComment: "# SH1",
  2262. FootComment: "# SF1",
  2263. Content: []*yaml.Node{{
  2264. Kind: yaml.ScalarNode,
  2265. Tag: "!!str",
  2266. Line: 6,
  2267. Column: 3,
  2268. Value: "la",
  2269. HeadComment: "# HA1",
  2270. LineComment: "# IA",
  2271. FootComment: "# FA1",
  2272. }, {
  2273. Kind: yaml.ScalarNode,
  2274. Tag: "!!str",
  2275. Line: 10,
  2276. Column: 3,
  2277. Value: "lb",
  2278. HeadComment: "# HB1",
  2279. LineComment: "# IB",
  2280. FootComment: "# FB1",
  2281. }},
  2282. }},
  2283. },
  2284. }, {
  2285. // Same as above, but with extra newlines before FB1 and FB2
  2286. "[decode]# DH1\n\n# SH1\n[\n # HA1\n la, # IA\n # FA1\n\n # HB1\n lb, # IB\n\n\n # FB1\n\n# FB2\n]\n# SF1\n\n# DF1\n",
  2287. yaml.Node{
  2288. Kind: yaml.DocumentNode,
  2289. Line: 4,
  2290. Column: 1,
  2291. HeadComment: "# DH1",
  2292. FootComment: "# DF1",
  2293. Content: []*yaml.Node{{
  2294. Kind: yaml.SequenceNode,
  2295. Tag: "!!seq",
  2296. Style: yaml.FlowStyle,
  2297. Line: 4,
  2298. Column: 1,
  2299. HeadComment: "# SH1",
  2300. FootComment: "# SF1",
  2301. Content: []*yaml.Node{{
  2302. Kind: yaml.ScalarNode,
  2303. Tag: "!!str",
  2304. Line: 6,
  2305. Column: 3,
  2306. Value: "la",
  2307. HeadComment: "# HA1",
  2308. LineComment: "# IA",
  2309. FootComment: "# FA1",
  2310. }, {
  2311. Kind: yaml.ScalarNode,
  2312. Tag: "!!str",
  2313. Line: 10,
  2314. Column: 3,
  2315. Value: "lb",
  2316. HeadComment: "# HB1",
  2317. LineComment: "# IB",
  2318. FootComment: "# FB1\n\n# FB2",
  2319. }},
  2320. }},
  2321. },
  2322. }, {
  2323. "# DH1\n\n# SH1\n[\n # HA1\n la,\n # FA1\n\n # HB1\n lb,\n # FB1\n]\n# SF1\n\n# DF1\n",
  2324. yaml.Node{
  2325. Kind: yaml.DocumentNode,
  2326. Line: 4,
  2327. Column: 1,
  2328. HeadComment: "# DH1",
  2329. FootComment: "# DF1",
  2330. Content: []*yaml.Node{{
  2331. Kind: yaml.SequenceNode,
  2332. Tag: "!!seq",
  2333. Style: yaml.FlowStyle,
  2334. Line: 4,
  2335. Column: 1,
  2336. HeadComment: "# SH1",
  2337. FootComment: "# SF1",
  2338. Content: []*yaml.Node{{
  2339. Kind: yaml.ScalarNode,
  2340. Tag: "!!str",
  2341. Line: 6,
  2342. Column: 3,
  2343. Value: "la",
  2344. HeadComment: "# HA1",
  2345. FootComment: "# FA1",
  2346. }, {
  2347. Kind: yaml.ScalarNode,
  2348. Tag: "!!str",
  2349. Line: 10,
  2350. Column: 3,
  2351. Value: "lb",
  2352. HeadComment: "# HB1",
  2353. FootComment: "# FB1",
  2354. }},
  2355. }},
  2356. },
  2357. }, {
  2358. "ka:\n kb: [\n # HA1\n la,\n # FA1\n\n # HB1\n lb,\n # FB1\n ]\n",
  2359. yaml.Node{
  2360. Kind: yaml.DocumentNode,
  2361. Line: 1,
  2362. Column: 1,
  2363. Content: []*yaml.Node{{
  2364. Kind: yaml.MappingNode,
  2365. Tag: "!!map",
  2366. Line: 1,
  2367. Column: 1,
  2368. Content: []*yaml.Node{{
  2369. Kind: yaml.ScalarNode,
  2370. Tag: "!!str",
  2371. Value: "ka",
  2372. Line: 1,
  2373. Column: 1,
  2374. }, {
  2375. Kind: 0x4,
  2376. Tag: "!!map",
  2377. Line: 2,
  2378. Column: 3,
  2379. Content: []*yaml.Node{{
  2380. Kind: yaml.ScalarNode,
  2381. Tag: "!!str",
  2382. Value: "kb",
  2383. Line: 2,
  2384. Column: 3,
  2385. }, {
  2386. Kind: yaml.SequenceNode,
  2387. Style: 0x20,
  2388. Tag: "!!seq",
  2389. Line: 2,
  2390. Column: 7,
  2391. Content: []*yaml.Node{{
  2392. Kind: yaml.ScalarNode,
  2393. Tag: "!!str",
  2394. Value: "la",
  2395. HeadComment: "# HA1",
  2396. FootComment: "# FA1",
  2397. Line: 4,
  2398. Column: 5,
  2399. }, {
  2400. Kind: yaml.ScalarNode,
  2401. Tag: "!!str",
  2402. Value: "lb",
  2403. HeadComment: "# HB1",
  2404. FootComment: "# FB1",
  2405. Line: 8,
  2406. Column: 5,
  2407. }},
  2408. }},
  2409. }},
  2410. }},
  2411. },
  2412. }, {
  2413. "# DH1\n\n# MH1\n{\n # HA1\n ka: va, # IA\n # FA1\n\n # HB1\n kb: vb, # IB\n # FB1\n}\n# MF1\n\n# DF1\n",
  2414. yaml.Node{
  2415. Kind: yaml.DocumentNode,
  2416. Line: 4,
  2417. Column: 1,
  2418. HeadComment: "# DH1",
  2419. FootComment: "# DF1",
  2420. Content: []*yaml.Node{{
  2421. Kind: yaml.MappingNode,
  2422. Tag: "!!map",
  2423. Style: yaml.FlowStyle,
  2424. Line: 4,
  2425. Column: 1,
  2426. HeadComment: "# MH1",
  2427. FootComment: "# MF1",
  2428. Content: []*yaml.Node{{
  2429. Kind: yaml.ScalarNode,
  2430. Tag: "!!str",
  2431. Line: 6,
  2432. Column: 3,
  2433. Value: "ka",
  2434. HeadComment: "# HA1",
  2435. FootComment: "# FA1",
  2436. }, {
  2437. Kind: yaml.ScalarNode,
  2438. Tag: "!!str",
  2439. Line: 6,
  2440. Column: 7,
  2441. Value: "va",
  2442. LineComment: "# IA",
  2443. }, {
  2444. Kind: yaml.ScalarNode,
  2445. Tag: "!!str",
  2446. Line: 10,
  2447. Column: 3,
  2448. Value: "kb",
  2449. HeadComment: "# HB1",
  2450. FootComment: "# FB1",
  2451. }, {
  2452. Kind: yaml.ScalarNode,
  2453. Tag: "!!str",
  2454. Line: 10,
  2455. Column: 7,
  2456. Value: "vb",
  2457. LineComment: "# IB",
  2458. }},
  2459. }},
  2460. },
  2461. }, {
  2462. "# DH1\n\n# MH1\n{\n # HA1\n ka: va,\n # FA1\n\n # HB1\n kb: vb,\n # FB1\n}\n# MF1\n\n# DF1\n",
  2463. yaml.Node{
  2464. Kind: yaml.DocumentNode,
  2465. Line: 4,
  2466. Column: 1,
  2467. HeadComment: "# DH1",
  2468. FootComment: "# DF1",
  2469. Content: []*yaml.Node{{
  2470. Kind: yaml.MappingNode,
  2471. Tag: "!!map",
  2472. Style: yaml.FlowStyle,
  2473. Line: 4,
  2474. Column: 1,
  2475. HeadComment: "# MH1",
  2476. FootComment: "# MF1",
  2477. Content: []*yaml.Node{{
  2478. Kind: yaml.ScalarNode,
  2479. Tag: "!!str",
  2480. Line: 6,
  2481. Column: 3,
  2482. Value: "ka",
  2483. HeadComment: "# HA1",
  2484. FootComment: "# FA1",
  2485. }, {
  2486. Kind: yaml.ScalarNode,
  2487. Tag: "!!str",
  2488. Line: 6,
  2489. Column: 7,
  2490. Value: "va",
  2491. }, {
  2492. Kind: yaml.ScalarNode,
  2493. Tag: "!!str",
  2494. Line: 10,
  2495. Column: 3,
  2496. Value: "kb",
  2497. HeadComment: "# HB1",
  2498. FootComment: "# FB1",
  2499. }, {
  2500. Kind: yaml.ScalarNode,
  2501. Tag: "!!str",
  2502. Line: 10,
  2503. Column: 7,
  2504. Value: "vb",
  2505. }},
  2506. }},
  2507. },
  2508. }, {
  2509. "# DH1\n\n# DH2\n\n# HA1\n# HA2\n- &x la # IA\n# FA1\n# FA2\n\n# HB1\n# HB2\n- *x # IB\n# FB1\n# FB2\n\n# DF1\n\n# DF2\n",
  2510. yaml.Node{
  2511. Kind: yaml.DocumentNode,
  2512. Line: 7,
  2513. Column: 1,
  2514. HeadComment: "# DH1\n\n# DH2",
  2515. FootComment: "# DF1\n\n# DF2",
  2516. Content: []*yaml.Node{{
  2517. Kind: yaml.SequenceNode,
  2518. Tag: "!!seq",
  2519. Line: 7,
  2520. Column: 1,
  2521. Content: []*yaml.Node{
  2522. saveNode("x", &yaml.Node{
  2523. Kind: yaml.ScalarNode,
  2524. Tag: "!!str",
  2525. Line: 7,
  2526. Column: 3,
  2527. Value: "la",
  2528. HeadComment: "# HA1\n# HA2",
  2529. LineComment: "# IA",
  2530. FootComment: "# FA1\n# FA2",
  2531. Anchor: "x",
  2532. }), {
  2533. Kind: yaml.AliasNode,
  2534. Line: 13,
  2535. Column: 3,
  2536. Value: "x",
  2537. Alias: dropNode("x"),
  2538. HeadComment: "# HB1\n# HB2",
  2539. LineComment: "# IB",
  2540. FootComment: "# FB1\n# FB2",
  2541. },
  2542. },
  2543. }},
  2544. },
  2545. },
  2546. }
  2547. func (s *S) TestNodeRoundtrip(c *C) {
  2548. defer os.Setenv("TZ", os.Getenv("TZ"))
  2549. os.Setenv("TZ", "UTC")
  2550. for i, item := range nodeTests {
  2551. c.Logf("test %d: %q", i, item.yaml)
  2552. if strings.Contains(item.yaml, "#") {
  2553. var buf bytes.Buffer
  2554. fprintComments(&buf, &item.node, " ")
  2555. c.Logf(" expected comments:\n%s", buf.Bytes())
  2556. }
  2557. decode := true
  2558. encode := true
  2559. testYaml := item.yaml
  2560. if s := strings.TrimPrefix(testYaml, "[decode]"); s != testYaml {
  2561. encode = false
  2562. testYaml = s
  2563. }
  2564. if s := strings.TrimPrefix(testYaml, "[encode]"); s != testYaml {
  2565. decode = false
  2566. testYaml = s
  2567. }
  2568. if decode {
  2569. var node yaml.Node
  2570. err := yaml.Unmarshal([]byte(testYaml), &node)
  2571. c.Assert(err, IsNil)
  2572. if strings.Contains(item.yaml, "#") {
  2573. var buf bytes.Buffer
  2574. fprintComments(&buf, &node, " ")
  2575. c.Logf(" obtained comments:\n%s", buf.Bytes())
  2576. }
  2577. c.Assert(&node, DeepEquals, &item.node)
  2578. }
  2579. if encode {
  2580. node := deepCopyNode(&item.node, nil)
  2581. buf := bytes.Buffer{}
  2582. enc := yaml.NewEncoder(&buf)
  2583. enc.SetIndent(2)
  2584. err := enc.Encode(node)
  2585. c.Assert(err, IsNil)
  2586. err = enc.Close()
  2587. c.Assert(err, IsNil)
  2588. c.Assert(buf.String(), Equals, testYaml)
  2589. // Ensure there were no mutations to the tree.
  2590. c.Assert(node, DeepEquals, &item.node)
  2591. }
  2592. }
  2593. }
  2594. func deepCopyNode(node *yaml.Node, cache map[*yaml.Node]*yaml.Node) *yaml.Node {
  2595. if n, ok := cache[node]; ok {
  2596. return n
  2597. }
  2598. if cache == nil {
  2599. cache = make(map[*yaml.Node]*yaml.Node)
  2600. }
  2601. copy := *node
  2602. cache[node] = &copy
  2603. copy.Content = nil
  2604. for _, elem := range node.Content {
  2605. copy.Content = append(copy.Content, deepCopyNode(elem, cache))
  2606. }
  2607. if node.Alias != nil {
  2608. copy.Alias = deepCopyNode(node.Alias, cache)
  2609. }
  2610. return &copy
  2611. }
  2612. var savedNodes = make(map[string]*yaml.Node)
  2613. func saveNode(name string, node *yaml.Node) *yaml.Node {
  2614. savedNodes[name] = node
  2615. return node
  2616. }
  2617. func peekNode(name string) *yaml.Node {
  2618. return savedNodes[name]
  2619. }
  2620. func dropNode(name string) *yaml.Node {
  2621. node := savedNodes[name]
  2622. delete(savedNodes, name)
  2623. return node
  2624. }
  2625. var setStringTests = []struct {
  2626. str string
  2627. yaml string
  2628. node yaml.Node
  2629. }{
  2630. {
  2631. "something simple",
  2632. "something simple\n",
  2633. yaml.Node{
  2634. Kind: yaml.ScalarNode,
  2635. Value: "something simple",
  2636. Tag: "!!str",
  2637. },
  2638. }, {
  2639. `"quoted value"`,
  2640. "'\"quoted value\"'\n",
  2641. yaml.Node{
  2642. Kind: yaml.ScalarNode,
  2643. Value: `"quoted value"`,
  2644. Tag: "!!str",
  2645. },
  2646. }, {
  2647. "multi\nline",
  2648. "|-\n multi\n line\n",
  2649. yaml.Node{
  2650. Kind: yaml.ScalarNode,
  2651. Value: "multi\nline",
  2652. Tag: "!!str",
  2653. Style: yaml.LiteralStyle,
  2654. },
  2655. }, {
  2656. "123",
  2657. "\"123\"\n",
  2658. yaml.Node{
  2659. Kind: yaml.ScalarNode,
  2660. Value: "123",
  2661. Tag: "!!str",
  2662. },
  2663. }, {
  2664. "multi\nline\n",
  2665. "|\n multi\n line\n",
  2666. yaml.Node{
  2667. Kind: yaml.ScalarNode,
  2668. Value: "multi\nline\n",
  2669. Tag: "!!str",
  2670. Style: yaml.LiteralStyle,
  2671. },
  2672. }, {
  2673. "\x80\x81\x82",
  2674. "!!binary gIGC\n",
  2675. yaml.Node{
  2676. Kind: yaml.ScalarNode,
  2677. Value: "gIGC",
  2678. Tag: "!!binary",
  2679. },
  2680. },
  2681. }
  2682. func (s *S) TestSetString(c *C) {
  2683. defer os.Setenv("TZ", os.Getenv("TZ"))
  2684. os.Setenv("TZ", "UTC")
  2685. for i, item := range setStringTests {
  2686. c.Logf("test %d: %q", i, item.str)
  2687. var node yaml.Node
  2688. node.SetString(item.str)
  2689. c.Assert(node, DeepEquals, item.node)
  2690. buf := bytes.Buffer{}
  2691. enc := yaml.NewEncoder(&buf)
  2692. enc.SetIndent(2)
  2693. err := enc.Encode(&item.node)
  2694. c.Assert(err, IsNil)
  2695. err = enc.Close()
  2696. c.Assert(err, IsNil)
  2697. c.Assert(buf.String(), Equals, item.yaml)
  2698. var doc yaml.Node
  2699. err = yaml.Unmarshal([]byte(item.yaml), &doc)
  2700. c.Assert(err, IsNil)
  2701. var str string
  2702. err = node.Decode(&str)
  2703. c.Assert(err, IsNil)
  2704. c.Assert(str, Equals, item.str)
  2705. }
  2706. }
  2707. var nodeEncodeDecodeTests = []struct {
  2708. value interface{}
  2709. yaml string
  2710. node yaml.Node
  2711. }{{
  2712. "something simple",
  2713. "something simple\n",
  2714. yaml.Node{
  2715. Kind: yaml.ScalarNode,
  2716. Value: "something simple",
  2717. Tag: "!!str",
  2718. },
  2719. }, {
  2720. `"quoted value"`,
  2721. "'\"quoted value\"'\n",
  2722. yaml.Node{
  2723. Kind: yaml.ScalarNode,
  2724. Style: yaml.SingleQuotedStyle,
  2725. Value: `"quoted value"`,
  2726. Tag: "!!str",
  2727. },
  2728. }, {
  2729. 123,
  2730. "123",
  2731. yaml.Node{
  2732. Kind: yaml.ScalarNode,
  2733. Value: `123`,
  2734. Tag: "!!int",
  2735. },
  2736. }, {
  2737. []interface{}{1, 2},
  2738. "[1, 2]",
  2739. yaml.Node{
  2740. Kind: yaml.SequenceNode,
  2741. Tag: "!!seq",
  2742. Content: []*yaml.Node{{
  2743. Kind: yaml.ScalarNode,
  2744. Value: "1",
  2745. Tag: "!!int",
  2746. }, {
  2747. Kind: yaml.ScalarNode,
  2748. Value: "2",
  2749. Tag: "!!int",
  2750. }},
  2751. },
  2752. }, {
  2753. map[string]interface{}{"a": "b"},
  2754. "a: b",
  2755. yaml.Node{
  2756. Kind: yaml.MappingNode,
  2757. Tag: "!!map",
  2758. Content: []*yaml.Node{{
  2759. Kind: yaml.ScalarNode,
  2760. Value: "a",
  2761. Tag: "!!str",
  2762. }, {
  2763. Kind: yaml.ScalarNode,
  2764. Value: "b",
  2765. Tag: "!!str",
  2766. }},
  2767. },
  2768. }}
  2769. func (s *S) TestNodeEncodeDecode(c *C) {
  2770. for i, item := range nodeEncodeDecodeTests {
  2771. c.Logf("Encode/Decode test value #%d: %#v", i, item.value)
  2772. var v interface{}
  2773. err := item.node.Decode(&v)
  2774. c.Assert(err, IsNil)
  2775. c.Assert(v, DeepEquals, item.value)
  2776. var n yaml.Node
  2777. err = n.Encode(item.value)
  2778. c.Assert(err, IsNil)
  2779. c.Assert(n, DeepEquals, item.node)
  2780. }
  2781. }
  2782. func (s *S) TestNodeZeroEncodeDecode(c *C) {
  2783. // Zero node value behaves as nil when encoding...
  2784. var n yaml.Node
  2785. data, err := yaml.Marshal(&n)
  2786. c.Assert(err, IsNil)
  2787. c.Assert(string(data), Equals, "null\n")
  2788. // ... and decoding.
  2789. var v *struct{} = &struct{}{}
  2790. c.Assert(n.Decode(&v), IsNil)
  2791. c.Assert(v, IsNil)
  2792. // ... and even when looking for its tag.
  2793. c.Assert(n.ShortTag(), Equals, "!!null")
  2794. // Kind zero is still unknown, though.
  2795. n.Line = 1
  2796. _, err = yaml.Marshal(&n)
  2797. c.Assert(err, ErrorMatches, "yaml: cannot encode node with unknown kind 0")
  2798. c.Assert(n.Decode(&v), ErrorMatches, "yaml: cannot decode node with unknown kind 0")
  2799. }
  2800. func (s *S) TestNodeOmitEmpty(c *C) {
  2801. var v struct {
  2802. A int
  2803. B yaml.Node ",omitempty"
  2804. }
  2805. v.A = 1
  2806. data, err := yaml.Marshal(&v)
  2807. c.Assert(err, IsNil)
  2808. c.Assert(string(data), Equals, "a: 1\n")
  2809. v.B.Line = 1
  2810. _, err = yaml.Marshal(&v)
  2811. c.Assert(err, ErrorMatches, "yaml: cannot encode node with unknown kind 0")
  2812. }
  2813. func fprintComments(out io.Writer, node *yaml.Node, indent string) {
  2814. switch node.Kind {
  2815. case yaml.ScalarNode:
  2816. fmt.Fprintf(out, "%s<%s> ", indent, node.Value)
  2817. fprintCommentSet(out, node)
  2818. fmt.Fprintf(out, "\n")
  2819. case yaml.DocumentNode:
  2820. fmt.Fprintf(out, "%s<DOC> ", indent)
  2821. fprintCommentSet(out, node)
  2822. fmt.Fprintf(out, "\n")
  2823. for i := 0; i < len(node.Content); i++ {
  2824. fprintComments(out, node.Content[i], indent+" ")
  2825. }
  2826. case yaml.MappingNode:
  2827. fmt.Fprintf(out, "%s<MAP> ", indent)
  2828. fprintCommentSet(out, node)
  2829. fmt.Fprintf(out, "\n")
  2830. for i := 0; i < len(node.Content); i += 2 {
  2831. fprintComments(out, node.Content[i], indent+" ")
  2832. fprintComments(out, node.Content[i+1], indent+" ")
  2833. }
  2834. case yaml.SequenceNode:
  2835. fmt.Fprintf(out, "%s<SEQ> ", indent)
  2836. fprintCommentSet(out, node)
  2837. fmt.Fprintf(out, "\n")
  2838. for i := 0; i < len(node.Content); i++ {
  2839. fprintComments(out, node.Content[i], indent+" ")
  2840. }
  2841. }
  2842. }
  2843. func fprintCommentSet(out io.Writer, node *yaml.Node) {
  2844. if len(node.HeadComment)+len(node.LineComment)+len(node.FootComment) > 0 {
  2845. fmt.Fprintf(out, "%q / %q / %q", node.HeadComment, node.LineComment, node.FootComment)
  2846. }
  2847. }