You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. non_partials.each do |testname|
  45. describe testname do
  46. it "should generate the correct html" do
  47. view, template, expect = load_test(__DIR__, testname)
  48. runner = <<-JS
  49. try {
  50. #{@mustache}
  51. #{view}
  52. var template = #{template};
  53. var result = Mustache.to_html(template, #{testname});
  54. print(result);
  55. } catch(e) {
  56. print('ERROR: ' + e.message);
  57. }
  58. JS
  59. run_js(runner).should == expect
  60. end
  61. it "should sendFun the correct html" do
  62. view, template, expect = load_test(__DIR__, testname)
  63. runner = <<-JS
  64. try {
  65. #{@mustache}
  66. #{view}
  67. var chunks = [];
  68. var sendFun = function(chunk) {
  69. if (chunk != "") {
  70. chunks.push(chunk);
  71. }
  72. }
  73. var template = #{template};
  74. Mustache.to_html(template, #{testname}, null, sendFun);
  75. print(chunks.join("\\n"));
  76. } catch(e) {
  77. print('ERROR: ' + e.message);
  78. }
  79. JS
  80. run_js(runner).strip.should == expect.strip
  81. end
  82. end
  83. end
  84. partials.each do |testname|
  85. describe testname do
  86. it "should generate the correct html" do
  87. view, template, partial, expect =
  88. load_test(__DIR__, testname, true)
  89. runner = <<-JS
  90. try {
  91. #{@mustache}
  92. #{view}
  93. var template = #{template};
  94. var partials = {"partial": #{partial}};
  95. var result = Mustache.to_html(template, partial_context, partials);
  96. print(result);
  97. } catch(e) {
  98. print('ERROR: ' + e.message);
  99. }
  100. JS
  101. run_js(runner).should == expect
  102. end
  103. it "should sendFun the correct html" do
  104. view, template, partial, expect =
  105. load_test(__DIR__, testname, true)
  106. runner = <<-JS
  107. try {
  108. #{@mustache}
  109. #{view};
  110. var template = #{template};
  111. var partials = {"partial": #{partial}};
  112. var chunks = [];
  113. var sendFun = function(chunk) {
  114. if (chunk != "") {
  115. chunks.push(chunk);
  116. }
  117. }
  118. Mustache.to_html(template, partial_context, partials, sendFun);
  119. print(chunks.join("\\n"));
  120. } catch(e) {
  121. print('ERROR: ' + e.message);
  122. }
  123. JS
  124. run_js(runner).strip.should == expect.strip
  125. end
  126. end
  127. end
  128. def run_js(js)
  129. File.open("runner.js", 'w') {|f| f << js}
  130. `js runner.js`
  131. end
  132. end