Skip to content

Commit c8ca3a0

Browse files
author
SymbolixAU
committed
tests for #170
1 parent c4c7e23 commit c8ca3a0

10 files changed

+90
-69
lines changed

R/google_directions.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#' list of unnamed elements, each element is either a numeric vector of lat/lon
1212
#' coordinates, an address string or place_id, or a vector of a pair of lat / lon coordinates
1313
#' @param mode \code{string} One of 'driving', 'walking', 'bicycling' or 'transit'.
14-
#' @param departure_time a \code{POSIXct} time since 1st January 1970.
15-
#' Specifies the desired time of departure. If no value
16-
#' is specified it defaults to \code{Sys.time()}
17-
#' @param arrival_time a \code{POSIXct} time since 1st January 1970.
18-
#' Specifies the desired time of arrival. Note you
19-
#' can only specify one of \code{arrival_time} or \code{departure_time}, not both.
14+
#' @param departure_time The desired time of departure.
15+
#' Use either a \code{POSIXct} time since 1st January 1970, or the string 'now'.
16+
#' If no value is specified it defaults to \code{Sys.time()}.
17+
#' @param arrival_time Specifies the desired time of arrival for transit requests.
18+
#' Use either a \code{POSIXct} time since 1st January 1970.
19+
#' Note you can only specify one of \code{arrival_time} or \code{departure_time}, not both.
2020
#' If both are supplied, \code{departure_time} will be used.
2121
#' @param waypoints list of waypoints, expressed as either \code{vectors} of
2222
#' lat/lon coordinates, or a \code{string} address to be geocoded, or an encoded
@@ -88,6 +88,11 @@
8888
#' units = "imperial",
8989
#' simplify = TRUE)
9090
#'
91+
#' ## using 'now' as departure time
92+
#' google_directions(origin = "Flinders Street Station, Melbourne",
93+
#' destination = "MCG, Melbourne",
94+
#' departure_time = 'now')
95+
#'
9196
#' ## waypoints expressed as an encoded polyline
9297
#' polyWaypoints <- encode_pl(tram_stops[1:2, c("stop_lat")], tram_stops[1:2, c("stop_lon")])
9398
#' polyWaypoints <- list(via = paste0("enc:", polyWaypoints, ":"))
@@ -98,7 +103,7 @@
98103
#'
99104
#'
100105
#' ## using bus and less walking
101-
#' google_directions(origin = "Melbourne Airport, Australia",
106+
#' res <- google_directions(origin = "Melbourne Airport, Australia",
102107
#' destination = "Portsea, Melbourne, Australia",
103108
#' departure_time = Sys.time() + (24 * 60 * 60),
104109
#' mode = "transit",
@@ -107,7 +112,7 @@
107112
#' simplify = FALSE)
108113
#'
109114
#' ## using arrival time
110-
#' google_directions(origin = "Melbourne Airport, Australia",
115+
#' res <- google_directions(origin = "Melbourne Airport, Australia",
111116
#' destination = "Portsea, Melbourne, Australia",
112117
#' arrival_time = Sys.time() + (24 * 60 * 60),
113118
#' mode = "transit",
@@ -116,7 +121,7 @@
116121
#' simplify = FALSE)
117122
#'
118123
#' ## return results in French
119-
#' google_directions(origin = "Melbourne Airport, Australia",
124+
#' res <- google_directions(origin = "Melbourne Airport, Australia",
120125
#' destination = "Portsea, Melbourne, Australia",
121126
#' arrival_time = Sys.time() + (24 * 60 * 60),
122127
#' mode = "transit",

