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