-
Notifications
You must be signed in to change notification settings - Fork 0
/
parking_lot_test.go
186 lines (145 loc) · 4.41 KB
/
parking_lot_test.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package parking_lot
import (
"errors"
"example.com/parking_lot/slot"
"example.com/parking_lot/slot/mock"
"example.com/parking_lot/vehicle"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewParkingSlotManager(t *testing.T) {
numSlots := 2
pm := NewParkingSlotManager(numSlots)
slots, err := pm.Status()
assert.Nil(t, err)
assert.EqualValues(t, len(slots), numSlots)
}
func TestParkingLotMgrImpl_FindVehicleSlot(t *testing.T) {
v := vehicle.NewCar("rno", "blue")
ctrl := gomock.NewController(t)
slotMock := mock.NewMockParkingSlot(ctrl)
someUnexpError := errors.New("Some unknown error ")
expDistance := 1
count := -1
slotMock.EXPECT().GetVehicle().DoAndReturn(func() (vehicle.Vehicle, error) {
count++
if count == 0 {
return v, errors.New(slot.NO_VEHICLE)
} else if count == 1 {
return v, someUnexpError
}
return v, nil
}).Times(3)
slotMock.EXPECT().Distance().DoAndReturn(func() (int, error) {
return expDistance, nil
}).Times(1)
slots := []slot.ParkingSlot{slotMock}
pm := parkingLotMgrImpl{slots}
// Executing the function with different cases
// CASE: No vehicles in slot
_, err := pm.FindVehicleSlot("rno2")
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), NOT_FOUND)
// CASE: Some unexpected error from slots while getting vehicle
_, err = pm.FindVehicleSlot("rno2")
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), someUnexpError.Error())
// CASE: Vehicle found
actualV, err := pm.FindVehicleSlot("rno")
assert.Nil(t, err)
assert.EqualValues(t, expDistance, actualV)
}
func TestParkingLotMgrImpl_LeaveVehicle(t *testing.T) {
ctrl := gomock.NewController(t)
slotMock := mock.NewMockParkingSlot(ctrl)
slotMock.EXPECT().Free().DoAndReturn(func() error {
return nil
}).Times(1)
slots := []slot.ParkingSlot{slotMock}
pm := parkingLotMgrImpl{slots}
// Executing the function with different cases
// CASE: Invalid input
err := pm.LeaveVehicle(0)
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), SLOT_OUT_OF_BOUND)
// CASE: Success
err = pm.LeaveVehicle(1)
assert.Nil(t, err)
}
func TestParkingLotMgrImpl_ParkVehicle(t *testing.T) {
v := vehicle.NewCar("rno", "blue")
ctrl := gomock.NewController(t)
slotMock := mock.NewMockParkingSlot(ctrl)
countIsFree := -1
slotMock.EXPECT().IsFree().DoAndReturn(func() (bool, error) {
countIsFree++
if countIsFree == 0 {
return false, nil
}
return true, nil
}).Times(3)
countPark := -1
errParking := errors.New("Unable to park vehicle ")
slotMock.EXPECT().ParkVehicle(gomock.Any()).DoAndReturn(func(vehicle.Vehicle) error {
countPark++
if countPark == 0 {
return errParking
}
return nil
}).Times(2)
expSlot := 1
slotMock.EXPECT().Distance().DoAndReturn(func() (int, error) {
return expSlot, nil
}).Times(1)
slots := []slot.ParkingSlot{slotMock}
pm := parkingLotMgrImpl{slots}
// Executing the function with different cases
// CASE: No slots free
_, err := pm.ParkVehicle(v)
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), FULL_PARKING)
// CASE: Unable to park
_, err = pm.ParkVehicle(v)
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), errParking.Error())
// CASE: Success
actualSlot, err := pm.ParkVehicle(v)
assert.Nil(t, err)
assert.EqualValues(t, expSlot, actualSlot)
}
func TestParkingLotMgrImpl_SlotsWithColor(t *testing.T) {
v := vehicle.NewCar("rno", "blue")
ctrl := gomock.NewController(t)
slotMock := mock.NewMockParkingSlot(ctrl)
someUnexpError := errors.New("Some unknown error ")
expDistance := 1
count := -1
slotMock.EXPECT().GetVehicle().DoAndReturn(func() (vehicle.Vehicle, error) {
count++
if count == 0 {
return v, errors.New(slot.NO_VEHICLE)
} else if count == 1 {
return v, someUnexpError
}
return v, nil
}).Times(3)
slotMock.EXPECT().Distance().DoAndReturn(func() (int, error) {
return expDistance, nil
}).Times(1)
slots := []slot.ParkingSlot{slotMock}
pm := parkingLotMgrImpl{slots}
// Executing the function with different cases
// CASE: No vehicles in slot
_, err := pm.SlotsWithColor("blue")
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), NOT_FOUND)
// CASE: Some unexpected error from slots while getting vehicle
_, err = pm.SlotsWithColor("blue")
assert.NotNil(t, err)
assert.EqualValues(t, err.Error(), someUnexpError.Error())
// CASE: Vehicle found
actualV, err := pm.SlotsWithColor("blue")
assert.Nil(t, err)
assert.EqualValues(t, slots, actualV)
}