scratch – Blame information for rev 125

Subversion Repositories:
Rev:
Rev Author Line No. Line
75 office 1  
2 unless YAML?
3 YAML = require '../../src/Yaml'
4  
5  
6 # Parsing
7 #
8  
9 describe 'Parsed YAML Collections', ->
10  
11 it 'can be simple sequence', ->
12  
13 expect YAML.parse """
14 - apple
15 - banana
16 - carrot
17 """
18 .toEqual ['apple', 'banana', 'carrot']
19  
20  
21 it 'can be nested sequences', ->
22  
23 expect YAML.parse """
24 -
25 - foo
26 - bar
27 - baz
28 """
29 .toEqual [['foo', 'bar', 'baz']]
30  
31  
32 it 'can be mixed sequences', ->
33  
34 expect YAML.parse """
35 - apple
36 -
37 - foo
38 - bar
39 - x123
40 - banana
41 - carrot
42 """
43 .toEqual ['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']
44  
45  
46 it 'can be deeply nested sequences', ->
47  
48 expect YAML.parse """
49 -
50 -
51 - uno
52 - dos
53 """
54 .toEqual [[['uno', 'dos']]]
55  
56  
57 it 'can be simple mapping', ->
58  
59 expect YAML.parse """
60 foo: whatever
61 bar: stuff
62 """
63 .toEqual foo: 'whatever', bar: 'stuff'
64  
65  
66 it 'can be sequence in a mapping', ->
67  
68 expect YAML.parse """
69 foo: whatever
70 bar:
71 - uno
72 - dos
73 """
74 .toEqual foo: 'whatever', bar: ['uno', 'dos']
75  
76  
77 it 'can be nested mappings', ->
78  
79 expect YAML.parse """
80 foo: whatever
81 bar:
82 fruit: apple
83 name: steve
84 sport: baseball
85 """
86 .toEqual foo: 'whatever', bar: (fruit: 'apple', name: 'steve', sport: 'baseball')
87  
88  
89 it 'can be mixed mapping', ->
90  
91 expect YAML.parse """
92 foo: whatever
93 bar:
94 -
95 fruit: apple
96 name: steve
97 sport: baseball
98 - more
99 -
100 python: rocks
101 perl: papers
102 ruby: scissorses
103 """
104 .toEqual foo: 'whatever', bar: [
105 (fruit: 'apple', name: 'steve', sport: 'baseball'),
106 'more',
107 (python: 'rocks', perl: 'papers', ruby: 'scissorses')
108 ]
109  
110  
111 it 'can have mapping-in-sequence shortcut', ->
112  
113 expect YAML.parse """
114 - work on YAML.py:
115 - work on Store
116 """
117 .toEqual [('work on YAML.py': ['work on Store'])]
118  
119  
120 it 'can have unindented sequence-in-mapping shortcut', ->
121  
122 expect YAML.parse """
123 allow:
124 - 'localhost'
125 - '%.sourceforge.net'
126 - '%.freepan.org'
127 """
128 .toEqual (allow: ['localhost', '%.sourceforge.net', '%.freepan.org'])
129  
130  
131 it 'can merge key', ->
132  
133 expect YAML.parse """
134 mapping:
135 name: Joe
136 job: Accountant
137 <<:
138 age: 38
139 """
140 .toEqual mapping:
141 name: 'Joe'
142 job: 'Accountant'
143 age: 38
144  
145 it 'can ignore trailing empty lines for smallest indent', ->
146  
147 expect YAML.parse """ trailing: empty lines\n"""
148 .toEqual trailing: 'empty lines'
149  
150 describe 'Parsed YAML Inline Collections', ->
151  
152 it 'can be simple inline array', ->
153  
154 expect YAML.parse """
155 ---
156 seq: [ a, b, c ]
157 """
158 .toEqual seq: ['a', 'b', 'c']
159  
160  
161 it 'can be simple inline hash', ->
162  
163 expect YAML.parse """
164 ---
165 hash: { name: Steve, foo: bar }
166 """
167 .toEqual hash: (name: 'Steve', foo: 'bar')
168  
169  
170 it 'can be nested inline hash', ->
171  
172 expect YAML.parse """
173 ---
174 hash: { val1: "string", val2: { v2k1: "v2k1v" } }
175 """
176 .toEqual hash: (val1: 'string', val2: (v2k1: 'v2k1v'))
177  
178  
179 it 'can be multi-line inline collections', ->
180  
181 expect YAML.parse """
182 languages: [ Ruby,
183 Perl,
184 Python ]
185 websites: { YAML: yaml.org,
186 Ruby: ruby-lang.org,
187 Python: python.org,
188 Perl: use.perl.org }
189 """
190 .toEqual (
191 languages: ['Ruby', 'Perl', 'Python']
192 websites:
193 YAML: 'yaml.org'
194 Ruby: 'ruby-lang.org'
195 Python: 'python.org'
196 Perl: 'use.perl.org'
197 )
198  
199  
200  
201 describe 'Parsed YAML Basic Types', ->
202  
203 it 'can be strings', ->
204  
205 expect YAML.parse """
206 ---
207 String
208 """
209 .toEqual 'String'
210  
211  
212 it 'can be double-quoted strings with backslashes', ->
213  
214 expect YAML.parse """
215 str:
216 "string with \\\\ inside"
217 """
218 .toEqual str: 'string with \\ inside'
219  
220  
221 it 'can be single-quoted strings with backslashes', ->
222  
223 expect YAML.parse """
224 str:
225 'string with \\\\ inside'
226 """
227 .toEqual str: 'string with \\\\ inside'
228  
229  
230 it 'can be double-quoted strings with line breaks', ->
231  
232 expect YAML.parse """
233 str:
234 "string with \\n inside"
235 """
236 .toEqual str: 'string with \n inside'
237  
238  
239 it 'can be single-quoted strings with escaped line breaks', ->
240  
241 expect YAML.parse """
242 str:
243 'string with \\n inside'
244 """
245 .toEqual str: 'string with \\n inside'
246  
247  
248 it 'can be double-quoted strings with line breaks and backslashes', ->
249  
250 expect YAML.parse """
251 str:
252 "string with \\n inside and \\\\ also"
253 """
254 .toEqual str: 'string with \n inside and \\ also'
255  
256  
257 it 'can be single-quoted strings with line breaks and backslashes', ->
258  
259 expect YAML.parse """
260 str:
261 'string with \\n inside and \\\\ also'
262 """
263 .toEqual str: 'string with \\n inside and \\\\ also'
264  
265  
266 it 'can have string characters in sequences', ->
267  
268 expect YAML.parse """
269 - What's Yaml?
270 - It's for writing data structures in plain text.
271 - And?
272 - And what? That's not good enough for you?
273 - No, I mean, "And what about Yaml?"
274 - Oh, oh yeah. Uh.. Yaml for JavaScript.
275 """
276 .toEqual [
277 "What's Yaml?",
278 "It's for writing data structures in plain text.",
279 "And?",
280 "And what? That's not good enough for you?",
281 "No, I mean, \"And what about Yaml?\"",
282 "Oh, oh yeah. Uh.. Yaml for JavaScript."
283 ]
284  
285  
286 it 'can have indicators in strings', ->
287  
288 expect YAML.parse """
289 the colon followed by space is an indicator: but is a string:right here
290 same for the pound sign: here we have it#in a string
291 the comma can, honestly, be used in most cases: [ but not in, inline collections ]
292 """
293 .toEqual (
294 'the colon followed by space is an indicator': 'but is a string:right here',
295 'same for the pound sign': 'here we have it#in a string',
296 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
297 )
298  
299  
300 it 'can force strings', ->
301  
302 expect YAML.parse """
303 date string: !str 2001-08-01
304 number string: !str 192
305 date string 2: !!str 2001-08-01
306 number string 2: !!str 192
307 """
308 .toEqual (
309 'date string': '2001-08-01',
310 'number string': '192' ,
311 'date string 2': '2001-08-01',
312 'number string 2': '192'
313 )
314  
315  
316 it 'can be single-quoted strings', ->
317  
318 expect YAML.parse """
319 all my favorite symbols: '#:!/%.)'
320 a few i hate: '&(*'
321 why do i hate them?: 'it''s very hard to explain'
322 """
323 .toEqual (
324 'all my favorite symbols': '#:!/%.)',
325 'a few i hate': '&(*',
326 'why do i hate them?': 'it\'s very hard to explain'
327 )
328  
329  
330 it 'can be double-quoted strings', ->
331  
332 expect YAML.parse """
333 i know where i want my line breaks: "one here\\nand another here\\n"
334 """
335 .toEqual (
336 'i know where i want my line breaks': "one here\nand another here\n"
337 )
338  
339  
340 it 'can be null', ->
341  
342 expect YAML.parse """
343 name: Mr. Show
344 hosted by: Bob and David
345 date of next season: ~
346 """
347 .toEqual (
348 'name': 'Mr. Show'
349 'hosted by': 'Bob and David'
350 'date of next season': null
351 )
352  
353  
354 it 'can be boolean', ->
355  
356 expect YAML.parse """
357 Is Gus a Liar?: true
358 Do I rely on Gus for Sustenance?: false
359 """
360 .toEqual (
361 'Is Gus a Liar?': true
362 'Do I rely on Gus for Sustenance?': false
363 )
364  
365  
366 it 'can be integers', ->
367  
368 expect YAML.parse """
369 zero: 0
370 simple: 12
371 one-thousand: 1,000
372 negative one-thousand: -1,000
373 """
374 .toEqual (
375 'zero': 0
376 'simple': 12
377 'one-thousand': 1000
378 'negative one-thousand': -1000
379 )
380  
381  
382 it 'can be integers as map keys', ->
383  
384 expect YAML.parse """
385 1: one
386 2: two
387 3: three
388 """
389 .toEqual (
390 1: 'one'
391 2: 'two'
392 3: 'three'
393 )
394  
395  
396 it 'can be floats', ->
397  
398 expect YAML.parse """
399 a simple float: 2.00
400 larger float: 1,000.09
401 scientific notation: 1.00009e+3
402 """
403 .toEqual (
404 'a simple float': 2.0
405 'larger float': 1000.09
406 'scientific notation': 1000.09
407 )
408  
409  
410 it 'can be time', ->
411  
412 iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
413 iso8601Date.setTime iso8601Date.getTime() - 5 * 3600 * 1000
414  
415 spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
125 office 416 spaceSeparatedDate.setTime spaceSeparatedDate.getTime() + 5 * 3600 * 1000
75 office 417  
418 withDatesToTime = (input) ->
419 res = {}
420 for key, val of input
125 office 421 res[key] = val.getTime()
75 office 422 return res
423  
424 expect withDatesToTime(YAML.parse """
125 office 425 iso8601: 2001-12-14t21:59:43.010+05:00
426 space separated: 2001-12-14 21:59:43.010 -05:00
75 office 427 """)
428 .toEqual withDatesToTime (
429 'iso8601': iso8601Date
125 office 430 'space separated': spaceSeparatedDate
75 office 431 )
432  
433  
434 it 'can be date', ->
435  
436 aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
437  
438 withDatesToTime = (input) ->
439 return input
440 res = {}
441 for key, val of input
125 office 442 res[key] = val.getTime()
75 office 443 return res
444  
445 expect withDatesToTime(YAML.parse """
446 date: 1976-07-31
447 """)
448 .toEqual withDatesToTime (
449 'date': aDate
450 )
451  
452  
453  
454 describe 'Parsed YAML Blocks', ->
455  
456 it 'can be single ending newline', ->
457  
458 expect YAML.parse """
459 ---
460 this: |
461 Foo
462 Bar
463 """
464 .toEqual 'this': "Foo\nBar\n"
465  
466  
467 it 'can be single ending newline with \'+\' indicator', ->
468  
469 expect YAML.parse """
470 normal: |
471 extra new lines not kept
472  
473 preserving: |+
474 extra new lines are kept
475  
476  
477 dummy: value
478 """
479 .toEqual (
480 'normal': "extra new lines not kept\n"
481 'preserving': "extra new lines are kept\n\n\n"
482 'dummy': 'value'
483 )
484  
485  
486 it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
487  
488 expect YAML.parse """
489 clipped: |
490 This has one newline.
491  
492  
493  
494 same as "clipped" above: "This has one newline.\\n"
495  
496 stripped: |-
497 This has no newline.
498  
499  
500  
501 same as "stripped" above: "This has no newline."
502  
503 kept: |+
504 This has four newlines.
505  
506  
507  
508 same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
509 """
510 .toEqual (
511 'clipped': "This has one newline.\n"
512 'same as "clipped" above': "This has one newline.\n"
513 'stripped':'This has no newline.'
514 'same as "stripped" above': 'This has no newline.'
515 'kept': "This has four newlines.\n\n\n\n"
516 'same as "kept" above': "This has four newlines.\n\n\n\n"
517 )
518  
519  
520 it 'can be folded block in a sequence', ->
521  
522 expect YAML.parse """
523 ---
524 - apple
525 - banana
526 - >
527 can't you see
528 the beauty of yaml?
529 hmm
530 - dog
531 """
532 .toEqual [
533 'apple',
534 'banana',
535 "can't you see the beauty of yaml? hmm\n",
536 'dog'
537 ]
538  
539  
540 it 'can be folded block as a mapping value', ->
541  
542 expect YAML.parse """
543 ---
544 quote: >
545 Mark McGwire's
546 year was crippled
547 by a knee injury.
548 source: espn
549 """
550 .toEqual (
551 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
552 'source': 'espn'
553 )
554  
555  
556 it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
557  
558 expect YAML.parse """
559 clipped: >
560 This has one newline.
561  
562  
563  
564 same as "clipped" above: "This has one newline.\\n"
565  
566 stripped: >-
567 This has no newline.
568  
569  
570  
571 same as "stripped" above: "This has no newline."
572  
573 kept: >+
574 This has four newlines.
575  
576  
577  
578 same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
579 """
580 .toEqual (
581 'clipped': "This has one newline.\n"
582 'same as "clipped" above': "This has one newline.\n"
583 'stripped': 'This has no newline.'
584 'same as "stripped" above': 'This has no newline.'
585 'kept': "This has four newlines.\n\n\n\n"
586 'same as "kept" above': "This has four newlines.\n\n\n\n"
587 )
588  
589  
590 it 'can be the whole document as intented block', ->
591  
592 expect YAML.parse """
593 ---
594 foo: "bar"
595 baz:
596 - "qux"
597 - "quxx"
598 corge: null
599 """
600 .toEqual (
601 'foo': "bar"
602 'baz': ['qux', 'quxx']
603 'corge': null
604 )
605  
606  
607  
608  
609 describe 'Parsed YAML Comments', ->
610  
611 it 'can begin the document', ->
612  
613 expect YAML.parse """
614 # This is a comment
615 hello: world
616 """
617 .toEqual (
618 hello: 'world'
619 )
620  
621  
622 it 'can be less indented in mapping', ->
623  
624 expect YAML.parse """
625 parts:
626 a: 'b'
627 # normally indented comment
628 c: 'd'
629 # less indented comment
630 e: 'f'
631 """
632 .toEqual (
633 parts: {a: 'b', c: 'd', e: 'f'}
634 )
635  
636  
637 it 'can be less indented in sequence', ->
638  
639 expect YAML.parse """
640 list-header:
641 - item1
642 # - item2
643 - item3
644 # - item4
645 """
646 .toEqual (
647 'list-header': ['item1', 'item3']
648 )
649  
650  
651 it 'can finish a line', ->
652  
653 expect YAML.parse """
654 hello: world # This is a comment
655 """
656 .toEqual (
657 hello: 'world'
658 )
659  
660  
661 it 'can end the document', ->
662  
663 expect YAML.parse """
664 hello: world
665 # This is a comment
666 """
667 .toEqual (
668 hello: 'world'
669 )
670  
671  
672  
673 describe 'Parsed YAML Aliases and Anchors', ->
674  
675 it 'can be simple alias', ->
676  
677 expect YAML.parse """
678 - &showell Steve
679 - Clark
680 - Brian
681 - Oren
682 - *showell
683 """
684 .toEqual ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
685  
686  
687 it 'can be alias of a mapping', ->
688  
689 expect YAML.parse """
690 - &hello
691 Meat: pork
692 Starch: potato
693 - banana
694 - *hello
695 """
696 .toEqual [
697 Meat: 'pork', Starch: 'potato'
698 ,
699 'banana'
700 ,
701 Meat: 'pork', Starch: 'potato'
702 ]
703  
704  
705  
706 describe 'Parsed YAML Documents', ->
707  
708 it 'can have YAML header', ->
709  
710 expect YAML.parse """
711 --- %YAML:1.0
712 foo: 1
713 bar: 2
714 """
715 .toEqual (
716 foo: 1
717 bar: 2
718 )
719  
720  
721 it 'can have leading document separator', ->
722  
723 expect YAML.parse """
724 ---
725 - foo: 1
726 bar: 2
727 """
728 .toEqual [(
729 foo: 1
730 bar: 2
731 )]
732  
733  
734 it 'can have multiple document separators in block', ->
735  
736 expect YAML.parse """
737 foo: |
738 ---
739 foo: bar
740 ---
741 yo: baz
742 bar: |
743 fooness
744 """
745 .toEqual (
746 foo: "---\nfoo: bar\n---\nyo: baz\n"
747 bar: "fooness\n"
748 )
749  
750  
751 # Dumping
752 #
753  
754 describe 'Dumped YAML Collections', ->
755  
756 it 'can be simple sequence', ->
757  
758 expect YAML.parse """
759 - apple
760 - banana
761 - carrot
762 """
763 .toEqual YAML.parse YAML.dump ['apple', 'banana', 'carrot']
764  
765  
766 it 'can be nested sequences', ->
767  
768 expect YAML.parse """
769 -
770 - foo
771 - bar
772 - baz
773 """
774 .toEqual YAML.parse YAML.dump [['foo', 'bar', 'baz']]
775  
776  
777 it 'can be mixed sequences', ->
778  
779 expect YAML.parse """
780 - apple
781 -
782 - foo
783 - bar
784 - x123
785 - banana
786 - carrot
787 """
788 .toEqual YAML.parse YAML.dump ['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']
789  
790  
791 it 'can be deeply nested sequences', ->
792  
793 expect YAML.parse """
794 -
795 -
796 - uno
797 - dos
798 """
799 .toEqual YAML.parse YAML.dump [[['uno', 'dos']]]
800  
801  
802 it 'can be simple mapping', ->
803  
804 expect YAML.parse """
805 foo: whatever
806 bar: stuff
807 """
808 .toEqual YAML.parse YAML.dump foo: 'whatever', bar: 'stuff'
809  
810  
811 it 'can be sequence in a mapping', ->
812  
813 expect YAML.parse """
814 foo: whatever
815 bar:
816 - uno
817 - dos
818 """
819 .toEqual YAML.parse YAML.dump foo: 'whatever', bar: ['uno', 'dos']
820  
821  
822 it 'can be nested mappings', ->
823  
824 expect YAML.parse """
825 foo: whatever
826 bar:
827 fruit: apple
828 name: steve
829 sport: baseball
830 """
831 .toEqual YAML.parse YAML.dump foo: 'whatever', bar: (fruit: 'apple', name: 'steve', sport: 'baseball')
832  
833  
834 it 'can be mixed mapping', ->
835  
836 expect YAML.parse """
837 foo: whatever
838 bar:
839 -
840 fruit: apple
841 name: steve
842 sport: baseball
843 - more
844 -
845 python: rocks
846 perl: papers
847 ruby: scissorses
848 """
849 .toEqual YAML.parse YAML.dump foo: 'whatever', bar: [
850 (fruit: 'apple', name: 'steve', sport: 'baseball'),
851 'more',
852 (python: 'rocks', perl: 'papers', ruby: 'scissorses')
853 ]
854  
855  
856 it 'can have mapping-in-sequence shortcut', ->
857  
858 expect YAML.parse """
859 - work on YAML.py:
860 - work on Store
861 """
862 .toEqual YAML.parse YAML.dump [('work on YAML.py': ['work on Store'])]
863  
864  
865 it 'can have unindented sequence-in-mapping shortcut', ->
866  
867 expect YAML.parse """
868 allow:
869 - 'localhost'
870 - '%.sourceforge.net'
871 - '%.freepan.org'
872 """
873 .toEqual YAML.parse YAML.dump (allow: ['localhost', '%.sourceforge.net', '%.freepan.org'])
874  
875  
876 it 'can merge key', ->
877  
878 expect YAML.parse """
879 mapping:
880 name: Joe
881 job: Accountant
882 <<:
883 age: 38
884 """
885 .toEqual YAML.parse YAML.dump mapping:
886 name: 'Joe'
887 job: 'Accountant'
888 age: 38
889  
890  
891  
892 describe 'Dumped YAML Inline Collections', ->
893  
894 it 'can be simple inline array', ->
895  
896 expect YAML.parse """
897 ---
898 seq: [ a, b, c ]
899 """
900 .toEqual YAML.parse YAML.dump seq: ['a', 'b', 'c']
901  
902  
903 it 'can be simple inline hash', ->
904  
905 expect YAML.parse """
906 ---
907 hash: { name: Steve, foo: bar }
908 """
909 .toEqual YAML.parse YAML.dump hash: (name: 'Steve', foo: 'bar')
910  
911  
912 it 'can be multi-line inline collections', ->
913  
914 expect YAML.parse """
915 languages: [ Ruby,
916 Perl,
917 Python ]
918 websites: { YAML: yaml.org,
919 Ruby: ruby-lang.org,
920 Python: python.org,
921 Perl: use.perl.org }
922 """
923 .toEqual YAML.parse YAML.dump (
924 languages: ['Ruby', 'Perl', 'Python']
925 websites:
926 YAML: 'yaml.org'
927 Ruby: 'ruby-lang.org'
928 Python: 'python.org'
929 Perl: 'use.perl.org'
930 )
931  
932 it 'can be dumped empty sequences in mappings', ->
933  
934 expect YAML.parse(YAML.dump({key:[]}))
935 .toEqual({key:[]})
936  
125 office 937 it 'can be dumpted empty inline collections', ->
75 office 938  
125 office 939 expect YAML.parse(YAML.dump({key:{}}))
940 .toEqual({key:{}})
75 office 941  
942 describe 'Dumped YAML Basic Types', ->
943  
944 it 'can be strings', ->
945  
946 expect YAML.parse """
947 ---
948 String
949 """
950 .toEqual YAML.parse YAML.dump 'String'
951  
952  
953 it 'can be double-quoted strings with backslashes', ->
954  
955 expect YAML.parse """
956 str:
957 "string with \\\\ inside"
958 """
959 .toEqual YAML.parse YAML.dump str: 'string with \\ inside'
960  
961  
962 it 'can be single-quoted strings with backslashes', ->
963  
964 expect YAML.parse """
965 str:
966 'string with \\\\ inside'
967 """
968 .toEqual YAML.parse YAML.dump str: 'string with \\\\ inside'
969  
970  
971 it 'can be double-quoted strings with line breaks', ->
972  
973 expect YAML.parse """
974 str:
975 "string with \\n inside"
976 """
977 .toEqual YAML.parse YAML.dump str: 'string with \n inside'
978  
979  
980 it 'can be double-quoted strings with line breaks and backslashes', ->
981  
982 expect YAML.parse """
983 str:
984 "string with \\n inside and \\\\ also"
985 """
986 .toEqual YAML.parse YAML.dump str: 'string with \n inside and \\ also'
987  
988  
989 it 'can be single-quoted strings with line breaks and backslashes', ->
990  
991 expect YAML.parse """
992 str:
993 'string with \\n inside and \\\\ also'
994 """
995 .toEqual YAML.parse YAML.dump str: 'string with \\n inside and \\\\ also'
996  
997  
998 it 'can be single-quoted strings with escaped line breaks', ->
999  
1000 expect YAML.parse """
1001 str:
1002 'string with \\n inside'
1003 """
1004 .toEqual YAML.parse YAML.dump str: 'string with \\n inside'
1005  
1006  
1007 it 'can have string characters in sequences', ->
1008  
1009 expect YAML.parse """
1010 - What's Yaml?
1011 - It's for writing data structures in plain text.
1012 - And?
1013 - And what? That's not good enough for you?
1014 - No, I mean, "And what about Yaml?"
1015 - Oh, oh yeah. Uh.. Yaml for JavaScript.
1016 """
1017 .toEqual YAML.parse YAML.dump [
1018 "What's Yaml?",
1019 "It's for writing data structures in plain text.",
1020 "And?",
1021 "And what? That's not good enough for you?",
1022 "No, I mean, \"And what about Yaml?\"",
1023 "Oh, oh yeah. Uh.. Yaml for JavaScript."
1024 ]
1025  
1026  
1027 it 'can have indicators in strings', ->
1028  
1029 expect YAML.parse """
1030 the colon followed by space is an indicator: but is a string:right here
1031 same for the pound sign: here we have it#in a string
1032 the comma can, honestly, be used in most cases: [ but not in, inline collections ]
1033 """
1034 .toEqual YAML.parse YAML.dump (
1035 'the colon followed by space is an indicator': 'but is a string:right here',
1036 'same for the pound sign': 'here we have it#in a string',
1037 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
1038 )
1039  
1040  
1041 it 'can force strings', ->
1042  
1043 expect YAML.parse """
1044 date string: !str 2001-08-01
1045 number string: !str 192
1046 date string 2: !!str 2001-08-01
1047 number string 2: !!str 192
1048 """
1049 .toEqual YAML.parse YAML.dump (
1050 'date string': '2001-08-01',
1051 'number string': '192' ,
1052 'date string 2': '2001-08-01',
1053 'number string 2': '192'
1054 )
1055  
1056  
1057 it 'can be single-quoted strings', ->
1058  
1059 expect YAML.parse """
1060 all my favorite symbols: '#:!/%.)'
1061 a few i hate: '&(*'
1062 why do i hate them?: 'it''s very hard to explain'
1063 """
1064 .toEqual YAML.parse YAML.dump (
1065 'all my favorite symbols': '#:!/%.)',
1066 'a few i hate': '&(*',
1067 'why do i hate them?': 'it\'s very hard to explain'
1068 )
1069  
1070  
1071 it 'can be double-quoted strings', ->
1072  
1073 expect YAML.parse """
1074 i know where i want my line breaks: "one here\\nand another here\\n"
1075 """
1076 .toEqual YAML.parse YAML.dump (
1077 'i know where i want my line breaks': "one here\nand another here\n"
1078 )
1079  
1080  
1081 it 'can be null', ->
1082  
1083 expect YAML.parse """
1084 name: Mr. Show
1085 hosted by: Bob and David
1086 date of next season: ~
1087 """
1088 .toEqual YAML.parse YAML.dump (
1089 'name': 'Mr. Show'
1090 'hosted by': 'Bob and David'
1091 'date of next season': null
1092 )
1093  
1094  
1095 it 'can be boolean', ->
1096  
1097 expect YAML.parse """
1098 Is Gus a Liar?: true
1099 Do I rely on Gus for Sustenance?: false
1100 """
1101 .toEqual YAML.parse YAML.dump (
1102 'Is Gus a Liar?': true
1103 'Do I rely on Gus for Sustenance?': false
1104 )
1105  
1106  
1107 it 'can be integers', ->
1108  
1109 expect YAML.parse """
1110 zero: 0
1111 simple: 12
1112 one-thousand: 1,000
1113 negative one-thousand: -1,000
1114 """
1115 .toEqual YAML.parse YAML.dump (
1116 'zero': 0
1117 'simple': 12
1118 'one-thousand': 1000
1119 'negative one-thousand': -1000
1120 )
1121  
1122  
1123 it 'can be integers as map keys', ->
1124  
1125 expect YAML.parse """
1126 1: one
1127 2: two
1128 3: three
1129 """
1130 .toEqual YAML.parse YAML.dump (
1131 1: 'one'
1132 2: 'two'
1133 3: 'three'
1134 )
1135  
1136  
1137 it 'can be floats', ->
1138  
1139 expect YAML.parse """
1140 a simple float: 2.00
1141 larger float: 1,000.09
1142 scientific notation: 1.00009e+3
1143 """
1144 .toEqual YAML.parse YAML.dump (
1145 'a simple float': 2.0
1146 'larger float': 1000.09
1147 'scientific notation': 1000.09
1148 )
1149  
1150  
1151 it 'can be time', ->
1152  
1153 iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
125 office 1154 iso8601Date.setTime iso8601Date.getTime() + 5 * 3600 * 1000
75 office 1155  
1156 spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
1157 spaceSeparatedDate.setTime spaceSeparatedDate.getTime() - 5 * 3600 * 1000
1158  
1159 withDatesToTime = (input) ->
1160 res = {}
1161 for key, val of input
125 office 1162 res[key] = val.getTime()
75 office 1163 return res
1164  
1165 expect withDatesToTime(YAML.parse """
125 office 1166 iso8601: 2001-12-14t21:59:43.010-05:00
1167 space separated: 2001-12-14 21:59:43.010 +05:00
75 office 1168 """)
1169 .toEqual YAML.parse YAML.dump withDatesToTime (
1170 'iso8601': iso8601Date
125 office 1171 'space separated': spaceSeparatedDate
75 office 1172 )
1173  
1174  
1175 it 'can be date', ->
1176  
1177 aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
1178  
1179 withDatesToTime = (input) ->
1180 return input
1181 res = {}
1182 for key, val of input
125 office 1183 res[key] = val.getTime()
75 office 1184 return res
1185  
1186 expect withDatesToTime(YAML.parse """
1187 date: 1976-07-31
1188 """)
1189 .toEqual YAML.parse YAML.dump withDatesToTime (
1190 'date': aDate
1191 )
1192  
1193  
1194  
1195 describe 'Dumped YAML Blocks', ->
1196  
1197 it 'can be single ending newline', ->
1198  
1199 expect YAML.parse """
1200 ---
1201 this: |
1202 Foo
1203 Bar
1204 """
1205 .toEqual YAML.parse YAML.dump 'this': "Foo\nBar\n"
1206  
1207  
1208 it 'can be single ending newline with \'+\' indicator', ->
1209  
1210 expect YAML.parse """
1211 normal: |
1212 extra new lines not kept
1213  
1214 preserving: |+
1215 extra new lines are kept
1216  
1217  
1218 dummy: value
1219 """
1220 .toEqual YAML.parse YAML.dump (
1221 'normal': "extra new lines not kept\n"
1222 'preserving': "extra new lines are kept\n\n\n"
1223 'dummy': 'value'
1224 )
1225  
1226  
1227 it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
1228  
1229 expect YAML.parse """
1230 clipped: |
1231 This has one newline.
1232  
1233  
1234  
1235 same as "clipped" above: "This has one newline.\\n"
1236  
1237 stripped: |-
1238 This has no newline.
1239  
1240  
1241  
1242 same as "stripped" above: "This has no newline."
1243  
1244 kept: |+
1245 This has four newlines.
1246  
1247  
1248  
1249 same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
1250 """
1251 .toEqual YAML.parse YAML.dump (
1252 'clipped': "This has one newline.\n"
1253 'same as "clipped" above': "This has one newline.\n"
1254 'stripped':'This has no newline.'
1255 'same as "stripped" above': 'This has no newline.'
1256 'kept': "This has four newlines.\n\n\n\n"
1257 'same as "kept" above': "This has four newlines.\n\n\n\n"
1258 )
1259  
1260  
1261 it 'can be folded block in a sequence', ->
1262  
1263 expect YAML.parse """
1264 ---
1265 - apple
1266 - banana
1267 - >
1268 can't you see
1269 the beauty of yaml?
1270 hmm
1271 - dog
1272 """
1273 .toEqual YAML.parse YAML.dump [
1274 'apple',
1275 'banana',
1276 "can't you see the beauty of yaml? hmm\n",
1277 'dog'
1278 ]
1279  
1280  
1281 it 'can be folded block as a mapping value', ->
1282  
1283 expect YAML.parse """
1284 ---
1285 quote: >
1286 Mark McGwire's
1287 year was crippled
1288 by a knee injury.
1289 source: espn
1290 """
1291 .toEqual YAML.parse YAML.dump (
1292 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
1293 'source': 'espn'
1294 )
1295  
1296  
1297 it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
1298  
1299 expect YAML.parse """
1300 clipped: >
1301 This has one newline.
1302  
1303  
1304  
1305 same as "clipped" above: "This has one newline.\\n"
1306  
1307 stripped: >-
1308 This has no newline.
1309  
1310  
1311  
1312 same as "stripped" above: "This has no newline."
1313  
1314 kept: >+
1315 This has four newlines.
1316  
1317  
1318  
1319 same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
1320 """
1321 .toEqual YAML.parse YAML.dump (
1322 'clipped': "This has one newline.\n"
1323 'same as "clipped" above': "This has one newline.\n"
1324 'stripped': 'This has no newline.'
1325 'same as "stripped" above': 'This has no newline.'
1326 'kept': "This has four newlines.\n\n\n\n"
1327 'same as "kept" above': "This has four newlines.\n\n\n\n"
1328 )
1329  
1330  
1331  
1332 describe 'Dumped YAML Comments', ->
1333  
1334 it 'can begin the document', ->
1335  
1336 expect YAML.parse """
1337 # This is a comment
1338 hello: world
1339 """
1340 .toEqual YAML.parse YAML.dump (
1341 hello: 'world'
1342 )
1343  
1344  
1345 it 'can finish a line', ->
1346  
1347 expect YAML.parse """
1348 hello: world # This is a comment
1349 """
1350 .toEqual YAML.parse YAML.dump (
1351 hello: 'world'
1352 )
1353  
1354  
1355 it 'can end the document', ->
1356  
1357 expect YAML.parse """
1358 hello: world
1359 # This is a comment
1360 """
1361 .toEqual YAML.parse YAML.dump (
1362 hello: 'world'
1363 )
1364  
1365  
1366  
1367 describe 'Dumped YAML Aliases and Anchors', ->
1368  
1369 it 'can be simple alias', ->
1370  
1371 expect YAML.parse """
1372 - &showell Steve
1373 - Clark
1374 - Brian
1375 - Oren
1376 - *showell
1377 """
1378 .toEqual YAML.parse YAML.dump ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
1379  
1380  
1381 it 'can be alias of a mapping', ->
1382  
1383 expect YAML.parse """
1384 - &hello
1385 Meat: pork
1386 Starch: potato
1387 - banana
1388 - *hello
1389 """
1390 .toEqual YAML.parse YAML.dump [
1391 Meat: 'pork', Starch: 'potato'
1392 ,
1393 'banana'
1394 ,
1395 Meat: 'pork', Starch: 'potato'
1396 ]
1397  
1398  
1399  
1400 describe 'Dumped YAML Documents', ->
1401  
1402 it 'can have YAML header', ->
1403  
1404 expect YAML.parse """
1405 --- %YAML:1.0
1406 foo: 1
1407 bar: 2
1408 """
1409 .toEqual YAML.parse YAML.dump (
1410 foo: 1
1411 bar: 2
1412 )
1413  
1414  
1415 it 'can have leading document separator', ->
1416  
1417 expect YAML.parse """
1418 ---
1419 - foo: 1
1420 bar: 2
1421 """
1422 .toEqual YAML.parse YAML.dump [(
1423 foo: 1
1424 bar: 2
1425 )]
1426  
1427  
1428 it 'can have multiple document separators in block', ->
1429  
1430 expect YAML.parse """
1431 foo: |
1432 ---
1433 foo: bar
1434 ---
1435 yo: baz
1436 bar: |
1437 fooness
1438 """
1439 .toEqual YAML.parse YAML.dump (
1440 foo: "---\nfoo: bar\n---\nyo: baz\n"
1441 bar: "fooness\n"
1442 )
1443  
1444  
1445 # Loading
1446 # (disable test when running locally from file)
1447 #
1448 url = document?.location?.href
1449 if not(url?) or url.indexOf('file://') is -1
1450  
1451 examplePath = 'spec/example.yml'
1452 if __dirname?
1453 examplePath = __dirname+'/example.yml'
1454  
1455 describe 'YAML loading', ->
1456  
1457 it 'can be done synchronously', ->
1458  
1459 expect(YAML.load(examplePath)).toEqual (
1460 this: 'is'
1461 a: ['YAML', 'example']
1462 )
1463  
1464  
1465 it 'can be done asynchronously', (done) ->
1466  
1467 YAML.load examplePath, (result) ->
1468  
1469 expect(result).toEqual (
1470 this: 'is'
1471 a: ['YAML', 'example']
1472 )
1473  
1474 done()