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

16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
15 лет назад
15 лет назад
15 лет назад
15 лет назад
16 лет назад
15 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
15 лет назад
16 лет назад
15 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. before(:all) do
  22. mustache = File.read(__DIR__ + "/../mustache.js")
  23. stubbed_gettext = <<-JS
  24. // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
  25. function _(text) {
  26. return text;
  27. }
  28. JS
  29. @boilerplate = <<-JS
  30. #{mustache}
  31. #{stubbed_gettext}
  32. JS
  33. end
  34. it "should return the same when invoked multiple times" do
  35. js = <<-JS
  36. #{@boilerplate}
  37. Mustache.to_html("x")
  38. print(Mustache.to_html("x"));
  39. JS
  40. run_js(js).should == "x\n"
  41. end
  42. it "should clear the context after each run" do
  43. js = <<-JS
  44. #{@boilerplate}
  45. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  46. try {
  47. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  48. } catch(e) {
  49. print('ERROR: ' + e.message);
  50. }
  51. JS
  52. run_js(js).should == "\n"
  53. end
  54. it "should not double-render" do
  55. js = <<-JS
  56. #{@boilerplate}
  57. var template = "{{#foo}}{{bar}}{{/foo}}";
  58. var ctx = {
  59. foo: true,
  60. bar: "{{win}}",
  61. win: "FAIL"
  62. };
  63. print(Mustache.to_html(template, ctx))
  64. JS
  65. run_js(js).strip.should == "{{win}}"
  66. end
  67. it "should work with i18n" do
  68. js = <<-JS
  69. #{@boilerplate}
  70. var template = "{{_i}}foo {{bar}}{{/i}}";
  71. var ctx = {
  72. bar: "BAR"
  73. };
  74. print(Mustache.to_html(template, ctx));
  75. JS
  76. run_js(js).strip.should == "foo BAR"
  77. end
  78. it "should not suck" do
  79. js = <<-JS
  80. try{
  81. #{@boilerplate}
  82. print(Mustache.to_html("{{#foo}}\\
  83. {{/foo}}\\
  84. {{#bar}}\\
  85. {{/bar}}\\
  86. ", {}));
  87. } catch(e) {
  88. print(e);
  89. }
  90. JS
  91. run_js(js).should == "foanfs"
  92. end
  93. non_partials.each do |testname|
  94. describe testname do
  95. it "should generate the correct html" do
  96. view, template, expect = load_test(__DIR__, testname)
  97. runner = <<-JS
  98. try {
  99. #{@boilerplate}
  100. #{view}
  101. var template = #{template};
  102. var result = Mustache.to_html(template, #{testname});
  103. print(result);
  104. } catch(e) {
  105. print('ERROR: ' + e.message);
  106. }
  107. JS
  108. run_js(runner).should == expect
  109. end
  110. it "should sendFun the correct html" do
  111. view, template, expect = load_test(__DIR__, testname)
  112. runner = <<-JS
  113. try {
  114. #{@boilerplate}
  115. #{view}
  116. var chunks = [];
  117. var sendFun = function(chunk) {
  118. if (chunk != "") {
  119. chunks.push(chunk);
  120. }
  121. }
  122. var template = #{template};
  123. Mustache.to_html(template, #{testname}, null, sendFun);
  124. print(chunks.join("\\n"));
  125. } catch(e) {
  126. print('ERROR: ' + e.message);
  127. }
  128. JS
  129. run_js(runner).strip.should == expect.strip
  130. end
  131. end
  132. end
  133. partials.each do |testname|
  134. describe testname do
  135. it "should generate the correct html" do
  136. view, template, partial, expect =
  137. load_test(__DIR__, testname, true)
  138. runner = <<-JS
  139. try {
  140. #{@boilerplate}
  141. #{view}
  142. var template = #{template};
  143. var partials = {"partial": #{partial}};
  144. var result = Mustache.to_html(template, partial_context, partials);
  145. print(result);
  146. } catch(e) {
  147. print('ERROR: ' + e.message);
  148. }
  149. JS
  150. run_js(runner).should == expect
  151. end
  152. it "should sendFun the correct html" do
  153. view, template, partial, expect =
  154. load_test(__DIR__, testname, true)
  155. runner = <<-JS
  156. try {
  157. #{@boilerplate}
  158. #{view};
  159. var template = #{template};
  160. var partials = {"partial": #{partial}};
  161. var chunks = [];
  162. var sendFun = function(chunk) {
  163. if (chunk != "") {
  164. chunks.push(chunk);
  165. }
  166. }
  167. Mustache.to_html(template, partial_context, partials, sendFun);
  168. print(chunks.join("\\n"));
  169. } catch(e) {
  170. print('ERROR: ' + e.message);
  171. }
  172. JS
  173. run_js(runner).strip.should == expect.strip
  174. end
  175. end
  176. end
  177. def run_js(js)
  178. File.open("runner.js", 'w') {|f| f << js}
  179. `js runner.js`
  180. end
  181. end