Skip to content

Commit 79eaa32

Browse files
wlandau-lillyjimhester
authored andcommitted
Add summary function for lint objects
* Fix #260 * Shorten `summary.lints()` Removes extra conditional. * Clean up tests in test-methods.R - Make the intent of the `summary.lints()` tests clearer. - Divide into 2 tests (no lints vs lints found). - Use assignment linter explicitly. * Reference summary.lints() in NEWS.md
1 parent eaa1d9e commit 79eaa32

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ S3method(names,lints)
66
S3method(print,lint)
77
S3method(print,lints)
88
S3method(split,lints)
9+
S3method(summary,lints)
910
export(Lint)
1011
export(T_and_F_symbol_linter)
1112
export(absolute_path_linter)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* Commas linter handles missing arguments calls properly (#145)
5151
* Add `function_left_parentheses_linter` to check that there is no space between
5252
a function name and its left parentheses (#204, @jrnold).
53+
* Implement `summary.lints()` (#260, #262, @wlandau).
5354

5455
# lintr 1.0.1 #
5556
* bugfix to work with knitr 1.16.7

R/methods.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,16 @@ as.data.frame.lints <- function(x, row.names = NULL, optional = FALSE, ...) {
141141
attributes(x) <- attrs
142142
x
143143
}
144+
145+
#' @export
146+
summary.lints <- function(object, ...) {
147+
filenames <- vapply(object, `[[`, character(1), "filename")
148+
types <- factor(vapply(object, `[[`, character(1), "type"),
149+
levels = c("style", "warning", "error"))
150+
tbl <- table(filenames, types)
151+
filenames <- rownames(tbl)
152+
res <- as.data.frame.matrix(tbl, stringsAsFactors = FALSE, row.names = NULL)
153+
res$filenames <- filenames %||% character()
154+
nms <- colnames(res)
155+
res[order(res$filenames), c("filenames", nms[nms != "filenames"])]
156+
}

tests/testthat/test-methods.R

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test_that("it returns the input trimmed to the last full lint if one exists with
1919
})
2020

2121
test_that("as.data.frame.lints", {
22-
# A minimum lint
22+
# A minimum lint
2323
expect_is(
2424
l1 <- Lint("dummy.R",
2525
line_number = 1L,
@@ -28,7 +28,7 @@ test_that("as.data.frame.lints", {
2828
line = ""),
2929
"lint"
3030
)
31-
31+
3232
# A larger lint
3333
expect_is(
3434
l2 <- Lint("dummy.R",
@@ -41,7 +41,7 @@ test_that("as.data.frame.lints", {
4141
linter = "custom_linter"),
4242
"lint"
4343
)
44-
44+
4545
# Convert lints to data.frame
4646
lints <- structure(list(l1, l2), class = "lints")
4747
expect_is(
@@ -58,9 +58,31 @@ test_that("as.data.frame.lints", {
5858
line = c("", "a <- 1"),
5959
linter = c("", "custom_linter"),
6060
stringsAsFactors = FALSE)
61-
61+
6262
expect_equal(
6363
df,
6464
exp
65-
)
65+
)
6666
})
67+
68+
test_that("summary.lints() works (no lints)", {
69+
no_lints <- lint(
70+
"x <- 1\n",
71+
linters = assignment_linter)
72+
no_lint_summary <- summary(no_lints)
73+
expect_true(is.data.frame(no_lint_summary))
74+
expect_equal(nrow(no_lint_summary), 0)
75+
})
76+
77+
test_that("summary.lints() works (lints found)", {
78+
has_lints <- lint(
79+
"x = 1\n",
80+
linters = assignment_linter)
81+
has_lint_summary <- summary(has_lints)
82+
expect_true(is.data.frame(has_lint_summary))
83+
expect_equal(nrow(has_lint_summary), 1)
84+
expect_true(has_lint_summary$style > 0)
85+
expect_equal(has_lint_summary$warning, 0)
86+
expect_equal(has_lint_summary$error, 0)
87+
})
88+

0 commit comments

Comments
 (0)