-
Notifications
You must be signed in to change notification settings - Fork 0
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
jidea-157 Support ISO codes as country parameter to daedalus #38
Changes from all commits
c32765d
8da9845
8466963
ebcb639
64054ee
cf386f7
a6bdac4
3c8cccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#' Get country name from arg | ||
#' @description Function to get a supported country name given an arg which | ||
#' may be a country name or may be an ISO2 or ISO3 code. In all cases, | ||
#' the function checks if the country identifier is supported. | ||
#' | ||
#' @param country A string which could be a country name, or an ISO2 or ISO3 | ||
#' code. | ||
#' | ||
#' @return A supported country name | ||
#' @keywords internal | ||
country_name_from_arg <- function(country) { | ||
lookup_country_name_from_code <- function(code, code_list) { | ||
# check code is known, and lookup country name from idx | ||
code <- rlang::arg_match(code, code_list) | ||
idx <- match(code, code_list) | ||
daedalus::country_names[[idx]] | ||
} | ||
|
||
if (any(grep("^[A-Z]{2}$", country))) { # regex check for ISO2 | ||
country <- lookup_country_name_from_code( | ||
country, daedalus::country_codes_iso2c | ||
) | ||
} else if (any(grep("^[A-Z]{3}$", country))) { # regex check for ISO3 | ||
country <- lookup_country_name_from_code( | ||
country, daedalus::country_codes_iso3c | ||
) | ||
} else { | ||
# check country name is known | ||
country <- rlang::arg_match(country, daedalus::country_names) | ||
} | ||
country | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,27 @@ test_that("class <daedalus_country>: basic expectations", { | |
) | ||
}) | ||
|
||
test_that("class <daedalus_country>`: works for all countries", { | ||
test_that("class <daedalus_country>`: works for all country names", { | ||
expect_no_condition({ | ||
countries <- lapply(daedalus::country_names, daedalus_country) # nolint | ||
}) | ||
checkmate::expect_list(countries, "daedalus_country") | ||
}) | ||
|
||
test_that("class <daedalus_country>`: works for all country ISO2 codes", { | ||
expect_no_condition({ | ||
countries <- lapply(daedalus::country_codes_iso2c, daedalus_country) # nolint | ||
}) | ||
checkmate::expect_list(countries, "daedalus_country") | ||
}) | ||
|
||
test_that("class <daedalus_country>`: works for all country ISO3 codes", { | ||
expect_no_condition({ | ||
countries <- lapply(daedalus::country_codes_iso3c, daedalus_country) # nolint | ||
}) | ||
checkmate::expect_list(countries, "daedalus_country") | ||
}) | ||
|
||
Comment on lines
+30
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is overkill, but it's following the pattern! |
||
test_that("class <daedalus_country>: access and assignment", { | ||
name <- "Canada" | ||
country_x <- daedalus_country(name) | ||
|
@@ -121,7 +135,19 @@ test_that("class <daedalus_country>`: errors", { | |
# invalid name | ||
expect_error( | ||
daedalus_country("dummy"), | ||
regexp = "`name` must be one of" | ||
regexp = "`country` must be one of" | ||
) | ||
|
||
# invalid ISO2 | ||
expect_error( | ||
daedalus_country("ZZ"), | ||
regexp = "`code` must be one of" | ||
) | ||
|
||
# invalid ISO3 | ||
expect_error( | ||
daedalus_country("ZZZ"), | ||
regexp = "`code` must be one of" | ||
) | ||
|
||
# invalid `parameter` type | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
test_that("country_name_from_arg looks up name from iso2", { | ||
expect_identical(country_name_from_arg("CA"), "Canada") | ||
}) | ||
|
||
test_that("country_name_from_arg looks up name from iso3", { | ||
expect_identical(country_name_from_arg("CAN"), "Canada") | ||
}) | ||
|
||
test_that("country_name_from_arg passes through supported country name", { | ||
expect_identical(country_name_from_arg("Canada"), "Canada") | ||
}) | ||
|
||
test_that("country_name_from_arg errors on unsupported iso2", { | ||
expect_error( | ||
country_name_from_arg("ZZ"), | ||
regexp = "must be one of" | ||
) | ||
}) | ||
|
||
test_that("country_name_from_arg errors on unsupported iso3", { | ||
expect_error( | ||
country_name_from_arg("ZZZ"), | ||
regexp = "must be one of" | ||
) | ||
}) | ||
|
||
test_that("country_name_from_arg errors on unsupported country name", { | ||
expect_error( | ||
country_name_from_arg("Narnia"), | ||
regexp = "must be one of" | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"name" didn't seem right for this parameter any more