summaryrefslogtreecommitdiff
path: root/t/20-vrr.t
blob: 96a8c69b41ff4506d89324709dde04e5b12dd550 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

use Test::More tests => 59;

BEGIN {
	use_ok('Net::Travel::DE::VRR');
}
require_ok('Net::Travel::DE::VRR');

sub efa_conf {
	my $ret = {
		origin      => ['Essen', 'HBf'],
		destination => ['Koeln', 'HBf'],
	};
	foreach my $p (@_) {
		$ret->{$p->[0]} = $p->[1];
	}
	return $ret;
}

sub efa_new {
	return new_ok(
		'Net::Travel::DE::VRR' => [%{efa_conf(@_)}]
	);
}

sub is_efa_post {
	my ($ck, $cv, @post) = @_;
	my $efa = efa_new([$ck, $cv]);

	my $ok = 1;

	is_deeply(
		$efa->{'config'}, efa_conf([$ck, $cv]),
		"$ck => $cv: conf ok",
	);

	foreach my $ref (@post) {
		my ($key, $value) = @{$ref};
		if (not defined $efa->{'post'}->{"key"} and
				not defined $value) {
			next;
		}
		if ($efa->{'post'}->{"$key"} ne $value) {
			$ok = 0;
			last;
		}
	}
	ok(
		$ok,
		"$ck => $cv: POST okay",
	);
}

sub is_efa_err {
	my ($key, $val, $str) = @_;
	return; # FIXME error handling
	my $efa = efa_new([$key, $val]);

	my $val_want = $val;

	if (ref $val eq 'ARRAY') {
		$val_want = join(q{ }, @{$val});
	}

	is_deeply(
		$efa->{'config'}, efa_conf([$key, $val]),
		"conf ok: $key => $val",
	);

	# FIXME actual error tests

}

is_efa_post('ignored', 'ignored');

my $efa;

is_efa_post(
	'via', ['MH', 'HBf'],
	['place_via', 'MH'],
	['name_via', 'HBf'],
	['type_via', 'stop'],
);

is_efa_post(
	'origin', ['D', 'Fuerstenwall 232', 'address'],
	['place_origin', 'D'],
	['name_origin', 'Fuerstenwall 232'],
	['type_origin', 'address'],
);

is_efa_post(
	'departure_time', '22:23',
	['itdTripDateTimeDepArr', 'dep'],
	['itdTimeHour', '22'],
	['itdTimeMinute', '23'],
);

is_efa_post(
	'arrival_time', '16:38',
	['itdTripDateTimeDepArr', 'arr'],
	['itdTimeHour', '16'],
	['itdTimeMinute', '38'],
);

is_efa_err(
	'departure_time', '37:00',
	'Must match HH:MM',
);

is_efa_err(
	'departure_time', '07',
	'Must match HH:MM',
);

is_efa_post(
	'date', '2.10.2009',
	['itdDateDay', '2'],
	['itdDateMonth', '10'],
	['itdDateYear', '2009'],
);

is_efa_post(
	'date', '26.12.',
	['itdDateDay', '26'],
	['itdDateMonth', '12'],
	['itdDateYear', (localtime(time))[5] + 1900],
);

is_efa_err(
	'date', '42.5.2003',
	'Invalid day',
);

is_efa_err(
	'date', '7.',
	'Invalid month',
);

is_efa_post(
	'exclude', [qw[zug]],
	['inclMOT_0', undef],
);

is_efa_post(
	'exclude', [qw[stadtbus schiff ast]],
	['inclMOT_5',  undef],
	['inclMOT_9',  undef],
	['inclMOT_10', undef],
);

is_efa_err(
	'exclude', [qw[sonstige invalid]],
	'Must consist of '
	. 'zug s-bahn u-bahn stadtbahn tram stadtbus regionalbus '
	. 'schnellbus seilbahn schiff ast sonstige',
);

is_efa_post(
	'select_interchange_by', 'speed',
	['routeType', 'LEASTTIME'],
);

is_efa_post(
	'select_interchange_by', 'waittime',
	['routeType', 'LEASTINTERCHANGE'],
);

is_efa_post(
	'select_interchange_by', 'distance',
	['routeType', 'LEASTWALKING'],
);

is_efa_err(
	'select_interchange_by', 'invalid',
	'Must be either speed, nowait or nowalk',
);

is_efa_post(
	'train_type', 'local',
	['lineRestriction', 403],
);

is_efa_post(
	'train_type', 'ic',
	['lineRestriction', 401],
);

is_efa_post(
	'train_type', 'ice',
	['lineRestriction', 400],
);

is_efa_err(
	'train_type', 'invalid',
	'Must be one of local/ic/ice',
);

is_efa_post(
	'walk_speed', 'normal',
	['changeSpeed', 'normal'],
);

is_efa_err(
	'walk_speed', 'invalid',
	'Must be normal, fast or slow',
);

is_efa_post(
	'max_interchanges', 5,
	['maxChanges', 5],
);

is_efa_post(
	'use_near_stops', 1,
	['useProxFootSearch', 1],
);

is_efa_post(
	'with_bike', 1,
	['bikeTakeAlong', 1],
);