plotrb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,258 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'Data' do
4
+
5
+ subject { ::Plotrb::Data.new }
6
+
7
+ describe '#name' do
8
+
9
+ it 'sets name if valid' do
10
+ subject.name('foo')
11
+ subject.name.should == 'foo'
12
+ end
13
+
14
+ it 'raises error if name is nil' do
15
+ expect { subject.send(:process_name) }.to raise_error(ArgumentError)
16
+ end
17
+
18
+ it 'raises error if name in blank' do
19
+ subject.name = ' '
20
+ expect { subject.send(:process_name) }.to raise_error(ArgumentError)
21
+ end
22
+
23
+ it 'raises error if name is not unique' do
24
+ ::Plotrb::Kernel.stub(:duplicate_data?).and_return(false)
25
+ expect { subject.send(:process_name) }.to raise_error(ArgumentError)
26
+ end
27
+
28
+ end
29
+
30
+ describe '#format' do
31
+
32
+ it 'sets format as a new Format instance' do
33
+ ::Plotrb::Data::Format.should_receive(:new).with(:foo)
34
+ subject.format(:foo)
35
+ end
36
+
37
+ end
38
+
39
+ describe '#values' do
40
+
41
+ it 'sets values if valid JSON is given' do
42
+ subject.values('{"foo":1, "bar":{"baz":2}}')
43
+ subject.values.should == '{"foo":1, "bar":{"baz":2}}'
44
+ end
45
+
46
+ it 'sets values if array is given' do
47
+ subject.values([1,2,3,4])
48
+ subject.values.should match_array([1,2,3,4])
49
+ end
50
+
51
+ it 'sets values if hash is given' do
52
+ subject.values(foo: 1, bar: 2)
53
+ subject.values.should == {foo: 1, bar: 2}
54
+ end
55
+
56
+ it 'raises error if values are invalid JSON' do
57
+ subject.values("{foo:1, 'bar':}")
58
+ expect { subject.send(:process_values) }.to raise_error ArgumentError
59
+ end
60
+
61
+ end
62
+
63
+ describe '#source' do
64
+
65
+ it 'sets source if valid' do
66
+ subject.source('foo')
67
+ subject.source.should == 'foo'
68
+ end
69
+
70
+ it 'raises error if source does not exist' do
71
+ ::Plotrb::Kernel.stub(:find_data).and_return(false)
72
+ subject.source('foo')
73
+ expect { subject.send(:process_source) }.to raise_error ArgumentError
74
+ end
75
+
76
+ it 'gets the name of the existing data object' do
77
+ foo = ::Plotrb::Data.new.name('foo')
78
+ subject.source(foo)
79
+ subject.send(:process_source)
80
+ subject.source.should == 'foo'
81
+ end
82
+
83
+ end
84
+
85
+ describe '#url' do
86
+
87
+ it 'sets valid absolute url' do
88
+ subject.url('http://foo.com/bar')
89
+ subject.url.should == 'http://foo.com/bar'
90
+ end
91
+
92
+ it 'sets valid relative url' do
93
+ subject.url('data/bar.json')
94
+ subject.url.should == 'data/bar.json'
95
+ end
96
+
97
+ it 'raises error when url is invalid' do
98
+ subject.url('http://foo/#-|r|$@')
99
+ expect { subject.send(:process_url) }.to raise_error ArgumentError
100
+ end
101
+
102
+ end
103
+
104
+ describe '#file' do
105
+
106
+ it 'sets url if file exists'
107
+
108
+ it 'raises error is file does not exist'
109
+
110
+ end
111
+
112
+ describe '#transform' do
113
+
114
+ class Bar; end
115
+
116
+ let(:foo) { ::Plotrb::Transform.new(:array) }
117
+ let(:bar) { Bar.new }
118
+
119
+ it 'sets transform if a transform object is given' do
120
+ subject.transform(foo)
121
+ subject.transform.should == [foo]
122
+ end
123
+
124
+ it 'sets transform if multiple transforms are given' do
125
+ subject.transform(foo, foo)
126
+ subject.transform.should == [foo, foo]
127
+ end
128
+
129
+ it 'raises error if array contains non-transforms' do
130
+ subject.transform(foo, bar)
131
+ expect { subject.send(:process_transform) }.to raise_error ArgumentError
132
+ end
133
+
134
+ end
135
+
136
+ describe '#method_missing' do
137
+
138
+ it 'sets format via as_foo' do
139
+ subject.should_receive(:format).with(:csv)
140
+ subject.as_csv
141
+ end
142
+
143
+ end
144
+
145
+ describe 'Format' do
146
+
147
+ it 'raises error if format type is not recognized' do
148
+ expect { ::Plotrb::Data::Format.new(:foo) }.to raise_error ArgumentError
149
+ end
150
+
151
+ context 'json' do
152
+
153
+ subject { ::Plotrb::Data::Format.new(:json) }
154
+
155
+ it 'has parse and property attributes' do
156
+ subject.attributes.should match_array([:type, :parse, :property])
157
+ end
158
+
159
+ describe '#parse' do
160
+
161
+ it 'sets parse if valid hash is given' do
162
+ subject.parse('foo' => :number, 'bar' => :date)
163
+ subject.parse.should == {'foo' => :number, 'bar' => :date }
164
+ end
165
+
166
+ it 'raises error if parse object has unknown data type' do
167
+ subject.parse('foo' => :bar)
168
+ expect { subject.send(:process_parse) }.to raise_error ArgumentError
169
+ end
170
+
171
+ end
172
+
173
+ describe '#as_date' do
174
+
175
+ it 'parses the field as date' do
176
+ subject.as_date('foo')
177
+ subject.parse['foo'].should == :date
178
+ end
179
+
180
+ it 'allows setting multiple fields' do
181
+ subject.as_date('foo', 'bar')
182
+ subject.parse.should == {'foo' => :date, 'bar' => :date}
183
+ end
184
+
185
+ end
186
+
187
+ describe '#as_boolean' do
188
+
189
+ it 'parses the field as boolean' do
190
+ subject.as_boolean('foo')
191
+ subject.parse['foo'].should == :boolean
192
+ end
193
+
194
+ end
195
+
196
+ describe '#as_number' do
197
+
198
+ it 'parses the field as number' do
199
+ subject.as_number('foo')
200
+ subject.parse['foo'].should == :number
201
+ end
202
+
203
+ end
204
+
205
+ describe '#property' do
206
+
207
+ it 'sets the property' do
208
+ subject.property('values.features')
209
+ subject.property.should == 'values.features'
210
+ end
211
+
212
+ end
213
+
214
+ end
215
+
216
+ context 'csv' do
217
+
218
+ subject { ::Plotrb::Data::Format.new(:csv) }
219
+
220
+ it 'has parse attribute' do
221
+ subject.attributes.should match_array([:type, :parse])
222
+ end
223
+
224
+ end
225
+
226
+ context 'tsv' do
227
+
228
+ subject { ::Plotrb::Data::Format.new(:tsv) }
229
+
230
+ it 'has parse attribute' do
231
+ subject.attributes.should match_array([:type, :parse])
232
+ end
233
+
234
+ end
235
+
236
+ context 'topojson' do
237
+
238
+ subject { ::Plotrb::Data::Format.new(:topojson) }
239
+
240
+ it 'has feature and mesh attribute' do
241
+ subject.attributes.should match_array([:type, :feature, :mesh])
242
+ end
243
+
244
+ end
245
+
246
+ context 'treejson' do
247
+
248
+ subject { ::Plotrb::Data::Format.new(:treejson) }
249
+
250
+ it 'has parse and children attribute' do
251
+ subject.attributes.should match_array([:type, :parse, :children])
252
+ end
253
+
254
+ end
255
+
256
+ end
257
+
258
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'Kernel' do
4
+
5
+ class Object
6
+ include ::Plotrb::Kernel
7
+ end
8
+
9
+ describe '#visualization' do
10
+
11
+ it 'creates new visualization object' do
12
+ v = visualization
13
+ v.is_a?(::Plotrb::Visualization).should be_true
14
+ end
15
+
16
+ end
17
+
18
+ describe '#pdata' do
19
+
20
+ it 'creates new data object' do
21
+ d = pdata
22
+ d.is_a?(::Plotrb::Data).should be_true
23
+ end
24
+
25
+ end
26
+
27
+ describe '#method_missing' do
28
+
29
+ it 'creates axis object' do
30
+ a = x_axis
31
+ a.is_a?(::Plotrb::Axis).should be_true
32
+ a.type.should == :x
33
+ end
34
+
35
+ it 'creates scale object' do
36
+ s = linear_scale
37
+ s.is_a?(::Plotrb::Scale).should be_true
38
+ s.type.should == :linear
39
+ end
40
+
41
+ it 'creates mark object' do
42
+ m = rect_mark
43
+ m.is_a?(::Plotrb::Mark).should be_true
44
+ m.type.should == :rect
45
+ end
46
+
47
+ it 'creates transform object' do
48
+ t = filter_transform
49
+ t.is_a?(::Plotrb::Transform).should be_true
50
+ t.type.should == :filter
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,157 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'Legend' do
4
+ subject { ::Plotrb::Legend.new }
5
+
6
+ describe '#size' do
7
+
8
+ it 'sets the scale backing the legend size by name' do
9
+ subject.size('foo_scale')
10
+ subject.size.should == 'foo_scale'
11
+ end
12
+
13
+ it 'sets the scale backing the legend size by the scale object' do
14
+ scale = ::Plotrb::Scale.new.name('foo_scale')
15
+ subject.size(scale)
16
+ subject.send(:process_size)
17
+ subject.size.should == 'foo_scale'
18
+ end
19
+
20
+ it 'raises error if scale is not found' do
21
+ subject.size('foo_scale')
22
+ ::Plotrb::Kernel.stub(:find_scale).and_return(nil)
23
+ expect { subject.send(:process_size) }.to raise_error(ArgumentError)
24
+ end
25
+
26
+ end
27
+
28
+ describe '#shape' do
29
+
30
+ it 'sets the scale backing the legend shape by name' do
31
+ subject.shape('foo_scale')
32
+ subject.shape.should == 'foo_scale'
33
+ end
34
+
35
+ it 'sets the scale backing the legend shape by the scale object' do
36
+ scale = ::Plotrb::Scale.new.name('foo_scale')
37
+ subject.shape(scale)
38
+ subject.send(:process_shape)
39
+ subject.shape.should == 'foo_scale'
40
+ end
41
+
42
+ it 'raises error if scale is not found' do
43
+ subject.shape('foo_scale')
44
+ ::Plotrb::Kernel.stub(:find_scale).and_return(nil)
45
+ expect { subject.send(:process_shape) }.to raise_error(ArgumentError)
46
+ end
47
+
48
+ end
49
+
50
+ describe '#fill' do
51
+
52
+ it 'sets the scale backing the legend fill color by name' do
53
+ subject.fill('foo_scale')
54
+ subject.fill.should == 'foo_scale'
55
+ end
56
+
57
+ it 'sets the scale backing the legend fill color by the scale object' do
58
+ scale = ::Plotrb::Scale.new.name('foo_scale')
59
+ subject.fill(scale)
60
+ subject.send(:process_fill)
61
+ subject.fill.should == 'foo_scale'
62
+ end
63
+
64
+ it 'raises error if scale is not found' do
65
+ subject.fill('foo_scale')
66
+ ::Plotrb::Kernel.stub(:find_scale).and_return(nil)
67
+ expect { subject.send(:process_fill) }.to raise_error(ArgumentError)
68
+ end
69
+
70
+ end
71
+
72
+ describe '#stroke' do
73
+
74
+ it 'sets the scale backing the legend stroke color by name' do
75
+ subject.stroke('foo_scale')
76
+ subject.stroke.should == 'foo_scale'
77
+ end
78
+
79
+ it 'sets the scale backing the legend stroke color by the scale object' do
80
+ scale = ::Plotrb::Scale.new.name('foo_scale')
81
+ subject.stroke(scale)
82
+ subject.send(:process_stroke)
83
+ subject.stroke.should == 'foo_scale'
84
+ end
85
+
86
+ it 'raises error if scale is not found' do
87
+ subject.stroke('foo_scale')
88
+ ::Plotrb::Kernel.stub(:find_scale).and_return(nil)
89
+ expect { subject.send(:process_stroke) }.to raise_error(ArgumentError)
90
+ end
91
+
92
+ end
93
+
94
+ describe '#orient' do
95
+
96
+ it 'sets the orient of the axis' do
97
+ subject.at_left
98
+ subject.orient.should == :left
99
+ end
100
+
101
+ it 'raises error if orient is invalid' do
102
+ subject.orient(:top)
103
+ expect { subject.send(:process_orient) }.to raise_error(ArgumentError)
104
+ end
105
+
106
+ end
107
+
108
+ describe '#title' do
109
+
110
+ it 'sets title of the axis' do
111
+ subject.title('foo')
112
+ subject.title.should == 'foo'
113
+ end
114
+
115
+ end
116
+
117
+ describe 'format' do
118
+
119
+ it 'accepts valid format specifier' do
120
+ subject.format('04d')
121
+ expect { subject.send(:process_format) }.to_not raise_error(ArgumentError)
122
+ end
123
+
124
+ it 'raises error if format specifier is invalid' do
125
+ subject.format('{$s04d,g')
126
+ expect { subject.send(:process_format) }.to raise_error(ArgumentError)
127
+ end
128
+
129
+ end
130
+
131
+ describe '#offset' do
132
+
133
+ it 'sets offset of the axis' do
134
+ subject.offset_by(10)
135
+ subject.offset.should == 10
136
+ end
137
+
138
+ end
139
+
140
+
141
+ describe '#values' do
142
+
143
+ it 'sets values if given as an array' do
144
+ subject.values([1,2,3,4])
145
+ subject.values.should match_array([1,2,3,4])
146
+ end
147
+
148
+ it 'sets values if given one by one as arguments' do
149
+ subject.values(1,2,3,4)
150
+ subject.values.should match_array([1,2,3,4])
151
+ end
152
+
153
+ end
154
+
155
+
156
+
157
+ end