File: node.rb

package info (click to toggle)
ruby-innate 2013.02.21-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 848 kB
  • ctags: 622
  • sloc: ruby: 4,340; makefile: 2
file content (210 lines) | stat: -rw-r--r-- 4,860 bytes parent folder | download | duplicates (3)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require File.expand_path('../../../helper', __FILE__)

Innate.options.merge!(:views => 'view', :layouts => 'view')

class SpecNode
  Innate.node('/')

  def foo; end
  def bar; end
  def one(arg) end
  def two(arg1, arg2) end
  def more(*args) end
  def default(arg = nil) end

  def cat1__cat11
    'cat1: cat11'
  end

  def cat1__cat11__cat111
    'cat1: cat11: cat111'
  end

  def cat3_cat33
    'The wrong 33rd cat.'
  end
end

class SpecNodeProvide
  Innate.node('/provide')

  def foo
    '#{21 * 2}'
  end

  def bar
    '#{84 / 2}'
  end
end

class SpecNodeProvideTemplate
  Innate.node('/provide_template')

  map_views '/'
end

class SpecNodeSub < SpecNode
  map '/sub'

  def bar(arg) end
end

class SpecNodeWithLayout < SpecNodeProvide
  map '/layout'
  layout 'with_layout'

  map_layouts '/'
end

class SpecNodeWithLayoutView < SpecNodeProvide
  map '/another_layout'
  layout 'another_layout'

  map_views 'node/another_layout'
  map_layouts 'another_layout'
end

class SpecNodeWithLayoutMethod < SpecNodeProvide
  map '/layout_method'
  layout 'layout_method'

  def layout_method
    '<div class="content">#{@content}</div>'
  end
end

class SpecNodeWithLayoutMethodSymbol < SpecNodeProvide
  map '/layout_method_symbol'
  layout :layout_method

  def layout_method
    '<div class="content">#{@content}</div>'
  end
end

class SpecNodeWithLayoutMethodSymbolAndBlock < SpecNodeProvide
  map '/layout_method_symbol_block'
  layout(:layout_method) { |wish,path| true }

  def layout_method
    '<div class="content">#{@content}</div>'
  end
end


class SpecNodeIndex
  Innate.node('/spec_index')

  def index
    "I have no parameters"
  end
end

class SpecNodeAliasView < SpecNodeProvideTemplate
  map '/alias_view'
  map_views '/'

  alias_view :aliased, :bar
end

describe 'Innate::Node' do
  behaves_like :rack_test

  should 'respond with 404 if no action was found' do
    got = get('/does_not_exist')
    got.status.should == 404
    got.body.should == 'No action found at: "/does_not_exist"'
    got['Content-Type'].should == 'text/plain'
  end

  should 'wrap with layout' do
    got = get('/layout/bar')
    got.status.should == 200
    got.body.should == %(<div class="content">42</div>)
    got['Content-Type'].should == 'text/html'
  end

  should 'find layout with view_root' do
    got = get('/another_layout/bar')
    got.status.should == 200
    got.body.should == %(<div class="content">\n  42\n</div>)
    got['Content-Type'].should == 'text/html'
  end

  should 'find layout from method' do
    got = get('/layout_method/bar')
    got.status.should == 200
    got.body.should == %(<div class="content">42</div>)
    got['Content-Type'].should == 'text/html'
  end

  should 'find layout from method specified as a symbol' do
    got = get('/layout_method_symbol/bar')
    got.status.should == 200
    got.body.should == %(<div class="content">42</div>)
    got['Content-Type'].should == 'text/html'
  end

  should 'find layout from method specified as a symbol and a filter block' do
    got = get('/layout_method_symbol_block/bar')
    got.status.should == 200
    got.body.should == %(<div class="content">42</div>)
    got['Content-Type'].should == 'text/html'
  end

  should 'not get an action with wrong parameters' do
    got = get('/spec_index/bar')
    got.status.should == 404
    got.body.should == 'No action found at: "/bar"'
  end

  should 'get an action view if there is no method' do
    got = get('/provide_template/only_view')
    got.status.should == 200
    got.body.strip.should == "Only template"
    got['Content-Type'].should == 'text/html'
  end

  should 'not get an action view with params if there is no method' do
    got = get('/provide_template/only_view/param')
    got.status.should == 404
    got.body.strip.should == 'No action found at: "/only_view/param"'
  end

  should 'use alias_view' do
    got = get('/alias_view/aliased')
    got.status.should == 200
    got.body.strip.should == "<h1>Hello, World!</h1>"
    got['Content-Type'].should == 'text/html'
  end

  it "does double underscore lookup for method only" do
    got = get('/cat1/cat11')
    got.body.should == 'cat1: cat11'
  end

  it "does double double underscore lookup for method only" do
    got = get('/cat1/cat11/cat111')
    got.body.should == 'cat1: cat11: cat111'
  end

  it "resolves double underscore for template only" do
    got = get('/cat2/cat22')
    got.body.should == 'The 22nd cat.'
  end

  it "resolves double underscore for template and method" do
    got = get('/cat3/cat33')
    got.body.should == 'The right 33rd cat.'
  end

  it 'resolves normal template in subnode ' do
    got = get('/sub/baz')
    got.body.should == 'This is baz, cheer up!'
  end

  it 'resolves nested template in subnode' do
    got = get('/sub/foo/baz')
    got.body.should == 'This is foo/baz, cheer up!'
  end
end