Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

16 лет назад
16 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. it "should not double-render" do
  56. js = <<-JS
  57. #{@boilerplate}
  58. var template = "{{#foo}}{{bar}}{{/foo}}";
  59. var ctx = {
  60. foo: true,
  61. bar: "{{win}}",
  62. win: "FAIL"
  63. };
  64. print(Mustache.to_html(template, ctx))
  65. JS
  66. run_js(@run_js, js).strip.should == "{{win}}"
  67. end
  68. it "should work with i18n" do
  69. js = <<-JS
  70. #{@boilerplate}
  71. var template = "{{_i}}foo {{bar}}{{/i}}";
  72. var ctx = {
  73. bar: "BAR"
  74. };
  75. print(Mustache.to_html(template, ctx));
  76. JS
  77. run_js(@run_js, js).strip.should == "foo BAR"
  78. end
  79. it "should work with empty sections" do
  80. js = <<-JS
  81. try{
  82. #{@boilerplate}
  83. print(Mustache.to_html("{{#foo}}{{/foo}}foo{{#bar}}{{/bar}}", {}));
  84. } catch(e) {
  85. print(e);
  86. }
  87. JS
  88. run_js(@run_js, js).strip.should == "foo"
  89. end
  90. non_partials.each do |testname|
  91. describe testname do
  92. it "should generate the correct html" do
  93. view, template, expect = load_test(__DIR__, testname)
  94. runner = <<-JS
  95. try {
  96. #{@boilerplate}
  97. #{view}
  98. var template = #{template};
  99. var result = Mustache.to_html(template, #{testname});
  100. print(result);
  101. } catch(e) {
  102. print('ERROR: ' + e.message);
  103. }
  104. JS
  105. run_js(@run_js, runner).should == expect
  106. end
  107. it "should sendFun the correct html" do
  108. view, template, expect = load_test(__DIR__, testname)
  109. runner = <<-JS
  110. try {
  111. #{@boilerplate}
  112. #{view}
  113. var chunks = [];
  114. var sendFun = function(chunk) {
  115. if (chunk != "") {
  116. chunks.push(chunk);
  117. }
  118. }
  119. var template = #{template};
  120. Mustache.to_html(template, #{testname}, null, sendFun);
  121. print(chunks.join("\\n"));
  122. } catch(e) {
  123. print('ERROR: ' + e.message);
  124. }
  125. JS
  126. run_js(@run_js, runner).strip.should == expect.strip
  127. end
  128. end
  129. end
  130. partials.each do |testname|
  131. describe testname do
  132. it "should generate the correct html" do
  133. view, template, partial, expect =
  134. load_test(__DIR__, testname, true)
  135. runner = <<-JS
  136. try {
  137. #{@boilerplate}
  138. #{view}
  139. var template = #{template};
  140. var partials = {"partial": #{partial}};
  141. var result = Mustache.to_html(template, partial_context, partials);
  142. print(result);
  143. } catch(e) {
  144. print('ERROR: ' + e.message);
  145. }
  146. JS
  147. run_js(@run_js, runner).should == expect
  148. end
  149. it "should sendFun the correct html" do
  150. view, template, partial, expect =
  151. load_test(__DIR__, testname, true)
  152. runner = <<-JS
  153. try {
  154. #{@boilerplate}
  155. #{view};
  156. var template = #{template};
  157. var partials = {"partial": #{partial}};
  158. var chunks = [];
  159. var sendFun = function(chunk) {
  160. if (chunk != "") {
  161. chunks.push(chunk);
  162. }
  163. }
  164. Mustache.to_html(template, partial_context, partials, sendFun);
  165. print(chunks.join("\\n"));
  166. } catch(e) {
  167. print('ERROR: ' + e.message);
  168. }
  169. JS
  170. run_js(@run_js, runner).strip.should == expect.strip
  171. end
  172. end
  173. end
  174. end
  175. context "running in JavaScriptCore (WebKit, Safari)" do
  176. before(:each) do
  177. @run_js = :run_js_jsc
  178. end
  179. it_should_behave_like "Mustache rendering"
  180. end
  181. context "running in Rhino (Mozilla)" do
  182. before(:each) do
  183. @run_js = :run_js_rhino
  184. end
  185. it_should_behave_like "Mustache rendering"
  186. end
  187. def run_js(runner, js)
  188. send(runner, js)
  189. # run_js_rhino(js)
  190. # js_jsc = run_js_jsc(js)
  191. # js_rhino = run_js_rhino(js)
  192. # return js_jsc unless js_jsc != js_rhino
  193. end
  194. def run_js_jsc(js)
  195. File.open("runner.js", 'w') {|f| f << js}
  196. `jsc runner.js`
  197. end
  198. def run_js_rhino(js)
  199. File.open("runner.js", 'w') {|f| f << js}
  200. `java org.mozilla.javascript.tools.shell.Main runner.js`
  201. end
  202. end