Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

268 lignes
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. if File.exist?(JS_PATH)
  150. before(:each) do
  151. @run_js = :run_js_js
  152. end
  153. before(:all) do
  154. puts "\nTesting mustache.js in SpiderMonkey:\n"
  155. end
  156. after(:all) do
  157. puts "\nDone\n"
  158. end
  159. it_should_behave_like "Mustache rendering"
  160. else
  161. puts "\nSkipping tests in SpiderMonkey (js not found)\n"
  162. end
  163. end
  164. context "running in JavaScriptCore (WebKit, Safari)" do
  165. if File.exist?(JSC_PATH)
  166. before(:each) do
  167. @run_js = :run_js_jsc
  168. end
  169. before(:all) do
  170. puts "\nTesting mustache.js in JavaScriptCore:\n"
  171. end
  172. after(:all) do
  173. puts "\nDone\n"
  174. end
  175. it_should_behave_like "Mustache rendering"
  176. else
  177. puts "\nSkipping tests in JavaScriptCore (jsc not found)\n"
  178. end
  179. end
  180. context "running in Rhino (Mozilla)" do
  181. if !`java #{RHINO_JAR} 'foo' 2>&1`.match(/ClassNotFoundException/)
  182. before(:each) do
  183. @run_js = :run_js_rhino
  184. end
  185. before(:all) do
  186. puts "\nTesting mustache.js in Rhino:\n"
  187. end
  188. after(:all) do
  189. puts "\nDone\n"
  190. end
  191. it_should_behave_like "Mustache rendering"
  192. else
  193. puts "\nSkipping tests in Rhino (JAR #{RHINO_JAR} was not found)\n"
  194. end
  195. end
  196. context "suite" do
  197. before(:all) do
  198. puts "\nVerifying that we ran at the tests in at least one engine\n"
  199. end
  200. after(:all) do
  201. puts "\nDone\n"
  202. end
  203. it "should have run at least one time" do
  204. engines_run.should > 0
  205. end
  206. end
  207. def run_js(runner, js)
  208. cmd = case runner
  209. when :run_js_js
  210. JS_PATH
  211. when :run_js_jsc
  212. JSC_PATH
  213. when :run_js_rhino
  214. "java #{RHINO_JAR}"
  215. end
  216. runner_file = "runner.js"
  217. File.open(runner_file, 'w') {|f| f << js}
  218. `#{cmd} #{runner_file}`
  219. ensure
  220. FileUtils.rm_r(runner_file)
  221. end
  222. end