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.

245 lines
6.1KB

  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. JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
  21. RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
  22. engines_run = 0
  23. describe "mustache" do
  24. shared_examples_for "Mustache rendering" do
  25. before(:all) do
  26. engines_run += 1
  27. mustache = File.read(__DIR__ + "/../mustache.js")
  28. stubbed_gettext = <<-JS
  29. // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
  30. function _(params) {
  31. if (typeof params === "string") {
  32. return params
  33. }
  34. return params.text;
  35. }
  36. JS
  37. @boilerplate = <<-JS
  38. #{mustache}
  39. #{stubbed_gettext}
  40. JS
  41. end
  42. it "should return the same when invoked multiple times" do
  43. js = <<-JS
  44. #{@boilerplate}
  45. Mustache.to_html("x")
  46. print(Mustache.to_html("x"));
  47. JS
  48. run_js(@run_js, js).should == "x\n"
  49. end
  50. it "should clear the context after each run" do
  51. js = <<-JS
  52. #{@boilerplate}
  53. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  54. try {
  55. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  56. } catch(e) {
  57. print('ERROR: ' + e.message);
  58. }
  59. JS
  60. run_js(@run_js, js).should == "\n"
  61. end
  62. non_partials.each do |testname|
  63. describe testname do
  64. it "should generate the correct html" do
  65. view, template, expect = load_test(__DIR__, testname)
  66. runner = <<-JS
  67. try {
  68. #{@boilerplate}
  69. #{view}
  70. var template = #{template};
  71. var result = Mustache.to_html(template, #{testname});
  72. print(result);
  73. } catch(e) {
  74. print('ERROR: ' + e.message);
  75. }
  76. JS
  77. run_js(@run_js, runner).should == expect
  78. end
  79. it "should sendFun the correct html" do
  80. view, template, expect = load_test(__DIR__, testname)
  81. runner = <<-JS
  82. try {
  83. #{@boilerplate}
  84. #{view}
  85. var chunks = [];
  86. var sendFun = function(chunk) {
  87. if (chunk != "") {
  88. chunks.push(chunk);
  89. }
  90. }
  91. var template = #{template};
  92. Mustache.to_html(template, #{testname}, null, sendFun);
  93. print(chunks.join("\\n"));
  94. } catch(e) {
  95. print('ERROR: ' + e.message);
  96. }
  97. JS
  98. run_js(@run_js, runner).strip.should == expect.strip
  99. end
  100. end
  101. end
  102. partials.each do |testname|
  103. describe testname do
  104. it "should generate the correct html" do
  105. view, template, partial, expect =
  106. load_test(__DIR__, testname, true)
  107. runner = <<-JS
  108. try {
  109. #{@boilerplate}
  110. #{view}
  111. var template = #{template};
  112. var partials = {"partial": #{partial}};
  113. var result = Mustache.to_html(template, partial_context, partials);
  114. print(result);
  115. } catch(e) {
  116. print('ERROR: ' + e.message);
  117. }
  118. JS
  119. run_js(@run_js, runner).should == expect
  120. end
  121. it "should sendFun the correct html" do
  122. view, template, partial, expect =
  123. load_test(__DIR__, testname, true)
  124. runner = <<-JS
  125. try {
  126. #{@boilerplate}
  127. #{view};
  128. var template = #{template};
  129. var partials = {"partial": #{partial}};
  130. var chunks = [];
  131. var sendFun = function(chunk) {
  132. if (chunk != "") {
  133. chunks.push(chunk);
  134. }
  135. }
  136. Mustache.to_html(template, partial_context, partials, sendFun);
  137. print(chunks.join("\\n"));
  138. } catch(e) {
  139. print('ERROR: ' + e.message);
  140. }
  141. JS
  142. run_js(@run_js, runner).strip.should == expect.strip
  143. end
  144. end
  145. end
  146. end
  147. context "running in JavaScriptCore (WebKit, Safari)" do
  148. if File.exists?(JSC_PATH)
  149. before(:each) do
  150. @run_js = :run_js_jsc
  151. end
  152. before(:all) do
  153. puts "\nTesting mustache.js in JavaScriptCore:\n"
  154. end
  155. after(:all) do
  156. puts "\nDone\n"
  157. end
  158. it_should_behave_like "Mustache rendering"
  159. else
  160. puts "\nSkipping tests in JavaScriptCore (jsc not found)\n"
  161. end
  162. end
  163. context "running in Rhino (Mozilla)" do
  164. if !`java #{RHINO_JAR} 'foo' 2>&1`.match(/ClassNotFoundException/)
  165. before(:each) do
  166. @run_js = :run_js_rhino
  167. end
  168. before(:all) do
  169. puts "\nTesting mustache.js in Rhino:\n"
  170. end
  171. after(:all) do
  172. puts "\nDone\n"
  173. end
  174. it_should_behave_like "Mustache rendering"
  175. else
  176. puts "\nSkipping tests in Rhino (JAR #{RHINO_JAR} was not found)\n"
  177. end
  178. end
  179. context "suite" do
  180. before(:all) do
  181. puts "\nVerifying that we ran at the tests in at least one engine\n"
  182. end
  183. after(:all) do
  184. puts "\nDone\n"
  185. end
  186. it "should have run at least one time" do
  187. engines_run.should > 0
  188. end
  189. end
  190. def run_js(runner, js)
  191. send(runner, js)
  192. end
  193. def run_js_jsc(js)
  194. File.open("runner.js", 'w') {|f| f << js}
  195. `#{JSC_PATH} runner.js`
  196. end
  197. def run_js_rhino(js)
  198. File.open("runner.js", 'w') {|f| f << js}
  199. `java #{RHINO_JAR} runner.js`
  200. end
  201. end