YamlSpec.coffee 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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] = Math.round(val.getTime() / 1000) * 1000
  314. return res
  315. expect withDatesToTime(YAML.parse """
  316. iso8601: 2001-12-14t21:59:43.10-05:00
  317. space seperated: 2001-12-14 21:59:43.10 -05:00
  318. """)
  319. .toEqual withDatesToTime (
  320. 'iso8601': iso8601Date
  321. 'space seperated': 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] = Math.round(val.getTime() / 1000) * 1000
  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. describe 'Dumped YAML Basic Types', ->
  690. it 'can be strings', ->
  691. expect YAML.parse """
  692. ---
  693. String
  694. """
  695. .toEqual YAML.parse YAML.dump 'String'
  696. it 'can be double-quoted strings with backslashes', ->
  697. expect YAML.parse """
  698. str:
  699. "string with \\\\ inside"
  700. """
  701. .toEqual YAML.parse YAML.dump str: 'string with \\ inside'
  702. it 'can be single-quoted strings with backslashes', ->
  703. expect YAML.parse """
  704. str:
  705. 'string with \\\\ inside'
  706. """
  707. .toEqual YAML.parse YAML.dump str: 'string with \\\\ inside'
  708. it 'can be double-quoted strings with line breaks', ->
  709. expect YAML.parse """
  710. str:
  711. "string with \\n inside"
  712. """
  713. .toEqual YAML.parse YAML.dump str: 'string with \n inside'
  714. it 'can be double-quoted strings with line breaks and backslashes', ->
  715. expect YAML.parse """
  716. str:
  717. "string with \\n inside and \\\\ also"
  718. """
  719. .toEqual YAML.parse YAML.dump str: 'string with \n inside and \\ also'
  720. it 'can be single-quoted strings with line breaks and backslashes', ->
  721. expect YAML.parse """
  722. str:
  723. 'string with \\n inside and \\\\ also'
  724. """
  725. .toEqual YAML.parse YAML.dump str: 'string with \\n inside and \\\\ also'
  726. it 'can be single-quoted strings with escaped line breaks', ->
  727. expect YAML.parse """
  728. str:
  729. 'string with \\n inside'
  730. """
  731. .toEqual YAML.parse YAML.dump str: 'string with \\n inside'
  732. it 'can have string characters in sequences', ->
  733. expect YAML.parse """
  734. - What's Yaml?
  735. - It's for writing data structures in plain text.
  736. - And?
  737. - And what? That's not good enough for you?
  738. - No, I mean, "And what about Yaml?"
  739. - Oh, oh yeah. Uh.. Yaml for JavaScript.
  740. """
  741. .toEqual YAML.parse YAML.dump [
  742. "What's Yaml?",
  743. "It's for writing data structures in plain text.",
  744. "And?",
  745. "And what? That's not good enough for you?",
  746. "No, I mean, \"And what about Yaml?\"",
  747. "Oh, oh yeah. Uh.. Yaml for JavaScript."
  748. ]
  749. it 'can have indicators in strings', ->
  750. expect YAML.parse """
  751. the colon followed by space is an indicator: but is a string:right here
  752. same for the pound sign: here we have it#in a string
  753. the comma can, honestly, be used in most cases: [ but not in, inline collections ]
  754. """
  755. .toEqual YAML.parse YAML.dump (
  756. 'the colon followed by space is an indicator': 'but is a string:right here',
  757. 'same for the pound sign': 'here we have it#in a string',
  758. 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
  759. )
  760. it 'can force strings', ->
  761. expect YAML.parse """
  762. date string: !str 2001-08-01
  763. number string: !str 192
  764. date string 2: !!str 2001-08-01
  765. number string 2: !!str 192
  766. """
  767. .toEqual YAML.parse YAML.dump (
  768. 'date string': '2001-08-01',
  769. 'number string': '192' ,
  770. 'date string 2': '2001-08-01',
  771. 'number string 2': '192'
  772. )
  773. it 'can be single-quoted strings', ->
  774. expect YAML.parse """
  775. all my favorite symbols: '#:!/%.)'
  776. a few i hate: '&(*'
  777. why do i hate them?: 'it''s very hard to explain'
  778. """
  779. .toEqual YAML.parse YAML.dump (
  780. 'all my favorite symbols': '#:!/%.)',
  781. 'a few i hate': '&(*',
  782. 'why do i hate them?': 'it\'s very hard to explain'
  783. )
  784. it 'can be double-quoted strings', ->
  785. expect YAML.parse """
  786. i know where i want my line breaks: "one here\\nand another here\\n"
  787. """
  788. .toEqual YAML.parse YAML.dump (
  789. 'i know where i want my line breaks': "one here\nand another here\n"
  790. )
  791. it 'can be null', ->
  792. expect YAML.parse """
  793. name: Mr. Show
  794. hosted by: Bob and David
  795. date of next season: ~
  796. """
  797. .toEqual YAML.parse YAML.dump (
  798. 'name': 'Mr. Show'
  799. 'hosted by': 'Bob and David'
  800. 'date of next season': null
  801. )
  802. it 'can be boolean', ->
  803. expect YAML.parse """
  804. Is Gus a Liar?: true
  805. Do I rely on Gus for Sustenance?: false
  806. """
  807. .toEqual YAML.parse YAML.dump (
  808. 'Is Gus a Liar?': true
  809. 'Do I rely on Gus for Sustenance?': false
  810. )
  811. it 'can be integers', ->
  812. expect YAML.parse """
  813. zero: 0
  814. simple: 12
  815. one-thousand: 1,000
  816. negative one-thousand: -1,000
  817. """
  818. .toEqual YAML.parse YAML.dump (
  819. 'zero': 0
  820. 'simple': 12
  821. 'one-thousand': 1000
  822. 'negative one-thousand': -1000
  823. )
  824. it 'can be integers as map keys', ->
  825. expect YAML.parse """
  826. 1: one
  827. 2: two
  828. 3: three
  829. """
  830. .toEqual YAML.parse YAML.dump (
  831. 1: 'one'
  832. 2: 'two'
  833. 3: 'three'
  834. )
  835. it 'can be floats', ->
  836. expect YAML.parse """
  837. a simple float: 2.00
  838. larger float: 1,000.09
  839. scientific notation: 1.00009e+3
  840. """
  841. .toEqual YAML.parse YAML.dump (
  842. 'a simple float': 2.0
  843. 'larger float': 1000.09
  844. 'scientific notation': 1000.09
  845. )
  846. it 'can be time', ->
  847. iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
  848. iso8601Date.setTime iso8601Date.getTime() - 5 * 3600 * 1000
  849. spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
  850. spaceSeparatedDate.setTime spaceSeparatedDate.getTime() - 5 * 3600 * 1000
  851. withDatesToTime = (input) ->
  852. res = {}
  853. for key, val of input
  854. res[key] = Math.round(val.getTime() / 1000) * 1000
  855. return res
  856. expect withDatesToTime(YAML.parse """
  857. iso8601: 2001-12-14t21:59:43.10-05:00
  858. space seperated: 2001-12-14 21:59:43.10 -05:00
  859. """)
  860. .toEqual YAML.parse YAML.dump withDatesToTime (
  861. 'iso8601': iso8601Date
  862. 'space seperated': spaceSeparatedDate
  863. )
  864. it 'can be date', ->
  865. aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
  866. withDatesToTime = (input) ->
  867. return input
  868. res = {}
  869. for key, val of input
  870. res[key] = Math.round(val.getTime() / 1000) * 1000
  871. return res
  872. expect withDatesToTime(YAML.parse """
  873. date: 1976-07-31
  874. """)
  875. .toEqual YAML.parse YAML.dump withDatesToTime (
  876. 'date': aDate
  877. )
  878. describe 'Dumped YAML Blocks', ->
  879. it 'can be single ending newline', ->
  880. expect YAML.parse """
  881. ---
  882. this: |
  883. Foo
  884. Bar
  885. """
  886. .toEqual YAML.parse YAML.dump 'this': "Foo\nBar\n"
  887. it 'can be single ending newline with \'+\' indicator', ->
  888. expect YAML.parse """
  889. normal: |
  890. extra new lines not kept
  891. preserving: |+
  892. extra new lines are kept
  893. dummy: value
  894. """
  895. .toEqual YAML.parse YAML.dump (
  896. 'normal': "extra new lines not kept\n"
  897. 'preserving': "extra new lines are kept\n\n\n"
  898. 'dummy': 'value'
  899. )
  900. it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
  901. expect YAML.parse """
  902. clipped: |
  903. This has one newline.
  904. same as "clipped" above: "This has one newline.\\n"
  905. stripped: |-
  906. This has no newline.
  907. same as "stripped" above: "This has no newline."
  908. kept: |+
  909. This has four newlines.
  910. same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
  911. """
  912. .toEqual YAML.parse YAML.dump (
  913. 'clipped': "This has one newline.\n"
  914. 'same as "clipped" above': "This has one newline.\n"
  915. 'stripped':'This has no newline.'
  916. 'same as "stripped" above': 'This has no newline.'
  917. 'kept': "This has four newlines.\n\n\n\n"
  918. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  919. )
  920. it 'can be folded block in a sequence', ->
  921. expect YAML.parse """
  922. ---
  923. - apple
  924. - banana
  925. - >
  926. can't you see
  927. the beauty of yaml?
  928. hmm
  929. - dog
  930. """
  931. .toEqual YAML.parse YAML.dump [
  932. 'apple',
  933. 'banana',
  934. "can't you see the beauty of yaml? hmm\n",
  935. 'dog'
  936. ]
  937. it 'can be folded block as a mapping value', ->
  938. expect YAML.parse """
  939. ---
  940. quote: >
  941. Mark McGwire's
  942. year was crippled
  943. by a knee injury.
  944. source: espn
  945. """
  946. .toEqual YAML.parse YAML.dump (
  947. 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
  948. 'source': 'espn'
  949. )
  950. it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
  951. expect YAML.parse """
  952. clipped: >
  953. This has one newline.
  954. same as "clipped" above: "This has one newline.\\n"
  955. stripped: >-
  956. This has no newline.
  957. same as "stripped" above: "This has no newline."
  958. kept: >+
  959. This has four newlines.
  960. same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
  961. """
  962. .toEqual YAML.parse YAML.dump (
  963. 'clipped': "This has one newline.\n"
  964. 'same as "clipped" above': "This has one newline.\n"
  965. 'stripped': 'This has no newline.'
  966. 'same as "stripped" above': 'This has no newline.'
  967. 'kept': "This has four newlines.\n\n\n\n"
  968. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  969. )
  970. describe 'Dumped YAML Comments', ->
  971. it 'can begin the document', ->
  972. expect YAML.parse """
  973. # This is a comment
  974. hello: world
  975. """
  976. .toEqual YAML.parse YAML.dump (
  977. hello: 'world'
  978. )
  979. it 'can finish a line', ->
  980. expect YAML.parse """
  981. hello: world # This is a comment
  982. """
  983. .toEqual YAML.parse YAML.dump (
  984. hello: 'world'
  985. )
  986. it 'can end the document', ->
  987. expect YAML.parse """
  988. hello: world
  989. # This is a comment
  990. """
  991. .toEqual YAML.parse YAML.dump (
  992. hello: 'world'
  993. )
  994. describe 'Dumped YAML Aliases and Anchors', ->
  995. it 'can be simple alias', ->
  996. expect YAML.parse """
  997. - &showell Steve
  998. - Clark
  999. - Brian
  1000. - Oren
  1001. - *showell
  1002. """
  1003. .toEqual YAML.parse YAML.dump ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
  1004. it 'can be alias of a mapping', ->
  1005. expect YAML.parse """
  1006. - &hello
  1007. Meat: pork
  1008. Starch: potato
  1009. - banana
  1010. - *hello
  1011. """
  1012. .toEqual YAML.parse YAML.dump [
  1013. Meat: 'pork', Starch: 'potato'
  1014. ,
  1015. 'banana'
  1016. ,
  1017. Meat: 'pork', Starch: 'potato'
  1018. ]
  1019. describe 'Dumped YAML Documents', ->
  1020. it 'can have YAML header', ->
  1021. expect YAML.parse """
  1022. --- %YAML:1.0
  1023. foo: 1
  1024. bar: 2
  1025. """
  1026. .toEqual YAML.parse YAML.dump (
  1027. foo: 1
  1028. bar: 2
  1029. )
  1030. it 'can have leading document separator', ->
  1031. expect YAML.parse """
  1032. ---
  1033. - foo: 1
  1034. bar: 2
  1035. """
  1036. .toEqual YAML.parse YAML.dump [(
  1037. foo: 1
  1038. bar: 2
  1039. )]
  1040. it 'can have multiple document separators in block', ->
  1041. expect YAML.parse """
  1042. foo: |
  1043. ---
  1044. foo: bar
  1045. ---
  1046. yo: baz
  1047. bar: |
  1048. fooness
  1049. """
  1050. .toEqual YAML.parse YAML.dump (
  1051. foo: "---\nfoo: bar\n---\nyo: baz\n"
  1052. bar: "fooness\n"
  1053. )
  1054. # Loading
  1055. # (disable test when running locally from file)
  1056. #
  1057. url = document?.location?.href
  1058. if not(url?) or url.indexOf('file://') is -1
  1059. examplePath = 'spec/example.yml'
  1060. if __dirname?
  1061. examplePath = __dirname+'/example.yml'
  1062. describe 'YAML loading', ->
  1063. it 'can be done synchronously', ->
  1064. expect(YAML.load(examplePath)).toEqual (
  1065. this: 'is'
  1066. a: ['YAML', 'example']
  1067. )
  1068. it 'can be done asynchronously', (done) ->
  1069. YAML.load examplePath, (result) ->
  1070. expect(result).toEqual (
  1071. this: 'is'
  1072. a: ['YAML', 'example']
  1073. )
  1074. done()