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

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