File: tilt_coffeescripttemplate_test.rb

package info (click to toggle)
ruby-tilt 2.0.0%2Breally1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 476 kB
  • ctags: 411
  • sloc: ruby: 3,546; makefile: 5
file content (114 lines) | stat: -rw-r--r-- 4,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'contest'
require 'tilt'

begin
  require 'coffee_script'

  class CoffeeScriptTemplateTest < Test::Unit::TestCase

    unless method_defined?(:assert_not_match)
      # assert_not_match is missing on 1.8.7, which uses assert_no_match
      def assert_not_match(a, b)
        unless a.kind_of?(Regexp)
          a = Regexp.new(Regexp.escape(a))
        end
        assert_no_match(a,b)
      end
    end

    test "is registered for '.coffee' files" do
      assert_equal Tilt::CoffeeScriptTemplate, Tilt['test.coffee']
    end

    test "bare is disabled by default" do
      assert_equal false, Tilt::CoffeeScriptTemplate.default_bare
    end

    test "compiles and evaluates the template on #render" do
      template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
      assert_match "puts('Hello, World!');", template.render
    end

    test "can be rendered more than once" do
      template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
      3.times { assert_match "puts('Hello, World!');", template.render }
    end

    test "disabling coffee-script wrapper" do
      str = 'name = "Josh"; puts "Hello #{name}"'

      template = Tilt::CoffeeScriptTemplate.new { str }
      assert_match "(function() {", template.render
      assert_match "puts(\"Hello \" + name);\n", template.render

      template = Tilt::CoffeeScriptTemplate.new(:bare => true) { str }
      assert_not_match "(function() {", template.render
      assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render

      template2 = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { str}
      assert_not_match "(function() {", template.render
      assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
    end

    context "wrapper globally enabled" do
      setup do
        @bare = Tilt::CoffeeScriptTemplate.default_bare
        Tilt::CoffeeScriptTemplate.default_bare = false
      end

      teardown do
        Tilt::CoffeeScriptTemplate.default_bare = @bare
      end

      test "no options" do
        template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
        assert_match "puts(\"Hello, \" + name);", template.render
        assert_match "(function() {", template.render
      end

      test "overridden by :bare" do
        template = Tilt::CoffeeScriptTemplate.new(:bare => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
        assert_match "puts(\"Hello, \" + name);", template.render
        assert_not_match "(function() {", template.render
      end

      test "overridden by :no_wrap" do
        template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
        assert_match "puts(\"Hello, \" + name);", template.render
        assert_not_match "(function() {", template.render
      end
    end

    context "wrapper globally disabled" do
      setup do
        @bare = Tilt::CoffeeScriptTemplate.default_bare
        Tilt::CoffeeScriptTemplate.default_bare = true
      end

      teardown do
        Tilt::CoffeeScriptTemplate.default_bare = @bare
      end

      test "no options" do
        template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
        assert_match "puts(\"Hello, \" + name);", template.render
        assert_not_match "(function() {", template.render
      end

      test "overridden by :bare" do
        template = Tilt::CoffeeScriptTemplate.new(:bare => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
        assert_match "puts(\"Hello, \" + name);", template.render
        assert_match "(function() {", template.render
      end

      test "overridden by :no_wrap" do
        template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
        assert_match "puts(\"Hello, \" + name);", template.render
        assert_match "(function() {", template.render
      end
    end
  end

rescue LoadError => boom
  warn "Tilt::CoffeeScriptTemplate (disabled)"
end