Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

219 linhas
5.3KB

  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. #
  87. # js = <<-JS
  88. # try {
  89. # #{@boilerplate}
  90. # var template = #{template.to_json};
  91. # #{view}
  92. # var partials = {
  93. # "partial": #{(partial || '').to_json}
  94. # };
  95. # var buffer = [];
  96. # var send = function (chunk) {
  97. # buffer.push(chunk);
  98. # };
  99. # Mustache.render(template, #{test}, partials, send);
  100. # print(buffer.join(""));
  101. # } catch(e) {
  102. # print('ERROR: ' + e.message);
  103. # }
  104. # JS
  105. #
  106. # run_js(@runner, js).chomp.should == expect
  107. # end
  108. end
  109. end
  110. end
  111. context "running in V8 (Chrome, node)" do
  112. if File.exist?(NODE_PATH)
  113. before(:all) do
  114. $stdout.write "Testing in V8 "
  115. @runner = :v8
  116. @boilerplate = MUSTACHE.dup
  117. @boilerplate << <<-JS
  118. var print = console.log;
  119. JS
  120. end
  121. after(:all) do
  122. puts " Done!"
  123. end
  124. it_should_behave_like "mustache rendering"
  125. else
  126. puts "Skipping tests in V8 (node not found)"
  127. end
  128. end
  129. context "running in SpiderMonkey (Mozilla, Firefox)" do
  130. if File.exist?(JS_PATH)
  131. before(:all) do
  132. $stdout.write "Testing in SpiderMonkey "
  133. @runner = :spidermonkey
  134. @boilerplate = MUSTACHE.dup
  135. end
  136. after(:all) do
  137. puts " Done!"
  138. end
  139. it_should_behave_like "mustache rendering"
  140. else
  141. puts "Skipping tests in SpiderMonkey (js not found)"
  142. end
  143. end
  144. context "running in JavaScriptCore (WebKit, Safari)" do
  145. if File.exist?(JSC_PATH)
  146. before(:all) do
  147. $stdout.write "Testing in JavaScriptCore "
  148. @runner = :jsc
  149. @boilerplate = MUSTACHE.dup
  150. end
  151. after(:all) do
  152. puts " Done!"
  153. end
  154. it_should_behave_like "mustache rendering"
  155. else
  156. puts "Skipping tests in JavaScriptCore (jsc not found)"
  157. end
  158. end
  159. context "running in Rhino (Mozilla, Java)" do
  160. if `java #{RHINO_JAR} 'foo' 2>&1` !~ /ClassNotFoundException/
  161. before(:all) do
  162. $stdout.write "Testing in Rhino "
  163. @runner = :rhino
  164. @boilerplate = MUSTACHE.dup
  165. end
  166. after(:all) do
  167. puts " Done!"
  168. end
  169. it_should_behave_like "mustache rendering"
  170. else
  171. puts "Skipping tests in Rhino (JAR #{RHINO_JAR} was not found)"
  172. end
  173. end
  174. context "suite" do
  175. before(:each) do
  176. $stdout.write "Verifying that we ran the tests in at least one engine ... "
  177. end
  178. after(:each) do
  179. puts @exception.nil? ? "OK" : "ERROR"
  180. end
  181. it "should have run at least one time" do
  182. $engines_run.should > 0
  183. end
  184. end
  185. end