Skip to content

Commit

Permalink
handle {box} recommended method of testing private functions
Browse files Browse the repository at this point in the history
  • Loading branch information
radbasa committed May 29, 2024
1 parent db2730f commit 27d9317
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: box.linters
Title: Linters for 'box' Modules
Version: 0.9.0.9005
Version: 0.9.0.9006
Authors@R:
c(
person("Ricardo Rodrigo", "Basa", role = c("aut", "cre"), email = "[email protected]"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# box.linters (development version)

* Handle `box` recommended method of testing private methods.
* Added handler for `glue` string templates.
* Added box_mod_fun_exists_linter() to default linters
* [bug fix] box_trailing_commas_linter() now properly handles a #nolint for other linters
Expand Down
4 changes: 3 additions & 1 deletion R/box_unused_attached_mod_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ box_unused_attached_mod_linter <- function() {
attached_three_dots <- get_attached_mod_three_dots(xml)
function_calls <- get_function_calls(xml)
glue_object_calls <- get_objects_in_strings(xml)
possible_module_calls <- get_object_calls(xml)

all_calls_text <- c(function_calls$text, glue_object_calls)

Expand All @@ -97,8 +98,9 @@ box_unused_attached_mod_linter <- function() {
)

functions_used <- length(intersect(func_list, all_calls_text))
modules_used <- length(intersect(aliased_module_text, possible_module_calls$text))

if (functions_used == 0) {
if (functions_used == 0 && modules_used == 0) {
lintr::xml_nodes_to_lints(
attached_module,
source_expression = source_expression,
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-box_unused_attached_mod_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,41 @@ test_that("box_unused_attached_mod_linter blocks unused objects in glue string t

lintr::expect_lint(bad_box_usage, list(message = lint_message), linters = linter)
})


# Box test interfaces, not implementations

test_that("box_unused_attached_mod_linter skips module call as implementation test", {
linter <- box_unused_attached_mod_linter()

code <- "box::use(
path/to/module_a,
)
impl = attr(module_a, \"namespace\")
test_that(\"implementation detail X works\", {
expect_true(impl$a_fun_c())
})
"

lintr::expect_lint(code, NULL, linter = linter)
})

test_that("box_unused_attached_mod_linter blocks unused module call as implementation test", {
linter <- box_unused_attached_mod_linter()
lint_message <- rex::rex("Attached module unused.")

code <- "box::use(
path/to/module_a,
)
impl = attr(module_b, \"namespace\")
test_that(\"implementation detail X works\", {
expect_true(impl$a_fun_c())
})
"

lintr::expect_lint(code, list(message = lint_message), linter = linter)
})
24 changes: 24 additions & 0 deletions tests/testthat/test-e2e_testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
options(box.path = file.path(getwd(), "mod"))

test_that("all linters skip valid box test for interface", {
linters <- lintr::linters_with_defaults(
defaults = box.linters::rhino_default_linters
)

code <- "
box::use(
testthat[expect_true, test_that],
)
box::use(
path/to/module_a,
)
impl <- attr(module_a, \"namespace\")
test_that(\"implementation detail X works\", {
expect_true(impl$this_works())
})"

lintr::expect_lint(code, NULL, linters = linters)
})

0 comments on commit 27d9317

Please sign in to comment.