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

214 строки
5.0KB

  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 work with empty sections" do
  79. js = <<-JS
  80. try{
  81. #{@boilerplate}
  82. print(Mustache.to_html("{{#foo}}{{/foo}}foo{{#bar}}{{/bar}}", {}));
  83. } catch(e) {
  84. print(e);
  85. }
  86. JS
  87. run_js(js).strip.should == "foo"
  88. end
  89. non_partials.each do |testname|
  90. describe testname do
  91. it "should generate the correct html" do
  92. view, template, expect = load_test(__DIR__, testname)
  93. runner = <<-JS
  94. try {
  95. #{@boilerplate}
  96. #{view}
  97. var template = #{template};
  98. var result = Mustache.to_html(template, #{testname});
  99. print(result);
  100. } catch(e) {
  101. print('ERROR: ' + e.message);
  102. }
  103. JS
  104. run_js(runner).should == expect
  105. end
  106. it "should sendFun the correct html" do
  107. view, template, expect = load_test(__DIR__, testname)
  108. runner = <<-JS
  109. try {
  110. #{@boilerplate}
  111. #{view}
  112. var chunks = [];
  113. var sendFun = function(chunk) {
  114. if (chunk != "") {
  115. chunks.push(chunk);
  116. }
  117. }
  118. var template = #{template};
  119. Mustache.to_html(template, #{testname}, null, sendFun);
  120. print(chunks.join("\\n"));
  121. } catch(e) {
  122. print('ERROR: ' + e.message);
  123. }
  124. JS
  125. run_js(runner).strip.should == expect.strip
  126. end
  127. end
  128. end
  129. partials.each do |testname|
  130. describe testname do
  131. it "should generate the correct html" do
  132. view, template, partial, expect =
  133. load_test(__DIR__, testname, true)
  134. runner = <<-JS
  135. try {
  136. #{@boilerplate}
  137. #{view}
  138. var template = #{template};
  139. var partials = {"partial": #{partial}};
  140. var result = Mustache.to_html(template, partial_context, partials);
  141. print(result);
  142. } catch(e) {
  143. print('ERROR: ' + e.message);
  144. }
  145. JS
  146. run_js(runner).should == expect
  147. end
  148. it "should sendFun the correct html" do
  149. view, template, partial, expect =
  150. load_test(__DIR__, testname, true)
  151. runner = <<-JS
  152. try {
  153. #{@boilerplate}
  154. #{view};
  155. var template = #{template};
  156. var partials = {"partial": #{partial}};
  157. var chunks = [];
  158. var sendFun = function(chunk) {
  159. if (chunk != "") {
  160. chunks.push(chunk);
  161. }
  162. }
  163. Mustache.to_html(template, partial_context, partials, sendFun);
  164. print(chunks.join("\\n"));
  165. } catch(e) {
  166. print('ERROR: ' + e.message);
  167. }
  168. JS
  169. run_js(runner).strip.should == expect.strip
  170. end
  171. end
  172. end
  173. def run_js(js)
  174. File.open("runner.js", 'w') {|f| f << js}
  175. `js runner.js`
  176. end
  177. end