R/google_map_layer_marker.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ add_markers <- function(map,
164164
shape <- createInfoWindowChart(shape, infoWindowChart, id)
165165
shape <- jsonlite::toJSON(shape, digits = digits)
166166

167-
# print(shape)
168167
map <- addDependency(map, googleMarkerDependency())
169168

170169
invoke_method(map, 'add_markers', shape, cluster, update_map_view, layer_id, usePolyline, load_interval, focus_layer, close_info_window)

R/google_map_layer_polyline.R

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,10 @@ add_polylines <- function(map,
183183
keep <- setdiff(n, c('id', 'lat', 'lng'))
184184

185185
lst_polyline <- objPolylineCoords(shape, ids, keep)
186-
# print(lst_polyline)
187-
# print("--shape--")
188-
# print(shape)
189186

190-
#print(infoWindowChart)
191187
lst_polyline <- createInfoWindowChart(lst_polyline, infoWindowChart, id)
192-
# print(lst_polyline)
193188

194189
shape <- jsonlite::toJSON(lst_polyline, digits = digits, auto_unbox = T)
195-
196-
# print(shape)
197190
}
198191

199192
map <- addDependency(map, googlePolylineDependency())

R/googleway-package.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@ directions_data <- function(base_url,
6868

6969
alternatives <- validateAlternatives(alternatives)
7070

71-
if(!is.null(traffic_model) & is.null(departure_time))
72-
departure_time <- Sys.time()
71+
# if(!is.null(traffic_model) & is.null(departure_time))
72+
# departure_time <- as.integer( Sys.time() )
7373

7474
traffic_model <- validateTrafficModel(traffic_model)
7575

7676
## check origin/destinations are valid
77-
if(information_type == "directions"){
77+
if( information_type == "directions" ) {
7878
origin <- check_location(origin, "Origin")
7979
destination <- check_location(destination, "Destination")
80-
}else if(information_type == "distance"){
80+
}else if( information_type == "distance" ) {
8181
origin <- check_multiple_locations(origin, "Origins elements")
8282
destination <- check_multiple_locations(destination, "Destinations elements")
8383
}
8484

8585
## times as integers
86-
departure_time <- ifelse(is.null(departure_time), as.integer(Sys.time()), as.integer(departure_time))
86+
#departure_time <- ifelse(is.null(departure_time), as.integer(Sys.time()), as.integer(departure_time))
8787
arrival_time <- as.integer(arrival_time)
8888

8989
waypoints <- validateWaypoints(waypoints, optimise_waypoints, mode)

R/legend.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ formatPalette <- function(palette, type){
7777

7878
if(rw[1] != 1) rw <- c(1, rw)
7979
if(rw[length(rw)] != nrow(palette)) rw <- c(rw, nrow(palette))
80-
#print(rw)
80+
8181
palette <- palette[rw, ]
8282
}
8383
return(palette)

R/parameter_checks.R

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,14 @@ validateAlternatives <- function(alternatives){
226226

227227
validateArrivalTime <- function(arrival_time){
228228

229-
if(is.null(arrival_time)) return(NULL)
229+
if(is.null(arrival_time)) return( NULL )
230230

231231
checkPosix(arrival_time)
232+
232233
return(arrival_time)
233234
}
234235

236+
235237
validateArrivalDepartureTimes <- function(arrival_time, departure_time){
236238

237239
if(!is.null(arrival_time) & !is.null(departure_time)){
@@ -306,13 +308,21 @@ validateComponentsCountries <- function(components){
306308
validateDepartureTime <- function(departure_time, mode){
307309
if(is.null(departure_time)) return(NULL)
308310

309-
checkPosix(departure_time)
311+
if (inherits(departure_time, "POSIXct")) {
312+
checkPosix( departure_time )
310313

311-
if(mode == "driving" && departure_time < Sys.time()){
312-
stop("departure_time for driving mode must not be in the past")
313-
}
314+
if(mode == "driving" && ( departure_time + 60) < Sys.time() ){ ## allowing a buffer
315+
stop("departure_time for driving mode must not be in the past")
316+
}
314317

315-
return(departure_time)
318+
return( as.integer(departure_time ) )
319+
320+
} else if ( inherits(departure_time, "character") ) {
321+
if( departure_time != "now") {
322+
stop("when using a string for departure_time you may only use 'now'")
323+
}
324+
}
325+
return( departure_time )
316326
}
317327

318328
validateFindInput <- function( input, inputtype ) {
@@ -643,9 +653,10 @@ validateTrafficModel <- function(traffic_model){
643653
}
644654

645655
## transit_mode is only valid where mode = transit
646-
validateTransitMode <- function(transit_mode, mode){
656+
validateTransitMode <- function(transit_mode, mode) {
657+
658+
if(!is.null(transit_mode) & mode != "transit") {
647659

648-
if(!is.null(transit_mode) & mode != "transit"){
649660
warning("You have specified a transit_mode, but are not using mode = 'transit'. Therefore this argument will be ignored")
650661
return(NULL)
651662
}else if(!is.null(transit_mode) & mode == "transit"){

man/google_directions.Rd

Lines changed: 14 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/google_distance.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-google_directions.R

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,18 @@ test_that("Avoid is a valid type", {
7070

7171
})
7272

73-
# test_that("Departure time is not in the past",{
74-
#
75-
# expect_error(
76-
# google_directions(origin = c(-37.8179746, 144.9668636),
77-
# destination = c(-37.81659, 144.9841),
78-
# departure_time = as.POSIXct("2015-01-01"),
79-
# key = "abc",
80-
# simplify = TRUE)
81-
# )
82-
#
83-
# })
73+
test_that("Departure time is not in the past",{
74+
75+
expect_error(
76+
google_directions(origin = c(-37.8179746, 144.9668636),
77+
destination = c(-37.81659, 144.9841),
78+
departure_time = as.POSIXct("2015-01-01"),
79+
key = "abc",
80+
simplify = TRUE),
81+
"departure_time for driving mode must not be in the past"
82+
)
83+
84+
})
8485

8586
test_that("waypoints only valid for certain modes",{
8687

tests/testthat/test-google_distances.R

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,28 @@ test_that("Avoid is a valid type", {
4242
expect_error(google_distance(origins = list(c(-37.8179746, 144.9668636)),
4343
destinations = list(c(-37.81659, 144.9841)),
4444
avoid = "dont avoid",
45-
key = "abc",
46-
simplify = TRUE),
45+
key = "abc"),
4746
"avoid can only include tolls, highways, ferries or indoor")
4847
})
4948

50-
# test_that("Departure time is not in the past",{
51-
#
52-
# expect_error(google_distance(origins = list(c(-37.8179746, 144.9668636)),
53-
# destinations = list(c(-37.81659, 144.9841)),
54-
# departure_time = as.POSIXct("2015-01-01"),
55-
# key = "abc",
56-
# simplify = TRUE),
57-
# "departure_time must not be in the past")
58-
#
59-
# })
49+
test_that("Departure time is not in the past",{
50+
51+
expect_error(google_distance(origins = list(c(-37.8179746, 144.9668636)),
52+
destinations = list(c(-37.81659, 144.9841)),
53+
departure_time = as.POSIXct("2015-01-01"),
54+
key = "abc"),
55+
"departure_time for driving mode must not be in the past")
56+
57+
})
58+
59+
test_that("now is a valid departure time", {
60+
61+
expect_silent(google_distance(origins = list(c(-37.8179746, 144.9668636)),
62+
destinations = list(c(-37.81659, 144.9841)),
63+
departure_time = 'now',
64+
key = "abc"))
65+
})
66+
6067

6168
# test_that("waypoints only valid for certain modes",{
6269
#

0 commit comments

Comments
 (0)