Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

272 řádky
6.6KB

  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. JS_PATH = `which js`.strip()
  21. JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
  22. RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
  23. engines_run = 0
  24. describe "mustache" do
  25. shared_examples_for "Mustache rendering" do
  26. before(:all) do
  27. engines_run += 1
  28. mustache = File.read(__DIR__ + "/../mustache.js")
  29. stubbed_gettext = <<-JS
  30. // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
  31. function _(params) {
  32. if (typeof params === "string") {
  33. return params
  34. }
  35. return params.text;
  36. }
  37. JS
  38. @boilerplate = <<-JS
  39. #{mustache}
  40. #{stubbed_gettext}
  41. JS
  42. end
  43. it "should return the same when invoked multiple times" do
  44. js = <<-JS
  45. #{@boilerplate}
  46. Mustache.to_html("x")
  47. print(Mustache.to_html("x"));
  48. JS
  49. run_js(@run_js, js).should == "x\n"
  50. end
  51. it "should clear the context after each run" do
  52. js = <<-JS
  53. #{@boilerplate}
  54. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  55. try {
  56. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  57. } catch(e) {
  58. print('ERROR: ' + e.message);
  59. }
  60. JS
  61. run_js(@run_js, js).should == "\n"
  62. end
  63. non_partials.each do |testname|
  64. describe testname do
  65. it "should generate the correct html" do
  66. view, template, expect = load_test(__DIR__, testname)
  67. runner = <<-JS
  68. try {
  69. #{@boilerplate}
  70. #{view}
  71. var template = #{template};
  72. var result = Mustache.to_html(template, #{testname});
  73. print(result);
  74. } catch(e) {
  75. print('ERROR: ' + e.message);
  76. }
  77. JS
  78. run_js(@run_js, runner).should == expect
  79. end
  80. it "should sendFun the correct html" do
  81. view, template, expect = load_test(__DIR__, testname)
  82. runner = <<-JS
  83. try {
  84. #{@boilerplate}
  85. #{view}
  86. var chunks = [];
  87. var sendFun = function(chunk) {
  88. if (chunk != "") {
  89. chunks.push(chunk);
  90. }
  91. }
  92. var template = #{template};
  93. Mustache.to_html(template, #{testname}, null, sendFun);
  94. print(chunks.join("\\n"));
  95. } catch(e) {
  96. print('ERROR: ' + e.message);
  97. }
  98. JS
  99. run_js(@run_js, runner).strip.should == expect.strip
  100. end
  101. end
  102. end
  103. partials.each do |testname|
  104. describe testname do
  105. it "should generate the correct html" do
  106. view, template, partial, expect =
  107. load_test(__DIR__, testname, true)
  108. runner = <<-JS
  109. try {
  110. #{@boilerplate}
  111. #{view}
  112. var template = #{template};
  113. var partials = {"partial": #{partial}};
  114. var result = Mustache.to_html(template, partial_context, partials);
  115. print(result);
  116. } catch(e) {
  117. print('ERROR: ' + e.message);
  118. }
  119. JS
  120. run_js(@run_js, runner).should == expect
  121. end
  122. it "should sendFun the correct html" do
  123. view, template, partial, expect =
  124. load_test(__DIR__, testname, true)
  125. runner = <<-JS
  126. try {
  127. #{@boilerplate}
  128. #{view};
  129. var template = #{template};
  130. var partials = {"partial": #{partial}};
  131. var chunks = [];
  132. var sendFun = function(chunk) {
  133. if (chunk != "") {
  134. chunks.push(chunk);
  135. }
  136. }
  137. Mustache.to_html(template, partial_context, partials, sendFun);
  138. print(chunks.join("\\n"));
  139. } catch(e) {
  140. print('ERROR: ' + e.message);
  141. }
  142. JS
  143. run_js(@run_js, runner).strip.should == expect.strip
  144. end
  145. end
  146. end
  147. end
  148. context "running in SpiderMonkey (Mozilla, Firefox)" do
  149. p JS_PATH
  150. if !JS_PATH.empty?
  151. before(:each) do
  152. @run_js = :run_js_js
  153. end
  154. before(:all) do
  155. puts "\nTesting mustache.js in SpiderMonkey:\n"
  156. end
  157. after(:all) do
  158. puts "\nDone\n"
  159. end
  160. it_should_behave_like "Mustache rendering"
  161. else
  162. puts "\nSkipping tests in SpiderMonkey (js not found)\n"
  163. end
  164. end
  165. context "running in JavaScriptCore (WebKit, Safari)" do
  166. if File.exists?(JSC_PATH)
  167. before(:each) do
  168. @run_js = :run_js_jsc
  169. end
  170. before(:all) do
  171. puts "\nTesting mustache.js in JavaScriptCore:\n"
  172. end
  173. after(:all) do
  174. puts "\nDone\n"
  175. end
  176. it_should_behave_like "Mustache rendering"
  177. else
  178. puts "\nSkipping tests in JavaScriptCore (jsc not found)\n"
  179. end
  180. end
  181. context "running in Rhino (Mozilla)" do
  182. if !`java #{RHINO_JAR} 'foo' 2>&1`.match(/ClassNotFoundException/)
  183. before(:each) do
  184. @run_js = :run_js_rhino
  185. end
  186. before(:all) do
  187. puts "\nTesting mustache.js in Rhino:\n"
  188. end
  189. after(:all) do
  190. puts "\nDone\n"
  191. end
  192. it_should_behave_like "Mustache rendering"
  193. else
  194. puts "\nSkipping tests in Rhino (JAR #{RHINO_JAR} was not found)\n"
  195. end
  196. end
  197. context "suite" do
  198. before(:all) do
  199. puts "\nVerifying that we ran at the tests in at least one engine\n"
  200. end
  201. after(:all) do
  202. puts "\nDone\n"
  203. end
  204. it "should have run at least one time" do
  205. engines_run.should > 0
  206. end
  207. end
  208. def run_js(runner, js)
  209. send(runner, js)
  210. end
  211. def run_js_js(js)
  212. File.open("runner.js", 'w') {|f| f << js}
  213. `#{JS_PATH} runner.js`
  214. end
  215. def run_js_jsc(js)
  216. File.open("runner.js", 'w') {|f| f << js}
  217. `#{JSC_PATH} runner.js`
  218. end
  219. def run_js_rhino(js)
  220. File.open("runner.js", 'w') {|f| f << js}
  221. `java #{RHINO_JAR} runner.js`
  222. end
  223. end