YamlSpec.coffee 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. unless YAML?
  2. YAML = require '../../src/Yaml'
  3. # Parsing
  4. #
  5. describe 'Parsed YAML Collections', ->
  6. it 'can be simple sequence', ->
  7. expect YAML.parse """
  8. - apple
  9. - banana
  10. - carrot
  11. """
  12. .toEqual ['apple', 'banana', 'carrot']
  13. it 'can be nested sequences', ->
  14. expect YAML.parse """
  15. -
  16. - foo
  17. - bar
  18. - baz
  19. """
  20. .toEqual [['foo', 'bar', 'baz']]
  21. it 'can be mixed sequences', ->
  22. expect YAML.parse """
  23. - apple
  24. -
  25. - foo
  26. - bar
  27. - x123
  28. - banana
  29. - carrot
  30. """
  31. .toEqual ['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']
  32. it 'can be deeply nested sequences', ->
  33. expect YAML.parse """
  34. -
  35. -
  36. - uno
  37. - dos
  38. """
  39. .toEqual [[['uno', 'dos']]]
  40. it 'can be simple mapping', ->
  41. expect YAML.parse """
  42. foo: whatever
  43. bar: stuff
  44. """
  45. .toEqual foo: 'whatever', bar: 'stuff'
  46. it 'can be sequence in a mapping', ->
  47. expect YAML.parse """
  48. foo: whatever
  49. bar:
  50. - uno
  51. - dos
  52. """
  53. .toEqual foo: 'whatever', bar: ['uno', 'dos']
  54. it 'can be nested mappings', ->
  55. expect YAML.parse """
  56. foo: whatever
  57. bar:
  58. fruit: apple
  59. name: steve
  60. sport: baseball
  61. """
  62. .toEqual foo: 'whatever', bar: (fruit: 'apple', name: 'steve', sport: 'baseball')
  63. it 'can be mixed mapping', ->
  64. expect YAML.parse """
  65. foo: whatever
  66. bar:
  67. -
  68. fruit: apple
  69. name: steve
  70. sport: baseball
  71. - more
  72. -
  73. python: rocks
  74. perl: papers
  75. ruby: scissorses
  76. """
  77. .toEqual foo: 'whatever', bar: [
  78. (fruit: 'apple', name: 'steve', sport: 'baseball'),
  79. 'more',
  80. (python: 'rocks', perl: 'papers', ruby: 'scissorses')
  81. ]
  82. it 'can have mapping-in-sequence shortcut', ->
  83. expect YAML.parse """
  84. - work on YAML.py:
  85. - work on Store
  86. """
  87. .toEqual [('work on YAML.py': ['work on Store'])]
  88. it 'can have unindented sequence-in-mapping shortcut', ->
  89. expect YAML.parse """
  90. allow:
  91. - 'localhost'
  92. - '%.sourceforge.net'
  93. - '%.freepan.org'
  94. """
  95. .toEqual (allow: ['localhost', '%.sourceforge.net', '%.freepan.org'])
  96. it 'can merge key', ->
  97. expect YAML.parse """
  98. mapping:
  99. name: Joe
  100. job: Accountant
  101. <<:
  102. age: 38
  103. """
  104. .toEqual mapping:
  105. name: 'Joe'
  106. job: 'Accountant'
  107. age: 38
  108. it 'can ignore trailing empty lines for smallest indent', ->
  109. expect YAML.parse """ trailing: empty lines\n"""
  110. .toEqual trailing: 'empty lines'
  111. describe 'Parsed YAML Inline Collections', ->
  112. it 'can be simple inline array', ->
  113. expect YAML.parse """
  114. ---
  115. seq: [ a, b, c ]
  116. """
  117. .toEqual seq: ['a', 'b', 'c']
  118. it 'can be simple inline hash', ->
  119. expect YAML.parse """
  120. ---
  121. hash: { name: Steve, foo: bar }
  122. """
  123. .toEqual hash: (name: 'Steve', foo: 'bar')
  124. it 'can be nested inline hash', ->
  125. expect YAML.parse """
  126. ---
  127. hash: { val1: "string", val2: { v2k1: "v2k1v" } }
  128. """
  129. .toEqual hash: (val1: 'string', val2: (v2k1: 'v2k1v'))
  130. it 'can be multi-line inline collections', ->
  131. expect YAML.parse """
  132. languages: [ Ruby,
  133. Perl,
  134. Python ]
  135. websites: { YAML: yaml.org,
  136. Ruby: ruby-lang.org,
  137. Python: python.org,
  138. Perl: use.perl.org }
  139. """
  140. .toEqual (
  141. languages: ['Ruby', 'Perl', 'Python']
  142. websites:
  143. YAML: 'yaml.org'
  144. Ruby: 'ruby-lang.org'
  145. Python: 'python.org'
  146. Perl: 'use.perl.org'
  147. )
  148. describe 'Parsed YAML Basic Types', ->
  149. it 'can be strings', ->
  150. expect YAML.parse """
  151. ---
  152. String
  153. """
  154. .toEqual 'String'
  155. it 'can be double-quoted strings with backslashes', ->
  156. expect YAML.parse """
  157. str:
  158. "string with \\\\ inside"
  159. """
  160. .toEqual str: 'string with \\ inside'
  161. it 'can be single-quoted strings with backslashes', ->
  162. expect YAML.parse """
  163. str:
  164. 'string with \\\\ inside'
  165. """
  166. .toEqual str: 'string with \\\\ inside'
  167. it 'can be double-quoted strings with line breaks', ->
  168. expect YAML.parse """
  169. str:
  170. "string with \\n inside"
  171. """
  172. .toEqual str: 'string with \n inside'
  173. it 'can be single-quoted strings with escaped line breaks', ->
  174. expect YAML.parse """
  175. str:
  176. 'string with \\n inside'
  177. """
  178. .toEqual str: 'string with \\n inside'
  179. it 'can be double-quoted strings with line breaks and backslashes', ->
  180. expect YAML.parse """
  181. str:
  182. "string with \\n inside and \\\\ also"
  183. """
  184. .toEqual str: 'string with \n inside and \\ also'
  185. it 'can be single-quoted strings with line breaks and backslashes', ->
  186. expect YAML.parse """
  187. str:
  188. 'string with \\n inside and \\\\ also'
  189. """
  190. .toEqual str: 'string with \\n inside and \\\\ also'
  191. it 'can have string characters in sequences', ->
  192. expect YAML.parse """
  193. - What's Yaml?
  194. - It's for writing data structures in plain text.
  195. - And?
  196. - And what? That's not good enough for you?
  197. - No, I mean, "And what about Yaml?"
  198. - Oh, oh yeah. Uh.. Yaml for JavaScript.
  199. """
  200. .toEqual [
  201. "What's Yaml?",
  202. "It's for writing data structures in plain text.",
  203. "And?",
  204. "And what? That's not good enough for you?",
  205. "No, I mean, \"And what about Yaml?\"",
  206. "Oh, oh yeah. Uh.. Yaml for JavaScript."
  207. ]
  208. it 'can have indicators in strings', ->
  209. expect YAML.parse """
  210. the colon followed by space is an indicator: but is a string:right here
  211. same for the pound sign: here we have it#in a string
  212. the comma can, honestly, be used in most cases: [ but not in, inline collections ]
  213. """
  214. .toEqual (
  215. 'the colon followed by space is an indicator': 'but is a string:right here',
  216. 'same for the pound sign': 'here we have it#in a string',
  217. 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
  218. )
  219. it 'can force strings', ->
  220. expect YAML.parse """
  221. date string: !str 2001-08-01
  222. number string: !str 192
  223. date string 2: !!str 2001-08-01
  224. number string 2: !!str 192
  225. """
  226. .toEqual (
  227. 'date string': '2001-08-01',
  228. 'number string': '192' ,
  229. 'date string 2': '2001-08-01',
  230. 'number string 2': '192'
  231. )
  232. it 'can be single-quoted strings', ->
  233. expect YAML.parse """
  234. all my favorite symbols: '#:!/%.)'
  235. a few i hate: '&(*'
  236. why do i hate them?: 'it''s very hard to explain'
  237. """
  238. .toEqual (
  239. 'all my favorite symbols': '#:!/%.)',
  240. 'a few i hate': '&(*',
  241. 'why do i hate them?': 'it\'s very hard to explain'
  242. )
  243. it 'can be double-quoted strings', ->
  244. expect YAML.parse """
  245. i know where i want my line breaks: "one here\\nand another here\\n"
  246. """
  247. .toEqual (
  248. 'i know where i want my line breaks': "one here\nand another here\n"
  249. )
  250. it 'can be null', ->
  251. expect YAML.parse """
  252. name: Mr. Show
  253. hosted by: Bob and David
  254. date of next season: ~
  255. """
  256. .toEqual (
  257. 'name': 'Mr. Show'
  258. 'hosted by': 'Bob and David'
  259. 'date of next season': null
  260. )
  261. it 'can be boolean', ->
  262. expect YAML.parse """
  263. Is Gus a Liar?: true
  264. Do I rely on Gus for Sustenance?: false
  265. """
  266. .toEqual (
  267. 'Is Gus a Liar?': true
  268. 'Do I rely on Gus for Sustenance?': false
  269. )
  270. it 'can be integers', ->
  271. expect YAML.parse """
  272. zero: 0
  273. simple: 12
  274. one-thousand: 1,000
  275. negative one-thousand: -1,000
  276. """
  277. .toEqual (
  278. 'zero': 0
  279. 'simple': 12
  280. 'one-thousand': 1000
  281. 'negative one-thousand': -1000
  282. )
  283. it 'can be integers as map keys', ->
  284. expect YAML.parse """
  285. 1: one
  286. 2: two
  287. 3: three
  288. """
  289. .toEqual (
  290. 1: 'one'
  291. 2: 'two'
  292. 3: 'three'
  293. )
  294. it 'can be floats', ->
  295. expect YAML.parse """
  296. a simple float: 2.00
  297. larger float: 1,000.09
  298. scientific notation: 1.00009e+3
  299. """
  300. .toEqual (
  301. 'a simple float': 2.0
  302. 'larger float': 1000.09
  303. 'scientific notation': 1000.09
  304. )
  305. it 'can be time', ->
  306. iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
  307. iso8601Date.setTime iso8601Date.getTime() - 5 * 3600 * 1000
  308. spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
  309. spaceSeparatedDate.setTime spaceSeparatedDate.getTime() + 5 * 3600 * 1000
  310. withDatesToTime = (input) ->
  311. res = {}
  312. for key, val of input
  313. res[key] = val.getTime()
  314. return res
  315. expect withDatesToTime(YAML.parse """
  316. iso8601: 2001-12-14t21:59:43.010+05:00
  317. space separated: 2001-12-14 21:59:43.010 -05:00
  318. """)
  319. .toEqual withDatesToTime (
  320. 'iso8601': iso8601Date
  321. 'space separated': spaceSeparatedDate
  322. )
  323. it 'can be date', ->
  324. aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
  325. withDatesToTime = (input) ->
  326. return input
  327. res = {}
  328. for key, val of input
  329. res[key] = val.getTime()
  330. return res
  331. expect withDatesToTime(YAML.parse """
  332. date: 1976-07-31
  333. """)
  334. .toEqual withDatesToTime (
  335. 'date': aDate
  336. )
  337. describe 'Parsed YAML Blocks', ->
  338. it 'can be single ending newline', ->
  339. expect YAML.parse """
  340. ---
  341. this: |
  342. Foo
  343. Bar
  344. """
  345. .toEqual 'this': "Foo\nBar\n"
  346. it 'can be single ending newline with \'+\' indicator', ->
  347. expect YAML.parse """
  348. normal: |
  349. extra new lines not kept
  350. preserving: |+
  351. extra new lines are kept
  352. dummy: value
  353. """
  354. .toEqual (
  355. 'normal': "extra new lines not kept\n"
  356. 'preserving': "extra new lines are kept\n\n\n"
  357. 'dummy': 'value'
  358. )
  359. it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
  360. expect YAML.parse """
  361. clipped: |
  362. This has one newline.
  363. same as "clipped" above: "This has one newline.\\n"
  364. stripped: |-
  365. This has no newline.
  366. same as "stripped" above: "This has no newline."
  367. kept: |+
  368. This has four newlines.
  369. same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
  370. """
  371. .toEqual (
  372. 'clipped': "This has one newline.\n"
  373. 'same as "clipped" above': "This has one newline.\n"
  374. 'stripped':'This has no newline.'
  375. 'same as "stripped" above': 'This has no newline.'
  376. 'kept': "This has four newlines.\n\n\n\n"
  377. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  378. )
  379. it 'can be folded block in a sequence', ->
  380. expect YAML.parse """
  381. ---
  382. - apple
  383. - banana
  384. - >
  385. can't you see
  386. the beauty of yaml?
  387. hmm
  388. - dog
  389. """
  390. .toEqual [
  391. 'apple',
  392. 'banana',
  393. "can't you see the beauty of yaml? hmm\n",
  394. 'dog'
  395. ]
  396. it 'can be folded block as a mapping value', ->
  397. expect YAML.parse """
  398. ---
  399. quote: >
  400. Mark McGwire's
  401. year was crippled
  402. by a knee injury.
  403. source: espn
  404. """
  405. .toEqual (
  406. 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
  407. 'source': 'espn'
  408. )
  409. it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
  410. expect YAML.parse """
  411. clipped: >
  412. This has one newline.
  413. same as "clipped" above: "This has one newline.\\n"
  414. stripped: >-
  415. This has no newline.
  416. same as "stripped" above: "This has no newline."
  417. kept: >+
  418. This has four newlines.
  419. same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
  420. """
  421. .toEqual (
  422. 'clipped': "This has one newline.\n"
  423. 'same as "clipped" above': "This has one newline.\n"
  424. 'stripped': 'This has no newline.'
  425. 'same as "stripped" above': 'This has no newline.'
  426. 'kept': "This has four newlines.\n\n\n\n"
  427. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  428. )
  429. it 'can be the whole document as intented block', ->
  430. expect YAML.parse """
  431. ---
  432. foo: "bar"
  433. baz:
  434. - "qux"
  435. - "quxx"
  436. corge: null
  437. """
  438. .toEqual (
  439. 'foo': "bar"
  440. 'baz': ['qux', 'quxx']
  441. 'corge': null
  442. )
  443. describe 'Parsed YAML Comments', ->
  444. it 'can begin the document', ->
  445. expect YAML.parse """
  446. # This is a comment
  447. hello: world
  448. """
  449. .toEqual (
  450. hello: 'world'
  451. )
  452. it 'can be less indented in mapping', ->
  453. expect YAML.parse """
  454. parts:
  455. a: 'b'
  456. # normally indented comment
  457. c: 'd'
  458. # less indented comment
  459. e: 'f'
  460. """
  461. .toEqual (
  462. parts: {a: 'b', c: 'd', e: 'f'}
  463. )
  464. it 'can be less indented in sequence', ->
  465. expect YAML.parse """
  466. list-header:
  467. - item1
  468. # - item2
  469. - item3
  470. # - item4
  471. """
  472. .toEqual (
  473. 'list-header': ['item1', 'item3']
  474. )
  475. it 'can finish a line', ->
  476. expect YAML.parse """
  477. hello: world # This is a comment
  478. """
  479. .toEqual (
  480. hello: 'world'
  481. )
  482. it 'can end the document', ->
  483. expect YAML.parse """
  484. hello: world
  485. # This is a comment
  486. """
  487. .toEqual (
  488. hello: 'world'
  489. )
  490. describe 'Parsed YAML Aliases and Anchors', ->
  491. it 'can be simple alias', ->
  492. expect YAML.parse """
  493. - &showell Steve
  494. - Clark
  495. - Brian
  496. - Oren
  497. - *showell
  498. """
  499. .toEqual ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
  500. it 'can be alias of a mapping', ->
  501. expect YAML.parse """
  502. - &hello
  503. Meat: pork
  504. Starch: potato
  505. - banana
  506. - *hello
  507. """
  508. .toEqual [
  509. Meat: 'pork', Starch: 'potato'
  510. ,
  511. 'banana'
  512. ,
  513. Meat: 'pork', Starch: 'potato'
  514. ]
  515. describe 'Parsed YAML Documents', ->
  516. it 'can have YAML header', ->
  517. expect YAML.parse """
  518. --- %YAML:1.0
  519. foo: 1
  520. bar: 2
  521. """
  522. .toEqual (
  523. foo: 1
  524. bar: 2
  525. )
  526. it 'can have leading document separator', ->
  527. expect YAML.parse """
  528. ---
  529. - foo: 1
  530. bar: 2
  531. """
  532. .toEqual [(
  533. foo: 1
  534. bar: 2
  535. )]
  536. it 'can have multiple document separators in block', ->
  537. expect YAML.parse """
  538. foo: |
  539. ---
  540. foo: bar
  541. ---
  542. yo: baz
  543. bar: |
  544. fooness
  545. """
  546. .toEqual (
  547. foo: "---\nfoo: bar\n---\nyo: baz\n"
  548. bar: "fooness\n"
  549. )
  550. # Dumping
  551. #
  552. describe 'Dumped YAML Collections', ->
  553. it 'can be simple sequence', ->
  554. expect YAML.parse """
  555. - apple
  556. - banana
  557. - carrot
  558. """
  559. .toEqual YAML.parse YAML.dump ['apple', 'banana', 'carrot']
  560. it 'can be nested sequences', ->
  561. expect YAML.parse """
  562. -
  563. - foo
  564. - bar
  565. - baz
  566. """
  567. .toEqual YAML.parse YAML.dump [['foo', 'bar', 'baz']]
  568. it 'can be mixed sequences', ->
  569. expect YAML.parse """
  570. - apple
  571. -
  572. - foo
  573. - bar
  574. - x123
  575. - banana
  576. - carrot
  577. """
  578. .toEqual YAML.parse YAML.dump ['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']
  579. it 'can be deeply nested sequences', ->
  580. expect YAML.parse """
  581. -
  582. -
  583. - uno
  584. - dos
  585. """
  586. .toEqual YAML.parse YAML.dump [[['uno', 'dos']]]
  587. it 'can be simple mapping', ->
  588. expect YAML.parse """
  589. foo: whatever
  590. bar: stuff
  591. """
  592. .toEqual YAML.parse YAML.dump foo: 'whatever', bar: 'stuff'
  593. it 'can be sequence in a mapping', ->
  594. expect YAML.parse """
  595. foo: whatever
  596. bar:
  597. - uno
  598. - dos
  599. """
  600. .toEqual YAML.parse YAML.dump foo: 'whatever', bar: ['uno', 'dos']
  601. it 'can be nested mappings', ->
  602. expect YAML.parse """
  603. foo: whatever
  604. bar:
  605. fruit: apple
  606. name: steve
  607. sport: baseball
  608. """
  609. .toEqual YAML.parse YAML.dump foo: 'whatever', bar: (fruit: 'apple', name: 'steve', sport: 'baseball')
  610. it 'can be mixed mapping', ->
  611. expect YAML.parse """
  612. foo: whatever
  613. bar:
  614. -
  615. fruit: apple
  616. name: steve
  617. sport: baseball
  618. - more
  619. -
  620. python: rocks
  621. perl: papers
  622. ruby: scissorses
  623. """
  624. .toEqual YAML.parse YAML.dump foo: 'whatever', bar: [
  625. (fruit: 'apple', name: 'steve', sport: 'baseball'),
  626. 'more',
  627. (python: 'rocks', perl: 'papers', ruby: 'scissorses')
  628. ]
  629. it 'can have mapping-in-sequence shortcut', ->
  630. expect YAML.parse """
  631. - work on YAML.py:
  632. - work on Store
  633. """
  634. .toEqual YAML.parse YAML.dump [('work on YAML.py': ['work on Store'])]
  635. it 'can have unindented sequence-in-mapping shortcut', ->
  636. expect YAML.parse """
  637. allow:
  638. - 'localhost'
  639. - '%.sourceforge.net'
  640. - '%.freepan.org'
  641. """
  642. .toEqual YAML.parse YAML.dump (allow: ['localhost', '%.sourceforge.net', '%.freepan.org'])
  643. it 'can merge key', ->
  644. expect YAML.parse """
  645. mapping:
  646. name: Joe
  647. job: Accountant
  648. <<:
  649. age: 38
  650. """
  651. .toEqual YAML.parse YAML.dump mapping:
  652. name: 'Joe'
  653. job: 'Accountant'
  654. age: 38
  655. describe 'Dumped YAML Inline Collections', ->
  656. it 'can be simple inline array', ->
  657. expect YAML.parse """
  658. ---
  659. seq: [ a, b, c ]
  660. """
  661. .toEqual YAML.parse YAML.dump seq: ['a', 'b', 'c']
  662. it 'can be simple inline hash', ->
  663. expect YAML.parse """
  664. ---
  665. hash: { name: Steve, foo: bar }
  666. """
  667. .toEqual YAML.parse YAML.dump hash: (name: 'Steve', foo: 'bar')
  668. it 'can be multi-line inline collections', ->
  669. expect YAML.parse """
  670. languages: [ Ruby,
  671. Perl,
  672. Python ]
  673. websites: { YAML: yaml.org,
  674. Ruby: ruby-lang.org,
  675. Python: python.org,
  676. Perl: use.perl.org }
  677. """
  678. .toEqual YAML.parse YAML.dump (
  679. languages: ['Ruby', 'Perl', 'Python']
  680. websites:
  681. YAML: 'yaml.org'
  682. Ruby: 'ruby-lang.org'
  683. Python: 'python.org'
  684. Perl: 'use.perl.org'
  685. )
  686. it 'can be dumped empty sequences in mappings', ->
  687. expect YAML.parse(YAML.dump({key:[]}))
  688. .toEqual({key:[]})
  689. it 'can be dumpted empty inline collections', ->
  690. expect YAML.parse(YAML.dump({key:{}}))
  691. .toEqual({key:{}})
  692. describe 'Dumped YAML Basic Types', ->
  693. it 'can be strings', ->
  694. expect YAML.parse """
  695. ---
  696. String
  697. """
  698. .toEqual YAML.parse YAML.dump 'String'
  699. it 'can be double-quoted strings with backslashes', ->
  700. expect YAML.parse """
  701. str:
  702. "string with \\\\ inside"
  703. """
  704. .toEqual YAML.parse YAML.dump str: 'string with \\ inside'
  705. it 'can be single-quoted strings with backslashes', ->
  706. expect YAML.parse """
  707. str:
  708. 'string with \\\\ inside'
  709. """
  710. .toEqual YAML.parse YAML.dump str: 'string with \\\\ inside'
  711. it 'can be double-quoted strings with line breaks', ->
  712. expect YAML.parse """
  713. str:
  714. "string with \\n inside"
  715. """
  716. .toEqual YAML.parse YAML.dump str: 'string with \n inside'
  717. it 'can be double-quoted strings with line breaks and backslashes', ->
  718. expect YAML.parse """
  719. str:
  720. "string with \\n inside and \\\\ also"
  721. """
  722. .toEqual YAML.parse YAML.dump str: 'string with \n inside and \\ also'
  723. it 'can be single-quoted strings with line breaks and backslashes', ->
  724. expect YAML.parse """
  725. str:
  726. 'string with \\n inside and \\\\ also'
  727. """
  728. .toEqual YAML.parse YAML.dump str: 'string with \\n inside and \\\\ also'
  729. it 'can be single-quoted strings with escaped line breaks', ->
  730. expect YAML.parse """
  731. str:
  732. 'string with \\n inside'
  733. """
  734. .toEqual YAML.parse YAML.dump str: 'string with \\n inside'
  735. it 'can have string characters in sequences', ->
  736. expect YAML.parse """
  737. - What's Yaml?
  738. - It's for writing data structures in plain text.
  739. - And?
  740. - And what? That's not good enough for you?
  741. - No, I mean, "And what about Yaml?"
  742. - Oh, oh yeah. Uh.. Yaml for JavaScript.
  743. """
  744. .toEqual YAML.parse YAML.dump [
  745. "What's Yaml?",
  746. "It's for writing data structures in plain text.",
  747. "And?",
  748. "And what? That's not good enough for you?",
  749. "No, I mean, \"And what about Yaml?\"",
  750. "Oh, oh yeah. Uh.. Yaml for JavaScript."
  751. ]
  752. it 'can have indicators in strings', ->
  753. expect YAML.parse """
  754. the colon followed by space is an indicator: but is a string:right here
  755. same for the pound sign: here we have it#in a string
  756. the comma can, honestly, be used in most cases: [ but not in, inline collections ]
  757. """
  758. .toEqual YAML.parse YAML.dump (
  759. 'the colon followed by space is an indicator': 'but is a string:right here',
  760. 'same for the pound sign': 'here we have it#in a string',
  761. 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
  762. )
  763. it 'can force strings', ->
  764. expect YAML.parse """
  765. date string: !str 2001-08-01
  766. number string: !str 192
  767. date string 2: !!str 2001-08-01
  768. number string 2: !!str 192
  769. """
  770. .toEqual YAML.parse YAML.dump (
  771. 'date string': '2001-08-01',
  772. 'number string': '192' ,
  773. 'date string 2': '2001-08-01',
  774. 'number string 2': '192'
  775. )
  776. it 'can be single-quoted strings', ->
  777. expect YAML.parse """
  778. all my favorite symbols: '#:!/%.)'
  779. a few i hate: '&(*'
  780. why do i hate them?: 'it''s very hard to explain'
  781. """
  782. .toEqual YAML.parse YAML.dump (
  783. 'all my favorite symbols': '#:!/%.)',
  784. 'a few i hate': '&(*',
  785. 'why do i hate them?': 'it\'s very hard to explain'
  786. )
  787. it 'can be double-quoted strings', ->
  788. expect YAML.parse """
  789. i know where i want my line breaks: "one here\\nand another here\\n"
  790. """
  791. .toEqual YAML.parse YAML.dump (
  792. 'i know where i want my line breaks': "one here\nand another here\n"
  793. )
  794. it 'can be null', ->
  795. expect YAML.parse """
  796. name: Mr. Show
  797. hosted by: Bob and David
  798. date of next season: ~
  799. """
  800. .toEqual YAML.parse YAML.dump (
  801. 'name': 'Mr. Show'
  802. 'hosted by': 'Bob and David'
  803. 'date of next season': null
  804. )
  805. it 'can be boolean', ->
  806. expect YAML.parse """
  807. Is Gus a Liar?: true
  808. Do I rely on Gus for Sustenance?: false
  809. """
  810. .toEqual YAML.parse YAML.dump (
  811. 'Is Gus a Liar?': true
  812. 'Do I rely on Gus for Sustenance?': false
  813. )
  814. it 'can be integers', ->
  815. expect YAML.parse """
  816. zero: 0
  817. simple: 12
  818. one-thousand: 1,000
  819. negative one-thousand: -1,000
  820. """
  821. .toEqual YAML.parse YAML.dump (
  822. 'zero': 0
  823. 'simple': 12
  824. 'one-thousand': 1000
  825. 'negative one-thousand': -1000
  826. )
  827. it 'can be integers as map keys', ->
  828. expect YAML.parse """
  829. 1: one
  830. 2: two
  831. 3: three
  832. """
  833. .toEqual YAML.parse YAML.dump (
  834. 1: 'one'
  835. 2: 'two'
  836. 3: 'three'
  837. )
  838. it 'can be floats', ->
  839. expect YAML.parse """
  840. a simple float: 2.00
  841. larger float: 1,000.09
  842. scientific notation: 1.00009e+3
  843. """
  844. .toEqual YAML.parse YAML.dump (
  845. 'a simple float': 2.0
  846. 'larger float': 1000.09
  847. 'scientific notation': 1000.09
  848. )
  849. it 'can be time', ->
  850. iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
  851. iso8601Date.setTime iso8601Date.getTime() + 5 * 3600 * 1000
  852. spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
  853. spaceSeparatedDate.setTime spaceSeparatedDate.getTime() - 5 * 3600 * 1000
  854. withDatesToTime = (input) ->
  855. res = {}
  856. for key, val of input
  857. res[key] = val.getTime()
  858. return res
  859. expect withDatesToTime(YAML.parse """
  860. iso8601: 2001-12-14t21:59:43.010-05:00
  861. space separated: 2001-12-14 21:59:43.010 +05:00
  862. """)
  863. .toEqual YAML.parse YAML.dump withDatesToTime (
  864. 'iso8601': iso8601Date
  865. 'space separated': spaceSeparatedDate
  866. )
  867. it 'can be date', ->
  868. aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
  869. withDatesToTime = (input) ->
  870. return input
  871. res = {}
  872. for key, val of input
  873. res[key] = val.getTime()
  874. return res
  875. expect withDatesToTime(YAML.parse """
  876. date: 1976-07-31
  877. """)
  878. .toEqual YAML.parse YAML.dump withDatesToTime (
  879. 'date': aDate
  880. )
  881. describe 'Dumped YAML Blocks', ->
  882. it 'can be single ending newline', ->
  883. expect YAML.parse """
  884. ---
  885. this: |
  886. Foo
  887. Bar
  888. """
  889. .toEqual YAML.parse YAML.dump 'this': "Foo\nBar\n"
  890. it 'can be single ending newline with \'+\' indicator', ->
  891. expect YAML.parse """
  892. normal: |
  893. extra new lines not kept
  894. preserving: |+
  895. extra new lines are kept
  896. dummy: value
  897. """
  898. .toEqual YAML.parse YAML.dump (
  899. 'normal': "extra new lines not kept\n"
  900. 'preserving': "extra new lines are kept\n\n\n"
  901. 'dummy': 'value'
  902. )
  903. it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
  904. expect YAML.parse """
  905. clipped: |
  906. This has one newline.
  907. same as "clipped" above: "This has one newline.\\n"
  908. stripped: |-
  909. This has no newline.
  910. same as "stripped" above: "This has no newline."
  911. kept: |+
  912. This has four newlines.
  913. same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
  914. """
  915. .toEqual YAML.parse YAML.dump (
  916. 'clipped': "This has one newline.\n"
  917. 'same as "clipped" above': "This has one newline.\n"
  918. 'stripped':'This has no newline.'
  919. 'same as "stripped" above': 'This has no newline.'
  920. 'kept': "This has four newlines.\n\n\n\n"
  921. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  922. )
  923. it 'can be folded block in a sequence', ->
  924. expect YAML.parse """
  925. ---
  926. - apple
  927. - banana
  928. - >
  929. can't you see
  930. the beauty of yaml?
  931. hmm
  932. - dog
  933. """
  934. .toEqual YAML.parse YAML.dump [
  935. 'apple',
  936. 'banana',
  937. "can't you see the beauty of yaml? hmm\n",
  938. 'dog'
  939. ]
  940. it 'can be folded block as a mapping value', ->
  941. expect YAML.parse """
  942. ---
  943. quote: >
  944. Mark McGwire's
  945. year was crippled
  946. by a knee injury.
  947. source: espn
  948. """
  949. .toEqual YAML.parse YAML.dump (
  950. 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
  951. 'source': 'espn'
  952. )
  953. it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
  954. expect YAML.parse """
  955. clipped: >
  956. This has one newline.
  957. same as "clipped" above: "This has one newline.\\n"
  958. stripped: >-
  959. This has no newline.
  960. same as "stripped" above: "This has no newline."
  961. kept: >+
  962. This has four newlines.
  963. same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
  964. """
  965. .toEqual YAML.parse YAML.dump (
  966. 'clipped': "This has one newline.\n"
  967. 'same as "clipped" above': "This has one newline.\n"
  968. 'stripped': 'This has no newline.'
  969. 'same as "stripped" above': 'This has no newline.'
  970. 'kept': "This has four newlines.\n\n\n\n"
  971. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  972. )
  973. describe 'Dumped YAML Comments', ->
  974. it 'can begin the document', ->
  975. expect YAML.parse """
  976. # This is a comment
  977. hello: world
  978. """
  979. .toEqual YAML.parse YAML.dump (
  980. hello: 'world'
  981. )
  982. it 'can finish a line', ->
  983. expect YAML.parse """
  984. hello: world # This is a comment
  985. """
  986. .toEqual YAML.parse YAML.dump (
  987. hello: 'world'
  988. )
  989. it 'can end the document', ->
  990. expect YAML.parse """
  991. hello: world
  992. # This is a comment
  993. """
  994. .toEqual YAML.parse YAML.dump (
  995. hello: 'world'
  996. )
  997. describe 'Dumped YAML Aliases and Anchors', ->
  998. it 'can be simple alias', ->
  999. expect YAML.parse """
  1000. - &showell Steve
  1001. - Clark
  1002. - Brian
  1003. - Oren
  1004. - *showell
  1005. """
  1006. .toEqual YAML.parse YAML.dump ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
  1007. it 'can be alias of a mapping', ->
  1008. expect YAML.parse """
  1009. - &hello
  1010. Meat: pork
  1011. Starch: potato
  1012. - banana
  1013. - *hello
  1014. """
  1015. .toEqual YAML.parse YAML.dump [
  1016. Meat: 'pork', Starch: 'potato'
  1017. ,
  1018. 'banana'
  1019. ,
  1020. Meat: 'pork', Starch: 'potato'
  1021. ]
  1022. describe 'Dumped YAML Documents', ->
  1023. it 'can have YAML header', ->
  1024. expect YAML.parse """
  1025. --- %YAML:1.0
  1026. foo: 1
  1027. bar: 2
  1028. """
  1029. .toEqual YAML.parse YAML.dump (
  1030. foo: 1
  1031. bar: 2
  1032. )
  1033. it 'can have leading document separator', ->
  1034. expect YAML.parse """
  1035. ---
  1036. - foo: 1
  1037. bar: 2
  1038. """
  1039. .toEqual YAML.parse YAML.dump [(
  1040. foo: 1
  1041. bar: 2
  1042. )]
  1043. it 'can have multiple document separators in block', ->
  1044. expect YAML.parse """
  1045. foo: |
  1046. ---
  1047. foo: bar
  1048. ---
  1049. yo: baz
  1050. bar: |
  1051. fooness
  1052. """
  1053. .toEqual YAML.parse YAML.dump (
  1054. foo: "---\nfoo: bar\n---\nyo: baz\n"
  1055. bar: "fooness\n"
  1056. )
  1057. # Loading
  1058. # (disable test when running locally from file)
  1059. #
  1060. url = document?.location?.href
  1061. if not(url?) or url.indexOf('file://') is -1
  1062. examplePath = 'spec/example.yml'
  1063. if __dirname?
  1064. examplePath = __dirname+'/example.yml'
  1065. describe 'YAML loading', ->
  1066. it 'can be done synchronously', ->
  1067. expect(YAML.load(examplePath)).toEqual (
  1068. this: 'is'
  1069. a: ['YAML', 'example']
  1070. )
  1071. it 'can be done asynchronously', (done) ->
  1072. YAML.load examplePath, (result) ->
  1073. expect(result).toEqual (
  1074. this: 'is'
  1075. a: ['YAML', 'example']
  1076. )
  1077. done()