mustache-spec-test.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. require('./helper');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var specsDir = path.join(__dirname, 'spec/specs');
  5. var skipTests = {
  6. comments: [
  7. 'Standalone Without Newline'
  8. ],
  9. delimiters: [
  10. 'Standalone Without Newline'
  11. ],
  12. inverted: [
  13. 'Standalone Without Newline'
  14. ],
  15. partials: [
  16. 'Standalone Without Previous Line',
  17. 'Standalone Without Newline'
  18. ],
  19. sections: [
  20. 'Standalone Without Newline'
  21. ],
  22. '~lambdas': [
  23. 'Interpolation',
  24. 'Interpolation - Expansion',
  25. 'Interpolation - Alternate Delimiters',
  26. 'Interpolation - Multiple Calls',
  27. 'Escaping',
  28. 'Section - Expansion',
  29. 'Section - Alternate Delimiters'
  30. ]
  31. };
  32. // You can run the skipped tests by setting the NOSKIP environment variable to
  33. // true (e.g. NOSKIP=true mocha test/mustache-spec-test.js)
  34. var noSkip = process.env.NOSKIP;
  35. // You can put the name of a specific test file to run in the TEST environment
  36. // variable (e.g. TEST=interpolation mocha test/mustache-spec-test.js)
  37. var fileToRun = process.env.TEST;
  38. // Mustache should work on node 0.6 that doesn't have fs.existsSync
  39. function existsDir (path) {
  40. try {
  41. return fs.statSync(path).isDirectory();
  42. } catch (x) {
  43. return false;
  44. }
  45. }
  46. var specFiles;
  47. if (fileToRun) {
  48. specFiles = [fileToRun];
  49. } else if (existsDir(specsDir)) {
  50. specFiles = fs.readdirSync(specsDir).filter(function (file) {
  51. return (/\.json$/).test(file);
  52. }).map(function (file) {
  53. return path.basename(file).replace(/\.json$/, '');
  54. }).sort();
  55. } else {
  56. specFiles = [];
  57. }
  58. function getSpecs (specArea) {
  59. return JSON.parse(fs.readFileSync(path.join(specsDir, specArea + '.' + 'json'), 'utf8'));
  60. }
  61. describe('Mustache spec compliance', function () {
  62. beforeEach(function () {
  63. Mustache.clearCache();
  64. });
  65. specFiles.forEach(function (specArea) {
  66. describe('- ' + specArea + ':', function () {
  67. var specs = getSpecs(specArea);
  68. specs.tests.forEach(function (test) {
  69. var it_ = (!noSkip && skipTests[specArea] && skipTests[specArea].indexOf(test.name) >= 0) ? it.skip : it;
  70. it_(test.name + ' - ' + test.desc, function () {
  71. if (test.data.lambda && test.data.lambda.__tag__ === 'code')
  72. test.data.lambda = eval('(function() { return ' + test.data.lambda.js + '; })');
  73. var output = Mustache.render(test.template, test.data, test.partials);
  74. assert.equal(output, test.expected);
  75. });
  76. });
  77. });
  78. });
  79. });