aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/abstract_controller/abstract_controller_test.rb
blob: 4834f8b7bb5ec03a3b5cf48963e2fd3f999ae963 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
$:.unshift(File.dirname(__FILE__) + '/../../lib')
$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib')

require 'test/unit'
require 'active_support'
require 'active_support/test_case'
require 'action_controller'
require 'action_view/base'

begin
  require 'ruby-debug'
  Debugger.settings[:autoeval] = true
  Debugger.start
rescue LoadError
  # Debugging disabled. `gem install ruby-debug` to enable.
end

require 'action_controller/abstract/base'
require 'action_controller/abstract/renderer'
require 'action_controller/abstract/layouts'

module AbstractController
  module Testing
  
    class SimpleController < AbstractController::Base
    end
    
    class Me < SimpleController
      def index
        self.response_body = "Hello world"
        "Something else"
      end      
    end
    
    class TestBasic < ActiveSupport::TestCase
      test "dispatching works" do
        result = Me.process(:index)
        assert_equal "Hello world", result.response_obj[:body]
      end
    end
    
    class RenderingController < AbstractController::Base
      include Renderer
            
      append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
    end
    
    class Me2 < RenderingController
      def index
        render "index.erb"
      end
      
      def action_with_ivars
        @my_ivar = "Hello"
        render "action_with_ivars.erb"
      end
      
      def naked_render
        render
      end
    end
    
    class TestRenderer < ActiveSupport::TestCase
      test "rendering templates works" do
        result = Me2.process(:index)
        assert_equal "Hello from index.erb", result.response_obj[:body]
      end
      
      test "rendering passes ivars to the view" do
        result = Me2.process(:action_with_ivars)
        assert_equal "Hello from index_with_ivars.erb", result.response_obj[:body]
      end
      
      test "rendering with no template name" do
        result = Me2.process(:naked_render)
        assert_equal "Hello from naked_render.erb", result.response_obj[:body]
      end
    end
    
    class PrefixedViews < RenderingController
      private
      def self.prefix
        name.underscore
      end
      
      def _prefix
        self.class.prefix
      end
    end
    
    class Me3 < PrefixedViews
      def index
        render
      end
      
      def formatted
        self.formats = [:html]
        render
      end
    end
    
    class TestPrefixedViews < ActiveSupport::TestCase
      test "templates are located inside their 'prefix' folder" do
        result = Me3.process(:index)
        assert_equal "Hello from me3/index.erb", result.response_obj[:body]
      end

      test "templates included their format" do
        result = Me3.process(:formatted)
        assert_equal "Hello from me3/formatted.html.erb", result.response_obj[:body]
      end
    end
    
    class WithLayouts < PrefixedViews
      include Layouts
      
      private
      def self.layout(formats)
        begin
          view_paths.find_by_parts(name.underscore, formats, "layouts")
        rescue ActionView::MissingTemplate
          begin
            view_paths.find_by_parts("application", formats, "layouts")
          rescue ActionView::MissingTemplate
          end
        end
      end
      
      def _layout
        self.class.layout(formats)
      end
    end
    
    class Me4 < WithLayouts
      def index
        render
      end
    end
    
    class Me5 < WithLayouts
      def index
        render
      end
    end
    
    class TestLayouts < ActiveSupport::TestCase
      test "layouts are included" do
        result = Me4.process(:index)
        assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", result.response_obj[:body]
      end
      
      test "it can fall back to the application layout" do
        result = Me5.process(:index)
        assert_equal "Application Enter : Hello from me5/index.erb : Exit", result.response_obj[:body]        
      end
    end
    
  end
end