Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

139 lines
3.5KB

  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. chunks.push(chunk);
  62. }
  63. var template = #{template};
  64. Mustache.to_html(template, #{testname}, null, sendFun);
  65. print(chunks.join("\\n"));
  66. } catch(e) {
  67. print('ERROR: ' + e.message);
  68. }
  69. JS
  70. run_js(runner).strip.should == expect.strip
  71. end
  72. end
  73. end
  74. partials.each do |testname|
  75. describe testname do
  76. it "should generate the correct html" do
  77. view, template, partial, expect =
  78. load_test(__DIR__, testname, true)
  79. runner = <<-JS
  80. try {
  81. #{@mustache}
  82. #{view};
  83. var template = #{template};
  84. var partials = {"partial": #{partial}};
  85. var result = Mustache.to_html(template, partial_context, partials);
  86. print(result);
  87. } catch(e) {
  88. print('ERROR: ' + e.message);
  89. }
  90. JS
  91. run_js(runner).should == expect
  92. end
  93. it "should sendFun the correct html" do
  94. view, template, partial, expect =
  95. load_test(__DIR__, testname, true)
  96. runner = <<-JS
  97. try {
  98. #{@mustache}
  99. #{view};
  100. var template = #{template};
  101. var partials = {"partial": #{partial}};
  102. var result = Mustache.to_html(template, partial_context, partials);
  103. print(result);
  104. } catch(e) {
  105. print('ERROR: ' + e.message);
  106. }
  107. JS
  108. run_js(runner).strip.should == expect.strip
  109. end
  110. end
  111. end
  112. def run_js(js)
  113. File.open("runner.js", 'w') {|f| f << js}
  114. `js runner.js`
  115. end
  116. end