Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

276 wiersze
6.9KB

  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. var #{testname}_helpers = null;
  71. #{view}
  72. var template = #{template};
  73. var result = Mustache.to_html(template, #{testname}, null, null, #{testname}_helpers);
  74. print(result);
  75. } catch(e) {
  76. print('ERROR: ' + e.message);
  77. }
  78. JS
  79. run_js(@run_js, runner).should == expect
  80. end
  81. it "should sendFun the correct html" do
  82. view, template, expect = load_test(__DIR__, testname)
  83. runner = <<-JS
  84. try {
  85. #{@boilerplate}
  86. var #{testname}_helpers = null;
  87. #{view}
  88. var chunks = [];
  89. var sendFun = function(chunk) {
  90. if (chunk != "") {
  91. chunks.push(chunk);
  92. }
  93. }
  94. var template = #{template};
  95. Mustache.to_html(template, #{testname}, null, sendFun, #{testname}_helpers);
  96. print(chunks.join("\\n"));
  97. } catch(e) {
  98. print('ERROR: ' + e.message);
  99. }
  100. JS
  101. run_js(@run_js, runner).strip.should == expect.strip
  102. end
  103. end
  104. end
  105. partials.each do |testname|
  106. describe testname do
  107. it "should generate the correct html" do
  108. view, template, partial, expect =
  109. load_test(__DIR__, testname, true)
  110. runner = <<-JS
  111. try {
  112. #{@boilerplate}
  113. var #{testname}_helpers = null;
  114. #{view}
  115. var template = #{template};
  116. var partials = {"partial": #{partial}};
  117. var result = Mustache.to_html(template, partial_context, partials, null, #{testname}_helpers);
  118. print(result);
  119. } catch(e) {
  120. print('ERROR: ' + e.message);
  121. }
  122. JS
  123. run_js(@run_js, runner).should == expect
  124. end
  125. it "should sendFun the correct html" do
  126. view, template, partial, expect =
  127. load_test(__DIR__, testname, true)
  128. runner = <<-JS
  129. try {
  130. #{@boilerplate}
  131. var #{testname}_helpers = null;
  132. #{view}
  133. var template = #{template};
  134. var partials = {"partial": #{partial}};
  135. var chunks = [];
  136. var sendFun = function(chunk) {
  137. if (chunk != "") {
  138. chunks.push(chunk);
  139. }
  140. }
  141. Mustache.to_html(template, partial_context, partials, sendFun, #{testname}_helpers);
  142. print(chunks.join("\\n"));
  143. } catch(e) {
  144. print('ERROR: ' + e.message);
  145. }
  146. JS
  147. run_js(@run_js, runner).strip.should == expect.strip
  148. end
  149. end
  150. end
  151. end
  152. context "running in SpiderMonkey (Mozilla, Firefox)" do
  153. p JS_PATH
  154. if !JS_PATH.empty?
  155. before(:each) do
  156. @run_js = :run_js_js
  157. end
  158. before(:all) do
  159. puts "\nTesting mustache.js in SpiderMonkey:\n"
  160. end
  161. after(:all) do
  162. puts "\nDone\n"
  163. end
  164. it_should_behave_like "Mustache rendering"
  165. else
  166. puts "\nSkipping tests in SpiderMonkey (js not found)\n"
  167. end
  168. end
  169. context "running in JavaScriptCore (WebKit, Safari)" do
  170. if File.exists?(JSC_PATH)
  171. before(:each) do
  172. @run_js = :run_js_jsc
  173. end
  174. before(:all) do
  175. puts "\nTesting mustache.js in JavaScriptCore:\n"
  176. end
  177. after(:all) do
  178. puts "\nDone\n"
  179. end
  180. it_should_behave_like "Mustache rendering"
  181. else
  182. puts "\nSkipping tests in JavaScriptCore (jsc not found)\n"
  183. end
  184. end
  185. context "running in Rhino (Mozilla)" do
  186. if !`java #{RHINO_JAR} 'foo' 2>&1`.match(/ClassNotFoundException/)
  187. before(:each) do
  188. @run_js = :run_js_rhino
  189. end
  190. before(:all) do
  191. puts "\nTesting mustache.js in Rhino:\n"
  192. end
  193. after(:all) do
  194. puts "\nDone\n"
  195. end
  196. it_should_behave_like "Mustache rendering"
  197. else
  198. puts "\nSkipping tests in Rhino (JAR #{RHINO_JAR} was not found)\n"
  199. end
  200. end
  201. context "suite" do
  202. before(:all) do
  203. puts "\nVerifying that we ran at the tests in at least one engine\n"
  204. end
  205. after(:all) do
  206. puts "\nDone\n"
  207. end
  208. it "should have run at least one time" do
  209. engines_run.should > 0
  210. end
  211. end
  212. def run_js(runner, js)
  213. send(runner, js)
  214. end
  215. def run_js_js(js)
  216. File.open("runner.js", 'w') {|f| f << js}
  217. `#{JS_PATH} runner.js`
  218. end
  219. def run_js_jsc(js)
  220. File.open("runner.js", 'w') {|f| f << js}
  221. `#{JSC_PATH} runner.js`
  222. end
  223. def run_js_rhino(js)
  224. File.open("runner.js", 'w') {|f| f << js}
  225. `java #{RHINO_JAR} runner.js`
  226. end
  227. end