rocketio 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +0,0 @@
1
- module RocketIO
2
- class TemplateError < RuntimeError; end
3
- class LayoutError < RuntimeError; end
4
- end
@@ -1 +0,0 @@
1
- <%= @var %>
@@ -1 +0,0 @@
1
- <%= var %>
@@ -1,71 +0,0 @@
1
- require 'setup'
2
-
3
- spec :engine do
4
- it 'inherits engine from superclass' do
5
- a = mock_controller {
6
- engine :Slim
7
- }
8
- b = mock_controller(a) {
9
- }
10
- assert(b.new.engine) == [:SlimTemplate, []]
11
- end
12
-
13
- it 'overrides engine inherited from superclass' do
14
- a = mock_controller {
15
- engine :Slim
16
- }
17
- b = mock_controller(a) {
18
- engine :Turbo
19
- }
20
- assert(b.new.engine) == [:TurboTemplate, []]
21
- end
22
-
23
- it 'can use include to override engine inherited from superclass' do
24
- a = mock_controller {
25
- engine :Slim
26
- }
27
- b = mock_controller(a) {
28
- engine :Turbo
29
- }
30
- c = mock_controller(a) {
31
- import :engine, from: b
32
- }
33
- assert(c.new.engine) == [:TurboTemplate, []]
34
- end
35
-
36
- it 'uses a block (that runs at instance level) to define engine' do
37
- app mock_controller {
38
- engine {@x = 'x'; :Slim}
39
- def index; engine[0].to_s + @x end
40
- }
41
- get
42
- assert(last_response.body) == 'SlimTemplatex'
43
- end
44
-
45
- it 'can accept engine options when a block used' do
46
- c = mock_controller {
47
- engine {[:Slim, {pretty_print: true}]}
48
- }
49
- e,o = c.allocate.engine
50
- assert(e) == :SlimTemplate
51
- assert(o) == [{pretty_print: true}]
52
- end
53
-
54
- it 'uses inherited engine if given block returns no engine' do
55
- a = mock_controller {
56
- engine :Slim
57
- }
58
- b = mock_controller(a) {
59
- engine {
60
- :ERB if env['IAMABOT']
61
- }
62
- def index; engine[0].to_s end
63
- }
64
- app b
65
- get
66
- assert(last_response.body) == 'SlimTemplate'
67
- env['IAMABOT'] = true
68
- get
69
- assert(last_response.body) == 'ERBTemplate'
70
- end
71
- end
@@ -1 +0,0 @@
1
- <%= url %>
@@ -1 +0,0 @@
1
- items
@@ -1 +0,0 @@
1
- =<%= yield %>=
@@ -1,123 +0,0 @@
1
- require 'setup'
2
-
3
- spec :Views do
4
- context :layouts do
5
- it 'inherits layout from superclass' do
6
- a = mock_controller {
7
- layout :master
8
- }
9
- b = mock_controller(a) {
10
- }
11
- assert(b.new.layout) == :master
12
- end
13
-
14
- it 'overrides layout inherited from superclass' do
15
- a = mock_controller {
16
- layout :master
17
- }
18
- b = mock_controller(a) {
19
- layout :baster
20
- }
21
- assert(b.new.layout) == :baster
22
- end
23
-
24
- it 'can use `inherit` to override layout inherited from superclass' do
25
- a = mock_controller {
26
- layout :master
27
- }
28
- b = mock_controller(a) {
29
- layout :baster
30
- }
31
- c = mock_controller(a) {
32
- import :layout, from: b
33
- }
34
- assert(c.new.layout) == :baster
35
- end
36
-
37
- it 'renders without layout if no default layout set' do
38
- app mock_controller {
39
- def index; render; end
40
- }
41
- get
42
- assert(last_response).is_ok_with_body('/')
43
- end
44
-
45
- it 'renders without layout if default layout set to false' do
46
- app mock_controller {
47
- layout false
48
- def index; render; end
49
- }
50
- get
51
- assert(last_response).is_ok_with_body('/')
52
- end
53
-
54
- it 'renders without layout if layout option set to nil or false' do
55
- app mock_controller {
56
- layout :layout
57
- def var; @var end
58
- def index; render(layout: nil); end
59
- def b; @var = 'x'; render(layout: false); end
60
- def c; @var = 'y'; render; end
61
- }
62
- get
63
- assert(last_response).is_ok_with_body('/')
64
-
65
- get :b
66
- assert(last_response).is_ok_with_body('x')
67
-
68
- get :c
69
- assert(last_response).is_ok_with_body("=y=\n")
70
- end
71
-
72
- it 'renders with a defined layout' do
73
- app mock_controller {
74
- define_layout(:master) {'=<%= yield %>='}
75
- layout :master
76
- def index; render; end
77
- }
78
- get
79
- assert(last_response).is_ok_with_body('=/=')
80
- end
81
-
82
- it 'prefers defined layouts over files' do
83
- app mock_controller {
84
- define_layout(:layout) {'-<%= yield %>-'}
85
- layout :layout
86
- def index; render; end
87
- }
88
- get
89
- assert(last_response).is_ok_with_body('-/-')
90
- end
91
-
92
- it 'prefers explicit layouts over implicit ones' do
93
- app mock_controller {
94
- define_layout(:x) {'x<%= yield %>'}
95
- define_layout(:y) {'y<%= yield %>'}
96
- layout :x
97
- def index; render {''}; end
98
- def b; render(layout: :y) {''}; end
99
- }
100
-
101
- get
102
- assert(last_response).is_ok_with_body('x')
103
-
104
- get :b
105
- assert(last_response).is_ok_with_body('y')
106
- end
107
-
108
- it 'accepts layout as :template option' do
109
- app mock_controller {
110
- def index; render_layout(template: '+<%= yield %>+') {'='}; end
111
- }
112
- get
113
- assert(last_response).is_ok_with_body('+=+')
114
- end
115
-
116
- it 'raises ArgumentError if both layout name and :template option given' do
117
- app mock_controller {
118
- def index; render_layout(:x, template: '+<%= yield %>+') {'='}; end
119
- }
120
- assert {get}.raise ArgumentError, /both/i
121
- end
122
- end
123
- end
@@ -1 +0,0 @@
1
- outside <%= yield %> layout
@@ -1,145 +0,0 @@
1
- require 'setup'
2
-
3
- spec :layouts do
4
- context :compositing do
5
- it 'inherits layouts from superclass' do
6
- a = mock_controller {
7
- define_layout(:a) {'a'}
8
- }
9
- b = mock_controller(a) {
10
- }.new
11
- assert(b.__send__ b.layouts[:a]) == 'a'
12
- end
13
-
14
- it 'complements layouts inherited from superclass' do
15
- a = mock_controller {
16
- define_layout(:a) {'a'}
17
- }
18
- b = mock_controller(a) {
19
- define_layout(:b) {'b'}
20
- }.new
21
- assert(b.__send__ b.layouts[:a]) == 'a'
22
- assert(b.__send__ b.layouts[:b]) == 'b'
23
- end
24
-
25
- it 'overrides layouts inherited from superclass' do
26
- a = mock_controller {
27
- define_layout(:x) {'a'}
28
- }
29
- b = mock_controller(a) {
30
- define_layout(:x) {'b'}
31
- }.new
32
- assert(b.__send__ b.layouts[:x]) == 'b'
33
- end
34
-
35
- test '`inherit` overrides layouts inherited from superclass' do
36
- a = mock_controller {
37
- define_layout(:x) {'a'}
38
- }
39
- b = mock_controller(a) {
40
- define_layout(:x) {'b'}
41
- }
42
- c = mock_controller(a) {
43
- import :layouts, from: b
44
- }.new
45
- assert(c.__send__ c.layouts[:x]) == 'b'
46
- end
47
-
48
- test '`inherit` complements layouts inherited from superclass' do
49
- a = mock_controller {
50
- define_layout(:a) {'a'}
51
- }
52
- b = mock_controller(a) {
53
- define_layout(:b) {'b'}
54
- }
55
- c = mock_controller(a) {
56
- import :layouts, from: b
57
- }.new
58
- assert(c.__send__ c.layouts[:a]) == 'a'
59
- assert(c.__send__ c.layouts[:b]) == 'b'
60
- end
61
-
62
- test '`inherit` overrides defined layouts' do
63
- a = mock_controller {
64
- define_layout(:x) {'a'}
65
- }
66
- b = mock_controller {
67
- define_layout(:x) {'b'}
68
- import :layouts, from: a
69
- }.new
70
- assert(b.__send__ b.layouts[:x]) == 'a'
71
- end
72
-
73
- test '`inherit` complements defined layouts' do
74
- a = mock_controller {
75
- define_layout(:a) {'a'}
76
- }
77
- b = mock_controller {
78
- define_layout(:b) {'b'}
79
- import :layouts, from: a
80
- }.new
81
- assert(b.__send__ b.layouts[:a]) == 'a'
82
- assert(b.__send__ b.layouts[:b]) == 'b'
83
- end
84
-
85
- it 'overrides layouts inherited via `inherit`' do
86
- a = mock_controller {
87
- define_layout(:x) {'a'}
88
- }
89
- b = mock_controller {
90
- import :layouts, from: a
91
- define_layout(:x) {'b'}
92
- }.new
93
- assert(b.__send__ b.layouts[:x]) == 'b'
94
- end
95
- end
96
-
97
- context :rendering do
98
- it 'uses a file with same name if only name given' do
99
- c = mock_controller {
100
- define_layout :master
101
- }.new
102
- assert(c.render_layout(:master) {'yo'}) == "master yo layout\n"
103
- end
104
-
105
- it 'raises a TemplateError if there is no file with same name' do
106
- layout = rand.to_s
107
- c = mock_controller {
108
- define_layout layout
109
- }.new
110
- assert {c.render_layout(layout) {}}.raise RocketIO::TemplateError
111
- end
112
-
113
- it 'uses given file - file resides in controller dirname' do
114
- c = mock_controller {
115
- define_layout :master, file: :layout
116
- }.new
117
- assert(c.render_layout(:master) {'yo'}) == "=yo=\n"
118
- end
119
-
120
- it 'uses given file - file resides outside controller dirname' do
121
- c = mock_controller {
122
- define_layout :master, file: './layouts/master'
123
- }.new
124
- assert(c.render_layout(:master) {'yo'}) == "outside yo layout\n"
125
- end
126
-
127
- it 'accepts a block for given file' do
128
- c = mock_controller {
129
- define_layout :master, file: -> {@outside ? './layouts/master' : :master}
130
- }.new
131
- assert(c.render_layout(:master) {'yo'}) == "master yo layout\n"
132
- c.instance_variable_set(:@outside, true)
133
- assert(c.render_layout(:master) {'yo'}) == "outside yo layout\n"
134
- end
135
-
136
- it 'accepts a block and uses returned value as layout without searching for file' do
137
- c = mock_controller {
138
- define_layout(:master) {@admin ? ':<%= yield %>:' : '|<%= yield %>|'}
139
- }.new
140
- assert(c.render_layout(:master) {'yo'}) == "|yo|"
141
- c.instance_variable_set(:@admin, true)
142
- assert(c.render_layout(:master) {'yo'}) == ":yo:"
143
- end
144
- end
145
- end
@@ -1 +0,0 @@
1
- master <%= yield %> layout
@@ -1,104 +0,0 @@
1
- require 'setup'
2
-
3
- spec :Views do
4
- context :cache do
5
-
6
- engine = Tilt::ERBTemplate
7
- path = File.expand_path('../templates/a', __FILE__)
8
- define_method(:file) {mock_controller(:a).new.send(:find_template, path, 'get', engine)}
9
- define_method(:read) {mock_controller(:a).new.send(:read_template, file).__id__}
10
- define_method(:compile) {|tpl| mock_controller(:a).new.send(:compile_template, tpl, engine).__id__}
11
-
12
- it 'resolves template path only once' do
13
- cache_id = file.__id__
14
- assert(file.__id__) == cache_id
15
- end
16
-
17
- it 'rereads templates only when template file modified' do
18
- cache_id = read
19
- assert(read) == cache_id
20
- FileUtils.touch(file)
21
- refute(read) == cache_id
22
- end
23
-
24
- it 'recompiles template only if it was modified' do
25
- cache_id = compile('')
26
- assert(compile('')) == cache_id
27
- refute(compile('changed')) == cache_id
28
- end
29
- end
30
-
31
- context :scope do
32
-
33
- it 'uses controller instance for scope' do
34
- app mock_controller {
35
- before {@var = :val}
36
- def b; render; end
37
- }
38
- get :b
39
- assert(last_response).is_ok_with_body('val')
40
- end
41
-
42
- it 'uses custom scope' do
43
- app mock_controller {
44
- before {@var = :val}
45
- def b
46
- scope = Class.new {def initialize; @var = :vax; end}.new
47
- render(scope: scope)
48
- end
49
- }
50
- get :b
51
- assert(last_response).is_ok_with_body('vax')
52
- end
53
- end
54
-
55
- context :locals do
56
- it 'makes :locals accessible via attributes' do
57
- app mock_controller {
58
- def c; render(locals: {var: :val}); end
59
- }
60
- get :c
61
- assert(last_response).is_ok_with_body('val')
62
- end
63
- end
64
-
65
- context 'implicit templates' do
66
- it 'uses the folder controller resides in as default path' do
67
- app controller = mock_controller(:a) {
68
- def index; render end
69
- }
70
- get
71
- assert(last_response).is_ok_with_body '/a'
72
- end
73
- end
74
-
75
- context 'defined templates' do
76
- it 'prefers defined templates over files' do
77
- app mock_controller {
78
- define_template(:index) {'x'}
79
- def index; render end
80
- }
81
- get
82
- assert(last_response).is_ok_with_body 'x'
83
- end
84
-
85
- it 'renders defined template with defined layout' do
86
- app mock_controller {
87
- define_template(:t) {'t'}
88
- define_layout(:x) {'x<%= yield %>x'}
89
- layout(:x)
90
- def index; render(:t) end
91
- }
92
- get
93
- assert(last_response).is_ok_with_body 'xtx'
94
- end
95
- end
96
-
97
- it 'uses given block for template' do
98
- app mock_controller {
99
- def index; render {'x'} end
100
- }
101
- get
102
- assert(last_response).is_ok_with_body 'x'
103
- end
104
- end