Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

173 řádky
4.2KB

  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. end
  24. it "should return the same when invoked multiple times" do
  25. js = <<-JS
  26. #{@mustache}
  27. Mustache.to_html("x")
  28. print(Mustache.to_html("x"));
  29. JS
  30. run_js(js).should == "x\n"
  31. end
  32. it "should clear the context after each run" do
  33. js = <<-JS
  34. #{@mustache}
  35. Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
  36. try {
  37. print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
  38. } catch(e) {
  39. print('ERROR: ' + e.message);
  40. }
  41. JS
  42. run_js(js).should == "\n"
  43. end
  44. it "should not double-render" do
  45. js = <<-JS
  46. #{@mustache}
  47. var template = "{{#foo}}{{bar}}{{/foo}}";
  48. var ctx = {
  49. foo: true,
  50. bar: "{{win}}",
  51. win: "FAIL"
  52. };
  53. print(Mustache.to_html(template, ctx))
  54. JS
  55. run_js(js).strip.should == "{{win}}"
  56. end
  57. non_partials.each do |testname|
  58. describe testname do
  59. it "should generate the correct html" do
  60. view, template, expect = load_test(__DIR__, testname)
  61. runner = <<-JS
  62. try {
  63. #{@mustache}
  64. #{view}
  65. var template = #{template};
  66. var result = Mustache.to_html(template, #{testname});
  67. print(result);
  68. } catch(e) {
  69. print('ERROR: ' + e.message);
  70. }
  71. JS
  72. run_js(runner).should == expect
  73. end
  74. it "should sendFun the correct html" do
  75. view, template, expect = load_test(__DIR__, testname)
  76. runner = <<-JS
  77. try {
  78. #{@mustache}
  79. #{view}
  80. var chunks = [];
  81. var sendFun = function(chunk) {
  82. if (chunk != "") {
  83. chunks.push(chunk);
  84. }
  85. }
  86. var template = #{template};
  87. Mustache.to_html(template, #{testname}, null, sendFun);
  88. print(chunks.join("\\n"));
  89. } catch(e) {
  90. print('ERROR: ' + e.message);
  91. }
  92. JS
  93. run_js(runner).strip.should == expect.strip
  94. end
  95. end
  96. end
  97. partials.each do |testname|
  98. describe testname do
  99. it "should generate the correct html" do
  100. view, template, partial, expect =
  101. load_test(__DIR__, testname, true)
  102. runner = <<-JS
  103. try {
  104. #{@mustache}
  105. #{view}
  106. var template = #{template};
  107. var partials = {"partial": #{partial}};
  108. var result = Mustache.to_html(template, partial_context, partials);
  109. print(result);
  110. } catch(e) {
  111. print('ERROR: ' + e.message);
  112. }
  113. JS
  114. run_js(runner).should == expect
  115. end
  116. it "should sendFun the correct html" do
  117. view, template, partial, expect =
  118. load_test(__DIR__, testname, true)
  119. runner = <<-JS
  120. try {
  121. #{@mustache}
  122. #{view};
  123. var template = #{template};
  124. var partials = {"partial": #{partial}};
  125. var chunks = [];
  126. var sendFun = function(chunk) {
  127. if (chunk != "") {
  128. chunks.push(chunk);
  129. }
  130. }
  131. Mustache.to_html(template, partial_context, partials, sendFun);
  132. print(chunks.join("\\n"));
  133. } catch(e) {
  134. print('ERROR: ' + e.message);
  135. }
  136. JS
  137. run_js(runner).strip.should == expect.strip
  138. end
  139. end
  140. end
  141. def run_js(js)
  142. File.open("runner.js", 'w') {|f| f << js}
  143. `js runner.js`
  144. end
  145. end