libgeo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Libgeo::Formatter::Compiler do
6
+ let(:instance) { described_class.new(pattern) }
7
+
8
+ describe '#compile' do
9
+ subject { instance.compile }
10
+
11
+ context 'empty string' do
12
+ let(:pattern) { '' }
13
+
14
+ it { expect(subject).to eql('') }
15
+ end
16
+
17
+ context 'keyword string' do
18
+ let(:pattern) { '%d' }
19
+
20
+ it { expect(subject).to eql('c.deg.to_s') }
21
+ end
22
+
23
+ context 'keyword with pad' do
24
+ let(:pattern) { '%3m' }
25
+
26
+ it { expect(subject).to eql('pad(c.mins.to_s, 3)') }
27
+ end
28
+
29
+ context 'strings' do
30
+ let(:pattern) { 'Hello world' }
31
+
32
+ it { expect(subject).to eql("'Hello world'") }
33
+ end
34
+
35
+ context 'keywords and strings' do
36
+ let(:pattern) { 'Degs: %d, Mins: %m' }
37
+ let(:expected) { %('Degs: ' << c.deg.to_s << ', Mins: ' << c.mins.to_s) }
38
+
39
+ it { expect(subject).to eql(expected) }
40
+ end
41
+
42
+ describe 'keywords' do
43
+ let(:pattern) { |ex| ex.description }
44
+
45
+ it '%d' do
46
+ expect(subject).to eql('c.deg.to_s')
47
+ end
48
+
49
+ it '%m' do
50
+ expect(subject).to eql('c.mins.to_s')
51
+ end
52
+
53
+ it '%M' do
54
+ expect(subject).to eql('c.minutes_only.to_s')
55
+ end
56
+
57
+ it '%s' do
58
+ expect(subject).to eql('c.secs.to_i.to_s')
59
+ end
60
+
61
+ it '%S' do
62
+ expect(subject).to eql('c.secs.to_s')
63
+ end
64
+
65
+ it '%h' do
66
+ expect(subject).to eql('c.hemi.to_s.downcase.to_s')
67
+ end
68
+
69
+ it '%H' do
70
+ expect(subject).to eql('c.hemi.to_s')
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Libgeo::Formatter::Evaluator do
6
+ let(:instance) { described_class.new(coordinate) }
7
+ let(:coordinate) { double('Latitude') }
8
+
9
+ describe '#pad' do
10
+ subject { instance.pad(val, num) }
11
+
12
+ describe 'Integers' do
13
+ let(:num) { 3 }
14
+
15
+ context 'as Fixnum' do
16
+ let(:val) { 42 }
17
+
18
+ it { expect(subject).to eql('042') }
19
+ end
20
+
21
+ context 'as String' do
22
+ let(:val) { '42' }
23
+
24
+ it { expect(subject).to eql('042') }
25
+ end
26
+
27
+ context 'when int length and num equals' do
28
+ let(:val) { '142' }
29
+
30
+ it { expect(subject).to eql(val) }
31
+ end
32
+
33
+ context 'when int length more than num' do
34
+ let(:val) { '1986' }
35
+
36
+ it { expect(subject).to eql(val) }
37
+ end
38
+ end
39
+
40
+ describe 'Floats' do
41
+ let(:num) { 2 }
42
+
43
+ context 'as Float' do
44
+ let(:val) { 3.14 }
45
+
46
+ it { expect(subject).to eql('03.14') }
47
+ end
48
+
49
+ context 'as String' do
50
+ let(:val) { '3.14' }
51
+
52
+ it { expect(subject).to eql('03.14') }
53
+ end
54
+
55
+ context 'when int length and num equals' do
56
+ let(:val) { '13.14' }
57
+
58
+ it { expect(subject).to eql('13.14') }
59
+ end
60
+
61
+ context 'when int length more than num' do
62
+ let(:val) { '133.14' }
63
+
64
+ it { expect(subject).to eql(val) }
65
+ end
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Libgeo::Formatter do
6
+ let(:instance) { described_class.new(pattern) }
7
+ let(:pattern) { '%d°%2m′' }
8
+
9
+ describe '#format' do
10
+ subject { instance.format(coord) }
11
+
12
+ let(:coord) { instance_double('Libgeo::Latitude') }
13
+
14
+ before do
15
+ allow(coord).to receive(:deg).and_return(48)
16
+ allow(coord).to receive(:mins).and_return(4)
17
+ end
18
+
19
+ it { expect(subject).to eql('48°04′') }
20
+ end
21
+
22
+ describe '#pattern' do
23
+ subject { instance.pattern }
24
+
25
+ it { expect(subject).to eql(pattern) }
26
+ end
27
+
28
+ describe '#inspect' do
29
+ subject { instance.inspect }
30
+
31
+ it { expect(subject).to eql('#<Libgeo::Formatter pattern=(%d°%2m′)>') }
32
+ end
33
+
34
+ end
@@ -0,0 +1,378 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Libgeo::Latitude do
6
+ let(:instance) { described_class.new(hemi, degrees, minutes, seconds) }
7
+
8
+ let(:hemi) { Libgeo::NORTH }
9
+ let(:degrees) { 48 }
10
+ let(:minutes) { 04 }
11
+ let(:seconds) { 15.7836 }
12
+
13
+ let(:decimal) { 48.071051 }
14
+
15
+ describe '.decimal' do
16
+ subject { described_class.decimal(decimal) }
17
+
18
+ context 'when positive' do
19
+ it { expect(subject.hemisphere).to eql(hemi) }
20
+ it { expect(subject.degrees).to eql(degrees) }
21
+ it { expect(subject.minutes).to eql(minutes) }
22
+ it { expect(subject.seconds).to eql(seconds) }
23
+ end
24
+
25
+ context 'when negative' do
26
+ let(:decimal) { -48.071051 }
27
+
28
+ it { expect(subject.hemisphere).to eql(Libgeo::SOUTH) }
29
+ it { expect(subject.degrees).to eql(degrees) }
30
+ it { expect(subject.minutes).to eql(minutes) }
31
+ it { expect(subject.seconds).to eql(seconds) }
32
+ end
33
+ end
34
+
35
+ describe '.dms' do
36
+ subject { described_class.dms(dms) }
37
+
38
+ context 'with characters' do
39
+ context 'when positive' do
40
+ let(:dms) { '58°39′13.5 N' }
41
+
42
+ it { expect(subject.hemisphere).to eql(Libgeo::NORTH) }
43
+ it { expect(subject.degrees).to eql(58) }
44
+ it { expect(subject.minutes).to eql(39) }
45
+ it { expect(subject.seconds).to eql(13.5) }
46
+ end
47
+
48
+ context 'when negative' do
49
+ let(:dms) { '58°39′13.5 S' }
50
+
51
+ it { expect(subject.hemisphere).to eql(Libgeo::SOUTH) }
52
+ it { expect(subject.degrees).to eql(58) }
53
+ it { expect(subject.minutes).to eql(39) }
54
+ it { expect(subject.seconds).to eql(13.5) }
55
+ end
56
+ end
57
+
58
+ context 'without characters' do
59
+ context 'when positive' do
60
+ let(:dms) { '58°39′13.5' }
61
+
62
+ it { expect(subject.hemisphere).to eql(Libgeo::NORTH) }
63
+ it { expect(subject.degrees).to eql(58) }
64
+ it { expect(subject.minutes).to eql(39) }
65
+ it { expect(subject.seconds).to eql(13.5) }
66
+ end
67
+
68
+ context 'when negative' do
69
+ let(:dms) { '-58°39′13.5' }
70
+
71
+ it { expect(subject.hemisphere).to eql(Libgeo::SOUTH) }
72
+ it { expect(subject.degrees).to eql(58) }
73
+ it { expect(subject.minutes).to eql(39) }
74
+ it { expect(subject.seconds).to eql(13.5) }
75
+ end
76
+ end
77
+ end
78
+
79
+ describe '.nmea' do
80
+ subject { described_class.nmea(nmea) }
81
+
82
+ context 'with characters' do
83
+ context 'when positive' do
84
+ let(:nmea) { '03920.56074,N' }
85
+
86
+ it { expect(subject.hemisphere).to eql(Libgeo::NORTH) }
87
+ it { expect(subject.degrees).to eql(39) }
88
+ it { expect(subject.minutes).to eql(20) }
89
+ it { expect(subject.seconds).to eql(33.6444) }
90
+ end
91
+
92
+ context 'when negative' do
93
+ let(:nmea) { '03920.56074,S' }
94
+
95
+ it { expect(subject.hemisphere).to eql(Libgeo::SOUTH) }
96
+ it { expect(subject.degrees).to eql(39) }
97
+ it { expect(subject.minutes).to eql(20) }
98
+ it { expect(subject.seconds).to eql(33.6444) }
99
+ end
100
+ end
101
+
102
+ context 'without characters' do
103
+ context 'when positive' do
104
+ let(:nmea) { '+03920.56074' }
105
+
106
+ it { expect(subject.hemisphere).to eql(Libgeo::NORTH) }
107
+ it { expect(subject.degrees).to eql(39) }
108
+ it { expect(subject.minutes).to eql(20) }
109
+ it { expect(subject.seconds).to eql(33.6444) }
110
+ end
111
+
112
+ context 'when negative' do
113
+ let(:nmea) { '-03920.56074' }
114
+
115
+ it { expect(subject.hemisphere).to eql(Libgeo::SOUTH) }
116
+ it { expect(subject.degrees).to eql(39) }
117
+ it { expect(subject.minutes).to eql(20) }
118
+ it { expect(subject.seconds).to eql(33.6444) }
119
+ end
120
+ end
121
+ end
122
+
123
+ describe '.degrees_minutes' do
124
+ subject { described_class.degrees_minutes(degrees, 4.26306) }
125
+
126
+ context 'when positive' do
127
+ it { expect(subject.hemisphere).to eql(hemi) }
128
+ it { expect(subject.degrees).to eql(degrees) }
129
+ it { expect(subject.minutes).to eql(minutes) }
130
+ it { expect(subject.seconds).to eql(seconds) }
131
+ end
132
+
133
+ context 'when negative' do
134
+ let(:degrees) { -48 }
135
+
136
+ it { expect(subject.hemisphere).to eql(Libgeo::SOUTH) }
137
+ it { expect(subject.degrees).to eql(48) }
138
+ it { expect(subject.minutes).to eql(minutes) }
139
+ it { expect(subject.seconds).to eql(seconds) }
140
+ end
141
+ end
142
+
143
+ describe '#type' do
144
+ subject { instance.type }
145
+
146
+ it { expect(subject).to equal(:latitude) }
147
+ end
148
+
149
+ describe '#degrees' do
150
+ it_behaves_like 'Coordinate#degrees'
151
+ end
152
+
153
+ describe '#minutes' do
154
+ it_behaves_like 'Coordinate#minutes'
155
+ end
156
+
157
+ describe '#seconds' do
158
+ it_behaves_like 'Coordinate#seconds'
159
+ end
160
+
161
+ describe 'different inputs' do
162
+ it_behaves_like 'Coordinate#inputs' do
163
+ let(:hemisphere) { :S }
164
+ end
165
+ end
166
+
167
+ describe 'values validation' do
168
+ it_behaves_like 'Coordinate#validation' do
169
+ let(:max_degrees) { 90 }
170
+ end
171
+ end
172
+
173
+ describe '#hemisphere' do
174
+ subject { instance.hemisphere }
175
+
176
+ describe 'Northen' do
177
+ context 'when symbol' do
178
+ it { expect(subject).to eql(:N) }
179
+ end
180
+
181
+ context 'when string' do
182
+ let(:hemi) { 'N' }
183
+
184
+ it { expect(subject).to eql(:N) }
185
+ end
186
+
187
+ context 'when direction' do
188
+ let(:hemi) { :> }
189
+
190
+ it { expect(subject).to eql(:N) }
191
+ end
192
+
193
+ context 'when word' do
194
+ let(:hemi) { 'north' }
195
+
196
+ it { expect(subject).to eql(:N) }
197
+ end
198
+ end
199
+
200
+ describe 'Southern' do
201
+ context 'when symbol' do
202
+ let(:hemi) { Libgeo::SOUTH }
203
+
204
+ it { expect(subject).to eql(:S) }
205
+ end
206
+
207
+ context 'when string' do
208
+ let(:hemi) { 'S' }
209
+
210
+ it { expect(subject).to eql(:S) }
211
+ end
212
+
213
+ context 'when direction' do
214
+ let(:hemi) { :< }
215
+
216
+ it { expect(subject).to eql(:S) }
217
+ end
218
+
219
+ context 'when word' do
220
+ let(:hemi) { 'south' }
221
+
222
+ it { expect(subject).to eql(:S) }
223
+ end
224
+ end
225
+ end
226
+
227
+ describe '#north!' do
228
+ let(:hemi) { Libgeo::SOUTH }
229
+
230
+ it { expect { instance.north! }.to change { instance.hemisphere }.from(:S).to(:N) }
231
+ end
232
+
233
+ describe '#south!' do
234
+ let(:hemi) { Libgeo::NORTH }
235
+
236
+ it { expect { instance.south! }.to change { instance.hemisphere }.from(:N).to(:S) }
237
+ end
238
+
239
+ describe '#west!' do
240
+ let(:hemi) { Libgeo::NORTH }
241
+
242
+ it { expect { instance.west! }.to_not change { instance.hemisphere } }
243
+ end
244
+
245
+ describe '#east!' do
246
+ it { expect { instance.east! }.to_not change { instance.hemisphere } }
247
+ end
248
+
249
+ describe '#north?' do
250
+ subject { instance.north? }
251
+
252
+ context 'when north' do
253
+ it { expect(subject).to be_truthy }
254
+ end
255
+
256
+ context 'when south' do
257
+ let(:hemi) { Libgeo::SOUTH }
258
+
259
+ it { expect(subject).to be_falsy }
260
+ end
261
+ end
262
+
263
+ describe '#south?' do
264
+ subject { instance.south? }
265
+
266
+ context 'when north' do
267
+ it { expect(subject).to be_falsy }
268
+ end
269
+
270
+ context 'when south' do
271
+ let(:hemi) { Libgeo::SOUTH }
272
+
273
+ it { expect(subject).to be_truthy }
274
+ end
275
+ end
276
+
277
+ describe '#west?' do
278
+ subject { instance.west? }
279
+
280
+ context 'when north' do
281
+ it { expect(subject).to be_falsy }
282
+ end
283
+
284
+ context 'when south' do
285
+ let(:hemi) { Libgeo::SOUTH }
286
+
287
+ it { expect(subject).to be_falsy }
288
+ end
289
+ end
290
+
291
+ describe '#east?' do
292
+ subject { instance.east? }
293
+
294
+ context 'when north' do
295
+ it { expect(subject).to be_falsy }
296
+ end
297
+
298
+ context 'when south' do
299
+ let(:hemi) { Libgeo::SOUTH }
300
+
301
+ it { expect(subject).to be_falsy }
302
+ end
303
+ end
304
+
305
+ describe '#to_f' do
306
+ subject { instance.to_f }
307
+
308
+ context 'when north' do
309
+ it { expect(subject).to eql(decimal) }
310
+ end
311
+
312
+ context 'when south' do
313
+ let(:hemi) { Libgeo::SOUTH }
314
+
315
+ it { expect(subject).to eql(-1 * decimal) }
316
+ end
317
+ end
318
+
319
+ describe '#to_hash' do
320
+ it_behaves_like 'Coordinate#to_hash'
321
+ end
322
+
323
+ describe '#to_dms' do
324
+ subject { instance.to_dms }
325
+
326
+ context 'when north' do
327
+ it { expect(subject).to eql(%[48°04′15.7836″N]) }
328
+ end
329
+
330
+ context 'when south' do
331
+ let(:hemi) { Libgeo::SOUTH }
332
+
333
+ it { expect(subject).to eql(%[48°04′15.7836″S]) }
334
+ end
335
+ end
336
+
337
+ describe '#to_nmea' do
338
+ subject { instance.to_nmea }
339
+
340
+ context 'when north' do
341
+ it { expect(subject).to eql('4804.26306,N') }
342
+ end
343
+
344
+ context 'when south' do
345
+ let(:hemi) { Libgeo::SOUTH }
346
+
347
+ it { expect(subject).to eql('4804.26306,S') }
348
+ end
349
+ end
350
+
351
+ describe '#to_s' do
352
+ context 'default format' do
353
+ subject { instance.to_s }
354
+
355
+ context 'when north' do
356
+ it { expect(subject).to eql(%[48°04′15.7836″N]) }
357
+ end
358
+
359
+ context 'when south' do
360
+ let(:hemi) { Libgeo::SOUTH }
361
+
362
+ it { expect(subject).to eql(%[48°04′15.7836″S]) }
363
+ end
364
+ end
365
+ end
366
+
367
+ describe '#inspect' do
368
+ subject { instance.inspect }
369
+ let(:expected) { "#<Libgeo::Latitude hemisphere=N degrees=48 minutes=4 seconds=15.7836>" }
370
+
371
+ it { expect(subject).to eql(expected) }
372
+ end
373
+
374
+ describe '#to_utm' do
375
+ pending 'TODO'
376
+ end
377
+
378
+ end