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.

mustache-spec-test.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. interpolation: [
  16. 'Dotted Names - Context Precedence'
  17. ],
  18. partials: [
  19. 'Standalone Without Previous Line',
  20. 'Standalone Without Newline'
  21. ],
  22. sections: [
  23. 'Standalone Without Newline'
  24. ],
  25. '~inheritance': [
  26. 'Default',
  27. 'Variable',
  28. 'Triple Mustache',
  29. 'Sections',
  30. 'Negative Sections',
  31. 'Mustache Injection',
  32. 'Inherit',
  33. 'Overridden content',
  34. 'Data does not override block',
  35. 'Data does not override block default',
  36. 'Overridden parent',
  37. 'Two overridden parents',
  38. 'Override parent with newlines',
  39. 'Inherit indentation',
  40. 'Only one override',
  41. 'Parent template',
  42. 'Recursion',
  43. 'Multi-level inheritance',
  44. 'Multi-level inheritance, no sub child',
  45. 'Text inside parent',
  46. 'Block scope',
  47. 'Standalone parent',
  48. 'Standalone block',
  49. 'Block reindentation',
  50. 'Intrinsic indentation',
  51. 'Nested block reindentation'
  52. ],
  53. '~lambdas': [
  54. 'Interpolation',
  55. 'Interpolation - Expansion',
  56. 'Interpolation - Alternate Delimiters',
  57. 'Interpolation - Multiple Calls',
  58. 'Escaping',
  59. 'Section - Expansion',
  60. 'Section - Alternate Delimiters'
  61. ]
  62. };
  63. // You can run the skipped tests by setting the NOSKIP environment variable to
  64. // true (e.g. NOSKIP=true mocha test/mustache-spec-test.js)
  65. var noSkip = process.env.NOSKIP;
  66. // You can put the name of a specific test file to run in the TEST environment
  67. // variable (e.g. TEST=interpolation mocha test/mustache-spec-test.js)
  68. var fileToRun = process.env.TEST;
  69. // Mustache should work on node 0.6 that doesn't have fs.existsSync
  70. function existsDir (path) {
  71. try {
  72. return fs.statSync(path).isDirectory();
  73. } catch (x) {
  74. return false;
  75. }
  76. }
  77. var specFiles;
  78. if (fileToRun) {
  79. specFiles = [fileToRun];
  80. } else if (existsDir(specsDir)) {
  81. specFiles = fs.readdirSync(specsDir).filter(function (file) {
  82. return (/\.json$/).test(file);
  83. }).map(function (file) {
  84. return path.basename(file).replace(/\.json$/, '');
  85. }).sort();
  86. } else {
  87. specFiles = [];
  88. }
  89. function getSpecs (specArea) {
  90. return JSON.parse(fs.readFileSync(path.join(specsDir, specArea + '.' + 'json'), 'utf8'));
  91. }
  92. describe('Mustache spec compliance', function () {
  93. beforeEach(function () {
  94. Mustache.clearCache();
  95. });
  96. specFiles.forEach(function (specArea) {
  97. describe('- ' + specArea + ':', function () {
  98. var specs = getSpecs(specArea);
  99. specs.tests.forEach(function (test) {
  100. var it_ = (!noSkip && skipTests[specArea] && skipTests[specArea].indexOf(test.name) >= 0) ? it.skip : it;
  101. it_(test.name + ' - ' + test.desc, function () {
  102. if (test.data.lambda && test.data.lambda.__tag__ === 'code')
  103. test.data.lambda = eval('(function() { return ' + test.data.lambda.js + '; })');
  104. var output = Mustache.render(test.template, test.data, test.partials);
  105. assert.equal(output, test.expected);
  106. });
  107. });
  108. });
  109. });
  110. });