js-yaml-front.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var should = require('should');
  2. describe('js-yaml-front', function() {
  3. var jsYaml = require('../lib/js-yaml-front')
  4. , fs = require('fs')
  5. , results
  6. , testStr = '---\npost: title one\nanArray:\n - one\n - two\nsubObject:\n obj1: cool\n obj2: two';
  7. testStr += '\nreg: !!js/regexp /pattern/gim';
  8. testStr += '\nfun: !!js/function function() { }\n---\ncontent\nmore';
  9. var testJsonStr = '---\n{"post": "title one",\n"anArray": ["one","two"],\n"subObject":\n';
  10. testJsonStr += '{"obj1": "cool", "obj2": "two"}}\n---\ncontent\nmore';
  11. beforeEach(function() {
  12. results = null;
  13. });
  14. var test = function() {
  15. results.should.have.property('post', 'title one');
  16. results.should.have.property('anArray');
  17. results.anArray.should.include('one');
  18. results.anArray.should.include('two');
  19. results.should.have.property('subObject');
  20. results.subObject.should.have.property('obj1', 'cool');
  21. results.subObject.should.have.property('obj2');
  22. results.should.have.property('reg');
  23. results.reg.should.be.an.instanceOf(RegExp);
  24. results.should.have.property('fun');
  25. results.fun.should.be.a('function');
  26. };
  27. var testJson = function() {
  28. results.should.have.property('post', 'title one');
  29. results.should.have.property('anArray');
  30. results.anArray.should.include('one');
  31. results.anArray.should.include('two');
  32. results.should.have.property('subObject');
  33. results.subObject.should.have.property('obj1', 'cool');
  34. results.subObject.should.have.property('obj2', 'two');
  35. };
  36. describe('parse', function() {
  37. it('should parse yaml at the front of a file', function() {
  38. results = jsYaml.parse(testStr);
  39. test();
  40. });
  41. }); // End describe parse
  42. describe('loadFront', function() {
  43. it('should load a string|buffer|file and return an object', function() {
  44. var buf = new Buffer(testStr);
  45. fs.writeFileSync('test/fixtures/testFile.html', testStr);
  46. results = jsYaml.loadFront('test/fixtures/testFile.html');
  47. test();
  48. results = jsYaml.loadFront(buf);
  49. test();
  50. results = jsYaml.loadFront(testStr);
  51. test();
  52. });
  53. }); // End describe loadFront
  54. describe('incorrect parse', function () {
  55. it('should return an object with just __content', function () {
  56. results = jsYaml.parse('Hello World');
  57. results.should.have.property('__content', 'Hello World');
  58. });
  59. });
  60. describe('Parsing Json', function() {
  61. it('should parse JSON at the start of the file', function() {
  62. results = jsYaml.parse(testJsonStr);
  63. testJson();
  64. });
  65. });
  66. describe('loadFront with JSON', function() {
  67. it('should load a string|buffer|file and return an object', function() {
  68. var buf = new Buffer(testStr);
  69. fs.writeFileSync('test/fixtures/testFile.html', testJsonStr);
  70. results = jsYaml.loadFront('test/fixtures/testFile.html');
  71. testJson();
  72. results = jsYaml.loadFront(buf);
  73. testJson();
  74. results = jsYaml.loadFront(testStr);
  75. testJson();
  76. });
  77. }); // End describe loadFront
  78. describe('extra dashes', function() {
  79. it('should handle three (or more) dashes within the content', function() {
  80. results = jsYaml.loadFront('test/fixtures/testExtraDashes.html');
  81. test();
  82. });
  83. });
  84. });// End describe js-yaml-front