scratch – Blame information for rev 75

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)
416 spaceSeparatedDate.setTime spaceSeparatedDate.getTime() - 5 * 3600 * 1000
417  
418 withDatesToTime = (input) ->
419 res = {}
420 for key, val of input
421 res[key] = Math.round(val.getTime() / 1000) * 1000
422 return res
423  
424 expect withDatesToTime(YAML.parse """
425 iso8601: 2001-12-14t21:59:43.10-05:00
426 space seperated: 2001-12-14 21:59:43.10 -05:00
427 """)
428 .toEqual withDatesToTime (
429 'iso8601': iso8601Date
430 'space seperated': spaceSeparatedDate
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
442 res[key] = Math.round(val.getTime() / 1000) * 1000
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  
937  
938  
939 describe 'Dumped YAML Basic Types', ->
940  
941 it 'can be strings', ->
942  
943 expect YAML.parse """
944 ---
945 String
946 """
947 .toEqual YAML.parse YAML.dump 'String'
948  
949  
950 it 'can be double-quoted strings with backslashes', ->
951  
952 expect YAML.parse """
953 str:
954 "string with \\\\ inside"
955 """
956 .toEqual YAML.parse YAML.dump str: 'string with \\ inside'
957  
958  
959 it 'can be single-quoted strings with backslashes', ->
960  
961 expect YAML.parse """
962 str:
963 'string with \\\\ inside'
964 """
965 .toEqual YAML.parse YAML.dump str: 'string with \\\\ inside'
966  
967  
968 it 'can be double-quoted strings with line breaks', ->
969  
970 expect YAML.parse """
971 str:
972 "string with \\n inside"
973 """
974 .toEqual YAML.parse YAML.dump str: 'string with \n inside'
975  
976  
977 it 'can be double-quoted strings with line breaks and backslashes', ->
978  
979 expect YAML.parse """
980 str:
981 "string with \\n inside and \\\\ also"
982 """
983 .toEqual YAML.parse YAML.dump str: 'string with \n inside and \\ also'
984  
985  
986 it 'can be single-quoted strings with line breaks and backslashes', ->
987  
988 expect YAML.parse """
989 str:
990 'string with \\n inside and \\\\ also'
991 """
992 .toEqual YAML.parse YAML.dump str: 'string with \\n inside and \\\\ also'
993  
994  
995 it 'can be single-quoted strings with escaped line breaks', ->
996  
997 expect YAML.parse """
998 str:
999 'string with \\n inside'
1000 """
1001 .toEqual YAML.parse YAML.dump str: 'string with \\n inside'
1002  
1003  
1004 it 'can have string characters in sequences', ->
1005  
1006 expect YAML.parse """
1007 - What's Yaml?
1008 - It's for writing data structures in plain text.
1009 - And?
1010 - And what? That's not good enough for you?
1011 - No, I mean, "And what about Yaml?"
1012 - Oh, oh yeah. Uh.. Yaml for JavaScript.
1013 """
1014 .toEqual YAML.parse YAML.dump [
1015 "What's Yaml?",
1016 "It's for writing data structures in plain text.",
1017 "And?",
1018 "And what? That's not good enough for you?",
1019 "No, I mean, \"And what about Yaml?\"",
1020 "Oh, oh yeah. Uh.. Yaml for JavaScript."
1021 ]
1022  
1023  
1024 it 'can have indicators in strings', ->
1025  
1026 expect YAML.parse """
1027 the colon followed by space is an indicator: but is a string:right here
1028 same for the pound sign: here we have it#in a string
1029 the comma can, honestly, be used in most cases: [ but not in, inline collections ]
1030 """
1031 .toEqual YAML.parse YAML.dump (
1032 'the colon followed by space is an indicator': 'but is a string:right here',
1033 'same for the pound sign': 'here we have it#in a string',
1034 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
1035 )
1036  
1037  
1038 it 'can force strings', ->
1039  
1040 expect YAML.parse """
1041 date string: !str 2001-08-01
1042 number string: !str 192
1043 date string 2: !!str 2001-08-01
1044 number string 2: !!str 192
1045 """
1046 .toEqual YAML.parse YAML.dump (
1047 'date string': '2001-08-01',
1048 'number string': '192' ,
1049 'date string 2': '2001-08-01',
1050 'number string 2': '192'
1051 )
1052  
1053  
1054 it 'can be single-quoted strings', ->
1055  
1056 expect YAML.parse """
1057 all my favorite symbols: '#:!/%.)'
1058 a few i hate: '&(*'
1059 why do i hate them?: 'it''s very hard to explain'
1060 """
1061 .toEqual YAML.parse YAML.dump (
1062 'all my favorite symbols': '#:!/%.)',
1063 'a few i hate': '&(*',
1064 'why do i hate them?': 'it\'s very hard to explain'
1065 )
1066  
1067  
1068 it 'can be double-quoted strings', ->
1069  
1070 expect YAML.parse """
1071 i know where i want my line breaks: "one here\\nand another here\\n"
1072 """
1073 .toEqual YAML.parse YAML.dump (
1074 'i know where i want my line breaks': "one here\nand another here\n"
1075 )
1076  
1077  
1078 it 'can be null', ->
1079  
1080 expect YAML.parse """
1081 name: Mr. Show
1082 hosted by: Bob and David
1083 date of next season: ~
1084 """
1085 .toEqual YAML.parse YAML.dump (
1086 'name': 'Mr. Show'
1087 'hosted by': 'Bob and David'
1088 'date of next season': null
1089 )
1090  
1091  
1092 it 'can be boolean', ->
1093  
1094 expect YAML.parse """
1095 Is Gus a Liar?: true
1096 Do I rely on Gus for Sustenance?: false
1097 """
1098 .toEqual YAML.parse YAML.dump (
1099 'Is Gus a Liar?': true
1100 'Do I rely on Gus for Sustenance?': false
1101 )
1102  
1103  
1104 it 'can be integers', ->
1105  
1106 expect YAML.parse """
1107 zero: 0
1108 simple: 12
1109 one-thousand: 1,000
1110 negative one-thousand: -1,000
1111 """
1112 .toEqual YAML.parse YAML.dump (
1113 'zero': 0
1114 'simple': 12
1115 'one-thousand': 1000
1116 'negative one-thousand': -1000
1117 )
1118  
1119  
1120 it 'can be integers as map keys', ->
1121  
1122 expect YAML.parse """
1123 1: one
1124 2: two
1125 3: three
1126 """
1127 .toEqual YAML.parse YAML.dump (
1128 1: 'one'
1129 2: 'two'
1130 3: 'three'
1131 )
1132  
1133  
1134 it 'can be floats', ->
1135  
1136 expect YAML.parse """
1137 a simple float: 2.00
1138 larger float: 1,000.09
1139 scientific notation: 1.00009e+3
1140 """
1141 .toEqual YAML.parse YAML.dump (
1142 'a simple float': 2.0
1143 'larger float': 1000.09
1144 'scientific notation': 1000.09
1145 )
1146  
1147  
1148 it 'can be time', ->
1149  
1150 iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
1151 iso8601Date.setTime iso8601Date.getTime() - 5 * 3600 * 1000
1152  
1153 spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
1154 spaceSeparatedDate.setTime spaceSeparatedDate.getTime() - 5 * 3600 * 1000
1155  
1156 withDatesToTime = (input) ->
1157 res = {}
1158 for key, val of input
1159 res[key] = Math.round(val.getTime() / 1000) * 1000
1160 return res
1161  
1162 expect withDatesToTime(YAML.parse """
1163 iso8601: 2001-12-14t21:59:43.10-05:00
1164 space seperated: 2001-12-14 21:59:43.10 -05:00
1165 """)
1166 .toEqual YAML.parse YAML.dump withDatesToTime (
1167 'iso8601': iso8601Date
1168 'space seperated': spaceSeparatedDate
1169 )
1170  
1171  
1172 it 'can be date', ->
1173  
1174 aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
1175  
1176 withDatesToTime = (input) ->
1177 return input
1178 res = {}
1179 for key, val of input
1180 res[key] = Math.round(val.getTime() / 1000) * 1000
1181 return res
1182  
1183 expect withDatesToTime(YAML.parse """
1184 date: 1976-07-31
1185 """)
1186 .toEqual YAML.parse YAML.dump withDatesToTime (
1187 'date': aDate
1188 )
1189  
1190  
1191  
1192 describe 'Dumped YAML Blocks', ->
1193  
1194 it 'can be single ending newline', ->
1195  
1196 expect YAML.parse """
1197 ---
1198 this: |
1199 Foo
1200 Bar
1201 """
1202 .toEqual YAML.parse YAML.dump 'this': "Foo\nBar\n"
1203  
1204  
1205 it 'can be single ending newline with \'+\' indicator', ->
1206  
1207 expect YAML.parse """
1208 normal: |
1209 extra new lines not kept
1210  
1211 preserving: |+
1212 extra new lines are kept
1213  
1214  
1215 dummy: value
1216 """
1217 .toEqual YAML.parse YAML.dump (
1218 'normal': "extra new lines not kept\n"
1219 'preserving': "extra new lines are kept\n\n\n"
1220 'dummy': 'value'
1221 )
1222  
1223  
1224 it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
1225  
1226 expect YAML.parse """
1227 clipped: |
1228 This has one newline.
1229  
1230  
1231  
1232 same as "clipped" above: "This has one newline.\\n"
1233  
1234 stripped: |-
1235 This has no newline.
1236  
1237  
1238  
1239 same as "stripped" above: "This has no newline."
1240  
1241 kept: |+
1242 This has four newlines.
1243  
1244  
1245  
1246 same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
1247 """
1248 .toEqual YAML.parse YAML.dump (
1249 'clipped': "This has one newline.\n"
1250 'same as "clipped" above': "This has one newline.\n"
1251 'stripped':'This has no newline.'
1252 'same as "stripped" above': 'This has no newline.'
1253 'kept': "This has four newlines.\n\n\n\n"
1254 'same as "kept" above': "This has four newlines.\n\n\n\n"
1255 )
1256  
1257  
1258 it 'can be folded block in a sequence', ->
1259  
1260 expect YAML.parse """
1261 ---
1262 - apple
1263 - banana
1264 - >
1265 can't you see
1266 the beauty of yaml?
1267 hmm
1268 - dog
1269 """
1270 .toEqual YAML.parse YAML.dump [
1271 'apple',
1272 'banana',
1273 "can't you see the beauty of yaml? hmm\n",
1274 'dog'
1275 ]
1276  
1277  
1278 it 'can be folded block as a mapping value', ->
1279  
1280 expect YAML.parse """
1281 ---
1282 quote: >
1283 Mark McGwire's
1284 year was crippled
1285 by a knee injury.
1286 source: espn
1287 """
1288 .toEqual YAML.parse YAML.dump (
1289 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
1290 'source': 'espn'
1291 )
1292  
1293  
1294 it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
1295  
1296 expect YAML.parse """
1297 clipped: >
1298 This has one newline.
1299  
1300  
1301  
1302 same as "clipped" above: "This has one newline.\\n"
1303  
1304 stripped: >-
1305 This has no newline.
1306  
1307  
1308  
1309 same as "stripped" above: "This has no newline."
1310  
1311 kept: >+
1312 This has four newlines.
1313  
1314  
1315  
1316 same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
1317 """
1318 .toEqual YAML.parse YAML.dump (
1319 'clipped': "This has one newline.\n"
1320 'same as "clipped" above': "This has one newline.\n"
1321 'stripped': 'This has no newline.'
1322 'same as "stripped" above': 'This has no newline.'
1323 'kept': "This has four newlines.\n\n\n\n"
1324 'same as "kept" above': "This has four newlines.\n\n\n\n"
1325 )
1326  
1327  
1328  
1329 describe 'Dumped YAML Comments', ->
1330  
1331 it 'can begin the document', ->
1332  
1333 expect YAML.parse """
1334 # This is a comment
1335 hello: world
1336 """
1337 .toEqual YAML.parse YAML.dump (
1338 hello: 'world'
1339 )
1340  
1341  
1342 it 'can finish a line', ->
1343  
1344 expect YAML.parse """
1345 hello: world # This is a comment
1346 """
1347 .toEqual YAML.parse YAML.dump (
1348 hello: 'world'
1349 )
1350  
1351  
1352 it 'can end the document', ->
1353  
1354 expect YAML.parse """
1355 hello: world
1356 # This is a comment
1357 """
1358 .toEqual YAML.parse YAML.dump (
1359 hello: 'world'
1360 )
1361  
1362  
1363  
1364 describe 'Dumped YAML Aliases and Anchors', ->
1365  
1366 it 'can be simple alias', ->
1367  
1368 expect YAML.parse """
1369 - &showell Steve
1370 - Clark
1371 - Brian
1372 - Oren
1373 - *showell
1374 """
1375 .toEqual YAML.parse YAML.dump ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
1376  
1377  
1378 it 'can be alias of a mapping', ->
1379  
1380 expect YAML.parse """
1381 - &hello
1382 Meat: pork
1383 Starch: potato
1384 - banana
1385 - *hello
1386 """
1387 .toEqual YAML.parse YAML.dump [
1388 Meat: 'pork', Starch: 'potato'
1389 ,
1390 'banana'
1391 ,
1392 Meat: 'pork', Starch: 'potato'
1393 ]
1394  
1395  
1396  
1397 describe 'Dumped YAML Documents', ->
1398  
1399 it 'can have YAML header', ->
1400  
1401 expect YAML.parse """
1402 --- %YAML:1.0
1403 foo: 1
1404 bar: 2
1405 """
1406 .toEqual YAML.parse YAML.dump (
1407 foo: 1
1408 bar: 2
1409 )
1410  
1411  
1412 it 'can have leading document separator', ->
1413  
1414 expect YAML.parse """
1415 ---
1416 - foo: 1
1417 bar: 2
1418 """
1419 .toEqual YAML.parse YAML.dump [(
1420 foo: 1
1421 bar: 2
1422 )]
1423  
1424  
1425 it 'can have multiple document separators in block', ->
1426  
1427 expect YAML.parse """
1428 foo: |
1429 ---
1430 foo: bar
1431 ---
1432 yo: baz
1433 bar: |
1434 fooness
1435 """
1436 .toEqual YAML.parse YAML.dump (
1437 foo: "---\nfoo: bar\n---\nyo: baz\n"
1438 bar: "fooness\n"
1439 )
1440  
1441  
1442 # Loading
1443 # (disable test when running locally from file)
1444 #
1445 url = document?.location?.href
1446 if not(url?) or url.indexOf('file://') is -1
1447  
1448 examplePath = 'spec/example.yml'
1449 if __dirname?
1450 examplePath = __dirname+'/example.yml'
1451  
1452 describe 'YAML loading', ->
1453  
1454 it 'can be done synchronously', ->
1455  
1456 expect(YAML.load(examplePath)).toEqual (
1457 this: 'is'
1458 a: ['YAML', 'example']
1459 )
1460  
1461  
1462 it 'can be done asynchronously', (done) ->
1463  
1464 YAML.load examplePath, (result) ->
1465  
1466 expect(result).toEqual (
1467 this: 'is'
1468 a: ['YAML', 'example']
1469 )
1470  
1471 done()