rats 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/rats_spec.rb ADDED
@@ -0,0 +1,690 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Rats" do
4
+
5
+ it "initializes the class" do
6
+ land = Rats.new
7
+ land.is_a?(Rats::Base).should be_true
8
+ end
9
+
10
+ describe "attributes" do
11
+
12
+ before(:each) do
13
+ @land = Rats.new
14
+ end
15
+
16
+ describe "writing and reading" do
17
+
18
+ it "writes quarter" do
19
+ @land.quarter = "NE"
20
+ @land.quarter.should == "NE"
21
+ end
22
+
23
+ it "writes section" do
24
+ @land.section = 1
25
+ @land.section.should == 1
26
+ end
27
+
28
+ it "writes township" do
29
+ @land.township = 1
30
+ @land.township.should == 1
31
+ end
32
+
33
+ it "writes range" do
34
+ @land.range = 1
35
+ @land.range.should == 1
36
+ end
37
+
38
+ it "writes meridian" do
39
+ @land.meridian = 4
40
+ #@land.meridian.should == "W4"
41
+ @land.meridian.should == 4
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ describe "parsing locations" do
49
+
50
+ it "understands NE 1-2-3 W4" do
51
+ land = Rats.new("NE 1-2-3 W4")
52
+ land.quarter.should == "NE"
53
+ land.section.should == 1
54
+ land.township.should == 2
55
+ land.range.should == 3
56
+ #land.meridian.should == "W4"
57
+ land.meridian.should == 4
58
+ end
59
+
60
+ it "understands NE 1-1-1-4" do
61
+ land = Rats.new("NE 1-2-3-4")
62
+ land.quarter.should == "NE"
63
+ land.section.should == 1
64
+ land.township.should == 2
65
+ land.range.should == 3
66
+ #land.meridian.should == "W4"
67
+ land.meridian.should == 4
68
+ end
69
+
70
+ it "understands NE 1 2 3 4" do
71
+ land = Rats.new("NE 1 2 3 4")
72
+ land.quarter.should == "NE"
73
+ land.section.should == 1
74
+ land.township.should == 2
75
+ land.range.should == 3
76
+ #land.meridian.should == "W4"
77
+ land.meridian.should == 4
78
+ end
79
+
80
+ it "understands 40300201NE" do
81
+ land = Rats.new("40300201NE")
82
+ land.quarter.should == "NE"
83
+ land.section.should == 1
84
+ land.township.should == 2
85
+ land.range.should == 3
86
+ #land.meridian.should == "W4"
87
+ land.meridian.should == 4
88
+ end
89
+
90
+ # NOT SURE THIS IS USEFUL
91
+ # it "understands NE01002034" do
92
+ # land = Rats.new("NE01002034")
93
+ # land.quarter.should == "NE"
94
+ # land.section.should == 1
95
+ # land.township.should == 2
96
+ # land.range.should == 3
97
+ # land.meridian.should == "W4"
98
+ # end
99
+
100
+ it "understands 1-2-3 W4" do
101
+ land = Rats.new("1-2-3 W4")
102
+ land.quarter.should be_nil
103
+ land.section.should == 1
104
+ land.township.should == 2
105
+ land.range.should == 3
106
+ #land.meridian.should == "W4"
107
+ land.meridian.should == 4
108
+ end
109
+
110
+ it "understands 2-3 W4" do
111
+ land = Rats.new("2-3 W4")
112
+ land.quarter.should be_nil
113
+ land.section.should be_nil
114
+ land.township.should == 2
115
+ land.range.should == 3
116
+ #land.meridian.should == "W4"
117
+ land.meridian.should == 4
118
+ end
119
+
120
+ it "understands 3 W4" do
121
+ land = Rats.new("3 W4")
122
+ land.quarter.should be_nil
123
+ land.section.should be_nil
124
+ land.township.should be_nil
125
+ land.range.should == 3
126
+ #land.meridian.should == "W4"
127
+ land.meridian.should == 4
128
+ end
129
+
130
+ it "understands W4" do
131
+ land = Rats.new("W4")
132
+ land.quarter.should be_nil
133
+ land.section.should be_nil
134
+ land.township.should be_nil
135
+ land.range.should be_nil
136
+ #land.meridian.should == "W4"
137
+ land.meridian.should == 4
138
+ end
139
+
140
+ it "understands individual values" do
141
+ land = Rats.new("NE", 1, 2, 3, 4)
142
+ land.quarter.should == "NE"
143
+ land.section.should == 1
144
+ land.township.should == 2
145
+ land.range.should == 3
146
+ #land.meridian.should == "W4"
147
+ land.meridian.should == 4
148
+ end
149
+
150
+ it "understands SE 1-119-24 W4" do
151
+ land = Rats.new("SE 1-119-24 W4")
152
+ land.quarter.should == "SE"
153
+ land.section.should == 1
154
+ land.township.should == 119
155
+ land.range.should == 24
156
+ #land.meridian.should == "W4"
157
+ land.meridian.should == 4
158
+ end
159
+
160
+ describe "alternate method" do
161
+
162
+ it "using location and a string" do
163
+ land = Rats.new
164
+ land.location = "NE 1-2-3 W4"
165
+ land.quarter.should == "NE"
166
+ land.section.should == 1
167
+ land.township.should == 2
168
+ land.range.should == 3
169
+ #land.meridian.should == "W4"
170
+ land.meridian.should == 4
171
+ end
172
+
173
+ it "using location and individual values" do
174
+ land = Rats.new
175
+ land.location = ["NE", 1, 2, 3, 4]
176
+ land.quarter.should == "NE"
177
+ land.section.should == 1
178
+ land.township.should == 2
179
+ land.range.should == 3
180
+ land.meridian.should == 4
181
+ #land.meridian.to_s.should == "W4"
182
+ end
183
+
184
+ end
185
+
186
+ end
187
+
188
+ describe "validation (does it exist)" do
189
+
190
+ # each individual field has already been tested to know what is a valid field
191
+ # here, I am testing the location as a whole, as a location can be valid
192
+ # in respect to its individual components, but not as a whole
193
+
194
+ it "knows a valid and existing location" do
195
+ land = Rats.new("SE 1-2-3 W4")
196
+ land.location.should == "SE 1-2-3 W4"
197
+ land.exists?.should be_true
198
+ end
199
+
200
+ it "knows an non-existing section (in a valid township)" do
201
+ land = Rats.new("SE 2-119-24 W4")
202
+ land.exists?.should be_false
203
+
204
+ # but it neightbour does
205
+ land = Rats.new("SE 1-119-24 W4")
206
+ land.exists?.should be_true
207
+ end
208
+
209
+ it "knows an non-existing township (in a valid range)" do
210
+ land = Rats.new("SE 1-19-30 W4")
211
+ land.exists?.should be_false
212
+ end
213
+
214
+ it "knows an non-existing range (in a valid township)" do
215
+ land = Rats.new("SE 1-20-30 W4")
216
+ land.exists?.should be_false
217
+ end
218
+
219
+ end
220
+
221
+ describe "boundaries" do
222
+
223
+ it "knows when a township is North of Alberta" do
224
+ lambda { Rats.new("SE 1-127-1 W4") }.should raise_error(ArgumentError)
225
+ end
226
+
227
+ it "knows when a township is East of Alberta" do
228
+ lambda { Rats.new("SE 1-1-1 W3") }.should raise_error(ArgumentError)
229
+ end
230
+
231
+ it "knows when a township is West of Alberta" do
232
+ lambda { Rats.new("SE 1-1-1 W7") }.should raise_error(ArgumentError)
233
+ end
234
+
235
+ end
236
+
237
+ describe "taversing" do
238
+
239
+ describe "from quarter to quarter" do
240
+
241
+ describe "within a township" do
242
+
243
+ describe "within a section" do
244
+
245
+ it "knows what is up (north)" do
246
+ land = Rats.new("SE 1-2-3 W4")
247
+ land.up.location.should == "NE 1-2-3 W4"
248
+ end
249
+
250
+ it "knows what is down (south)" do
251
+ land = Rats.new("NE 1-2-3 W4")
252
+ land.down.location.should == "SE 1-2-3 W4"
253
+ end
254
+
255
+ it "knows what is left (west)" do
256
+ land = Rats.new("SE 1-2-3 W4")
257
+ land.left.location.should == "SW 1-2-3 W4"
258
+ end
259
+
260
+ it "knows what is right (east)" do
261
+ land = Rats.new("SW 1-2-3 W4")
262
+ land.right.location.should == "SE 1-2-3 W4"
263
+ end
264
+
265
+ end
266
+
267
+ describe "outside a section" do
268
+
269
+ it "knows what is up (north)" do
270
+ land = Rats.new("NE 1-2-3 W4")
271
+ land.up.location.should == "SE 12-2-3 W4"
272
+ end
273
+
274
+ it "knows what is down (south)" do
275
+ land = Rats.new("SE 12-2-3 W4")
276
+ land.down.location.should == "NE 1-2-3 W4"
277
+ end
278
+
279
+ it "knows what is left (west)" do
280
+ land = Rats.new("SW 1-2-3 W4")
281
+ land.left.location.should == "SE 2-2-3 W4"
282
+ end
283
+
284
+ it "knows what is right (east)" do
285
+ land = Rats.new("NE 2-2-3 W4")
286
+ land.right.location.should == "NW 1-2-3 W4"
287
+ end
288
+
289
+ end
290
+
291
+ end
292
+
293
+ describe "outside a township (which implies outside a section)" do
294
+
295
+ # townships do not line up North to South, so you can't know
296
+ # what township/section/quarter is above another when you
297
+ # leave a township
298
+
299
+ it "knows it can't go up (north)" do
300
+ land = Rats.new("NE 36-2-3 W4")
301
+ lambda { land.up }.should raise_error(IllegalTraverse)
302
+ end
303
+
304
+ it "knows it can't go down (south)" do
305
+ land = Rats.new("SE 1-2-3 W4")
306
+ lambda { land.down }.should raise_error(IllegalTraverse)
307
+ end
308
+
309
+ it "knows what is left (west)" do
310
+ land = Rats.new("SW 6-2-3 W4")
311
+ land.left.location.should == "SE 1-2-4 W4"
312
+ end
313
+
314
+ it "knows what is right (east)" do
315
+ land = Rats.new("NE 1-2-3 W4")
316
+ land.right.location.should == "NW 6-2-2 W4"
317
+ end
318
+
319
+ end
320
+
321
+ describe "outside a meridian (which implies outside a section and township)" do
322
+
323
+ # NOTE: you cannot leave a meridian by going North or South
324
+
325
+ it "knows what is left (west)" do
326
+ land = Rats.new("SW 6-2-30 W4")
327
+ land.left.location.should == "SE 1-2-1 W5"
328
+ end
329
+
330
+ it "knows what is right (east)" do
331
+ land = Rats.new("NE 1-2-1 W5")
332
+ land.right.location.should == "NW 6-2-30 W4"
333
+ end
334
+
335
+ end
336
+
337
+ describe "with complicated gaps in valid quarter sections" do
338
+
339
+ # this is the ultimate test(s). Can we traverse to the next quarter
340
+ # when the normal rules do not apply for what is next?
341
+
342
+ def skip(q,s,t,m)
343
+ skips = (q + s*2 + t*6*2 + m*30*6*2) || 0
344
+ skips -= 1 unless skips == 0
345
+ answers = ([false]*skips + [true]).to_a
346
+
347
+ Rats::Base.any_instance.stubs(:valid?).returns(*answers)
348
+ end
349
+
350
+ it "should skip sections going left (west)" do
351
+ skip(1,0,0,0)
352
+
353
+ land = Rats.new("NE 1-1-2 W4")
354
+ new_land = land.left
355
+ new_land.location.should == "NW 1-1-2 W4"
356
+ end
357
+
358
+ it "should skip sections going left (west)" do
359
+ skip(0,1,0,0)
360
+
361
+ land = Rats.new("NW 1-1-2 W4")
362
+ new_land = land.left
363
+ new_land.location.should == "NW 2-1-2 W4"
364
+ end
365
+
366
+ it "should skip sections going left (west)" do
367
+ skip(0,0,1,0)
368
+
369
+ land = Rats.new("NW 1-1-2 W4")
370
+ new_land = land.left
371
+ new_land.location.should == "NW 1-1-3 W4"
372
+ end
373
+
374
+ it "should skip sections going left (west)" do
375
+ skip(0,0,0,1)
376
+
377
+ land = Rats.new("NW 1-1-2 W4")
378
+ new_land = land.left
379
+ new_land.location.should == "NW 1-1-2 W5"
380
+ end
381
+
382
+ end
383
+
384
+ end
385
+
386
+ describe "from section to section" do
387
+
388
+ describe "within a township" do
389
+
390
+ it "knows what is up (north)" do
391
+ land = Rats.new("1-2-3 W4")
392
+ land.up.location.should == "12-2-3 W4"
393
+ end
394
+
395
+ it "knows what is down (south)" do
396
+ land = Rats.new("7-2-3 W4")
397
+ land.down.location.should == "6-2-3 W4"
398
+ end
399
+
400
+ it "knows what is left (west)" do
401
+ land = Rats.new("12-2-3 W4")
402
+ land.left.location.should == "11-2-3 W4"
403
+ end
404
+
405
+ it "knows what is right (east)" do
406
+ land = Rats.new("6-2-3 W4")
407
+ land.right.location.should == "5-2-3 W4"
408
+ end
409
+
410
+ end
411
+
412
+ describe "outside a township" do
413
+
414
+ # townships do not line up North to South, so you can't know
415
+ # what township/section/quarter is above another when you
416
+ # leave a township
417
+
418
+ it "knows it can't go up (north)" do
419
+ land = Rats.new("35-2-3 W4")
420
+ lambda { land.up }.should raise_error(IllegalTraverse)
421
+ end
422
+
423
+ it "knows it can't go down (south)" do
424
+ land = Rats.new("1-2-3 W4")
425
+ lambda { land.down }.should raise_error(IllegalTraverse)
426
+ end
427
+
428
+ it "knows what is left (west)" do
429
+ land = Rats.new("6-2-3 W4")
430
+ land.left.location.should == "1-2-4 W4"
431
+ end
432
+
433
+ it "knows what is right (east)" do
434
+ land = Rats.new("1-2-3 W4")
435
+ land.right.location.should == "6-2-2 W4"
436
+ end
437
+
438
+ end
439
+
440
+ end
441
+
442
+ describe "from township to township" do
443
+
444
+ describe "within a meridian" do
445
+
446
+ it "knows it can't go up (north)" do
447
+ land = Rats.new("2-3 W4")
448
+ lambda { land.up }.should raise_error(IllegalTraverse)
449
+ end
450
+
451
+ it "knows it can't go down (south)" do
452
+ land = Rats.new("2-3 W4")
453
+ lambda { land.down }.should raise_error(IllegalTraverse)
454
+ end
455
+
456
+ it "knows what is left (west)" do
457
+ land = Rats.new("2-3 W4")
458
+ land.left.location.should == "2-4 W4"
459
+ end
460
+
461
+ it "knows what is right (east)" do
462
+ land = Rats.new("2-3 W4")
463
+ land.right.location.should == "2-2 W4"
464
+ end
465
+
466
+ end
467
+
468
+ end
469
+
470
+ describe "from quarter to section" do
471
+
472
+ describe "within a township" do
473
+
474
+ it "knows what is up (north)" do
475
+ land = Rats.new("SE 1-2-3 W4")
476
+ land.up(:section).location.should == "12-2-3 W4"
477
+ end
478
+
479
+ it "knows what is down (south)" do
480
+ land = Rats.new("NE 7-2-3 W4")
481
+ land.down(:section).location.should == "6-2-3 W4"
482
+ end
483
+
484
+ it "knows what is left (west)" do
485
+ land = Rats.new("NE 12-2-3 W4")
486
+ land.left(:section).location.should == "11-2-3 W4"
487
+ end
488
+
489
+ it "knows what is right (east)" do
490
+ land = Rats.new("NW 6-2-3 W4")
491
+ land.right(:section).location.should == "5-2-3 W4"
492
+ end
493
+
494
+ end
495
+
496
+ describe "outside a township" do
497
+
498
+ # townships do not line up North to South, so you can't know
499
+ # what township/section/quarter is above another when you
500
+ # leave a township
501
+
502
+ it "knows it can't go up (north)" do
503
+ land = Rats.new("SE 35-2-3 W4")
504
+ lambda { land.up(:section) }.should raise_error(IllegalTraverse)
505
+ end
506
+
507
+ it "knows it can't go down (south)" do
508
+ land = Rats.new("NE 1-2-3 W4")
509
+ lambda { land.down(:section) }.should raise_error(IllegalTraverse)
510
+ end
511
+
512
+ it "knows what is left (west)" do
513
+ land = Rats.new("NE 6-2-3 W4")
514
+ land.left(:section).location.should == "1-2-4 W4"
515
+ end
516
+
517
+ it "knows what is right (east)" do
518
+ land = Rats.new("NW 1-2-3 W4")
519
+ land.right(:section).location.should == "6-2-2 W4"
520
+ end
521
+
522
+ end
523
+
524
+ end
525
+
526
+ describe "from quarter to township" do
527
+
528
+ describe "within a meridian" do
529
+
530
+ it "knows it can't go up (north)" do
531
+ land = Rats.new("NE 1-2-3 W4")
532
+ lambda { land.up(:township) }.should raise_error(IllegalTraverse)
533
+ end
534
+
535
+ it "knows it can't go down (south)" do
536
+ land = Rats.new("NE 1-2-3 W4")
537
+ lambda { land.down(:township) }.should raise_error(IllegalTraverse)
538
+ end
539
+
540
+ it "knows what is left (west)" do
541
+ land = Rats.new("NE 1-2-3 W4")
542
+ land.left(:township).location.should == "2-4 W4"
543
+ end
544
+
545
+ it "knows what is right (east)" do
546
+ land = Rats.new("NE 1-2-3 W4")
547
+ land.right(:township).location.should == "2-2 W4"
548
+ end
549
+
550
+ end
551
+
552
+ end
553
+
554
+ describe "from section to township" do
555
+
556
+ describe "within a meridian" do
557
+
558
+ it "knows it can't go up (north)" do
559
+ land = Rats.new("1-2-3 W4")
560
+ lambda { land.up(:township) }.should raise_error(IllegalTraverse)
561
+ end
562
+
563
+ it "knows it can't go down (south)" do
564
+ land = Rats.new("1-2-3 W4")
565
+ lambda { land.down(:township) }.should raise_error(IllegalTraverse)
566
+ end
567
+
568
+ it "knows what is left (west)" do
569
+ land = Rats.new("1-2-3 W4")
570
+ land.left(:township).location.should == "2-4 W4"
571
+ end
572
+
573
+ it "knows what is right (east)" do
574
+ land = Rats.new("1-2-3 W4")
575
+ land.right(:township).location.should == "2-2 W4"
576
+ end
577
+
578
+ end
579
+
580
+ end
581
+
582
+ describe "chaining" do
583
+
584
+ it "allows chaining of traversing" do
585
+ land = Rats.new("NE 1-2-3 W4")
586
+ land.up.down.left.right.location.should == "NE 1-2-3 W4"
587
+ end
588
+
589
+ end
590
+
591
+ end
592
+
593
+ describe "formatting" do
594
+
595
+ describe ":long" do
596
+
597
+ it "returns NE 1-2-3 W4" do
598
+ land = Rats.new("NE 1-2-3 W4")
599
+ land.location.should == "NE 1-2-3 W4"
600
+ end
601
+
602
+ it "returns 1-2-3 W4" do
603
+ land = Rats.new("1-2-3 W4")
604
+ land.location.should == "1-2-3 W4"
605
+ end
606
+
607
+ it "returns 2-3 W4" do
608
+ land = Rats.new("2-3 W4")
609
+ land.location.should == "2-3 W4"
610
+ end
611
+
612
+ end
613
+
614
+ describe ":short" do
615
+
616
+ it "returns NE01002034" do
617
+ land = Rats.new("NE 1-2-3 W4")
618
+ land.location(:short).should == "NE01002034"
619
+ end
620
+
621
+ it "returns 01001014" do
622
+ land = Rats.new("1-2-3 W4")
623
+ land.location(:short).should == "01002034"
624
+ end
625
+
626
+ it "returns 001014" do
627
+ land = Rats.new("2-3 W4")
628
+ land.location(:short).should == "002034"
629
+ end
630
+
631
+ end
632
+
633
+ end
634
+
635
+ describe "scope" do
636
+
637
+ it "knows quarter" do
638
+ land = Rats.new("NE 1-2-3 W4")
639
+ land.scope.should == :quarter
640
+ end
641
+
642
+ it "knows section" do
643
+ land = Rats.new("1-2-3 W4")
644
+ land.scope.should == :section
645
+ end
646
+
647
+ it "knows township" do
648
+ land = Rats.new("2-3 W4")
649
+ land.scope.should == :township
650
+ end
651
+
652
+ it "anything else is unknown" do
653
+ land = Rats.new("3 W4")
654
+ land.scope.should == :unknown
655
+ end
656
+
657
+ end
658
+
659
+ describe "methods" do
660
+
661
+ describe "validity" do
662
+
663
+ it "knows when it is valid" do
664
+ land = Rats.new("NE 1-2-3 W4")
665
+ land.valid?.should be_true
666
+ end
667
+
668
+ it "knows when it isn't valid"do
669
+ land = Rats.new("3 W4")
670
+ land.valid?.should be_false
671
+ end
672
+
673
+ end
674
+
675
+ it "responds_to :to_s" do
676
+ land = Rats.new("NE 1-2-3 W4")
677
+ land.to_s.should == land.location
678
+ end
679
+
680
+ it "responds_to :to_a" do
681
+ land = Rats.new("NE 1-2-3 W4")
682
+ land.to_a.should == ['NE', 1, 2, 3, 4]
683
+
684
+ land = Rats.new("1-2-3 W4")
685
+ land.to_a.should == [1, 2, 3, 4]
686
+ end
687
+
688
+ end
689
+
690
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
5
+ require 'rats'
6
+ include Rats
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ config.mock_with :mocha
11
+
12
+ end