You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
6.7KB

  1. require 'rubygems'
  2. require 'json'
  3. ROOT = File.expand_path("../..", __FILE__)
  4. MUSTACHE = File.read(File.join(ROOT, "mustache.js"))
  5. TESTS = Dir.glob(File.join(ROOT, 'examples', '*.js')).map do |name|
  6. File.basename name, '.js'
  7. end
  8. PARTIALS = TESTS.select {|t| t.include? "partial" }
  9. NON_PARTIALS = TESTS.select {|t| not t.include? "partial" }
  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, is_partial=false)
  15. view = File.read(File.join(ROOT, "examples", "#{name}.js"))
  16. template = File.read(File.join(ROOT, "examples", "#{name}.html")).to_json
  17. expect = File.read(File.join(ROOT, "examples", "#{name}.txt"))
  18. test = [view, template, expect]
  19. if is_partial
  20. test << File.read(File.join(ROOT, "examples", "#{name}.2.html")).to_json
  21. end
  22. test
  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 :node
  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 when invoked multiple times" do
  48. js = <<-JS
  49. #{@boilerplate}
  50. Mustache.to_html("x")
  51. print(Mustache.to_html("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.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  59. try {
  60. print(Mustache.to_html("{{#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. NON_PARTIALS.each do |test|
  68. describe test do
  69. it "should generate the correct html" do
  70. view, template, expect = load_test(test)
  71. js = <<-JS
  72. try {
  73. #{@boilerplate}
  74. #{view}
  75. var template = #{template};
  76. var result = Mustache.to_html(template, #{test});
  77. print(result);
  78. } catch(e) {
  79. print('ERROR: ' + e.message);
  80. }
  81. JS
  82. run_js(@runner, js).should == expect
  83. end
  84. it "should sendFun the correct html" do
  85. view, template, expect = load_test(test)
  86. js = <<-JS
  87. try {
  88. #{@boilerplate}
  89. #{view}
  90. var chunks = [];
  91. var sendFun = function(chunk) {
  92. if (chunk != "") {
  93. chunks.push(chunk);
  94. }
  95. }
  96. var template = #{template};
  97. Mustache.to_html(template, #{test}, null, sendFun);
  98. print(chunks.join("\\n"));
  99. } catch(e) {
  100. print('ERROR: ' + e.message);
  101. }
  102. JS
  103. run_js(@runner, js).strip.should == expect.strip
  104. end
  105. end
  106. end
  107. PARTIALS.each do |test|
  108. describe test do
  109. it "should generate the correct html" do
  110. view, template, expect, partial = load_test(test, true)
  111. js = <<-JS
  112. try {
  113. #{@boilerplate}
  114. #{view}
  115. var template = #{template};
  116. var partials = {"partial": #{partial}};
  117. var result = Mustache.to_html(template, partial_context, partials);
  118. print(result);
  119. } catch(e) {
  120. print('ERROR: ' + e.message);
  121. }
  122. JS
  123. run_js(@runner, js).should == expect
  124. end
  125. it "should sendFun the correct html" do
  126. view, template, expect, partial = load_test(test, true)
  127. js = <<-JS
  128. try {
  129. #{@boilerplate}
  130. #{view};
  131. var template = #{template};
  132. var partials = {"partial": #{partial}};
  133. var chunks = [];
  134. var sendFun = function(chunk) {
  135. if (chunk != "") {
  136. chunks.push(chunk);
  137. }
  138. }
  139. Mustache.to_html(template, partial_context, partials, sendFun);
  140. print(chunks.join("\\n"));
  141. } catch(e) {
  142. print('ERROR: ' + e.message);
  143. }
  144. JS
  145. run_js(@runner, js).strip.should == expect.strip
  146. end
  147. end
  148. end
  149. end
  150. context "running in node" do
  151. if File.exist?(NODE_PATH)
  152. before(:all) do
  153. $stdout.write "Testing in node "
  154. @runner = :node
  155. @boilerplate = MUSTACHE.dup
  156. @boilerplate << <<-JS
  157. function print(message) {
  158. console.log(message);
  159. }
  160. JS
  161. end
  162. after(:all) do
  163. puts " Done!"
  164. end
  165. it_should_behave_like "mustache rendering"
  166. else
  167. puts "Skipping tests in node (node not found)"
  168. end
  169. end
  170. context "running in SpiderMonkey (Mozilla, Firefox)" do
  171. if File.exist?(JS_PATH)
  172. before(:all) do
  173. $stdout.write "Testing in SpiderMonkey "
  174. @runner = :spidermonkey
  175. @boilerplate = MUSTACHE.dup
  176. end
  177. after(:all) do
  178. puts " Done!"
  179. end
  180. it_should_behave_like "mustache rendering"
  181. else
  182. puts "Skipping tests in SpiderMonkey (js not found)"
  183. end
  184. end
  185. context "running in JavaScriptCore (WebKit, Safari)" do
  186. if File.exist?(JSC_PATH)
  187. before(:all) do
  188. $stdout.write "Testing in JavaScriptCore "
  189. @runner = :jsc
  190. @boilerplate = MUSTACHE.dup
  191. end
  192. after(:all) do
  193. puts " Done!"
  194. end
  195. it_should_behave_like "mustache rendering"
  196. else
  197. puts "Skipping tests in JavaScriptCore (jsc not found)"
  198. end
  199. end
  200. context "running in Rhino (Mozilla)" do
  201. if `java #{RHINO_JAR} 'foo' 2>&1` !~ /ClassNotFoundException/
  202. before(:all) do
  203. $stdout.write "Testing in Rhino "
  204. @runner = :rhino
  205. @boilerplate = MUSTACHE.dup
  206. end
  207. after(:all) do
  208. puts " Done!"
  209. end
  210. it_should_behave_like "mustache rendering"
  211. else
  212. puts "Skipping tests in Rhino (JAR #{RHINO_JAR} was not found)"
  213. end
  214. end
  215. context "suite" do
  216. before(:each) do
  217. $stdout.write "Verifying that we ran at the tests in at least one engine ... "
  218. end
  219. after(:each) do
  220. if @exception.nil?
  221. puts "OK"
  222. else
  223. puts "ERROR!"
  224. end
  225. end
  226. it "should have run at least one time" do
  227. $engines_run.should > 0
  228. end
  229. end
  230. end