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.

199 line
4.7KB

  1. require 'rubygems'
  2. require 'json'
  3. __DIR__ = File.dirname(__FILE__)
  4. testnames = Dir.glob(__DIR__ + '/../examples/*.js').map do |name|
  5. File.basename name, '.js'
  6. end
  7. non_partials = testnames.select{|t| not t.include? "partial"}
  8. partials = testnames.select{|t| t.include? "partial"}
  9. def load_test(dir, name, partial=false)
  10. view = File.read(dir + "/../examples/#{name}.js")
  11. template = File.read(dir + "/../examples/#{name}.html").to_json
  12. expect = File.read(dir + "/../examples/#{name}.txt")
  13. if not partial
  14. [view, template, expect]
  15. else
  16. partial = File.read(dir + "/../examples/#{name}.2.html").to_json
  17. [view, template, partial, expect]
  18. end
  19. end
  20. describe "mustache" do
  21. before(:all) do
  22. mustache = File.read(__DIR__ + "/../mustache.js")
  23. stubbed_gettext = <<-JS
  24. // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
  25. function _(text) {
  26. return text;
  27. }
  28. JS
  29. @boilerplate = <<-JS
  30. #{mustache}
  31. #{stubbed_gettext}
  32. JS
  33. end
  34. it "should return the same when invoked multiple times" do
  35. js = <<-JS
  36. #{@boilerplate}
  37. Mustache.to_html("x")
  38. print(Mustache.to_html("x"));
  39. JS
  40. run_js(js).should == "x\n"
  41. end
  42. it "should clear the context after each run" do
  43. js = <<-JS
  44. #{@boilerplate}
  45. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  46. try {
  47. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  48. } catch(e) {
  49. print('ERROR: ' + e.message);
  50. }
  51. JS
  52. run_js(js).should == "\n"
  53. end
  54. it "should not double-render" do
  55. js = <<-JS
  56. #{@boilerplate}
  57. var template = "{{#foo}}{{bar}}{{/foo}}";
  58. var ctx = {
  59. foo: true,
  60. bar: "{{win}}",
  61. win: "FAIL"
  62. };
  63. print(Mustache.to_html(template, ctx))
  64. JS
  65. run_js(js).strip.should == "{{win}}"
  66. end
  67. it "should work with i18n" do
  68. js = <<-JS
  69. #{@boilerplate}
  70. var template = "{{_i}}foo {{bar}}{{/i}}";
  71. var ctx = {
  72. bar: "BAR"
  73. };
  74. print(Mustache.to_html(template, ctx));
  75. JS
  76. run_js(js).strip.should == "foo BAR"
  77. end
  78. non_partials.each do |testname|
  79. describe testname do
  80. it "should generate the correct html" do
  81. view, template, expect = load_test(__DIR__, testname)
  82. runner = <<-JS
  83. try {
  84. #{@boilerplate}
  85. #{view}
  86. var template = #{template};
  87. var result = Mustache.to_html(template, #{testname});
  88. print(result);
  89. } catch(e) {
  90. print('ERROR: ' + e.message);
  91. }
  92. JS
  93. run_js(runner).should == expect
  94. end
  95. it "should sendFun the correct html" do
  96. view, template, expect = load_test(__DIR__, testname)
  97. runner = <<-JS
  98. try {
  99. #{@boilerplate}
  100. #{view}
  101. var chunks = [];
  102. var sendFun = function(chunk) {
  103. if (chunk != "") {
  104. chunks.push(chunk);
  105. }
  106. }
  107. var template = #{template};
  108. Mustache.to_html(template, #{testname}, null, sendFun);
  109. print(chunks.join("\\n"));
  110. } catch(e) {
  111. print('ERROR: ' + e.message);
  112. }
  113. JS
  114. run_js(runner).strip.should == expect.strip
  115. end
  116. end
  117. end
  118. partials.each do |testname|
  119. describe testname do
  120. it "should generate the correct html" do
  121. view, template, partial, expect =
  122. load_test(__DIR__, testname, true)
  123. runner = <<-JS
  124. try {
  125. #{@boilerplate}
  126. #{view}
  127. var template = #{template};
  128. var partials = {"partial": #{partial}};
  129. var result = Mustache.to_html(template, partial_context, partials);
  130. print(result);
  131. } catch(e) {
  132. print('ERROR: ' + e.message);
  133. }
  134. JS
  135. run_js(runner).should == expect
  136. end
  137. it "should sendFun the correct html" do
  138. view, template, partial, expect =
  139. load_test(__DIR__, testname, true)
  140. runner = <<-JS
  141. try {
  142. #{@boilerplate}
  143. #{view};
  144. var template = #{template};
  145. var partials = {"partial": #{partial}};
  146. var chunks = [];
  147. var sendFun = function(chunk) {
  148. if (chunk != "") {
  149. chunks.push(chunk);
  150. }
  151. }
  152. Mustache.to_html(template, partial_context, partials, sendFun);
  153. print(chunks.join("\\n"));
  154. } catch(e) {
  155. print('ERROR: ' + e.message);
  156. }
  157. JS
  158. run_js(runner).strip.should == expect.strip
  159. end
  160. end
  161. end
  162. def run_js(js)
  163. File.open("runner.js", 'w') {|f| f << js}
  164. `js runner.js`
  165. end
  166. end