Repository navigation
Expand file tree
/
Copy pathbusstops_test.go
More file actions
111 lines (104 loc) · 2.47 KB
/
Copy pathbusstops_test.go
File metadata and controls
111 lines (104 loc) · 2.47 KB
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
package main
import (
"reflect"
"testing"
)
// We need to ensure compiler actually returns the value
// A clever compiler might optimise it out, rendering our
// benchmarking results incorrect
var stop BusStop
func TestNewServerRejectsBadStops(t *testing.T) {
for _, path := range []string{"static/missing.json", "static/index.html"} {
t.Run(path, func(t *testing.T) {
server, err := NewServer(path)
if err == nil || server != nil {
t.Fatalf("NewServer(%q) = %v, %v; want nil server and an error", path, server, err)
}
})
}
}
func Benchmark_closest(b *testing.B) {
bs, _ := loadBusJSON("static/all.json")
b.ResetTimer()
for n := 0; n < b.N; n++ {
stop = bs.closest(Point{}) // ensure the compiler returns a value
}
}
func TestBusStops_closest(t *testing.T) {
// Keep coordinates fixed: the live dataset is updated weekly.
bs := BusStops{
{BusStopCode: "farther", Latitude: 2, Longitude: 104},
{
BusStopCode: "25059",
RoadName: "Tuas Sth Way",
Description: "Aft Tuas Sth Blvd",
Latitude: 1.270007,
Longitude: 103.61725,
},
{BusStopCode: "farthest", Latitude: 3, Longitude: 105},
}
type args struct {
location Point
}
tests := []struct {
name string
BusStops BusStops
args args
wantB BusStop
}{
{
name: "Middle Earth",
BusStops: bs,
args: args{
location: Point{
lat: 0.0,
lng: 0.0,
},
},
wantB: BusStop{
BusStopCode: "25059",
RoadName: "Tuas Sth Way",
Description: "Aft Tuas Sth Blvd",
Latitude: 1.270007,
Longitude: 103.61725,
},
},
}
for _, tt := range tests {
t.Parallel()
t.Run(tt.name, func(t *testing.T) {
if gotB := tt.BusStops.closest(tt.args.location); !reflect.DeepEqual(gotB, tt.wantB) {
t.Errorf("BusStops.closest() = %+v, want %+v", gotB, tt.wantB)
}
})
}
}
func TestBusStops_nameBusStopID(t *testing.T) {
bs, _ := loadBusJSON("static/all.json")
type args struct {
busid string
}
tests := []struct {
name string
BusStops BusStops
args args
wantDescription string
}{
{
name: "Bras Basah",
BusStops: bs,
args: args{
busid: "01019",
},
wantDescription: "Bras Basah Cplx",
},
}
for _, tt := range tests {
t.Parallel()
t.Run(tt.name, func(t *testing.T) {
if gotDescription := tt.BusStops.nameBusStop(tt.args.busid); gotDescription != tt.wantDescription {
t.Errorf("BusStops.nameBusStopID() = %v, want %v", gotDescription, tt.wantDescription)
}
})
}
}