This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
/
transport_spec.rb
143 lines (122 loc) · 5.7 KB
/
transport_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'spec_helper'
describe Transport do
it 'have a valid generator' do
lambda{ Fabricate(:transport) }.should_not raise_error
end
it 'should have roadmap' do
lambda{ Transport.new(:roadmap => nil, :schema => Fabricate(:schema)) }.should raise_error
end
it 'should have schema' do
lambda{ Transport.new(:roadmap => Fabricate(:roadmap), :schema => nil) }.should raise_error
end
it 'should get a random vehicle when none is given' do
10.times{ Fabricate(:vehicle) }
transport = Transport.new(:roadmap => Fabricate(:roadmap), :schema => Fabricate(:schema), :vehicle => nil)
lambda{ transport.vehicle }.should_not raise_error
transport.vehicle.class.should == Vehicle
end
context '5 positions schema' do
before(:each) do
# A ----- B
# | \ / |
# | C |
# | / \ |
# |/ \ |
# D ---- E
@schema = Fabricate(:schema)
@machineA = Fabricate(:machine)
@machineB = Fabricate(:machine)
@machineC = Fabricate(:machine)
@machineD = Fabricate(:machine)
@machineE = Fabricate(:machine)
@positionA = Fabricate(:position, :schema => @schema, :machine => @machineA)
@positionB = Fabricate(:position, :schema => @schema, :machine => @machineB)
@positionC = Fabricate(:position, :schema => @schema, :machine => @machineC)
@positionD = Fabricate(:position, :schema => @schema, :machine => @machineD)
@positionE = Fabricate(:position, :schema => @schema, :machine => @machineE)
@pathAB = Fabricate(:segment, :origin => @positionA, :target => @positionB, :distance => 100)
@pathAC = Fabricate(:segment, :origin => @positionA, :target => @positionC, :distance => 100)
@pathAD = Fabricate(:segment, :origin => @positionA, :target => @positionD, :distance => 300)
@pathBC = Fabricate(:segment, :origin => @positionB, :target => @positionC, :distance => 100)
@pathBE = Fabricate(:segment, :origin => @positionB, :target => @positionE, :distance => 300)
@pathCD = Fabricate(:segment, :origin => @positionC, :target => @positionD, :distance => 200)
@pathCE = Fabricate(:segment, :origin => @positionC, :target => @positionE, :distance => 200)
@pathDE = Fabricate(:segment, :origin => @positionD, :target => @positionE, :distance => 100)
end
it 'getting a valid transport roadmap' do
transport = Transport.new(
:schema => @schema,
:roadmap => Fabricate(:roadmap, :machines => [@machineA, @machineC, @machineD])
)
pathAC = transport.paths[0]
pathAC.first[:position].should == @positionA
pathAC.last[:position].should == @positionC
pathCD = transport.paths[1]
pathCD.first[:position].should == @positionC
pathCD.last[:position].should == @positionD
end
context 'follow the roadmap order' do
before(:each) do
@transport = Transport.new(
:schema => @schema,
:roadmap => Fabricate(:roadmap, :machines => [@machineA, @machineB, @machineC, @machineD])
)
end
it 'first path (A-B)' do
@transport.paths[0].start.should == @positionA
@transport.paths[0].goal.should == @positionB
@transport.paths[0].first[:parent].should == nil
@transport.paths[0].first[:position].should == @positionA
@transport.paths[0].last[:parent][:position].should == @positionA
@transport.paths[0].last[:position].should == @positionB
end
it 'second path (B-C)' do
@transport.paths[1].start.should == @positionB
@transport.paths[1].goal.should == @positionC
@transport.paths[1].first[:parent].should == nil
@transport.paths[1].first[:position].should == @positionB
@transport.paths[1].last[:parent][:position].should == @positionB
@transport.paths[1].last[:position].should == @positionC
end
it 'third path (C-D)' do
@transport.paths[2].start.should == @positionC
@transport.paths[2].goal.should == @positionD
@transport.paths[2].first[:parent].should == nil
@transport.paths[2].first[:position].should == @positionC
@transport.paths[2].last[:parent][:position].should == @positionC
@transport.paths[2].last[:position].should == @positionD
end
end
it 'should be able to fit to a new roadmap' do
transport = Transport.new(
:schema => @schema,
:roadmap => Fabricate(:roadmap, :machines => [@machineA, @machineB, @machineC, @machineD])
)
paths_before = transport.paths.dup
transport.roadmap = Fabricate(:roadmap, :machines => [@machineB, @machineC, @machineD, @machineE])
transport.paths[0].should == paths_before[1]
transport.paths[1].should == paths_before[2]
transport.paths[2].start.should == @positionD
transport.paths[2].goal.should == @positionE
transport.paths[2].first[:position].should == @positionD
transport.paths[2].last[:position].should == @positionE
end
context 'distance and time' do
before(:each) do
@transport = Transport.new(
:schema => @schema,
:roadmap => Fabricate(:roadmap, :machines => [@machineA, @machineC, @machineD]),
:vehicle => Fabricate(:vehicle, :speed => 3)
)
end
it 'should be able to get the distance from a machine to other' do
@transport.distance_to(@machineA, @machineC).should == 100
@transport.distance_to(@machineC, @machineD).should == 200
end
it 'should be able to get the time from a machine to other' do
@transport.time_to(@machineA, @machineC).should == 2 #minutes
@transport.time_to(@machineC, @machineD).should == 4 #minutes
end
end
end
end