25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

219 satır
5.2KB

  1. require 'rubygems'
  2. require 'json'
  3. ROOT = File.expand_path('../..', __FILE__)
  4. SPEC = File.join(ROOT, 'spec')
  5. FILES = File.join(SPEC, '_files')
  6. MUSTACHE = File.read(File.join(ROOT, "mustache.js"))
  7. TESTS = Dir.glob(File.join(FILES, '*.js')).map do |name|
  8. File.basename name, '.js'
  9. end
  10. NODE_PATH = `which node`.strip
  11. JS_PATH = `which js`.strip
  12. JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
  13. RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
  14. def load_test(name)
  15. template = File.read(File.join(FILES, "#{name}.mustache"))
  16. view = File.read(File.join(FILES, "#{name}.js"))
  17. partial_file = File.join(FILES, "#{name}.partial")
  18. partial = if File.exist?(partial_file)
  19. File.read(partial_file)
  20. end
  21. expect = File.read(File.join(FILES, "#{name}.txt"))
  22. [template, view, partial, expect]
  23. end
  24. def run_js(runner, js)
  25. cmd = case runner
  26. when :spidermonkey
  27. JS_PATH
  28. when :jsc
  29. JSC_PATH
  30. when :rhino
  31. "java #{RHINO_JAR}"
  32. when :v8
  33. NODE_PATH
  34. end
  35. runner_file = "runner.js"
  36. File.open(runner_file, 'w') {|file| file.write(js) }
  37. `#{cmd} #{runner_file}`
  38. ensure
  39. FileUtils.rm_r(runner_file)
  40. end
  41. $engines_run = 0
  42. describe "mustache" do
  43. shared_examples_for "mustache rendering" do
  44. before(:all) do
  45. $engines_run += 1
  46. end
  47. it "should return the same result when invoked multiple times" do
  48. js = <<-JS
  49. #{@boilerplate}
  50. Mustache.render("x")
  51. print(Mustache.render("x"));
  52. JS
  53. run_js(@runner, js).should == "x\n"
  54. end
  55. it "should clear the context after each run" do
  56. js = <<-JS
  57. #{@boilerplate}
  58. Mustache.render("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  59. try {
  60. print(Mustache.render("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  61. } catch(e) {
  62. print('ERROR: ' + e.message);
  63. }
  64. JS
  65. run_js(@runner, js).should == "\n"
  66. end
  67. TESTS.each do |test|
  68. describe test do
  69. it "should render the correct output" do
  70. template, view, partial, expect = load_test(test)
  71. js = <<-JS
  72. try {
  73. #{@boilerplate}
  74. var template = #{template.to_json};
  75. #{view}
  76. var partials = {partial: #{partial ? partial.to_json : '""'}};
  77. print(Mustache.render(template, #{test}, partials));
  78. } catch(e) {
  79. print('ERROR: ' + e.message);
  80. }
  81. JS
  82. run_js(@runner, js).chomp.should == expect
  83. end
  84. it "should send the correct output" do
  85. template, view, partial, expect = load_test(test)
  86. js = <<-JS
  87. try {
  88. #{@boilerplate}
  89. var template = #{template.to_json};
  90. #{view}
  91. var partials = {
  92. "partial": #{(partial || '').to_json}
  93. };
  94. var buffer = [];
  95. var send = function (chunk) {
  96. buffer.push(chunk);
  97. };
  98. Mustache.render(template, #{test}, partials, send);
  99. print(buffer.join(""));
  100. } catch(e) {
  101. print('ERROR: ' + e.message);
  102. }
  103. JS
  104. run_js(@runner, js).chomp.should == expect
  105. end
  106. end
  107. end
  108. end
  109. context "running in V8 (Chrome, node)" do
  110. if File.exist?(NODE_PATH)
  111. before(:all) do
  112. $stdout.write "Testing in V8 "
  113. @runner = :v8
  114. @boilerplate = MUSTACHE.dup
  115. @boilerplate << <<-JS
  116. var print = console.log;
  117. JS
  118. end
  119. after(:all) do
  120. puts " Done!"
  121. end
  122. it_should_behave_like "mustache rendering"
  123. else
  124. puts "Skipping tests in V8 (node not found)"
  125. end
  126. end
  127. context "running in SpiderMonkey (Mozilla, Firefox)" do
  128. if File.exist?(JS_PATH)
  129. before(:all) do
  130. $stdout.write "Testing in SpiderMonkey "
  131. @runner = :spidermonkey
  132. @boilerplate = MUSTACHE.dup
  133. end
  134. after(:all) do
  135. puts " Done!"
  136. end
  137. it_should_behave_like "mustache rendering"
  138. else
  139. puts "Skipping tests in SpiderMonkey (js not found)"
  140. end
  141. end
  142. context "running in JavaScriptCore (WebKit, Safari)" do
  143. if File.exist?(JSC_PATH)
  144. before(:all) do
  145. $stdout.write "Testing in JavaScriptCore "
  146. @runner = :jsc
  147. @boilerplate = MUSTACHE.dup
  148. end
  149. after(:all) do
  150. puts " Done!"
  151. end
  152. it_should_behave_like "mustache rendering"
  153. else
  154. puts "Skipping tests in JavaScriptCore (jsc not found)"
  155. end
  156. end
  157. context "running in Rhino (Mozilla, Java)" do
  158. if `java #{RHINO_JAR} 'foo' 2>&1` !~ /ClassNotFoundException/
  159. before(:all) do
  160. $stdout.write "Testing in Rhino "
  161. @runner = :rhino
  162. @boilerplate = MUSTACHE.dup
  163. end
  164. after(:all) do
  165. puts " Done!"
  166. end
  167. it_should_behave_like "mustache rendering"
  168. else
  169. puts "Skipping tests in Rhino (JAR #{RHINO_JAR} was not found)"
  170. end
  171. end
  172. context "suite" do
  173. before(:each) do
  174. $stdout.write "Verifying that we ran the tests in at least one engine ... "
  175. end
  176. after(:each) do
  177. puts @exception.nil? ? "OK" : "ERROR"
  178. end
  179. it "should have run at least one time" do
  180. $engines_run.should > 0
  181. end
  182. end
  183. end