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.

73 lines
1.8KB

  1. require('./helper');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var _files = path.join(__dirname, '_files');
  5. function getContents(testName, ext) {
  6. try {
  7. return fs.readFileSync(path.join(_files, testName + '.' + ext), 'utf8');
  8. } catch (ex) {
  9. return null;
  10. }
  11. }
  12. function getView(testName) {
  13. var view = getContents(testName, 'js');
  14. if (!view) throw new Error('Cannot find view for test "' + testName + '"');
  15. return eval(view);
  16. }
  17. function getPartial(testName) {
  18. try {
  19. return getContents(testName, 'partial');
  20. } catch (error) {
  21. // No big deal. Not all tests need to test partial support.
  22. }
  23. }
  24. function getTest(testName) {
  25. var test = {};
  26. test.view = getView(testName);
  27. test.template = getContents(testName, 'mustache');
  28. test.partial = getPartial(testName);
  29. test.expect = getContents(testName, 'txt');
  30. return test;
  31. }
  32. // You can put the name of a specific test to run in the TEST environment
  33. // variable (e.g. TEST=backslashes vows test/render-test.js)
  34. var testToRun = process.env.TEST;
  35. var testNames;
  36. if (testToRun) {
  37. testNames = [testToRun];
  38. } else {
  39. testNames = fs.readdirSync(_files).filter(function (file) {
  40. return (/\.js$/).test(file);
  41. }).map(function (file) {
  42. return path.basename(file).replace(/\.js$/, '');
  43. });
  44. }
  45. describe('Mustache.render', function () {
  46. beforeEach(function () {
  47. Mustache.clearCache();
  48. });
  49. testNames.forEach(function (testName) {
  50. var test = getTest(testName);
  51. it('knows how to render ' + testName, function () {
  52. var output;
  53. if (test.partial) {
  54. output = Mustache.render(test.template, test.view, { partial: test.partial });
  55. } else {
  56. output = Mustache.render(test.template, test.view);
  57. }
  58. assert.equal(output, test.expect);
  59. });
  60. });
  61. });