Skip to content

Commit

Permalink
custom message
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Aug 12, 2023
1 parent 60d43a6 commit e1eca60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion R/sort_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ sort_linter <- function() {

sorted_expr <- xml_find_all(xml, sorted_xpath)

sorted_op <- xml_text(xml_find_first(sorted_expr, "*[2]"))
lint_message <- ifelse(
sorted_op == "==",
"Use !is.unsorted(x) to test the sortedness of a vector.",
"Use is.unsorted(x) to test the unsortedness of a vector."
)

sorted_lints <- xml_nodes_to_lints(
sorted_expr,
source_expression = source_expression,
lint_message = "Use is.unsorted() to test the (un-)sortedness of a vector.",
lint_message = lint_message,
type = "warning"
)

Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-sort_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,17 @@ test_that("sort_linter skips when inputs don't match", {

test_that("sort_linter blocks simple disallowed usages", {
linter <- sort_linter()
lint_msg <- rex::rex("Use is.unsorted() to test the (un-)sortedness of a vector.")
unsorted_msg <- rex::rex("Use is.unsorted(x) to test the unsortedness of a vector.")
sorted_msg <- rex::rex("Use !is.unsorted(x) to test the sortedness of a vector.")

expect_lint("sort(x) == x", lint_msg, linter)
expect_lint("sort(x) == x", sorted_msg, linter)

# argument order doesn't matter
expect_lint("x == sort(x)", lint_msg, linter)
expect_lint("x == sort(x)", sorted_msg, linter)

# inverted version
expect_lint("sort(x) != x", lint_msg, linter)
expect_lint("sort(x) != x", unsorted_msg, linter)

# expression matching
expect_lint("sort(foo(x)) == foo(x)", lint_msg, linter)
expect_lint("sort(foo(x)) == foo(x)", sorted_msg, linter)
})

0 comments on commit e1eca60

Please sign in to comment.