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 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. end
  54. end
  55. partials.each do |testname|
  56. describe testname do
  57. it "should generate the correct html" do
  58. view, template, partial, expect =
  59. load_test(__DIR__, testname, true)
  60. runner = <<-JS
  61. try {
  62. #{@mustache}
  63. #{view};
  64. var template = #{template};
  65. var partials = {"partial": #{partial}};
  66. var result = Mustache.to_html(template, partial_context, partials);
  67. print(result);
  68. } catch(e) {
  69. print('ERROR: ' + e.message);
  70. }
  71. JS
  72. run_js(runner).should == expect
  73. end
  74. end
  75. end
  76. def run_js(js)
  77. File.open("runner.js", 'w') {|f| f << js}
  78. `js runner.js`
  79. end
  80. end