No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

200 líneas
5.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. describe "mustache" do
  21. shared_examples_for "Mustache rendering" do
  22. before(:all) do
  23. mustache = File.read(__DIR__ + "/../mustache.js")
  24. stubbed_gettext = <<-JS
  25. // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
  26. function _(text) {
  27. return text;
  28. }
  29. JS
  30. @boilerplate = <<-JS
  31. #{mustache}
  32. #{stubbed_gettext}
  33. JS
  34. end
  35. it "should return the same when invoked multiple times" do
  36. js = <<-JS
  37. #{@boilerplate}
  38. Mustache.to_html("x")
  39. print(Mustache.to_html("x"));
  40. JS
  41. run_js(@run_js, js).should == "x\n"
  42. end
  43. it "should clear the context after each run" do
  44. js = <<-JS
  45. #{@boilerplate}
  46. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  47. try {
  48. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  49. } catch(e) {
  50. print('ERROR: ' + e.message);
  51. }
  52. JS
  53. run_js(@run_js, js).should == "\n"
  54. end
  55. non_partials.each do |testname|
  56. describe testname do
  57. it "should generate the correct html" do
  58. view, template, expect = load_test(__DIR__, testname)
  59. runner = <<-JS
  60. try {
  61. #{@boilerplate}
  62. #{view}
  63. var template = #{template};
  64. var result = Mustache.to_html(template, #{testname});
  65. print(result);
  66. } catch(e) {
  67. print('ERROR: ' + e.message);
  68. }
  69. JS
  70. run_js(@run_js, runner).should == expect
  71. end
  72. it "should sendFun the correct html" do
  73. view, template, expect = load_test(__DIR__, testname)
  74. runner = <<-JS
  75. try {
  76. #{@boilerplate}
  77. #{view}
  78. var chunks = [];
  79. var sendFun = function(chunk) {
  80. if (chunk != "") {
  81. chunks.push(chunk);
  82. }
  83. }
  84. var template = #{template};
  85. Mustache.to_html(template, #{testname}, null, sendFun);
  86. print(chunks.join("\\n"));
  87. } catch(e) {
  88. print('ERROR: ' + e.message);
  89. }
  90. JS
  91. run_js(@run_js, runner).strip.should == expect.strip
  92. end
  93. end
  94. end
  95. partials.each do |testname|
  96. describe testname do
  97. it "should generate the correct html" do
  98. view, template, partial, expect =
  99. load_test(__DIR__, testname, true)
  100. runner = <<-JS
  101. try {
  102. #{@boilerplate}
  103. #{view}
  104. var template = #{template};
  105. var partials = {"partial": #{partial}};
  106. var result = Mustache.to_html(template, partial_context, partials);
  107. print(result);
  108. } catch(e) {
  109. print('ERROR: ' + e.message);
  110. }
  111. JS
  112. run_js(@run_js, runner).should == expect
  113. end
  114. it "should sendFun the correct html" do
  115. view, template, partial, expect =
  116. load_test(__DIR__, testname, true)
  117. runner = <<-JS
  118. try {
  119. #{@boilerplate}
  120. #{view};
  121. var template = #{template};
  122. var partials = {"partial": #{partial}};
  123. var chunks = [];
  124. var sendFun = function(chunk) {
  125. if (chunk != "") {
  126. chunks.push(chunk);
  127. }
  128. }
  129. Mustache.to_html(template, partial_context, partials, sendFun);
  130. print(chunks.join("\\n"));
  131. } catch(e) {
  132. print('ERROR: ' + e.message);
  133. }
  134. JS
  135. run_js(@run_js, runner).strip.should == expect.strip
  136. end
  137. end
  138. end
  139. end
  140. context "running in JavaScriptCore (WebKit, Safari)" do
  141. before(:each) do
  142. @run_js = :run_js_jsc
  143. end
  144. it_should_behave_like "Mustache rendering"
  145. end
  146. context "running in Rhino (Mozilla)" do
  147. before(:each) do
  148. @run_js = :run_js_rhino
  149. end
  150. it_should_behave_like "Mustache rendering"
  151. end
  152. def run_js(runner, js)
  153. send(runner, js)
  154. # run_js_rhino(js)
  155. # js_jsc = run_js_jsc(js)
  156. # js_rhino = run_js_rhino(js)
  157. # return js_jsc unless js_jsc != js_rhino
  158. end
  159. def run_js_jsc(js)
  160. File.open("runner.js", 'w') {|f| f << js}
  161. `jsc runner.js`
  162. end
  163. def run_js_rhino(js)
  164. File.open("runner.js", 'w') {|f| f << js}
  165. `java org.mozilla.javascript.tools.shell.Main runner.js`
  166. end
  167. end