|
1 | 1 | # gon_spec_rb |
2 | 2 | require 'gon' |
3 | 3 |
|
4 | | -describe Gon, '#all_variables' do |
| 4 | +describe Gon do |
5 | 5 |
|
6 | 6 | before(:each) do |
7 | 7 | Gon.request_env = {} |
8 | 8 | end |
9 | 9 |
|
10 | | - it 'returns all variables in hash' do |
11 | | - Gon.a = 1 |
12 | | - Gon.b = 2 |
13 | | - Gon.c = Gon.a + Gon.b |
14 | | - Gon.c.should == 3 |
15 | | - Gon.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3} |
16 | | - end |
| 10 | + describe '#all_variables' do |
| 11 | + |
| 12 | + it 'returns all variables in hash' do |
| 13 | + Gon.a = 1 |
| 14 | + Gon.b = 2 |
| 15 | + Gon.c = Gon.a + Gon.b |
| 16 | + Gon.c.should == 3 |
| 17 | + Gon.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3} |
| 18 | + end |
| 19 | + |
| 20 | + it 'supports all data types' do |
| 21 | + Gon.clear |
| 22 | + Gon.int = 1 |
| 23 | + Gon.float = 1.1 |
| 24 | + Gon.string = 'string' |
| 25 | + Gon.array = [ 1, 'string' ] |
| 26 | + Gon.hash_var = { :a => 1, :b => '2'} |
| 27 | + Gon.hash_w_array = { :a => [ 2, 3 ] } |
| 28 | + Gon.klass = Hash |
| 29 | + end |
17 | 30 |
|
18 | | - it 'supports all data types' do |
19 | | - Gon.clear |
20 | | - Gon.int = 1 |
21 | | - Gon.float = 1.1 |
22 | | - Gon.string = 'string' |
23 | | - Gon.array = [ 1, 'string' ] |
24 | | - Gon.hash_var = { :a => 1, :b => '2'} |
25 | | - Gon.hash_w_array = { :a => [ 2, 3 ] } |
26 | | - Gon.klass = Hash |
27 | 31 | end |
28 | 32 |
|
29 | 33 | describe '#include_gon' do |
|
66 | 70 | end |
67 | 71 |
|
68 | 72 | describe '.rabl' do |
| 73 | + |
69 | 74 | require 'rabl' |
70 | 75 | require 'gon/rabl' |
71 | 76 |
|
|
74 | 79 | controller.instance_variable_set('@objects', objects) |
75 | 80 | end |
76 | 81 |
|
77 | | - let(:controller) { ActionController::Base.new} |
78 | | - let(:objects) { [1,2]} |
| 82 | + let(:controller) { ActionController::Base.new } |
| 83 | + let(:objects) { [1,2] } |
79 | 84 |
|
80 | 85 | context 'render template with deprecation' do |
81 | 86 | it 'still works' do |
|
96 | 101 | load 'gon/rabl.rb' |
97 | 102 | end |
98 | 103 |
|
99 | | - end |
100 | | - |
101 | | - describe '.get_template_path' do |
| 104 | + context '.get_template_path' do |
| 105 | + context 'template is specified' do |
102 | 106 |
|
103 | | - context 'template is specified' do |
104 | | - it 'add the extension if not included in the template name' do |
105 | | - Gon.send(:get_template_path, { :template => 'spec/test_data/sample'}, 'rabl').should eql('spec/test_data/sample.rabl') |
106 | | - end |
| 107 | + it 'add the extension if not included in the template name' do |
| 108 | + Gon.send(:get_template_path, { :template => 'spec/test_data/sample'}, 'rabl').should eql('spec/test_data/sample.rabl') |
| 109 | + end |
107 | 110 |
|
108 | | - it 'return the specified template' do |
109 | | - Gon.send(:get_template_path, { :template => 'spec/test_data/sample.rabl'}, 'rabl').should eql('spec/test_data/sample.rabl') |
110 | | - end |
111 | | - end |
| 111 | + it 'return the specified template' do |
| 112 | + Gon.send(:get_template_path, { :template => 'spec/test_data/sample.rabl'}, 'rabl').should eql('spec/test_data/sample.rabl') |
| 113 | + end |
112 | 114 |
|
113 | | - context 'template is not specified' do |
114 | | - before do |
115 | | - Gon.clear |
116 | | - controller.instance_variable_set('@objects', objects) |
117 | | - controller.action_name = 'show' |
118 | 115 | end |
119 | 116 |
|
120 | | - let(:controller) { ActionController::Base.new} |
121 | | - let(:objects) { [1,2]} |
| 117 | + context 'template is not specified' do |
122 | 118 |
|
123 | | - context 'the action doesn as a template at a different format' do |
124 | | - it 'return the same template as the action with rabl extension' do |
125 | | - Gon.send(:get_template_path, {:controller => controller}, 'rabl').should eql('app/views/action_controller/base/show.json.rabl') |
| 119 | + before do |
| 120 | + Gon.clear |
| 121 | + controller.instance_variable_set('@objects', objects) |
| 122 | + controller.action_name = 'show' |
126 | 123 | end |
127 | | - end |
128 | | - context 'the action as a template at a different format' do |
129 | | - it 'return the same template as the action with rabl extension' do |
130 | | - Gon.send(:get_template_path, {:controller => controller}, 'rabl').should eql('app/views/action_controller/base/show.json.rabl') |
| 124 | + |
| 125 | + let(:controller) { ActionController::Base.new } |
| 126 | + let(:objects) { [1,2] } |
| 127 | + |
| 128 | + context 'the action doesn as a template at a different format' do |
| 129 | + it 'return the same template as the action with rabl extension' do |
| 130 | + Gon.send(:get_template_path, {:controller => controller}, 'rabl').should eql('app/views/action_controller/base/show.json.rabl') |
| 131 | + end |
131 | 132 | end |
| 133 | + |
132 | 134 | end |
133 | 135 | end |
| 136 | + |
134 | 137 | end |
135 | 138 |
|
136 | | - if RUBY_VERSION =~ /1.9/ |
| 139 | + if RUBY_VERSION > '1.9' |
137 | 140 | require 'jbuilder' |
138 | 141 | require 'gon/jbuilder' |
139 | 142 |
|
140 | | - it 'render json from jbuilder template' do |
141 | | - Gon.clear |
142 | | - controller = ActionController::Base.new |
143 | | - objects = [1,2] |
144 | | - controller.instance_variable_set('@objects', objects) |
145 | | - Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller |
146 | | - Gon.objects.length.should == 2 |
147 | | - end |
| 143 | + describe '.jbuilder' do |
| 144 | + context 'render jbuilder templates' do |
148 | 145 |
|
149 | | - it 'render json from jbuilder template with a partial' do |
150 | | - Gon.clear |
151 | | - controller = ActionController::Base.new |
152 | | - controller.view_paths << 'spec/test_data' |
153 | | - objects = [1,2] |
154 | | - controller.instance_variable_set('@objects', objects) |
155 | | - Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller |
156 | | - Gon.objects.length.should == 2 |
157 | | - end |
| 146 | + before do |
| 147 | + Gon.clear |
| 148 | + controller.instance_variable_set('@objects', objects) |
| 149 | + end |
| 150 | + |
| 151 | + let(:controller) { ActionController::Base.new } |
| 152 | + let(:objects) { [1,2] } |
| 153 | + |
| 154 | + it 'render json from jbuilder template' do |
| 155 | + Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller |
| 156 | + Gon.objects.length.should == 2 |
| 157 | + end |
| 158 | + |
| 159 | + it 'render json from jbuilder template with a partial' do |
| 160 | + controller.view_paths << 'spec/test_data' |
| 161 | + Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller |
| 162 | + Gon.objects.length.should == 2 |
| 163 | + end |
158 | 164 |
|
159 | | - it 'should throw error if you use gon.jbuilder with ruby < 1.9+' do |
160 | | - RUBY_VERSION = '1.8.7' |
| 165 | + end |
| 166 | + |
| 167 | + it 'should throw error if you use gon.jbuilder with ruby < 1.9+' do |
| 168 | + RUBY_VERSION = '1.8.7' |
| 169 | + |
| 170 | + expect { Gon.jbuilder 'some_path'}.to raise_error(NoMethodError, /1.9/) |
| 171 | + end |
| 172 | + |
| 173 | + it 'should raise error if you use gon.jbuilder without requiring jbuilder gem' do |
| 174 | + RUBY_VERSION = '1.9.2' |
| 175 | + Gon.send(:remove_const, :Jbuilder) |
| 176 | + |
| 177 | + expect { Gon.jbuilder 'some_path' }.to raise_error(NoMethodError, /Gemfile/) |
| 178 | + load 'jbuilder.rb' |
| 179 | + load 'gon/jbuilder.rb' |
| 180 | + end |
161 | 181 |
|
162 | | - expect { Gon.jbuilder 'some_path'}.to raise_error(NoMethodError, /1.9/) |
163 | 182 | end |
164 | 183 |
|
165 | | - it 'should raise error if you use gon.jbuilder without requiring jbuilder gem' do |
166 | | - RUBY_VERSION = '1.9.2' |
167 | | - Gon.send(:remove_const, :Jbuilder) |
| 184 | + describe '.get_template_path' do |
| 185 | + context 'template is specified' do |
168 | 186 |
|
169 | | - expect { Gon.jbuilder 'some_path' }.to raise_error(NoMethodError, /Gemfile/) |
| 187 | + it 'add the extension if not included in the template name' do |
| 188 | + Gon.send(:get_template_path, { :template => 'spec/test_data/sample'}, 'jbuilder').should eql('spec/test_data/sample.jbuilder') |
| 189 | + end |
| 190 | + |
| 191 | + it 'return the specified template' do |
| 192 | + Gon.send(:get_template_path, { :template => 'spec/test_data/sample.jbuilder'}, 'jbuilder').should eql('spec/test_data/sample.jbuilder') |
| 193 | + end |
| 194 | + |
| 195 | + end |
| 196 | + |
| 197 | + context 'template is not specified' do |
| 198 | + |
| 199 | + before do |
| 200 | + Gon.clear |
| 201 | + controller.instance_variable_set('@objects', objects) |
| 202 | + controller.action_name = 'show' |
| 203 | + end |
| 204 | + |
| 205 | + let(:controller) { ActionController::Base.new } |
| 206 | + let(:objects) { [1,2] } |
| 207 | + |
| 208 | + context 'the action doesn as a template at a different format' do |
| 209 | + it 'return the same template as the action with rabl extension' do |
| 210 | + Gon.send(:get_template_path, {:controller => controller}, 'jbuilder').should eql('app/views/action_controller/base/show.json.jbuilder') |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + end |
170 | 215 | end |
| 216 | + |
171 | 217 | end |
172 | 218 |
|
173 | 219 | def request |
|
0 commit comments