Skip to content

Commit

Permalink
bug fix related to param return_char (fix issue #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMuir committed Aug 7, 2017
1 parent 1c1ceee commit b1590fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 2 additions & 3 deletions R/commodity_lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ commodity_lookup <- function(values, lookuptable, return_code = FALSE,
ans <- as.vector(ans)
} else {
ans <- ans %>%
as.vector %>%
list %>%
`names<-`(values)
dplyr::as_data_frame() %>%
as.list
}
}

Expand Down
14 changes: 11 additions & 3 deletions tests/testthat/test-commodity_lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,33 @@ test_that("lookup return values are correct, and fail when expected", {
return_code = TRUE,
return_char = TRUE,
verbose = TRUE)
ex_4 <- commodity_lookup(values = c("tomato", "trout"),
lookuptable = df,
return_code = FALSE,
return_char = FALSE,
verbose = TRUE)

# Correct return data type.
expect_is(ex_1, "character")
expect_is(ex_2, "list")
expect_is(ex_3, "character")
expect_is(ex_4, "list")

# Number of return values.
expect_equal(length(ex_1), 2)
expect_equal(length(ex_2), 1)
expect_equal(length(ex_3), 3)
expect_equal(length(ex_4), 2)

# Make sure example 3 is not a named vector.
# Make sure ex_3 is not a named vector, and ex_4 is a named list.
expect_named(ex_3, NULL)
expect_named(ex_4, c("tomato", "trout"))

# Correct return values when input for "value" not found in lookup table.
expect_warning(ex_4 <- commodity_lookup(values = "not_a_trade_item",
expect_warning(ex_5 <- commodity_lookup(values = "not_a_trade_item",
lookuptable = df,
return_char = TRUE))
expect_equal(length(ex_4), 0)
expect_equal(length(ex_5), 0)

# Throw error with invalid input for param "value".
expect_error(commodity_lookup(value = list(), lookuptable = df))
Expand Down

0 comments on commit b1590fb

Please sign in to comment.