YamlSpec.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // Generated by CoffeeScript 1.12.4
  2. var YAML, examplePath, ref, url;
  3. if (typeof YAML === "undefined" || YAML === null) {
  4. YAML = require('../../src/Yaml');
  5. }
  6. describe('Parsed YAML Collections', function() {
  7. it('can be simple sequence', function() {
  8. return expect(YAML.parse("- apple\n- banana\n- carrot")).toEqual(['apple', 'banana', 'carrot']);
  9. });
  10. it('can be nested sequences', function() {
  11. return expect(YAML.parse("-\n - foo\n - bar\n - baz")).toEqual([['foo', 'bar', 'baz']]);
  12. });
  13. it('can be mixed sequences', function() {
  14. return expect(YAML.parse("- apple\n-\n - foo\n - bar\n - x123\n- banana\n- carrot")).toEqual(['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot']);
  15. });
  16. it('can be deeply nested sequences', function() {
  17. return expect(YAML.parse("-\n -\n - uno\n - dos")).toEqual([[['uno', 'dos']]]);
  18. });
  19. it('can be simple mapping', function() {
  20. return expect(YAML.parse("foo: whatever\nbar: stuff")).toEqual({
  21. foo: 'whatever',
  22. bar: 'stuff'
  23. });
  24. });
  25. it('can be sequence in a mapping', function() {
  26. return expect(YAML.parse("foo: whatever\nbar:\n - uno\n - dos")).toEqual({
  27. foo: 'whatever',
  28. bar: ['uno', 'dos']
  29. });
  30. });
  31. it('can be nested mappings', function() {
  32. return expect(YAML.parse("foo: whatever\nbar:\n fruit: apple\n name: steve\n sport: baseball")).toEqual({
  33. foo: 'whatever',
  34. bar: {
  35. fruit: 'apple',
  36. name: 'steve',
  37. sport: 'baseball'
  38. }
  39. });
  40. });
  41. it('can be mixed mapping', function() {
  42. return expect(YAML.parse("foo: whatever\nbar:\n -\n fruit: apple\n name: steve\n sport: baseball\n - more\n -\n python: rocks\n perl: papers\n ruby: scissorses")).toEqual({
  43. foo: 'whatever',
  44. bar: [
  45. {
  46. fruit: 'apple',
  47. name: 'steve',
  48. sport: 'baseball'
  49. }, 'more', {
  50. python: 'rocks',
  51. perl: 'papers',
  52. ruby: 'scissorses'
  53. }
  54. ]
  55. });
  56. });
  57. it('can have mapping-in-sequence shortcut', function() {
  58. return expect(YAML.parse("- work on YAML.py:\n - work on Store")).toEqual([
  59. {
  60. 'work on YAML.py': ['work on Store']
  61. }
  62. ]);
  63. });
  64. it('can have unindented sequence-in-mapping shortcut', function() {
  65. return expect(YAML.parse("allow:\n- 'localhost'\n- '%.sourceforge.net'\n- '%.freepan.org'")).toEqual({
  66. allow: ['localhost', '%.sourceforge.net', '%.freepan.org']
  67. });
  68. });
  69. it('can merge key', function() {
  70. return expect(YAML.parse("mapping:\n name: Joe\n job: Accountant\n <<:\n age: 38")).toEqual({
  71. mapping: {
  72. name: 'Joe',
  73. job: 'Accountant',
  74. age: 38
  75. }
  76. });
  77. });
  78. return it('can ignore trailing empty lines for smallest indent', function() {
  79. return expect(YAML.parse(" trailing: empty lines\n")).toEqual({
  80. trailing: 'empty lines'
  81. });
  82. });
  83. });
  84. describe('Parsed YAML Inline Collections', function() {
  85. it('can be simple inline array', function() {
  86. return expect(YAML.parse("---\nseq: [ a, b, c ]")).toEqual({
  87. seq: ['a', 'b', 'c']
  88. });
  89. });
  90. it('can be simple inline hash', function() {
  91. return expect(YAML.parse("---\nhash: { name: Steve, foo: bar }")).toEqual({
  92. hash: {
  93. name: 'Steve',
  94. foo: 'bar'
  95. }
  96. });
  97. });
  98. it('can be nested inline hash', function() {
  99. return expect(YAML.parse("---\nhash: { val1: \"string\", val2: { v2k1: \"v2k1v\" } }")).toEqual({
  100. hash: {
  101. val1: 'string',
  102. val2: {
  103. v2k1: 'v2k1v'
  104. }
  105. }
  106. });
  107. });
  108. return it('can be multi-line inline collections', function() {
  109. return expect(YAML.parse("languages: [ Ruby,\n Perl,\n Python ]\nwebsites: { YAML: yaml.org,\n Ruby: ruby-lang.org,\n Python: python.org,\n Perl: use.perl.org }")).toEqual({
  110. languages: ['Ruby', 'Perl', 'Python'],
  111. websites: {
  112. YAML: 'yaml.org',
  113. Ruby: 'ruby-lang.org',
  114. Python: 'python.org',
  115. Perl: 'use.perl.org'
  116. }
  117. });
  118. });
  119. });
  120. describe('Parsed YAML Basic Types', function() {
  121. it('can be strings', function() {
  122. return expect(YAML.parse("---\nString")).toEqual('String');
  123. });
  124. it('can be double-quoted strings with backslashes', function() {
  125. return expect(YAML.parse("str:\n \"string with \\\\ inside\"")).toEqual({
  126. str: 'string with \\ inside'
  127. });
  128. });
  129. it('can be single-quoted strings with backslashes', function() {
  130. return expect(YAML.parse("str:\n 'string with \\\\ inside'")).toEqual({
  131. str: 'string with \\\\ inside'
  132. });
  133. });
  134. it('can be double-quoted strings with line breaks', function() {
  135. return expect(YAML.parse("str:\n \"string with \\n inside\"")).toEqual({
  136. str: 'string with \n inside'
  137. });
  138. });
  139. it('can be single-quoted strings with escaped line breaks', function() {
  140. return expect(YAML.parse("str:\n 'string with \\n inside'")).toEqual({
  141. str: 'string with \\n inside'
  142. });
  143. });
  144. it('can be double-quoted strings with line breaks and backslashes', function() {
  145. return expect(YAML.parse("str:\n \"string with \\n inside and \\\\ also\"")).toEqual({
  146. str: 'string with \n inside and \\ also'
  147. });
  148. });
  149. it('can be single-quoted strings with line breaks and backslashes', function() {
  150. return expect(YAML.parse("str:\n 'string with \\n inside and \\\\ also'")).toEqual({
  151. str: 'string with \\n inside and \\\\ also'
  152. });
  153. });
  154. it('can have string characters in sequences', function() {
  155. return expect(YAML.parse("- What's Yaml?\n- It's for writing data structures in plain text.\n- And?\n- And what? That's not good enough for you?\n- No, I mean, \"And what about Yaml?\"\n- 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."]);
  156. });
  157. it('can have indicators in strings', function() {
  158. return expect(YAML.parse("the colon followed by space is an indicator: but is a string:right here\nsame for the pound sign: here we have it#in a string\nthe comma can, honestly, be used in most cases: [ but not in, inline collections ]")).toEqual({
  159. 'the colon followed by space is an indicator': 'but is a string:right here',
  160. 'same for the pound sign': 'here we have it#in a string',
  161. 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
  162. });
  163. });
  164. it('can force strings', function() {
  165. return expect(YAML.parse("date string: !str 2001-08-01\nnumber string: !str 192\ndate string 2: !!str 2001-08-01\nnumber string 2: !!str 192")).toEqual({
  166. 'date string': '2001-08-01',
  167. 'number string': '192',
  168. 'date string 2': '2001-08-01',
  169. 'number string 2': '192'
  170. });
  171. });
  172. it('can be single-quoted strings', function() {
  173. return expect(YAML.parse("all my favorite symbols: '#:!/%.)'\na few i hate: '&(*'\nwhy do i hate them?: 'it''s very hard to explain'")).toEqual({
  174. 'all my favorite symbols': '#:!/%.)',
  175. 'a few i hate': '&(*',
  176. 'why do i hate them?': 'it\'s very hard to explain'
  177. });
  178. });
  179. it('can be double-quoted strings', function() {
  180. return expect(YAML.parse("i know where i want my line breaks: \"one here\\nand another here\\n\"")).toEqual({
  181. 'i know where i want my line breaks': "one here\nand another here\n"
  182. });
  183. });
  184. it('can be null', function() {
  185. return expect(YAML.parse("name: Mr. Show\nhosted by: Bob and David\ndate of next season: ~")).toEqual({
  186. 'name': 'Mr. Show',
  187. 'hosted by': 'Bob and David',
  188. 'date of next season': null
  189. });
  190. });
  191. it('can be boolean', function() {
  192. return expect(YAML.parse("Is Gus a Liar?: true\nDo I rely on Gus for Sustenance?: false")).toEqual({
  193. 'Is Gus a Liar?': true,
  194. 'Do I rely on Gus for Sustenance?': false
  195. });
  196. });
  197. it('can be integers', function() {
  198. return expect(YAML.parse("zero: 0\nsimple: 12\none-thousand: 1,000\nnegative one-thousand: -1,000")).toEqual({
  199. 'zero': 0,
  200. 'simple': 12,
  201. 'one-thousand': 1000,
  202. 'negative one-thousand': -1000
  203. });
  204. });
  205. it('can be integers as map keys', function() {
  206. return expect(YAML.parse("1: one\n2: two\n3: three")).toEqual({
  207. 1: 'one',
  208. 2: 'two',
  209. 3: 'three'
  210. });
  211. });
  212. it('can be floats', function() {
  213. return expect(YAML.parse("a simple float: 2.00\nlarger float: 1,000.09\nscientific notation: 1.00009e+3")).toEqual({
  214. 'a simple float': 2.0,
  215. 'larger float': 1000.09,
  216. 'scientific notation': 1000.09
  217. });
  218. });
  219. it('can be time', function() {
  220. var iso8601Date, spaceSeparatedDate, withDatesToTime;
  221. iso8601Date = new Date(Date.UTC(2001, 12 - 1, 14, 21, 59, 43, 10));
  222. iso8601Date.setTime(iso8601Date.getTime() - 5 * 3600 * 1000);
  223. spaceSeparatedDate = new Date(Date.UTC(2001, 12 - 1, 14, 21, 59, 43, 10));
  224. spaceSeparatedDate.setTime(spaceSeparatedDate.getTime() + 5 * 3600 * 1000);
  225. withDatesToTime = function(input) {
  226. var key, res, val;
  227. res = {};
  228. for (key in input) {
  229. val = input[key];
  230. res[key] = val.getTime();
  231. }
  232. return res;
  233. };
  234. return expect(withDatesToTime(YAML.parse("iso8601: 2001-12-14t21:59:43.010+05:00\nspace separated: 2001-12-14 21:59:43.010 -05:00"))).toEqual(withDatesToTime({
  235. 'iso8601': iso8601Date,
  236. 'space separated': spaceSeparatedDate
  237. }));
  238. });
  239. return it('can be date', function() {
  240. var aDate, withDatesToTime;
  241. aDate = new Date(Date.UTC(1976, 7 - 1, 31, 0, 0, 0, 0));
  242. withDatesToTime = function(input) {
  243. var key, res, val;
  244. return input;
  245. res = {};
  246. for (key in input) {
  247. val = input[key];
  248. res[key] = val.getTime();
  249. }
  250. return res;
  251. };
  252. return expect(withDatesToTime(YAML.parse("date: 1976-07-31"))).toEqual(withDatesToTime({
  253. 'date': aDate
  254. }));
  255. });
  256. });
  257. describe('Parsed YAML Blocks', function() {
  258. it('can be single ending newline', function() {
  259. return expect(YAML.parse("---\nthis: |\n Foo\n Bar")).toEqual({
  260. 'this': "Foo\nBar\n"
  261. });
  262. });
  263. it('can be single ending newline with \'+\' indicator', function() {
  264. return expect(YAML.parse("normal: |\n extra new lines not kept\n\npreserving: |+\n extra new lines are kept\n\n\ndummy: value")).toEqual({
  265. 'normal': "extra new lines not kept\n",
  266. 'preserving': "extra new lines are kept\n\n\n",
  267. 'dummy': 'value'
  268. });
  269. });
  270. it('can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', function() {
  271. return expect(YAML.parse("clipped: |\n This has one newline.\n\n\n\nsame as \"clipped\" above: \"This has one newline.\\n\"\n\nstripped: |-\n This has no newline.\n\n\n\nsame as \"stripped\" above: \"This has no newline.\"\n\nkept: |+\n This has four newlines.\n\n\n\nsame as \"kept\" above: \"This has four newlines.\\n\\n\\n\\n\"")).toEqual({
  272. 'clipped': "This has one newline.\n",
  273. 'same as "clipped" above': "This has one newline.\n",
  274. 'stripped': 'This has no newline.',
  275. 'same as "stripped" above': 'This has no newline.',
  276. 'kept': "This has four newlines.\n\n\n\n",
  277. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  278. });
  279. });
  280. it('can be folded block in a sequence', function() {
  281. return expect(YAML.parse("---\n- apple\n- banana\n- >\n can't you see\n the beauty of yaml?\n hmm\n- dog")).toEqual(['apple', 'banana', "can't you see the beauty of yaml? hmm\n", 'dog']);
  282. });
  283. it('can be folded block as a mapping value', function() {
  284. return expect(YAML.parse("---\nquote: >\n Mark McGwire's\n year was crippled\n by a knee injury.\nsource: espn")).toEqual({
  285. 'quote': "Mark McGwire's year was crippled by a knee injury.\n",
  286. 'source': 'espn'
  287. });
  288. });
  289. it('can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', function() {
  290. return expect(YAML.parse("clipped: >\n This has one newline.\n\n\n\nsame as \"clipped\" above: \"This has one newline.\\n\"\n\nstripped: >-\n This has no newline.\n\n\n\nsame as \"stripped\" above: \"This has no newline.\"\n\nkept: >+\n This has four newlines.\n\n\n\nsame as \"kept\" above: \"This has four newlines.\\n\\n\\n\\n\"")).toEqual({
  291. 'clipped': "This has one newline.\n",
  292. 'same as "clipped" above': "This has one newline.\n",
  293. 'stripped': 'This has no newline.',
  294. 'same as "stripped" above': 'This has no newline.',
  295. 'kept': "This has four newlines.\n\n\n\n",
  296. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  297. });
  298. });
  299. return it('can be the whole document as intented block', function() {
  300. return expect(YAML.parse("---\n foo: \"bar\"\n baz:\n - \"qux\"\n - \"quxx\"\n corge: null")).toEqual({
  301. 'foo': "bar",
  302. 'baz': ['qux', 'quxx'],
  303. 'corge': null
  304. });
  305. });
  306. });
  307. describe('Parsed YAML Comments', function() {
  308. it('can begin the document', function() {
  309. return expect(YAML.parse("# This is a comment\nhello: world")).toEqual({
  310. hello: 'world'
  311. });
  312. });
  313. it('can be less indented in mapping', function() {
  314. return expect(YAML.parse("parts:\n a: 'b'\n # normally indented comment\n c: 'd'\n# less indented comment\n e: 'f'")).toEqual({
  315. parts: {
  316. a: 'b',
  317. c: 'd',
  318. e: 'f'
  319. }
  320. });
  321. });
  322. it('can be less indented in sequence', function() {
  323. return expect(YAML.parse("list-header:\n - item1\n# - item2\n - item3\n # - item4")).toEqual({
  324. 'list-header': ['item1', 'item3']
  325. });
  326. });
  327. it('can finish a line', function() {
  328. return expect(YAML.parse("hello: world # This is a comment")).toEqual({
  329. hello: 'world'
  330. });
  331. });
  332. return it('can end the document', function() {
  333. return expect(YAML.parse("hello: world\n# This is a comment")).toEqual({
  334. hello: 'world'
  335. });
  336. });
  337. });
  338. describe('Parsed YAML Aliases and Anchors', function() {
  339. it('can be simple alias', function() {
  340. return expect(YAML.parse("- &showell Steve\n- Clark\n- Brian\n- Oren\n- *showell")).toEqual(['Steve', 'Clark', 'Brian', 'Oren', 'Steve']);
  341. });
  342. return it('can be alias of a mapping', function() {
  343. return expect(YAML.parse("- &hello\n Meat: pork\n Starch: potato\n- banana\n- *hello")).toEqual([
  344. {
  345. Meat: 'pork',
  346. Starch: 'potato'
  347. }, 'banana', {
  348. Meat: 'pork',
  349. Starch: 'potato'
  350. }
  351. ]);
  352. });
  353. });
  354. describe('Parsed YAML Documents', function() {
  355. it('can have YAML header', function() {
  356. return expect(YAML.parse("--- %YAML:1.0\nfoo: 1\nbar: 2")).toEqual({
  357. foo: 1,
  358. bar: 2
  359. });
  360. });
  361. it('can have leading document separator', function() {
  362. return expect(YAML.parse("---\n- foo: 1\n bar: 2")).toEqual([
  363. {
  364. foo: 1,
  365. bar: 2
  366. }
  367. ]);
  368. });
  369. return it('can have multiple document separators in block', function() {
  370. return expect(YAML.parse("foo: |\n ---\n foo: bar\n ---\n yo: baz\nbar: |\n fooness")).toEqual({
  371. foo: "---\nfoo: bar\n---\nyo: baz\n",
  372. bar: "fooness\n"
  373. });
  374. });
  375. });
  376. describe('Dumped YAML Collections', function() {
  377. it('can be simple sequence', function() {
  378. return expect(YAML.parse("- apple\n- banana\n- carrot")).toEqual(YAML.parse(YAML.dump(['apple', 'banana', 'carrot'])));
  379. });
  380. it('can be nested sequences', function() {
  381. return expect(YAML.parse("-\n - foo\n - bar\n - baz")).toEqual(YAML.parse(YAML.dump([['foo', 'bar', 'baz']])));
  382. });
  383. it('can be mixed sequences', function() {
  384. return expect(YAML.parse("- apple\n-\n - foo\n - bar\n - x123\n- banana\n- carrot")).toEqual(YAML.parse(YAML.dump(['apple', ['foo', 'bar', 'x123'], 'banana', 'carrot'])));
  385. });
  386. it('can be deeply nested sequences', function() {
  387. return expect(YAML.parse("-\n -\n - uno\n - dos")).toEqual(YAML.parse(YAML.dump([[['uno', 'dos']]])));
  388. });
  389. it('can be simple mapping', function() {
  390. return expect(YAML.parse("foo: whatever\nbar: stuff")).toEqual(YAML.parse(YAML.dump({
  391. foo: 'whatever',
  392. bar: 'stuff'
  393. })));
  394. });
  395. it('can be sequence in a mapping', function() {
  396. return expect(YAML.parse("foo: whatever\nbar:\n - uno\n - dos")).toEqual(YAML.parse(YAML.dump({
  397. foo: 'whatever',
  398. bar: ['uno', 'dos']
  399. })));
  400. });
  401. it('can be nested mappings', function() {
  402. return expect(YAML.parse("foo: whatever\nbar:\n fruit: apple\n name: steve\n sport: baseball")).toEqual(YAML.parse(YAML.dump({
  403. foo: 'whatever',
  404. bar: {
  405. fruit: 'apple',
  406. name: 'steve',
  407. sport: 'baseball'
  408. }
  409. })));
  410. });
  411. it('can be mixed mapping', function() {
  412. return expect(YAML.parse("foo: whatever\nbar:\n -\n fruit: apple\n name: steve\n sport: baseball\n - more\n -\n python: rocks\n perl: papers\n ruby: scissorses")).toEqual(YAML.parse(YAML.dump({
  413. foo: 'whatever',
  414. bar: [
  415. {
  416. fruit: 'apple',
  417. name: 'steve',
  418. sport: 'baseball'
  419. }, 'more', {
  420. python: 'rocks',
  421. perl: 'papers',
  422. ruby: 'scissorses'
  423. }
  424. ]
  425. })));
  426. });
  427. it('can have mapping-in-sequence shortcut', function() {
  428. return expect(YAML.parse("- work on YAML.py:\n - work on Store")).toEqual(YAML.parse(YAML.dump([
  429. {
  430. 'work on YAML.py': ['work on Store']
  431. }
  432. ])));
  433. });
  434. it('can have unindented sequence-in-mapping shortcut', function() {
  435. return expect(YAML.parse("allow:\n- 'localhost'\n- '%.sourceforge.net'\n- '%.freepan.org'")).toEqual(YAML.parse(YAML.dump({
  436. allow: ['localhost', '%.sourceforge.net', '%.freepan.org']
  437. })));
  438. });
  439. return it('can merge key', function() {
  440. return expect(YAML.parse("mapping:\n name: Joe\n job: Accountant\n <<:\n age: 38")).toEqual(YAML.parse(YAML.dump({
  441. mapping: {
  442. name: 'Joe',
  443. job: 'Accountant',
  444. age: 38
  445. }
  446. })));
  447. });
  448. });
  449. describe('Dumped YAML Inline Collections', function() {
  450. it('can be simple inline array', function() {
  451. return expect(YAML.parse("---\nseq: [ a, b, c ]")).toEqual(YAML.parse(YAML.dump({
  452. seq: ['a', 'b', 'c']
  453. })));
  454. });
  455. it('can be simple inline hash', function() {
  456. return expect(YAML.parse("---\nhash: { name: Steve, foo: bar }")).toEqual(YAML.parse(YAML.dump({
  457. hash: {
  458. name: 'Steve',
  459. foo: 'bar'
  460. }
  461. })));
  462. });
  463. it('can be multi-line inline collections', function() {
  464. return expect(YAML.parse("languages: [ Ruby,\n Perl,\n Python ]\nwebsites: { YAML: yaml.org,\n Ruby: ruby-lang.org,\n Python: python.org,\n Perl: use.perl.org }")).toEqual(YAML.parse(YAML.dump({
  465. languages: ['Ruby', 'Perl', 'Python'],
  466. websites: {
  467. YAML: 'yaml.org',
  468. Ruby: 'ruby-lang.org',
  469. Python: 'python.org',
  470. Perl: 'use.perl.org'
  471. }
  472. })));
  473. });
  474. it('can be dumped empty sequences in mappings', function() {
  475. return expect(YAML.parse(YAML.dump({
  476. key: []
  477. }))).toEqual({
  478. key: []
  479. });
  480. });
  481. return it('can be dumpted empty inline collections', function() {
  482. return expect(YAML.parse(YAML.dump({
  483. key: {}
  484. }))).toEqual({
  485. key: {}
  486. });
  487. });
  488. });
  489. describe('Dumped YAML Basic Types', function() {
  490. it('can be strings', function() {
  491. return expect(YAML.parse("---\nString")).toEqual(YAML.parse(YAML.dump('String')));
  492. });
  493. it('can be double-quoted strings with backslashes', function() {
  494. return expect(YAML.parse("str:\n \"string with \\\\ inside\"")).toEqual(YAML.parse(YAML.dump({
  495. str: 'string with \\ inside'
  496. })));
  497. });
  498. it('can be single-quoted strings with backslashes', function() {
  499. return expect(YAML.parse("str:\n 'string with \\\\ inside'")).toEqual(YAML.parse(YAML.dump({
  500. str: 'string with \\\\ inside'
  501. })));
  502. });
  503. it('can be double-quoted strings with line breaks', function() {
  504. return expect(YAML.parse("str:\n \"string with \\n inside\"")).toEqual(YAML.parse(YAML.dump({
  505. str: 'string with \n inside'
  506. })));
  507. });
  508. it('can be double-quoted strings with line breaks and backslashes', function() {
  509. return expect(YAML.parse("str:\n \"string with \\n inside and \\\\ also\"")).toEqual(YAML.parse(YAML.dump({
  510. str: 'string with \n inside and \\ also'
  511. })));
  512. });
  513. it('can be single-quoted strings with line breaks and backslashes', function() {
  514. return expect(YAML.parse("str:\n 'string with \\n inside and \\\\ also'")).toEqual(YAML.parse(YAML.dump({
  515. str: 'string with \\n inside and \\\\ also'
  516. })));
  517. });
  518. it('can be single-quoted strings with escaped line breaks', function() {
  519. return expect(YAML.parse("str:\n 'string with \\n inside'")).toEqual(YAML.parse(YAML.dump({
  520. str: 'string with \\n inside'
  521. })));
  522. });
  523. it('can have string characters in sequences', function() {
  524. return expect(YAML.parse("- What's Yaml?\n- It's for writing data structures in plain text.\n- And?\n- And what? That's not good enough for you?\n- No, I mean, \"And what about Yaml?\"\n- 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."])));
  525. });
  526. it('can have indicators in strings', function() {
  527. return expect(YAML.parse("the colon followed by space is an indicator: but is a string:right here\nsame for the pound sign: here we have it#in a string\nthe comma can, honestly, be used in most cases: [ but not in, inline collections ]")).toEqual(YAML.parse(YAML.dump({
  528. 'the colon followed by space is an indicator': 'but is a string:right here',
  529. 'same for the pound sign': 'here we have it#in a string',
  530. 'the comma can, honestly, be used in most cases': ['but not in', 'inline collections']
  531. })));
  532. });
  533. it('can force strings', function() {
  534. return expect(YAML.parse("date string: !str 2001-08-01\nnumber string: !str 192\ndate string 2: !!str 2001-08-01\nnumber string 2: !!str 192")).toEqual(YAML.parse(YAML.dump({
  535. 'date string': '2001-08-01',
  536. 'number string': '192',
  537. 'date string 2': '2001-08-01',
  538. 'number string 2': '192'
  539. })));
  540. });
  541. it('can be single-quoted strings', function() {
  542. return expect(YAML.parse("all my favorite symbols: '#:!/%.)'\na few i hate: '&(*'\nwhy do i hate them?: 'it''s very hard to explain'")).toEqual(YAML.parse(YAML.dump({
  543. 'all my favorite symbols': '#:!/%.)',
  544. 'a few i hate': '&(*',
  545. 'why do i hate them?': 'it\'s very hard to explain'
  546. })));
  547. });
  548. it('can be double-quoted strings', function() {
  549. return expect(YAML.parse("i know where i want my line breaks: \"one here\\nand another here\\n\"")).toEqual(YAML.parse(YAML.dump({
  550. 'i know where i want my line breaks': "one here\nand another here\n"
  551. })));
  552. });
  553. it('can be null', function() {
  554. return expect(YAML.parse("name: Mr. Show\nhosted by: Bob and David\ndate of next season: ~")).toEqual(YAML.parse(YAML.dump({
  555. 'name': 'Mr. Show',
  556. 'hosted by': 'Bob and David',
  557. 'date of next season': null
  558. })));
  559. });
  560. it('can be boolean', function() {
  561. return expect(YAML.parse("Is Gus a Liar?: true\nDo I rely on Gus for Sustenance?: false")).toEqual(YAML.parse(YAML.dump({
  562. 'Is Gus a Liar?': true,
  563. 'Do I rely on Gus for Sustenance?': false
  564. })));
  565. });
  566. it('can be integers', function() {
  567. return expect(YAML.parse("zero: 0\nsimple: 12\none-thousand: 1,000\nnegative one-thousand: -1,000")).toEqual(YAML.parse(YAML.dump({
  568. 'zero': 0,
  569. 'simple': 12,
  570. 'one-thousand': 1000,
  571. 'negative one-thousand': -1000
  572. })));
  573. });
  574. it('can be integers as map keys', function() {
  575. return expect(YAML.parse("1: one\n2: two\n3: three")).toEqual(YAML.parse(YAML.dump({
  576. 1: 'one',
  577. 2: 'two',
  578. 3: 'three'
  579. })));
  580. });
  581. it('can be floats', function() {
  582. return expect(YAML.parse("a simple float: 2.00\nlarger float: 1,000.09\nscientific notation: 1.00009e+3")).toEqual(YAML.parse(YAML.dump({
  583. 'a simple float': 2.0,
  584. 'larger float': 1000.09,
  585. 'scientific notation': 1000.09
  586. })));
  587. });
  588. it('can be time', function() {
  589. var iso8601Date, spaceSeparatedDate, withDatesToTime;
  590. iso8601Date = new Date(Date.UTC(2001, 12 - 1, 14, 21, 59, 43, 10));
  591. iso8601Date.setTime(iso8601Date.getTime() + 5 * 3600 * 1000);
  592. spaceSeparatedDate = new Date(Date.UTC(2001, 12 - 1, 14, 21, 59, 43, 10));
  593. spaceSeparatedDate.setTime(spaceSeparatedDate.getTime() - 5 * 3600 * 1000);
  594. withDatesToTime = function(input) {
  595. var key, res, val;
  596. res = {};
  597. for (key in input) {
  598. val = input[key];
  599. res[key] = val.getTime();
  600. }
  601. return res;
  602. };
  603. return expect(withDatesToTime(YAML.parse("iso8601: 2001-12-14t21:59:43.010-05:00\nspace separated: 2001-12-14 21:59:43.010 +05:00"))).toEqual(YAML.parse(YAML.dump(withDatesToTime({
  604. 'iso8601': iso8601Date,
  605. 'space separated': spaceSeparatedDate
  606. }))));
  607. });
  608. return it('can be date', function() {
  609. var aDate, withDatesToTime;
  610. aDate = new Date(Date.UTC(1976, 7 - 1, 31, 0, 0, 0, 0));
  611. withDatesToTime = function(input) {
  612. var key, res, val;
  613. return input;
  614. res = {};
  615. for (key in input) {
  616. val = input[key];
  617. res[key] = val.getTime();
  618. }
  619. return res;
  620. };
  621. return expect(withDatesToTime(YAML.parse("date: 1976-07-31"))).toEqual(YAML.parse(YAML.dump(withDatesToTime({
  622. 'date': aDate
  623. }))));
  624. });
  625. });
  626. describe('Dumped YAML Blocks', function() {
  627. it('can be single ending newline', function() {
  628. return expect(YAML.parse("---\nthis: |\n Foo\n Bar")).toEqual(YAML.parse(YAML.dump({
  629. 'this': "Foo\nBar\n"
  630. })));
  631. });
  632. it('can be single ending newline with \'+\' indicator', function() {
  633. return expect(YAML.parse("normal: |\n extra new lines not kept\n\npreserving: |+\n extra new lines are kept\n\n\ndummy: value")).toEqual(YAML.parse(YAML.dump({
  634. 'normal': "extra new lines not kept\n",
  635. 'preserving': "extra new lines are kept\n\n\n",
  636. 'dummy': 'value'
  637. })));
  638. });
  639. it('can be multi-line block handling trailing newlines in function of \'+\', \'-\' indicators', function() {
  640. return expect(YAML.parse("clipped: |\n This has one newline.\n\n\n\nsame as \"clipped\" above: \"This has one newline.\\n\"\n\nstripped: |-\n This has no newline.\n\n\n\nsame as \"stripped\" above: \"This has no newline.\"\n\nkept: |+\n This has four newlines.\n\n\n\nsame as \"kept\" above: \"This has four newlines.\\n\\n\\n\\n\"")).toEqual(YAML.parse(YAML.dump({
  641. 'clipped': "This has one newline.\n",
  642. 'same as "clipped" above': "This has one newline.\n",
  643. 'stripped': 'This has no newline.',
  644. 'same as "stripped" above': 'This has no newline.',
  645. 'kept': "This has four newlines.\n\n\n\n",
  646. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  647. })));
  648. });
  649. it('can be folded block in a sequence', function() {
  650. return expect(YAML.parse("---\n- apple\n- banana\n- >\n can't you see\n the beauty of yaml?\n hmm\n- dog")).toEqual(YAML.parse(YAML.dump(['apple', 'banana', "can't you see the beauty of yaml? hmm\n", 'dog'])));
  651. });
  652. it('can be folded block as a mapping value', function() {
  653. return expect(YAML.parse("---\nquote: >\n Mark McGwire's\n year was crippled\n by a knee injury.\nsource: espn")).toEqual(YAML.parse(YAML.dump({
  654. 'quote': "Mark McGwire's year was crippled by a knee injury.\n",
  655. 'source': 'espn'
  656. })));
  657. });
  658. return it('can be folded block handling trailing newlines in function of \'+\', \'-\' indicators', function() {
  659. return expect(YAML.parse("clipped: >\n This has one newline.\n\n\n\nsame as \"clipped\" above: \"This has one newline.\\n\"\n\nstripped: >-\n This has no newline.\n\n\n\nsame as \"stripped\" above: \"This has no newline.\"\n\nkept: >+\n This has four newlines.\n\n\n\nsame as \"kept\" above: \"This has four newlines.\\n\\n\\n\\n\"")).toEqual(YAML.parse(YAML.dump({
  660. 'clipped': "This has one newline.\n",
  661. 'same as "clipped" above': "This has one newline.\n",
  662. 'stripped': 'This has no newline.',
  663. 'same as "stripped" above': 'This has no newline.',
  664. 'kept': "This has four newlines.\n\n\n\n",
  665. 'same as "kept" above': "This has four newlines.\n\n\n\n"
  666. })));
  667. });
  668. });
  669. describe('Dumped YAML Comments', function() {
  670. it('can begin the document', function() {
  671. return expect(YAML.parse("# This is a comment\nhello: world")).toEqual(YAML.parse(YAML.dump({
  672. hello: 'world'
  673. })));
  674. });
  675. it('can finish a line', function() {
  676. return expect(YAML.parse("hello: world # This is a comment")).toEqual(YAML.parse(YAML.dump({
  677. hello: 'world'
  678. })));
  679. });
  680. return it('can end the document', function() {
  681. return expect(YAML.parse("hello: world\n# This is a comment")).toEqual(YAML.parse(YAML.dump({
  682. hello: 'world'
  683. })));
  684. });
  685. });
  686. describe('Dumped YAML Aliases and Anchors', function() {
  687. it('can be simple alias', function() {
  688. return expect(YAML.parse("- &showell Steve\n- Clark\n- Brian\n- Oren\n- *showell")).toEqual(YAML.parse(YAML.dump(['Steve', 'Clark', 'Brian', 'Oren', 'Steve'])));
  689. });
  690. return it('can be alias of a mapping', function() {
  691. return expect(YAML.parse("- &hello\n Meat: pork\n Starch: potato\n- banana\n- *hello")).toEqual(YAML.parse(YAML.dump([
  692. {
  693. Meat: 'pork',
  694. Starch: 'potato'
  695. }, 'banana', {
  696. Meat: 'pork',
  697. Starch: 'potato'
  698. }
  699. ])));
  700. });
  701. });
  702. describe('Dumped YAML Documents', function() {
  703. it('can have YAML header', function() {
  704. return expect(YAML.parse("--- %YAML:1.0\nfoo: 1\nbar: 2")).toEqual(YAML.parse(YAML.dump({
  705. foo: 1,
  706. bar: 2
  707. })));
  708. });
  709. it('can have leading document separator', function() {
  710. return expect(YAML.parse("---\n- foo: 1\n bar: 2")).toEqual(YAML.parse(YAML.dump([
  711. {
  712. foo: 1,
  713. bar: 2
  714. }
  715. ])));
  716. });
  717. return it('can have multiple document separators in block', function() {
  718. return expect(YAML.parse("foo: |\n ---\n foo: bar\n ---\n yo: baz\nbar: |\n fooness")).toEqual(YAML.parse(YAML.dump({
  719. foo: "---\nfoo: bar\n---\nyo: baz\n",
  720. bar: "fooness\n"
  721. })));
  722. });
  723. });
  724. url = typeof document !== "undefined" && document !== null ? (ref = document.location) != null ? ref.href : void 0 : void 0;
  725. if (!(url != null) || url.indexOf('file://') === -1) {
  726. examplePath = 'spec/example.yml';
  727. if (typeof __dirname !== "undefined" && __dirname !== null) {
  728. examplePath = __dirname + '/example.yml';
  729. }
  730. describe('YAML loading', function() {
  731. it('can be done synchronously', function() {
  732. return expect(YAML.load(examplePath)).toEqual({
  733. "this": 'is',
  734. a: ['YAML', 'example']
  735. });
  736. });
  737. return it('can be done asynchronously', function(done) {
  738. return YAML.load(examplePath, function(result) {
  739. expect(result).toEqual({
  740. "this": 'is',
  741. a: ['YAML', 'example']
  742. });
  743. return done();
  744. });
  745. });
  746. });
  747. }