Skip to content

Commit

Permalink
Improve code coverage (#29)
Browse files Browse the repository at this point in the history
* Remove the package down component for now... It fails?

* - Disable testing on functions that require interactivity.
- Rename open_browser() to open_web_browser()

* Add site creation and retrieval failure conditions

* Split validating search_site tests

* Add a unit test for utilities that modifies system environment variables.

* Test the .onLoad() component by verifying options are set.

* Remove context

* We test a bit more now...
  • Loading branch information
coatless authored Dec 31, 2019
1 parent d2eaca3 commit 9b3b928
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 48 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/pkgdown.yaml

This file was deleted.

6 changes: 1 addition & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@
## Fixes

- Addressed internal vignette index name being used as the title.
- Block new CI/CD routines from being included with the package.

## Deployment

- Switched from using [TravisCI](http://travis-ci.com/) to using
[GitHub Actions for R](https://github.com/r-lib/actions).
([#25](https://github.com/r-assist/searcher/issues/25),
[#27](https://github.com/r-assist/searcher/pull/27))
- Added [pkgdown](https://pkgdown.r-lib.org/) website for `searcher` available
at: <https://r-assist.github.io/searcher>.
([#26](https://github.com/r-assist/searcher/issues/26),
[#27](https://github.com/r-assist/searcher/pull/27))
- Improved code coverage of unit tests ([#29](https://github.com/r-assist/searcher/pull/29))

# searcher 0.0.4

Expand Down
14 changes: 7 additions & 7 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ browse_url = function(base,

url = encode_url(base, unencoded_query, encoded_query)
if (open_browser) {
if (is_rstudio() && getOption("searcher.use_rstudio_viewer")) {
if (is_rstudio() && getOption("searcher.use_rstudio_viewer")) { # nocov start
open_rstudio_viewer(url)
} else {
open_browser(url)
}
open_web_browser(url)
} # nocov end
} else {
message("Please type into your browser:\n", invisible(url))
}

invisible(url)
}

open_rstudio_viewer = function(url) {
open_rstudio_viewer = function(url) { # nocov start
message("Searching query in RStudio's Viewer panel ... ")
Sys.sleep(getOption("searcher.launch_delay"))

# If in RStudio, this should be set.
viewer <- getOption("viewer")
viewer(url)
}
} # nocov end

open_browser = function(url) {
open_web_browser = function(url) { # nocov start
message("Searching query in a web browser ... ")
Sys.sleep(getOption("searcher.launch_delay"))
utils::browseURL(url)
}
} # nocov end

#' Form Encoded URL
#'
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-index-sites.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ test_that("Keyword generation", {
)
})

test_that("Site creation", {
expect_equal(
site_entry("fake", "http://example.com"),
list(
"site_long_name" = "fake",
"site_short_name" = "fake",
"site_url" = "http://example.com",
"keywords" = list(base = "r programming", tidyverse = "tidyverse"),
"suffix" = NULL
)
)
})

test_that("Site retrieval", {

expect_equal(
Expand All @@ -25,3 +38,7 @@ test_that("Site retrieval", {
)

})

test_that("Site retrieval failure", {
expect_error(site_details("fake"))
})
47 changes: 34 additions & 13 deletions tests/testthat/test-search-functions.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("searcher")

test_that("Check link generation", {

##### Google
Expand Down Expand Up @@ -100,12 +98,7 @@ test_that("Check link generation", {

})

test_that("Validate selection", {

expect_identical(
search_site("toad", "bb", rlang = FALSE),
"https://bitbucket.com/search?q=toad"
)
test_that("Validate selection long name - search_site", {

expect_error(
search_site("toad", "", rlang = FALSE)
Expand All @@ -121,21 +114,46 @@ test_that("Validate selection", {
"https://bing.com/search?q=toad"
)

expect_identical(
search_site("", rlang = FALSE),
"",
"Verify empty query fall through"
)

})

test_that("Validate selection short name - search_site", {

expect_identical(
search_site("toad", "ddg", rlang = FALSE),
"https://duckduckgo.com/?q=toad"
)

expect_identical(
search_site("", rlang = FALSE),
"",
"Verify empty query fall through"
search_site("toad", "sp", rlang = FALSE),
"https://startpage.com/do/dsearch?query=toad"
)

})
expect_identical(
search_site("toad", "rscom", rlang = FALSE),
"https://community.rstudio.com/search?q=toad"
)

expect_identical(
search_site("toad", "gh", rlang = FALSE),
"https://github.com/search?q=toad&type=Issues"
)

expect_identical(
search_site("toad", "so", rlang = FALSE),
"https://stackoverflow.com/search?q=toad"
)

expect_identical(
search_site("toad", "bb", rlang = FALSE),
"https://bitbucket.com/search?q=toad"
)
})

test_that("Verify search handler generation", {
expect_message(searcher("bing")(""))
Expand All @@ -145,7 +163,6 @@ test_that("Verify search handler generation", {
)
})


test_that("Malformed search query validation", {

expect_identical(
Expand Down Expand Up @@ -205,3 +222,7 @@ test_that("Malformed search query validation", {
"NULL value handling"
)
})

test_that("Ensure deprecation", {
expect_error(search_ixquick())
})
32 changes: 32 additions & 0 deletions tests/testthat/test-searcher-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("Verify package load settings", {

pkg_default_options = searcher_default_options
pkg_default_names = names(pkg_default_options)

# Clean environment
for(default_name in pkg_default_names) {
options(default_name = NULL)
}

is_option_present = function(x) {
!is.null(getOption(x, NULL))
}

# Verify names have been unset
expect_true(all(sapply(pkg_default_names, is_option_present)))

# And call the onload..
.onLoad()

# Check if names are registered
expect_true(all(pkg_default_names %in% names(options())))

# Verify if contents were set.
set_option_values = sapply(pkg_default_names, getOption,
simplify = FALSE, USE.NAMES = TRUE)
expect_equal(
set_option_values,
pkg_default_options
)

})
14 changes: 14 additions & 0 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("Detect RStudio", {
# Obtain value
before = Sys.getenv("RSTUDIO")

# Modify check environment variable
Sys.setenv("RSTUDIO" = 1)
expect_true(is_rstudio(), info = "Within RStudio")

Sys.setenv("RSTUDIO" = 0)
expect_false(is_rstudio(), info = "Not in RStudio")

# Restore
Sys.setenv("RSTUDIO" = before)
})

0 comments on commit 9b3b928

Please sign in to comment.