Skip to content

Commit 12a904a

Browse files
committed
Some new specs
1 parent d77aaf0 commit 12a904a

File tree

2 files changed

+119
-73
lines changed

2 files changed

+119
-73
lines changed

lib/gon.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
if RUBY_VERSION =~ /9/ && defined?(Jbuilder)
2+
if RUBY_VERSION > '1.9' && defined?(Jbuilder)
33
gem 'blankslate'
44
end
55
require 'action_view'
@@ -9,7 +9,7 @@
99
if defined?(Rabl)
1010
require 'gon/rabl'
1111
end
12-
if RUBY_VERSION =~ /9/ && defined?(Jbuilder)
12+
if RUBY_VERSION > '1.9' && defined?(Jbuilder)
1313
require 'gon/jbuilder'
1414
end
1515

@@ -45,7 +45,7 @@ def request=(request_id)
4545

4646
def method_missing(m, *args, &block)
4747
if ( m.to_s =~ /=$/ )
48-
if public_methods.include?(RUBY_VERSION =~ /1.9/ ? m.to_s[0..-2].to_sym : m.to_s[0..-2])
48+
if public_methods.include?(RUBY_VERSION > '1.9' ? m.to_s[0..-2].to_sym : m.to_s[0..-2])
4949
raise "You can't use Gon public methods for storing data"
5050
end
5151
set_variable(m.to_s.delete('='), args[0])
@@ -91,7 +91,7 @@ def set_variable(name, value)
9191
alias_method :orig_jbuilder, :jbuilder
9292

9393
def jbuilder(*options)
94-
raise NoMethodError.new('You can use Jbuilder support only in 1.9+') if RUBY_VERSION !~ /9/
94+
raise NoMethodError.new('You can use Jbuilder support only in 1.9+') if RUBY_VERSION < '1.9'
9595
orig_jbuilder(*options)
9696
end
9797

spec/gon/gon_spec.rb

Lines changed: 115 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
# gon_spec_rb
22
require 'gon'
33

4-
describe Gon, '#all_variables' do
4+
describe Gon do
55

66
before(:each) do
77
Gon.request_env = {}
88
end
99

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
1730

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
2731
end
2832

2933
describe '#include_gon' do
@@ -66,6 +70,7 @@
6670
end
6771

6872
describe '.rabl' do
73+
6974
require 'rabl'
7075
require 'gon/rabl'
7176

@@ -74,8 +79,8 @@
7479
controller.instance_variable_set('@objects', objects)
7580
end
7681

77-
let(:controller) { ActionController::Base.new}
78-
let(:objects) { [1,2]}
82+
let(:controller) { ActionController::Base.new }
83+
let(:objects) { [1,2] }
7984

8085
context 'render template with deprecation' do
8186
it 'still works' do
@@ -96,78 +101,119 @@
96101
load 'gon/rabl.rb'
97102
end
98103

99-
end
100-
101-
describe '.get_template_path' do
104+
context '.get_template_path' do
105+
context 'template is specified' do
102106

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
107110

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
112114

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'
118115
end
119116

120-
let(:controller) { ActionController::Base.new}
121-
let(:objects) { [1,2]}
117+
context 'template is not specified' do
122118

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'
126123
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
131132
end
133+
132134
end
133135
end
136+
134137
end
135138

136-
if RUBY_VERSION =~ /1.9/
139+
if RUBY_VERSION > '1.9'
137140
require 'jbuilder'
138141
require 'gon/jbuilder'
139142

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
148145

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
158164

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
161181

162-
expect { Gon.jbuilder 'some_path'}.to raise_error(NoMethodError, /1.9/)
163182
end
164183

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
168186

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
170215
end
216+
171217
end
172218

173219
def request

0 commit comments

Comments
 (0)