Skip to content

Commit 9b3b928

Browse files
authored
Improve code coverage (#29)
* 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...
1 parent d2eaca3 commit 9b3b928

File tree

7 files changed

+105
-48
lines changed

7 files changed

+105
-48
lines changed

.github/workflows/pkgdown.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

NEWS.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@
2727
## Fixes
2828

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

3231
## Deployment
3332

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

4339
# searcher 0.0.4
4440

R/utilities.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,32 @@ browse_url = function(base,
3131

3232
url = encode_url(base, unencoded_query, encoded_query)
3333
if (open_browser) {
34-
if (is_rstudio() && getOption("searcher.use_rstudio_viewer")) {
34+
if (is_rstudio() && getOption("searcher.use_rstudio_viewer")) { # nocov start
3535
open_rstudio_viewer(url)
3636
} else {
37-
open_browser(url)
38-
}
37+
open_web_browser(url)
38+
} # nocov end
3939
} else {
4040
message("Please type into your browser:\n", invisible(url))
4141
}
4242

4343
invisible(url)
4444
}
4545

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

5050
# If in RStudio, this should be set.
5151
viewer <- getOption("viewer")
5252
viewer(url)
53-
}
53+
} # nocov end
5454

55-
open_browser = function(url) {
55+
open_web_browser = function(url) { # nocov start
5656
message("Searching query in a web browser ... ")
5757
Sys.sleep(getOption("searcher.launch_delay"))
5858
utils::browseURL(url)
59-
}
59+
} # nocov end
6060

6161
#' Form Encoded URL
6262
#'

tests/testthat/test-index-sites.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ test_that("Keyword generation", {
1010
)
1111
})
1212

13+
test_that("Site creation", {
14+
expect_equal(
15+
site_entry("fake", "http://example.com"),
16+
list(
17+
"site_long_name" = "fake",
18+
"site_short_name" = "fake",
19+
"site_url" = "http://example.com",
20+
"keywords" = list(base = "r programming", tidyverse = "tidyverse"),
21+
"suffix" = NULL
22+
)
23+
)
24+
})
25+
1326
test_that("Site retrieval", {
1427

1528
expect_equal(
@@ -25,3 +38,7 @@ test_that("Site retrieval", {
2538
)
2639

2740
})
41+
42+
test_that("Site retrieval failure", {
43+
expect_error(site_details("fake"))
44+
})

tests/testthat/test-search-functions.R

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
context("searcher")
2-
31
test_that("Check link generation", {
42

53
##### Google
@@ -100,12 +98,7 @@ test_that("Check link generation", {
10098

10199
})
102100

103-
test_that("Validate selection", {
104-
105-
expect_identical(
106-
search_site("toad", "bb", rlang = FALSE),
107-
"https://bitbucket.com/search?q=toad"
108-
)
101+
test_that("Validate selection long name - search_site", {
109102

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

117+
expect_identical(
118+
search_site("", rlang = FALSE),
119+
"",
120+
"Verify empty query fall through"
121+
)
122+
123+
})
124+
125+
test_that("Validate selection short name - search_site", {
126+
124127
expect_identical(
125128
search_site("toad", "ddg", rlang = FALSE),
126129
"https://duckduckgo.com/?q=toad"
127130
)
128131

129132
expect_identical(
130-
search_site("", rlang = FALSE),
131-
"",
132-
"Verify empty query fall through"
133+
search_site("toad", "sp", rlang = FALSE),
134+
"https://startpage.com/do/dsearch?query=toad"
133135
)
134136

135-
})
137+
expect_identical(
138+
search_site("toad", "rscom", rlang = FALSE),
139+
"https://community.rstudio.com/search?q=toad"
140+
)
136141

142+
expect_identical(
143+
search_site("toad", "gh", rlang = FALSE),
144+
"https://github.com/search?q=toad&type=Issues"
145+
)
137146

147+
expect_identical(
148+
search_site("toad", "so", rlang = FALSE),
149+
"https://stackoverflow.com/search?q=toad"
150+
)
138151

152+
expect_identical(
153+
search_site("toad", "bb", rlang = FALSE),
154+
"https://bitbucket.com/search?q=toad"
155+
)
156+
})
139157

140158
test_that("Verify search handler generation", {
141159
expect_message(searcher("bing")(""))
@@ -145,7 +163,6 @@ test_that("Verify search handler generation", {
145163
)
146164
})
147165

148-
149166
test_that("Malformed search query validation", {
150167

151168
expect_identical(
@@ -205,3 +222,7 @@ test_that("Malformed search query validation", {
205222
"NULL value handling"
206223
)
207224
})
225+
226+
test_that("Ensure deprecation", {
227+
expect_error(search_ixquick())
228+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test_that("Verify package load settings", {
2+
3+
pkg_default_options = searcher_default_options
4+
pkg_default_names = names(pkg_default_options)
5+
6+
# Clean environment
7+
for(default_name in pkg_default_names) {
8+
options(default_name = NULL)
9+
}
10+
11+
is_option_present = function(x) {
12+
!is.null(getOption(x, NULL))
13+
}
14+
15+
# Verify names have been unset
16+
expect_true(all(sapply(pkg_default_names, is_option_present)))
17+
18+
# And call the onload..
19+
.onLoad()
20+
21+
# Check if names are registered
22+
expect_true(all(pkg_default_names %in% names(options())))
23+
24+
# Verify if contents were set.
25+
set_option_values = sapply(pkg_default_names, getOption,
26+
simplify = FALSE, USE.NAMES = TRUE)
27+
expect_equal(
28+
set_option_values,
29+
pkg_default_options
30+
)
31+
32+
})

tests/testthat/test-utilities.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
test_that("Detect RStudio", {
2+
# Obtain value
3+
before = Sys.getenv("RSTUDIO")
4+
5+
# Modify check environment variable
6+
Sys.setenv("RSTUDIO" = 1)
7+
expect_true(is_rstudio(), info = "Within RStudio")
8+
9+
Sys.setenv("RSTUDIO" = 0)
10+
expect_false(is_rstudio(), info = "Not in RStudio")
11+
12+
# Restore
13+
Sys.setenv("RSTUDIO" = before)
14+
})

0 commit comments

Comments
 (0)