Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using equality checks / %in% on class() #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions R/otp-connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ otp_connect <- function(hostname = "localhost",
#' @noRd
#'
make_url <- function(x, type = "routers") {
if(!"otpconnect" %in% class(x)){
stop("Object is not of class otpconnect, class is ", class(x))
if (!is_otpconnect(x)) {
stop("Object is not of class otpconnect, class is ", toString(class(x)))
}

if(type == "routers"){
Expand Down Expand Up @@ -167,8 +167,8 @@ make_url <- function(x, type = "routers") {
#' @noRd
#'
check_router <- function(x) {
if(!"otpconnect" %in% class(x)){
stop("Object is not of class otpconnect, class is ", class(x))
if (!is_otpconnect(x)) {
stop("Object is not of class otpconnect, class is ", toString(class(x)))
}
check <- try(curl::curl_fetch_memory(make_url(x)), silent = TRUE)
if (inherits(check, "try-error")) {
Expand All @@ -184,8 +184,8 @@ check_router <- function(x) {
#' @noRd
#'
check_routers <- function(otpcon) {
if(!"otpconnect" %in% class(otpcon)){
stop("Object is not of class otpconnect, class is ", class(otpcon))
if (!is_otpconnect(otpcon)) {
stop("Object is not of class otpconnect, class is ", toString(class(otpcon)))
}

if (is.null(otpcon$url)) {
Expand Down
6 changes: 3 additions & 3 deletions R/otp-plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,12 @@ otp_parse_missing <- function(x){

otp_clean_input <- function(imp, imp_name) {
# For single point inputs
if (all(class(imp) == "numeric")) {
if (!is.matrix(imp) && is.double(imp)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test looked to me like "non-matrix numeric vector", which matches the new test. CMIIW.

checkmate::assert_numeric(imp, len = 2)
imp <- matrix(imp, nrow = 1, byrow = TRUE)
}
# For SF inputs
if ("sf" %in% class(imp)) {
if (inherits(imp, "sf")) {
if (all(sf::st_geometry_type(imp) == "POINT")) {
imp <- sf::st_coordinates(imp)
imp[] <- imp[, c(1, 2)]
Expand All @@ -440,7 +440,7 @@ otp_clean_input <- function(imp, imp_name) {

# For matrix inputs
# if (all(class(imp) == "matrix")) { # to pass CRAN checks
if ("matrix" %in% class(imp)) {
if (is.matrix(imp)) {
checkmate::assert_matrix(imp,
any.missing = FALSE,
min.rows = 1,
Expand Down
2 changes: 1 addition & 1 deletion R/otp-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ otp_setup <- function(otp = NULL,
check = TRUE
), silent = TRUE)

if ("otpconnect" %in% class(otpcon)) {
if (is_otpconnect(otpcon)) {
message(paste0(
Sys.time(),
" OTP is ready to use Go to localhost:",
Expand Down
2 changes: 1 addition & 1 deletion R/otp-surface.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ otp_pointset <- function(points = NULL,
name = NULL,
dir = NULL) {

if(!"sf" %in% class(points)){
if (!inherits(point, "sf")) {
stop("points is not an sf object")
}

Expand Down
6 changes: 6 additions & 0 deletions R/utility-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ split_alternating <- function(x){
check.names = FALSE,
check.rows = FALSE))
}

#' Check for 'otpconnect' class
#' @param x An object.
#' @family internal
#' @noRd
is_otpconnect <- function(x) inherits(x, "otpconnect")
12 changes: 6 additions & 6 deletions tests/testthat/test_01_internal_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test_that("test otp_json2sf", {


r1 <- otp_json2sf(itineraries = r1, fp = "", tp = "", get_elevation = FALSE)
expect_true("data.frame" %in% class(r1))
expect_s3_class(r1, "data.frame")
expect_true(nrow(r1) == 1)


Expand All @@ -63,7 +63,7 @@ test_that("test otp_json2sf", {


r2 <- otp_json2sf(r2, fp = "", tp = "", get_geometry = FALSE)
expect_true("data.frame" %in% class(r2))
expect_s3_class(r2, "data.frame")
expect_true(nrow(r2) == 1)


Expand All @@ -73,7 +73,7 @@ test_that("test otp_json2sf", {


r4 <- otp_json2sf(itineraries = r4, fp = "", tp = "")
expect_true("data.frame" %in% class(r4))
expect_s3_class(r4, "data.frame")
expect_true(nrow(r4) == 9)
})

Expand All @@ -88,10 +88,10 @@ test_that("get elevations", {
get_geometry = TRUE,
full_elevation = TRUE
)
expect_true("data.frame" %in% class(r3))
expect_s3_class(r3, "data.frame")
expect_true(nrow(r3) == 1)
expect_true("leg_elevation" %in% names(r3))
expect_true(class(r3$leg_elevation) == "list")
expect_true(is.list(r3$leg_elevation))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could also be expect_type(., "list")

})


Expand Down Expand Up @@ -159,7 +159,7 @@ test_that("test polyline2linestring", {

test_that("test otp_check_java", {
suppressWarnings(r1 <- otp_check_java())
expect_true(class(r1) == "logical")
expect_type(r1, "logical")
})


Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_02_without_OTP.R
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ context("test routing options")
test_that("otp_routing_options creation", {
skip_on_cran()
routingOptions <- otp_routing_options()
expect_true(class(routingOptions) == "list")
expect_true(is.list(routingOptions))
routingOptions$walkSpeed <- 999
routingOptions <- otp_validate_routing_options(routingOptions)
expect_true(class(routingOptions) == "list")
expect_true(is.list(routingOptions))
expect_true(length(routingOptions) == 1)
})

Expand Down
Loading