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

16 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
15 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
15 лет назад
15 лет назад
14 лет назад
15 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
16 лет назад
14 лет назад
14 лет назад
16 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
16 лет назад
16 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
16 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. require 'rubygems'
  2. require 'json'
  3. ROOT = File.expand_path('../..', __FILE__)
  4. SPEC = File.join(ROOT, 'spec')
  5. FILES = File.join(SPEC, '_files')
  6. MUSTACHE = File.read(File.join(ROOT, "mustache.js"))
  7. TESTS = Dir.glob(File.join(FILES, '*.js')).map do |name|
  8. File.basename name, '.js'
  9. end
  10. PARTIALS = TESTS.select {|t| t.include? "partial" }
  11. NON_PARTIALS = TESTS.select {|t| not t.include? "partial" }
  12. NODE_PATH = `which node`.strip
  13. JS_PATH = `which js`.strip
  14. JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
  15. RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
  16. def load_test(name, is_partial=false)
  17. view = File.read(File.join(FILES, "#{name}.js"))
  18. template = File.read(File.join(FILES, "#{name}.mustache")).to_json
  19. expect = File.read(File.join(FILES, "#{name}.txt"))
  20. test = [view, template, expect]
  21. if is_partial
  22. test << File.read(File.join(FILES, "#{name}.2.mustache")).to_json
  23. end
  24. test
  25. end
  26. def run_js(runner, js)
  27. cmd = case runner
  28. when :spidermonkey
  29. JS_PATH
  30. when :jsc
  31. JSC_PATH
  32. when :rhino
  33. "java #{RHINO_JAR}"
  34. when :node
  35. NODE_PATH
  36. end
  37. runner_file = "runner.js"
  38. File.open(runner_file, 'w') {|file| file.write(js) }
  39. `#{cmd} #{runner_file}`
  40. ensure
  41. FileUtils.rm_r(runner_file)
  42. end
  43. $engines_run = 0
  44. describe "mustache" do
  45. shared_examples_for "mustache rendering" do
  46. before(:all) do
  47. $engines_run += 1
  48. end
  49. it "should return the same when invoked multiple times" do
  50. js = <<-JS
  51. #{@boilerplate}
  52. Mustache.to_html("x")
  53. print(Mustache.to_html("x"));
  54. JS
  55. run_js(@runner, js).should == "x\n"
  56. end
  57. it "should clear the context after each run" do
  58. js = <<-JS
  59. #{@boilerplate}
  60. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  61. try {
  62. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  63. } catch(e) {
  64. print('ERROR: ' + e.message);
  65. }
  66. JS
  67. run_js(@runner, js).should == "\n"
  68. end
  69. NON_PARTIALS.each do |test|
  70. describe test do
  71. it "should generate the correct html" do
  72. view, template, expect = load_test(test)
  73. js = <<-JS
  74. try {
  75. #{@boilerplate}
  76. #{view}
  77. var template = #{template};
  78. var result = Mustache.to_html(template, #{test});
  79. print(result);
  80. } catch(e) {
  81. print('ERROR: ' + e.message);
  82. }
  83. JS
  84. run_js(@runner, js).should == expect
  85. end
  86. it "should sendFun the correct html" do
  87. view, template, expect = load_test(test)
  88. js = <<-JS
  89. try {
  90. #{@boilerplate}
  91. #{view}
  92. var chunks = [];
  93. var sendFun = function(chunk) {
  94. if (chunk != "") {
  95. chunks.push(chunk);
  96. }
  97. }
  98. var template = #{template};
  99. Mustache.to_html(template, #{test}, null, sendFun);
  100. print(chunks.join("\\n"));
  101. } catch(e) {
  102. print('ERROR: ' + e.message);
  103. }
  104. JS
  105. run_js(@runner, js).strip.should == expect.strip
  106. end
  107. end
  108. end
  109. PARTIALS.each do |test|
  110. describe test do
  111. it "should generate the correct html" do
  112. view, template, expect, partial = load_test(test, true)
  113. js = <<-JS
  114. try {
  115. #{@boilerplate}
  116. #{view}
  117. var template = #{template};
  118. var partials = {"partial": #{partial}};
  119. var result = Mustache.to_html(template, partial_context, partials);
  120. print(result);
  121. } catch(e) {
  122. print('ERROR: ' + e.message);
  123. }
  124. JS
  125. run_js(@runner, js).should == expect
  126. end
  127. it "should sendFun the correct html" do
  128. view, template, expect, partial = load_test(test, true)
  129. js = <<-JS
  130. try {
  131. #{@boilerplate}
  132. #{view};
  133. var template = #{template};
  134. var partials = {"partial": #{partial}};
  135. var chunks = [];
  136. var sendFun = function(chunk) {
  137. if (chunk != "") {
  138. chunks.push(chunk);
  139. }
  140. }
  141. Mustache.to_html(template, partial_context, partials, sendFun);
  142. print(chunks.join("\\n"));
  143. } catch(e) {
  144. print('ERROR: ' + e.message);
  145. }
  146. JS
  147. run_js(@runner, js).strip.should == expect.strip
  148. end
  149. end
  150. end
  151. end
  152. context "running in node" do
  153. if File.exist?(NODE_PATH)
  154. before(:all) do
  155. $stdout.write "Testing in node "
  156. @runner = :node
  157. @boilerplate = MUSTACHE.dup
  158. @boilerplate << <<-JS
  159. function print(message) {
  160. console.log(message);
  161. }
  162. JS
  163. end
  164. after(:all) do
  165. puts " Done!"
  166. end
  167. it_should_behave_like "mustache rendering"
  168. else
  169. puts "Skipping tests in node (node not found)"
  170. end
  171. end
  172. context "running in SpiderMonkey (Mozilla, Firefox)" do
  173. if File.exist?(JS_PATH)
  174. before(:all) do
  175. $stdout.write "Testing in SpiderMonkey "
  176. @runner = :spidermonkey
  177. @boilerplate = MUSTACHE.dup
  178. end
  179. after(:all) do
  180. puts " Done!"
  181. end
  182. it_should_behave_like "mustache rendering"
  183. else
  184. puts "Skipping tests in SpiderMonkey (js not found)"
  185. end
  186. end
  187. context "running in JavaScriptCore (WebKit, Safari)" do
  188. if File.exist?(JSC_PATH)
  189. before(:all) do
  190. $stdout.write "Testing in JavaScriptCore "
  191. @runner = :jsc
  192. @boilerplate = MUSTACHE.dup
  193. end
  194. after(:all) do
  195. puts " Done!"
  196. end
  197. it_should_behave_like "mustache rendering"
  198. else
  199. puts "Skipping tests in JavaScriptCore (jsc not found)"
  200. end
  201. end
  202. context "running in Rhino (Mozilla, Java)" do
  203. if `java #{RHINO_JAR} 'foo' 2>&1` !~ /ClassNotFoundException/
  204. before(:all) do
  205. $stdout.write "Testing in Rhino "
  206. @runner = :rhino
  207. @boilerplate = MUSTACHE.dup
  208. end
  209. after(:all) do
  210. puts " Done!"
  211. end
  212. it_should_behave_like "mustache rendering"
  213. else
  214. puts "Skipping tests in Rhino (JAR #{RHINO_JAR} was not found)"
  215. end
  216. end
  217. context "suite" do
  218. before(:each) do
  219. $stdout.write "Verifying that we ran at the tests in at least one engine ... "
  220. end
  221. after(:each) do
  222. if @exception.nil?
  223. puts "OK"
  224. else
  225. puts "ERROR!"
  226. end
  227. end
  228. it "should have run at least one time" do
  229. $engines_run.should > 0
  230. end
  231. end
  232. end