1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474 |
- unless YAML?
- YAML = require '../../src/Yaml'
- # Parsing
- #
- describe 'Parsed YAML Collections', ->
- it 'can be simple sequence', ->
- expect YAML.parse """
- - apple
- - banana
- - carrot
- """
- .toEqual ['apple', 'banana', 'carrot']
- it 'can be nested sequences', ->
- expect YAML.parse """
- -
- - foo
- - bar
- - baz
- """
- .toEqual [['foo', 'bar', 'baz']]
- it 'can be mixed sequences', ->
- expect YAML.parse """
- - apple
- -
- - foo
- - bar
- - x123
- - banana
- - carrot
- """
- .toEqual ['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']
- it 'can be deeply nested sequences', ->
- expect YAML.parse """
- -
- -
- - uno
- - dos
- """
- .toEqual [[['uno', 'dos']]]
- it 'can be simple mapping', ->
- expect YAML.parse """
- foo: whatever
- bar: stuff
- """
- .toEqual foo: 'whatever', bar: 'stuff'
- it 'can be sequence in a mapping', ->
- expect YAML.parse """
- foo: whatever
- bar:
- - uno
- - dos
- """
- .toEqual foo: 'whatever', bar: ['uno', 'dos']
- it 'can be nested mappings', ->
- expect YAML.parse """
- foo: whatever
- bar:
- fruit: apple
- name: steve
- sport: baseball
- """
- .toEqual foo: 'whatever', bar: (fruit: 'apple', name: 'steve', sport: 'baseball')
- it 'can be mixed mapping', ->
- expect YAML.parse """
- foo: whatever
- bar:
- -
- fruit: apple
- name: steve
- sport: baseball
- - more
- -
- python: rocks
- perl: papers
- ruby: scissorses
- """
- .toEqual foo: 'whatever', bar: [
- (fruit: 'apple', name: 'steve', sport: 'baseball'),
- 'more',
- (python: 'rocks', perl: 'papers', ruby: 'scissorses')
- ]
- it 'can have mapping-in-sequence shortcut', ->
- expect YAML.parse """
- - work on YAML.py:
- - work on Store
- """
- .toEqual [('work on YAML.py': ['work on Store'])]
- it 'can have unindented sequence-in-mapping shortcut', ->
- expect YAML.parse """
- allow:
- - 'localhost'
- - '%.sourceforge.net'
- - '%.freepan.org'
- """
- .toEqual (allow: ['localhost', '%.sourceforge.net', '%.freepan.org'])
- it 'can merge key', ->
- expect YAML.parse """
- mapping:
- name: Joe
- job: Accountant
- <<:
- age: 38
- """
- .toEqual mapping:
- name: 'Joe'
- job: 'Accountant'
- age: 38
- it 'can ignore trailing empty lines for smallest indent', ->
- expect YAML.parse """ trailing: empty lines\n"""
- .toEqual trailing: 'empty lines'
- describe 'Parsed YAML Inline Collections', ->
- it 'can be simple inline array', ->
- expect YAML.parse """
- ---
- seq: [ a, b, c ]
- """
- .toEqual seq: ['a', 'b', 'c']
- it 'can be simple inline hash', ->
- expect YAML.parse """
- ---
- hash: { name: Steve, foo: bar }
- """
- .toEqual hash: (name: 'Steve', foo: 'bar')
- it 'can be nested inline hash', ->
- expect YAML.parse """
- ---
- hash: { val1: "string", val2: { v2k1: "v2k1v" } }
- """
- .toEqual hash: (val1: 'string', val2: (v2k1: 'v2k1v'))
- it 'can be multi-line inline collections', ->
- expect YAML.parse """
- languages: [ Ruby,
- Perl,
- Python ]
- websites: { YAML: yaml.org,
- Ruby: ruby-lang.org,
- Python: python.org,
- Perl: use.perl.org }
- """
- .toEqual (
- languages: ['Ruby', 'Perl', 'Python']
- websites:
- YAML: 'yaml.org'
- Ruby: 'ruby-lang.org'
- Python: 'python.org'
- Perl: 'use.perl.org'
- )
- describe 'Parsed YAML Basic Types', ->
- it 'can be strings', ->
- expect YAML.parse """
- ---
- String
- """
- .toEqual 'String'
- it 'can be double-quoted strings with backslashes', ->
- expect YAML.parse """
- str:
- "string with \\\\ inside"
- """
- .toEqual str: 'string with \\ inside'
- it 'can be single-quoted strings with backslashes', ->
- expect YAML.parse """
- str:
- 'string with \\\\ inside'
- """
- .toEqual str: 'string with \\\\ inside'
- it 'can be double-quoted strings with line breaks', ->
- expect YAML.parse """
- str:
- "string with \\n inside"
- """
- .toEqual str: 'string with \n inside'
- it 'can be single-quoted strings with escaped line breaks', ->
- expect YAML.parse """
- str:
- 'string with \\n inside'
- """
- .toEqual str: 'string with \\n inside'
- it 'can be double-quoted strings with line breaks and backslashes', ->
- expect YAML.parse """
- str:
- "string with \\n inside and \\\\ also"
- """
- .toEqual str: 'string with \n inside and \\ also'
- it 'can be single-quoted strings with line breaks and backslashes', ->
- expect YAML.parse """
- str:
- 'string with \\n inside and \\\\ also'
- """
- .toEqual str: 'string with \\n inside and \\\\ also'
- it 'can have string characters in sequences', ->
- expect YAML.parse """
- - What's Yaml?
- - It's for writing data structures in plain text.
- - And?
- - And what? That's not good enough for you?
- - No, I mean, "And what about Yaml?"
- - Oh, oh yeah. Uh.. Yaml for JavaScript.
- """
- .toEqual [
- "What's Yaml?",
- "It's for writing data structures in plain text.",
- "And?",
- "And what? That's not good enough for you?",
- "No, I mean, \"And what about Yaml?\"",
- "Oh, oh yeah. Uh.. Yaml for JavaScript."
- ]
- it 'can have indicators in strings', ->
- expect YAML.parse """
- the colon followed by space is an indicator: but is a string:right here
- same for the pound sign: here we have it#in a string
- the comma can, honestly, be used in most cases: [ but not in, inline collections ]
- """
- .toEqual (
- 'the colon followed by space is an indicator': 'but is a string:right here',
- 'same for the pound sign': 'here we have it#in a string',
- 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
- )
- it 'can force strings', ->
- expect YAML.parse """
- date string: !str 2001-08-01
- number string: !str 192
- date string 2: !!str 2001-08-01
- number string 2: !!str 192
- """
- .toEqual (
- 'date string': '2001-08-01',
- 'number string': '192' ,
- 'date string 2': '2001-08-01',
- 'number string 2': '192'
- )
- it 'can be single-quoted strings', ->
- expect YAML.parse """
- all my favorite symbols: '#:!/%.)'
- a few i hate: '&(*'
- why do i hate them?: 'it''s very hard to explain'
- """
- .toEqual (
- 'all my favorite symbols': '#:!/%.)',
- 'a few i hate': '&(*',
- 'why do i hate them?': 'it\'s very hard to explain'
- )
- it 'can be double-quoted strings', ->
- expect YAML.parse """
- i know where i want my line breaks: "one here\\nand another here\\n"
- """
- .toEqual (
- 'i know where i want my line breaks': "one here\nand another here\n"
- )
- it 'can be null', ->
- expect YAML.parse """
- name: Mr. Show
- hosted by: Bob and David
- date of next season: ~
- """
- .toEqual (
- 'name': 'Mr. Show'
- 'hosted by': 'Bob and David'
- 'date of next season': null
- )
- it 'can be boolean', ->
- expect YAML.parse """
- Is Gus a Liar?: true
- Do I rely on Gus for Sustenance?: false
- """
- .toEqual (
- 'Is Gus a Liar?': true
- 'Do I rely on Gus for Sustenance?': false
- )
- it 'can be integers', ->
- expect YAML.parse """
- zero: 0
- simple: 12
- one-thousand: 1,000
- negative one-thousand: -1,000
- """
- .toEqual (
- 'zero': 0
- 'simple': 12
- 'one-thousand': 1000
- 'negative one-thousand': -1000
- )
- it 'can be integers as map keys', ->
- expect YAML.parse """
- 1: one
- 2: two
- 3: three
- """
- .toEqual (
- 1: 'one'
- 2: 'two'
- 3: 'three'
- )
- it 'can be floats', ->
- expect YAML.parse """
- a simple float: 2.00
- larger float: 1,000.09
- scientific notation: 1.00009e+3
- """
- .toEqual (
- 'a simple float': 2.0
- 'larger float': 1000.09
- 'scientific notation': 1000.09
- )
- it 'can be time', ->
- iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
- iso8601Date.setTime iso8601Date.getTime() - 5 * 3600 * 1000
- spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
- spaceSeparatedDate.setTime spaceSeparatedDate.getTime() + 5 * 3600 * 1000
- withDatesToTime = (input) ->
- res = {}
- for key, val of input
- res[key] = val.getTime()
- return res
- expect withDatesToTime(YAML.parse """
- iso8601: 2001-12-14t21:59:43.010+05:00
- space separated: 2001-12-14 21:59:43.010 -05:00
- """)
- .toEqual withDatesToTime (
- 'iso8601': iso8601Date
- 'space separated': spaceSeparatedDate
- )
- it 'can be date', ->
- aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
- withDatesToTime = (input) ->
- return input
- res = {}
- for key, val of input
- res[key] = val.getTime()
- return res
- expect withDatesToTime(YAML.parse """
- date: 1976-07-31
- """)
- .toEqual withDatesToTime (
- 'date': aDate
- )
- describe 'Parsed YAML Blocks', ->
- it 'can be single ending newline', ->
- expect YAML.parse """
- ---
- this: |
- Foo
- Bar
- """
- .toEqual 'this': "Foo\nBar\n"
- it 'can be single ending newline with \'+\' indicator', ->
- expect YAML.parse """
- normal: |
- extra new lines not kept
- preserving: |+
- extra new lines are kept
- dummy: value
- """
- .toEqual (
- 'normal': "extra new lines not kept\n"
- 'preserving': "extra new lines are kept\n\n\n"
- 'dummy': 'value'
- )
- it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
- expect YAML.parse """
- clipped: |
- This has one newline.
- same as "clipped" above: "This has one newline.\\n"
- stripped: |-
- This has no newline.
- same as "stripped" above: "This has no newline."
- kept: |+
- This has four newlines.
- same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
- """
- .toEqual (
- 'clipped': "This has one newline.\n"
- 'same as "clipped" above': "This has one newline.\n"
- 'stripped':'This has no newline.'
- 'same as "stripped" above': 'This has no newline.'
- 'kept': "This has four newlines.\n\n\n\n"
- 'same as "kept" above': "This has four newlines.\n\n\n\n"
- )
- it 'can be folded block in a sequence', ->
- expect YAML.parse """
- ---
- - apple
- - banana
- - >
- can't you see
- the beauty of yaml?
- hmm
- - dog
- """
- .toEqual [
- 'apple',
- 'banana',
- "can't you see the beauty of yaml? hmm\n",
- 'dog'
- ]
- it 'can be folded block as a mapping value', ->
- expect YAML.parse """
- ---
- quote: >
- Mark McGwire's
- year was crippled
- by a knee injury.
- source: espn
- """
- .toEqual (
- 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
- 'source': 'espn'
- )
- it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
- expect YAML.parse """
- clipped: >
- This has one newline.
- same as "clipped" above: "This has one newline.\\n"
- stripped: >-
- This has no newline.
- same as "stripped" above: "This has no newline."
- kept: >+
- This has four newlines.
- same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
- """
- .toEqual (
- 'clipped': "This has one newline.\n"
- 'same as "clipped" above': "This has one newline.\n"
- 'stripped': 'This has no newline.'
- 'same as "stripped" above': 'This has no newline.'
- 'kept': "This has four newlines.\n\n\n\n"
- 'same as "kept" above': "This has four newlines.\n\n\n\n"
- )
- it 'can be the whole document as intented block', ->
- expect YAML.parse """
- ---
- foo: "bar"
- baz:
- - "qux"
- - "quxx"
- corge: null
- """
- .toEqual (
- 'foo': "bar"
- 'baz': ['qux', 'quxx']
- 'corge': null
- )
- describe 'Parsed YAML Comments', ->
- it 'can begin the document', ->
- expect YAML.parse """
- # This is a comment
- hello: world
- """
- .toEqual (
- hello: 'world'
- )
- it 'can be less indented in mapping', ->
- expect YAML.parse """
- parts:
- a: 'b'
- # normally indented comment
- c: 'd'
- # less indented comment
- e: 'f'
- """
- .toEqual (
- parts: {a: 'b', c: 'd', e: 'f'}
- )
- it 'can be less indented in sequence', ->
- expect YAML.parse """
- list-header:
- - item1
- # - item2
- - item3
- # - item4
- """
- .toEqual (
- 'list-header': ['item1', 'item3']
- )
- it 'can finish a line', ->
- expect YAML.parse """
- hello: world # This is a comment
- """
- .toEqual (
- hello: 'world'
- )
- it 'can end the document', ->
- expect YAML.parse """
- hello: world
- # This is a comment
- """
- .toEqual (
- hello: 'world'
- )
- describe 'Parsed YAML Aliases and Anchors', ->
- it 'can be simple alias', ->
- expect YAML.parse """
- - &showell Steve
- - Clark
- - Brian
- - Oren
- - *showell
- """
- .toEqual ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
- it 'can be alias of a mapping', ->
- expect YAML.parse """
- - &hello
- Meat: pork
- Starch: potato
- - banana
- - *hello
- """
- .toEqual [
- Meat: 'pork', Starch: 'potato'
- ,
- 'banana'
- ,
- Meat: 'pork', Starch: 'potato'
- ]
- describe 'Parsed YAML Documents', ->
- it 'can have YAML header', ->
- expect YAML.parse """
- --- %YAML:1.0
- foo: 1
- bar: 2
- """
- .toEqual (
- foo: 1
- bar: 2
- )
- it 'can have leading document separator', ->
- expect YAML.parse """
- ---
- - foo: 1
- bar: 2
- """
- .toEqual [(
- foo: 1
- bar: 2
- )]
- it 'can have multiple document separators in block', ->
- expect YAML.parse """
- foo: |
- ---
- foo: bar
- ---
- yo: baz
- bar: |
- fooness
- """
- .toEqual (
- foo: "---\nfoo: bar\n---\nyo: baz\n"
- bar: "fooness\n"
- )
- # Dumping
- #
- describe 'Dumped YAML Collections', ->
- it 'can be simple sequence', ->
- expect YAML.parse """
- - apple
- - banana
- - carrot
- """
- .toEqual YAML.parse YAML.dump ['apple', 'banana', 'carrot']
- it 'can be nested sequences', ->
- expect YAML.parse """
- -
- - foo
- - bar
- - baz
- """
- .toEqual YAML.parse YAML.dump [['foo', 'bar', 'baz']]
- it 'can be mixed sequences', ->
- expect YAML.parse """
- - apple
- -
- - foo
- - bar
- - x123
- - banana
- - carrot
- """
- .toEqual YAML.parse YAML.dump ['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']
- it 'can be deeply nested sequences', ->
- expect YAML.parse """
- -
- -
- - uno
- - dos
- """
- .toEqual YAML.parse YAML.dump [[['uno', 'dos']]]
- it 'can be simple mapping', ->
- expect YAML.parse """
- foo: whatever
- bar: stuff
- """
- .toEqual YAML.parse YAML.dump foo: 'whatever', bar: 'stuff'
- it 'can be sequence in a mapping', ->
- expect YAML.parse """
- foo: whatever
- bar:
- - uno
- - dos
- """
- .toEqual YAML.parse YAML.dump foo: 'whatever', bar: ['uno', 'dos']
- it 'can be nested mappings', ->
- expect YAML.parse """
- foo: whatever
- bar:
- fruit: apple
- name: steve
- sport: baseball
- """
- .toEqual YAML.parse YAML.dump foo: 'whatever', bar: (fruit: 'apple', name: 'steve', sport: 'baseball')
- it 'can be mixed mapping', ->
- expect YAML.parse """
- foo: whatever
- bar:
- -
- fruit: apple
- name: steve
- sport: baseball
- - more
- -
- python: rocks
- perl: papers
- ruby: scissorses
- """
- .toEqual YAML.parse YAML.dump foo: 'whatever', bar: [
- (fruit: 'apple', name: 'steve', sport: 'baseball'),
- 'more',
- (python: 'rocks', perl: 'papers', ruby: 'scissorses')
- ]
- it 'can have mapping-in-sequence shortcut', ->
- expect YAML.parse """
- - work on YAML.py:
- - work on Store
- """
- .toEqual YAML.parse YAML.dump [('work on YAML.py': ['work on Store'])]
- it 'can have unindented sequence-in-mapping shortcut', ->
- expect YAML.parse """
- allow:
- - 'localhost'
- - '%.sourceforge.net'
- - '%.freepan.org'
- """
- .toEqual YAML.parse YAML.dump (allow: ['localhost', '%.sourceforge.net', '%.freepan.org'])
- it 'can merge key', ->
- expect YAML.parse """
- mapping:
- name: Joe
- job: Accountant
- <<:
- age: 38
- """
- .toEqual YAML.parse YAML.dump mapping:
- name: 'Joe'
- job: 'Accountant'
- age: 38
- describe 'Dumped YAML Inline Collections', ->
- it 'can be simple inline array', ->
- expect YAML.parse """
- ---
- seq: [ a, b, c ]
- """
- .toEqual YAML.parse YAML.dump seq: ['a', 'b', 'c']
- it 'can be simple inline hash', ->
- expect YAML.parse """
- ---
- hash: { name: Steve, foo: bar }
- """
- .toEqual YAML.parse YAML.dump hash: (name: 'Steve', foo: 'bar')
- it 'can be multi-line inline collections', ->
- expect YAML.parse """
- languages: [ Ruby,
- Perl,
- Python ]
- websites: { YAML: yaml.org,
- Ruby: ruby-lang.org,
- Python: python.org,
- Perl: use.perl.org }
- """
- .toEqual YAML.parse YAML.dump (
- languages: ['Ruby', 'Perl', 'Python']
- websites:
- YAML: 'yaml.org'
- Ruby: 'ruby-lang.org'
- Python: 'python.org'
- Perl: 'use.perl.org'
- )
- it 'can be dumped empty sequences in mappings', ->
- expect YAML.parse(YAML.dump({key:[]}))
- .toEqual({key:[]})
- it 'can be dumpted empty inline collections', ->
- expect YAML.parse(YAML.dump({key:{}}))
- .toEqual({key:{}})
- describe 'Dumped YAML Basic Types', ->
- it 'can be strings', ->
- expect YAML.parse """
- ---
- String
- """
- .toEqual YAML.parse YAML.dump 'String'
- it 'can be double-quoted strings with backslashes', ->
- expect YAML.parse """
- str:
- "string with \\\\ inside"
- """
- .toEqual YAML.parse YAML.dump str: 'string with \\ inside'
- it 'can be single-quoted strings with backslashes', ->
- expect YAML.parse """
- str:
- 'string with \\\\ inside'
- """
- .toEqual YAML.parse YAML.dump str: 'string with \\\\ inside'
- it 'can be double-quoted strings with line breaks', ->
- expect YAML.parse """
- str:
- "string with \\n inside"
- """
- .toEqual YAML.parse YAML.dump str: 'string with \n inside'
- it 'can be double-quoted strings with line breaks and backslashes', ->
- expect YAML.parse """
- str:
- "string with \\n inside and \\\\ also"
- """
- .toEqual YAML.parse YAML.dump str: 'string with \n inside and \\ also'
- it 'can be single-quoted strings with line breaks and backslashes', ->
- expect YAML.parse """
- str:
- 'string with \\n inside and \\\\ also'
- """
- .toEqual YAML.parse YAML.dump str: 'string with \\n inside and \\\\ also'
- it 'can be single-quoted strings with escaped line breaks', ->
- expect YAML.parse """
- str:
- 'string with \\n inside'
- """
- .toEqual YAML.parse YAML.dump str: 'string with \\n inside'
- it 'can have string characters in sequences', ->
- expect YAML.parse """
- - What's Yaml?
- - It's for writing data structures in plain text.
- - And?
- - And what? That's not good enough for you?
- - No, I mean, "And what about Yaml?"
- - Oh, oh yeah. Uh.. Yaml for JavaScript.
- """
- .toEqual YAML.parse YAML.dump [
- "What's Yaml?",
- "It's for writing data structures in plain text.",
- "And?",
- "And what? That's not good enough for you?",
- "No, I mean, \"And what about Yaml?\"",
- "Oh, oh yeah. Uh.. Yaml for JavaScript."
- ]
- it 'can have indicators in strings', ->
- expect YAML.parse """
- the colon followed by space is an indicator: but is a string:right here
- same for the pound sign: here we have it#in a string
- the comma can, honestly, be used in most cases: [ but not in, inline collections ]
- """
- .toEqual YAML.parse YAML.dump (
- 'the colon followed by space is an indicator': 'but is a string:right here',
- 'same for the pound sign': 'here we have it#in a string',
- 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
- )
- it 'can force strings', ->
- expect YAML.parse """
- date string: !str 2001-08-01
- number string: !str 192
- date string 2: !!str 2001-08-01
- number string 2: !!str 192
- """
- .toEqual YAML.parse YAML.dump (
- 'date string': '2001-08-01',
- 'number string': '192' ,
- 'date string 2': '2001-08-01',
- 'number string 2': '192'
- )
- it 'can be single-quoted strings', ->
- expect YAML.parse """
- all my favorite symbols: '#:!/%.)'
- a few i hate: '&(*'
- why do i hate them?: 'it''s very hard to explain'
- """
- .toEqual YAML.parse YAML.dump (
- 'all my favorite symbols': '#:!/%.)',
- 'a few i hate': '&(*',
- 'why do i hate them?': 'it\'s very hard to explain'
- )
- it 'can be double-quoted strings', ->
- expect YAML.parse """
- i know where i want my line breaks: "one here\\nand another here\\n"
- """
- .toEqual YAML.parse YAML.dump (
- 'i know where i want my line breaks': "one here\nand another here\n"
- )
- it 'can be null', ->
- expect YAML.parse """
- name: Mr. Show
- hosted by: Bob and David
- date of next season: ~
- """
- .toEqual YAML.parse YAML.dump (
- 'name': 'Mr. Show'
- 'hosted by': 'Bob and David'
- 'date of next season': null
- )
- it 'can be boolean', ->
- expect YAML.parse """
- Is Gus a Liar?: true
- Do I rely on Gus for Sustenance?: false
- """
- .toEqual YAML.parse YAML.dump (
- 'Is Gus a Liar?': true
- 'Do I rely on Gus for Sustenance?': false
- )
- it 'can be integers', ->
- expect YAML.parse """
- zero: 0
- simple: 12
- one-thousand: 1,000
- negative one-thousand: -1,000
- """
- .toEqual YAML.parse YAML.dump (
- 'zero': 0
- 'simple': 12
- 'one-thousand': 1000
- 'negative one-thousand': -1000
- )
- it 'can be integers as map keys', ->
- expect YAML.parse """
- 1: one
- 2: two
- 3: three
- """
- .toEqual YAML.parse YAML.dump (
- 1: 'one'
- 2: 'two'
- 3: 'three'
- )
- it 'can be floats', ->
- expect YAML.parse """
- a simple float: 2.00
- larger float: 1,000.09
- scientific notation: 1.00009e+3
- """
- .toEqual YAML.parse YAML.dump (
- 'a simple float': 2.0
- 'larger float': 1000.09
- 'scientific notation': 1000.09
- )
- it 'can be time', ->
- iso8601Date = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
- iso8601Date.setTime iso8601Date.getTime() + 5 * 3600 * 1000
- spaceSeparatedDate = new Date Date.UTC(2001, 12-1, 14, 21, 59, 43, 10)
- spaceSeparatedDate.setTime spaceSeparatedDate.getTime() - 5 * 3600 * 1000
- withDatesToTime = (input) ->
- res = {}
- for key, val of input
- res[key] = val.getTime()
- return res
- expect withDatesToTime(YAML.parse """
- iso8601: 2001-12-14t21:59:43.010-05:00
- space separated: 2001-12-14 21:59:43.010 +05:00
- """)
- .toEqual YAML.parse YAML.dump withDatesToTime (
- 'iso8601': iso8601Date
- 'space separated': spaceSeparatedDate
- )
- it 'can be date', ->
- aDate = new Date Date.UTC(1976, 7-1, 31, 0, 0, 0, 0)
- withDatesToTime = (input) ->
- return input
- res = {}
- for key, val of input
- res[key] = val.getTime()
- return res
- expect withDatesToTime(YAML.parse """
- date: 1976-07-31
- """)
- .toEqual YAML.parse YAML.dump withDatesToTime (
- 'date': aDate
- )
- describe 'Dumped YAML Blocks', ->
- it 'can be single ending newline', ->
- expect YAML.parse """
- ---
- this: |
- Foo
- Bar
- """
- .toEqual YAML.parse YAML.dump 'this': "Foo\nBar\n"
- it 'can be single ending newline with \'+\' indicator', ->
- expect YAML.parse """
- normal: |
- extra new lines not kept
- preserving: |+
- extra new lines are kept
- dummy: value
- """
- .toEqual YAML.parse YAML.dump (
- 'normal': "extra new lines not kept\n"
- 'preserving': "extra new lines are kept\n\n\n"
- 'dummy': 'value'
- )
- it 'can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', ->
- expect YAML.parse """
- clipped: |
- This has one newline.
- same as "clipped" above: "This has one newline.\\n"
- stripped: |-
- This has no newline.
- same as "stripped" above: "This has no newline."
- kept: |+
- This has four newlines.
- same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
- """
- .toEqual YAML.parse YAML.dump (
- 'clipped': "This has one newline.\n"
- 'same as "clipped" above': "This has one newline.\n"
- 'stripped':'This has no newline.'
- 'same as "stripped" above': 'This has no newline.'
- 'kept': "This has four newlines.\n\n\n\n"
- 'same as "kept" above': "This has four newlines.\n\n\n\n"
- )
- it 'can be folded block in a sequence', ->
- expect YAML.parse """
- ---
- - apple
- - banana
- - >
- can't you see
- the beauty of yaml?
- hmm
- - dog
- """
- .toEqual YAML.parse YAML.dump [
- 'apple',
- 'banana',
- "can't you see the beauty of yaml? hmm\n",
- 'dog'
- ]
- it 'can be folded block as a mapping value', ->
- expect YAML.parse """
- ---
- quote: >
- Mark McGwire's
- year was crippled
- by a knee injury.
- source: espn
- """
- .toEqual YAML.parse YAML.dump (
- 'quote': "Mark McGwire's year was crippled by a knee injury.\n"
- 'source': 'espn'
- )
- it 'can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', ->
- expect YAML.parse """
- clipped: >
- This has one newline.
- same as "clipped" above: "This has one newline.\\n"
- stripped: >-
- This has no newline.
- same as "stripped" above: "This has no newline."
- kept: >+
- This has four newlines.
- same as "kept" above: "This has four newlines.\\n\\n\\n\\n"
- """
- .toEqual YAML.parse YAML.dump (
- 'clipped': "This has one newline.\n"
- 'same as "clipped" above': "This has one newline.\n"
- 'stripped': 'This has no newline.'
- 'same as "stripped" above': 'This has no newline.'
- 'kept': "This has four newlines.\n\n\n\n"
- 'same as "kept" above': "This has four newlines.\n\n\n\n"
- )
- describe 'Dumped YAML Comments', ->
- it 'can begin the document', ->
- expect YAML.parse """
- # This is a comment
- hello: world
- """
- .toEqual YAML.parse YAML.dump (
- hello: 'world'
- )
- it 'can finish a line', ->
- expect YAML.parse """
- hello: world # This is a comment
- """
- .toEqual YAML.parse YAML.dump (
- hello: 'world'
- )
- it 'can end the document', ->
- expect YAML.parse """
- hello: world
- # This is a comment
- """
- .toEqual YAML.parse YAML.dump (
- hello: 'world'
- )
- describe 'Dumped YAML Aliases and Anchors', ->
- it 'can be simple alias', ->
- expect YAML.parse """
- - &showell Steve
- - Clark
- - Brian
- - Oren
- - *showell
- """
- .toEqual YAML.parse YAML.dump ['Steve', 'Clark', 'Brian', 'Oren', 'Steve']
- it 'can be alias of a mapping', ->
- expect YAML.parse """
- - &hello
- Meat: pork
- Starch: potato
- - banana
- - *hello
- """
- .toEqual YAML.parse YAML.dump [
- Meat: 'pork', Starch: 'potato'
- ,
- 'banana'
- ,
- Meat: 'pork', Starch: 'potato'
- ]
- describe 'Dumped YAML Documents', ->
- it 'can have YAML header', ->
- expect YAML.parse """
- --- %YAML:1.0
- foo: 1
- bar: 2
- """
- .toEqual YAML.parse YAML.dump (
- foo: 1
- bar: 2
- )
- it 'can have leading document separator', ->
- expect YAML.parse """
- ---
- - foo: 1
- bar: 2
- """
- .toEqual YAML.parse YAML.dump [(
- foo: 1
- bar: 2
- )]
- it 'can have multiple document separators in block', ->
- expect YAML.parse """
- foo: |
- ---
- foo: bar
- ---
- yo: baz
- bar: |
- fooness
- """
- .toEqual YAML.parse YAML.dump (
- foo: "---\nfoo: bar\n---\nyo: baz\n"
- bar: "fooness\n"
- )
- # Loading
- # (disable test when running locally from file)
- #
- url = document?.location?.href
- if not(url?) or url.indexOf('file://') is -1
- examplePath = 'spec/example.yml'
- if __dirname?
- examplePath = __dirname+'/example.yml'
- describe 'YAML loading', ->
- it 'can be done synchronously', ->
- expect(YAML.load(examplePath)).toEqual (
- this: 'is'
- a: ['YAML', 'example']
- )
- it 'can be done asynchronously', (done) ->
- YAML.load examplePath, (result) ->
- expect(result).toEqual (
- this: 'is'
- a: ['YAML', 'example']
- )
- done()
|