選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

render_test.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var fs = require("fs"),
  2. path = require("path"),
  3. assert = require("assert"),
  4. vows = require("vows");
  5. var Mustache = require(path.join(__dirname, "..", "mustache"));
  6. var _files = path.join(__dirname, "_files");
  7. function getContents(testName, ext) {
  8. var file = path.join(_files, testName + "." + ext);
  9. try {
  10. return fs.readFileSync(file, "utf8");
  11. } catch (e) {}
  12. }
  13. // You can put the name of a specific test to run in the TEST environment
  14. // variable (e.g. TEST=backslashes vows test/render_test.js)
  15. var testToRun = process.env["TEST"];
  16. var testNames;
  17. if (testToRun) {
  18. testNames = [testToRun];
  19. } else {
  20. testNames = fs.readdirSync(_files).filter(function (file) {
  21. return (/\.js$/).test(file);
  22. }).map(function (file) {
  23. return path.basename(file).replace(/\.js$/, "");
  24. });
  25. }
  26. var spec = {};
  27. testNames.forEach(function (testName) {
  28. var view = getContents(testName, "js");
  29. if (view) {
  30. view = eval(view);
  31. } else {
  32. console.log("Cannot find view for test: " + testName);
  33. process.exit();
  34. }
  35. var template = getContents(testName, "mustache");
  36. var expect = getContents(testName, "txt");
  37. var partial = getContents(testName, "partial");
  38. spec["knows how to render " + testName] = function () {
  39. Mustache.clearCache();
  40. var output;
  41. if (partial) {
  42. output = Mustache.render(template, view, {partial: partial});
  43. } else {
  44. output = Mustache.render(template, view);
  45. }
  46. assert.equal(output, expect);
  47. };
  48. });
  49. vows.describe("Mustache.render").addBatch({
  50. "render": spec
  51. }).export(module);