You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 line
2.4KB

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