dbgrandi-googlecharts 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,411 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require File.dirname(__FILE__) + '/../lib/gchart'
3
+
4
+ # Time to add your specs!
5
+ # http://rspec.rubyforge.org/
6
+ describe "generating a default Gchart" do
7
+
8
+ before(:each) do
9
+ @chart = Gchart.line
10
+ end
11
+
12
+ it "should include the Google URL" do
13
+ @chart.include?("http://chart.apis.google.com/chart?").should be_true
14
+ end
15
+
16
+ it "should have a default size" do
17
+ @chart.include?('chs=300x200').should be_true
18
+ end
19
+
20
+ it "should be able to have a custom size" do
21
+ Gchart.line(:size => '400x600').include?('chs=400x600').should be_true
22
+ Gchart.line(:width => 400, :height => 600).include?('chs=400x600').should be_true
23
+ end
24
+
25
+ it "should have a type" do
26
+ @chart.include?('cht=lc').should be_true
27
+ end
28
+
29
+ it "should use the simple encoding by default with auto max value" do
30
+ # 9 is the max value in simple encoding, 26 being our max value the 2nd encoded value should be 9
31
+ Gchart.line(:data => [0, 26]).include?('chd=s:A9').should be_true
32
+ Gchart.line(:data => [0, 26], :max_value => 26).should == Gchart.line(:data => [0, 26])
33
+ end
34
+
35
+ it "should support simple encoding with and without max_value" do
36
+ Gchart.line(:data => [0, 26], :max_value => 26).include?('chd=s:A9').should be_true
37
+ Gchart.line(:data => [0, 26], :max_value => false).include?('chd=s:Aa').should be_true
38
+ end
39
+
40
+ it "should support the extended encoding and encode properly" do
41
+ Gchart.line(:data => [0, 10], :encoding => 'extended', :max_value => false).include?('chd=e:AA').should be_true
42
+ Gchart.line(:encoding => 'extended',
43
+ :max_value => false,
44
+ :data => [[0,25,26,51,52,61,62,63], [64,89,90,115,4084]]
45
+ ).include?('chd=e:AAAZAaAzA0A9A-A.,BABZBaBz.0').should be_true
46
+ end
47
+
48
+ it "should auto set the max value for extended encoding" do
49
+ Gchart.line(:data => [0, 25], :encoding => 'extended', :max_value => false).include?('chd=e:AAAZ').should be_true
50
+ # Extended encoding max value is '..'
51
+ Gchart.line(:data => [0, 25], :encoding => 'extended').include?('chd=e:AA..').should be_true
52
+ end
53
+
54
+ it "should be able to have data with text encoding" do
55
+ Gchart.line(:data => [10, 5.2, 4, 45, 78], :encoding => 'text').include?('chd=t:10,5.2,4,45,78').should be_true
56
+ end
57
+
58
+ it "should be able to have muliple set of data with text encoding" do
59
+ Gchart.line(:data => [[10, 5.2, 4, 45, 78], [20, 40, 70, 15, 99]], :encoding => 'text').include?(Gchart.jstize('chd=t:10,5.2,4,45,78|20,40,70,15,99')).should be_true
60
+ end
61
+
62
+ it "should be able to receive a custom param" do
63
+ Gchart.line(:custom => 'ceci_est_une_pipe').include?('ceci_est_une_pipe').should be_true
64
+ end
65
+
66
+ it "should be able to set label axis" do
67
+ Gchart.line(:axis_with_labels => 'x,y,r').include?('chxt=x,y,r').should be_true
68
+ Gchart.line(:axis_with_labels => ['x','y','r']).include?('chxt=x,y,r').should be_true
69
+ end
70
+
71
+ it "should be able to have axis labels" do
72
+ Gchart.line(:axis_labels => ['Jan|July|Jan|July|Jan', '0|100', 'A|B|C', '2005|2006|2007']).include?(Gchart.jstize('chxl=0:|Jan|July|Jan|July|Jan|1:|0|100|2:|A|B|C|3:|2005|2006|2007')).should be_true
73
+ Gchart.line(:axis_labels => ['Jan|July|Jan|July|Jan']).include?(Gchart.jstize('chxl=0:|Jan|July|Jan|July|Jan')).should be_true
74
+ Gchart.line(:axis_labels => [['Jan','July','Jan','July','Jan']]).include?(Gchart.jstize('chxl=0:|Jan|July|Jan|July|Jan')).should be_true
75
+ Gchart.line(:axis_labels => [['Jan','July','Jan','July','Jan'], ['0','100'], ['A','B','C'], ['2005','2006','2007']]).include?(Gchart.jstize('chxl=0:|Jan|July|Jan|July|Jan|1:|0|100|2:|A|B|C|3:|2005|2006|2007')).should be_true
76
+ end
77
+
78
+ end
79
+
80
+ describe "generating different type of charts" do
81
+
82
+ it "should be able to generate a line chart" do
83
+ Gchart.line.should be_an_instance_of(String)
84
+ Gchart.line.include?('cht=lc').should be_true
85
+ end
86
+
87
+ it "should be able to generate a sparkline chart" do
88
+ Gchart.sparkline.should be_an_instance_of(String)
89
+ Gchart.sparkline.include?('cht=ls').should be_true
90
+ end
91
+
92
+ it "should be able to generate a line xy chart" do
93
+ Gchart.line_xy.should be_an_instance_of(String)
94
+ Gchart.line_xy.include?('cht=lxy').should be_true
95
+ end
96
+
97
+ it "should be able to generate a scatter chart" do
98
+ Gchart.scatter.should be_an_instance_of(String)
99
+ Gchart.scatter.include?('cht=s').should be_true
100
+ end
101
+
102
+ it "should be able to generate a bar chart" do
103
+ Gchart.bar.should be_an_instance_of(String)
104
+ Gchart.bar.include?('cht=bvs').should be_true
105
+ end
106
+
107
+ it "should be able to generate a Venn diagram" do
108
+ Gchart.venn.should be_an_instance_of(String)
109
+ Gchart.venn.include?('cht=v').should be_true
110
+ end
111
+
112
+ it "should be able to generate a Pie Chart" do
113
+ Gchart.pie.should be_an_instance_of(String)
114
+ Gchart.pie.include?('cht=p').should be_true
115
+ end
116
+
117
+ it "should be able to generate a Google-O-Meter" do
118
+ Gchart.meter.should be_an_instance_of(String)
119
+ Gchart.meter.include?('cht=gom').should be_true
120
+ end
121
+
122
+ it "should not support other types" do
123
+ Gchart.sexy.should == "sexy is not a supported chart format, please use one of the following: #{Gchart.supported_types}."
124
+ end
125
+
126
+ end
127
+
128
+ describe "a bar graph" do
129
+
130
+ it "should have a default vertical orientation" do
131
+ Gchart.bar.include?('cht=bvs').should be_true
132
+ end
133
+
134
+ it "should be able to have a different orientation" do
135
+ Gchart.bar(:orientation => 'vertical').include?('cht=bvs').should be_true
136
+ Gchart.bar(:orientation => 'v').include?('cht=bvs').should be_true
137
+ Gchart.bar(:orientation => 'h').include?('cht=bhs').should be_true
138
+ Gchart.bar(:orientation => 'horizontal').include?('cht=bhs').should be_true
139
+ Gchart.bar(:horizontal => false).include?('cht=bvs').should be_true
140
+ end
141
+
142
+ it "should be set to be stacked by default" do
143
+ Gchart.bar.include?('cht=bvs').should be_true
144
+ end
145
+
146
+ it "should be able to stacked or grouped" do
147
+ Gchart.bar(:stacked => true).include?('cht=bvs').should be_true
148
+ Gchart.bar(:stacked => false).include?('cht=bvg').should be_true
149
+ Gchart.bar(:grouped => true).include?('cht=bvg').should be_true
150
+ Gchart.bar(:grouped => false).include?('cht=bvs').should be_true
151
+ end
152
+
153
+ it "should be able to have different bar colors" do
154
+ Gchart.bar(:bar_colors => 'efefef,00ffff').include?('chco=').should be_true
155
+ Gchart.bar(:bar_colors => 'efefef,00ffff').include?('chco=efefef,00ffff').should be_true
156
+ # alias
157
+ Gchart.bar(:bar_color => 'efefef').include?('chco=efefef').should be_true
158
+ end
159
+
160
+ it "should be able to have different bar colors when using an array of colors" do
161
+ Gchart.bar(:bar_colors => ['efefef','00ffff']).include?('chco=efefef,00ffff').should be_true
162
+ end
163
+
164
+ it 'should be able to accept a string of width and spacing options' do
165
+ Gchart.bar(:bar_width_and_spacing => '25,6').include?('chbh=25,6').should be_true
166
+ end
167
+
168
+ it 'should be able to accept a single fixnum width and spacing option to set the bar width' do
169
+ Gchart.bar(:bar_width_and_spacing => 25).include?('chbh=25').should be_true
170
+ end
171
+
172
+ it 'should be able to accept an array of width and spacing options' do
173
+ Gchart.bar(:bar_width_and_spacing => [25,6,12]).include?('chbh=25,6,12').should be_true
174
+ Gchart.bar(:bar_width_and_spacing => [25,6]).include?('chbh=25,6').should be_true
175
+ Gchart.bar(:bar_width_and_spacing => [25]).include?('chbh=25').should be_true
176
+ end
177
+
178
+ describe "with a hash of width and spacing options" do
179
+
180
+ before(:each) do
181
+ @default_width = 23
182
+ @default_spacing = 4
183
+ @default_group_spacing = 8
184
+ end
185
+
186
+ it 'should be able to have a custom bar width' do
187
+ Gchart.bar(:bar_width_and_spacing => {:width => 19}).include?("chbh=19,#{@default_spacing},#{@default_group_spacing}").should be_true
188
+ end
189
+
190
+ it 'should be able to have custom spacing' do
191
+ Gchart.bar(:bar_width_and_spacing => {:spacing => 19}).include?("chbh=#{@default_width},19,#{@default_group_spacing}").should be_true
192
+ end
193
+
194
+ it 'should be able to have custom group spacing' do
195
+ Gchart.bar(:bar_width_and_spacing => {:group_spacing => 19}).include?("chbh=#{@default_width},#{@default_spacing},19").should be_true
196
+ end
197
+
198
+ end
199
+
200
+ end
201
+
202
+ describe "a line chart" do
203
+
204
+ before(:each) do
205
+ @title = 'Chart Title'
206
+ @legend = ['first data set label', 'n data set label']
207
+ @chart = Gchart.line(:title => @title, :legend => @legend)
208
+ end
209
+
210
+ it 'should be able have a chart title' do
211
+ @chart.include?("chtt=Chart+Title").should be_true
212
+ end
213
+
214
+ it "should be able to a custom color and size title" do
215
+ Gchart.line(:title => @title, :title_color => 'FF0000').include?('chts=FF0000').should be_true
216
+ Gchart.line(:title => @title, :title_size => '20').include?('chts=454545,20').should be_true
217
+ end
218
+
219
+ it "should be able to have multiple legends" do
220
+ @chart.include?(Gchart.jstize("chdl=first+data+set+label|n+data+set+label")).should be_true
221
+ end
222
+
223
+ it "should be able to have one legend" do
224
+ chart = Gchart.line(:legend => 'legend label')
225
+ chart.include?("chdl=legend+label").should be_true
226
+ end
227
+
228
+ it "should be able to set the background fill" do
229
+ Gchart.line(:bg => 'efefef').include?("chf=bg,s,efefef").should be_true
230
+ Gchart.line(:bg => {:color => 'efefef', :type => 'solid'}).include?("chf=bg,s,efefef").should be_true
231
+
232
+ Gchart.line(:bg => {:color => 'efefef', :type => 'gradient'}).include?("chf=bg,lg,0,efefef,0,ffffff,1").should be_true
233
+ Gchart.line(:bg => {:color => 'efefef,0,ffffff,1', :type => 'gradient'}).include?("chf=bg,lg,0,efefef,0,ffffff,1").should be_true
234
+ Gchart.line(:bg => {:color => 'efefef', :type => 'gradient', :angle => 90}).include?("chf=bg,lg,90,efefef,0,ffffff,1").should be_true
235
+
236
+ Gchart.line(:bg => {:color => 'efefef', :type => 'stripes'}).include?("chf=bg,ls,90,efefef,0.2,ffffff,0.2").should be_true
237
+ end
238
+
239
+ it "should be able to set a graph fill" do
240
+ Gchart.line(:graph_bg => 'efefef').include?("chf=c,s,efefef").should be_true
241
+ Gchart.line(:graph_bg => {:color => 'efefef', :type => 'solid'}).include?("chf=c,s,efefef").should be_true
242
+ Gchart.line(:graph_bg => {:color => 'efefef', :type => 'gradient'}).include?("chf=c,lg,0,efefef,0,ffffff,1").should be_true
243
+ Gchart.line(:graph_bg => {:color => 'efefef,0,ffffff,1', :type => 'gradient'}).include?("chf=c,lg,0,efefef,0,ffffff,1").should be_true
244
+ Gchart.line(:graph_bg => {:color => 'efefef', :type => 'gradient', :angle => 90}).include?("chf=c,lg,90,efefef,0,ffffff,1").should be_true
245
+ end
246
+
247
+ it "should be able to set both a graph and a background fill" do
248
+ Gchart.line(:bg => 'efefef', :graph_bg => '76A4FB').include?("bg,s,efefef").should be_true
249
+ Gchart.line(:bg => 'efefef', :graph_bg => '76A4FB').include?("c,s,76A4FB").should be_true
250
+ Gchart.line(:bg => 'efefef', :graph_bg => '76A4FB').include?(Gchart.jstize("chf=c,s,76A4FB|bg,s,efefef")).should be_true
251
+ end
252
+
253
+ it "should be able to have different line colors" do
254
+ Gchart.line(:line_colors => 'efefef|00ffff').include?(Gchart.jstize('chco=efefef|00ffff')).should be_true
255
+ Gchart.line(:line_color => 'efefef|00ffff').include?(Gchart.jstize('chco=efefef|00ffff')).should be_true
256
+ end
257
+
258
+ it "should be able to render a graph where all the data values are 0" do
259
+ Gchart.line(:data => [0, 0, 0]).include?("chd=s:AAA").should be_true
260
+ end
261
+
262
+ end
263
+
264
+ describe "a sparkline chart" do
265
+
266
+ before(:each) do
267
+ @title = 'Chart Title'
268
+ @legend = ['first data set label', 'n data set label']
269
+ @jstized_legend = Gchart.jstize(@legend.join('|'))
270
+ @data = [27,25,25,25,25,27,100,31,25,36,25,25,39,25,31,25,25,25,26,26,25,25,28,25,25,100,28,27,31,25,27,27,29,25,27,26,26,25,26,26,35,33,34,25,26,25,36,25,26,37,33,33,37,37,39,25,25,25,25]
271
+ @chart = Gchart.sparkline(:title => @title, :data => @data, :legend => @legend)
272
+ end
273
+
274
+ it "should create a sparkline" do
275
+ @chart.include?('cht=ls').should be_true
276
+ end
277
+
278
+ it 'should be able have a chart title' do
279
+ @chart.include?("chtt=Chart+Title").should be_true
280
+ end
281
+
282
+ it "should be able to a custom color and size title" do
283
+ Gchart.sparkline(:title => @title, :title_color => 'FF0000').include?('chts=FF0000').should be_true
284
+ Gchart.sparkline(:title => @title, :title_size => '20').include?('chts=454545,20').should be_true
285
+ end
286
+
287
+ it "should be able to have multiple legends" do
288
+ @chart.include?(Gchart.jstize("chdl=first+data+set+label|n+data+set+label")).should be_true
289
+ end
290
+
291
+ it "should be able to have one legend" do
292
+ chart = Gchart.sparkline(:legend => 'legend label')
293
+ chart.include?("chdl=legend+label").should be_true
294
+ end
295
+
296
+ it "should be able to set the background fill" do
297
+ Gchart.sparkline(:bg => 'efefef').include?("chf=bg,s,efefef").should be_true
298
+ Gchart.sparkline(:bg => {:color => 'efefef', :type => 'solid'}).include?("chf=bg,s,efefef").should be_true
299
+
300
+ Gchart.sparkline(:bg => {:color => 'efefef', :type => 'gradient'}).include?("chf=bg,lg,0,efefef,0,ffffff,1").should be_true
301
+ Gchart.sparkline(:bg => {:color => 'efefef,0,ffffff,1', :type => 'gradient'}).include?("chf=bg,lg,0,efefef,0,ffffff,1").should be_true
302
+ Gchart.sparkline(:bg => {:color => 'efefef', :type => 'gradient', :angle => 90}).include?("chf=bg,lg,90,efefef,0,ffffff,1").should be_true
303
+
304
+ Gchart.sparkline(:bg => {:color => 'efefef', :type => 'stripes'}).include?("chf=bg,ls,90,efefef,0.2,ffffff,0.2").should be_true
305
+ end
306
+
307
+ it "should be able to set a graph fill" do
308
+ Gchart.sparkline(:graph_bg => 'efefef').include?("chf=c,s,efefef").should be_true
309
+ Gchart.sparkline(:graph_bg => {:color => 'efefef', :type => 'solid'}).include?("chf=c,s,efefef").should be_true
310
+ Gchart.sparkline(:graph_bg => {:color => 'efefef', :type => 'gradient'}).include?("chf=c,lg,0,efefef,0,ffffff,1").should be_true
311
+ Gchart.sparkline(:graph_bg => {:color => 'efefef,0,ffffff,1', :type => 'gradient'}).include?("chf=c,lg,0,efefef,0,ffffff,1").should be_true
312
+ Gchart.sparkline(:graph_bg => {:color => 'efefef', :type => 'gradient', :angle => 90}).include?("chf=c,lg,90,efefef,0,ffffff,1").should be_true
313
+ end
314
+
315
+ it "should be able to set both a graph and a background fill" do
316
+ Gchart.sparkline(:bg => 'efefef', :graph_bg => '76A4FB').include?("bg,s,efefef").should be_true
317
+ Gchart.sparkline(:bg => 'efefef', :graph_bg => '76A4FB').include?("c,s,76A4FB").should be_true
318
+ Gchart.sparkline(:bg => 'efefef', :graph_bg => '76A4FB').include?(Gchart.jstize("chf=c,s,76A4FB|bg,s,efefef")).should be_true
319
+ end
320
+
321
+ it "should be able to have different line colors" do
322
+ Gchart.sparkline(:line_colors => 'efefef|00ffff').include?(Gchart.jstize('chco=efefef|00ffff')).should be_true
323
+ Gchart.sparkline(:line_color => 'efefef|00ffff').include?(Gchart.jstize('chco=efefef|00ffff')).should be_true
324
+ end
325
+
326
+ end
327
+
328
+ describe "a 3d pie chart" do
329
+
330
+ before(:each) do
331
+ @title = 'Chart Title'
332
+ @legend = ['first data set label', 'n data set label']
333
+ @jstized_legend = Gchart.jstize(@legend.join('|'))
334
+ @data = [12,8,40,15,5]
335
+ @chart = Gchart.pie(:title => @title, :legend => @legend, :data => @data)
336
+ end
337
+
338
+ it "should create a pie" do
339
+ @chart.include?('cht=p').should be_true
340
+ end
341
+
342
+ it "should be able to be in 3d" do
343
+ Gchart.pie_3d(:title => @title, :legend => @legend, :data => @data).include?('cht=p3').should be_true
344
+ end
345
+
346
+ it "should be able to set labels by using the legend or labesl accessor" do
347
+ Gchart.pie_3d(:title => @title, :legend => @legend, :data => @data).include?("chl=#{@jstized_legend}").should be_true
348
+ Gchart.pie_3d(:title => @title, :labels => @legend, :data => @data).include?("chl=#{@jstized_legend}").should be_true
349
+ Gchart.pie_3d(:title => @title, :labels => @legend, :data => @data).should == Gchart.pie_3d(:title => @title, :legend => @legend, :data => @data)
350
+ end
351
+
352
+ end
353
+
354
+ describe "a google-o-meter" do
355
+
356
+ before(:each) do
357
+ @data = [70]
358
+ @legend = ['arrow points here']
359
+ @jstized_legend = Gchart.jstize(@legend.join('|'))
360
+ @chart = Gchart.meter(:data => @data)
361
+ end
362
+
363
+ it "should create a meter" do
364
+ @chart.include?('cht=gom').should be_true
365
+ end
366
+
367
+ it "should be able to set a solid background fill" do
368
+ Gchart.meter(:bg => 'efefef').include?("chf=bg,s,efefef").should be_true
369
+ Gchart.meter(:bg => {:color => 'efefef', :type => 'solid'}).include?("chf=bg,s,efefef").should be_true
370
+ end
371
+
372
+ end
373
+
374
+ describe 'exporting a chart' do
375
+
376
+ it "should be available in the url format by default" do
377
+ Gchart.line(:data => [0, 26], :format => 'url').should == Gchart.line(:data => [0, 26])
378
+ end
379
+
380
+ it "should be available as the img tag" do
381
+ Gchart.line(:data => [0, 26], :format => 'img_tag').should match(/<img src=(.*)\/>/)
382
+ end
383
+
384
+ it "should be available as a file" do
385
+ File.delete('chart.png') if File.exist?('chart.png')
386
+ Gchart.line(:data => [0, 26], :format => 'file')
387
+ File.exist?('chart.png').should be_true
388
+ File.delete('chart.png') if File.exist?('chart.png')
389
+ end
390
+
391
+ it "should be available as a file using a custom file name" do
392
+ File.delete('custom_file_name.png') if File.exist?('custom_file_name.png')
393
+ Gchart.line(:data => [0, 26], :format => 'file', :filename => 'custom_file_name.png')
394
+ File.exist?('custom_file_name.png').should be_true
395
+ File.delete('custom_file_name.png') if File.exist?('custom_file_name.png')
396
+ end
397
+
398
+ it "should work even with multiple attrs" do
399
+ File.delete('foo.png') if File.exist?('foo.png')
400
+ Gchart.line(:size => '400x200',
401
+ :data => [1,2,3,4,5],
402
+ :axis_labels => [[1,2,3,4, 5], %w[foo bar]],
403
+ :axis_with_labels => 'x,r',
404
+ :format => "file",
405
+ :filename => "foo.png"
406
+ )
407
+ File.exist?('foo.png').should be_true
408
+ File.delete('foo.png') if File.exist?('foo.png')
409
+ end
410
+
411
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/*_spec.rb']
21
+ end