Skip to content

Commit 40c36ba

Browse files
committed
Move file
1 parent 2d1a2e5 commit 40c36ba

File tree

5 files changed

+106
-105
lines changed

5 files changed

+106
-105
lines changed

R/criterion.R

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -122,64 +122,3 @@ check_testfun <- function(testfun) {
122122

123123
testfun
124124
}
125-
126-
#' @rdname root_criterion
127-
#' @param x An object
128-
#' @export
129-
is_root_criterion <- function(x) {
130-
inherits(x, "root_criterion")
131-
}
132-
133-
#' @rdname root_criterion
134-
#' @export
135-
as_root_criterion <- function(x) UseMethod("as_root_criterion", x)
136-
137-
#' @details
138-
#' The `as_root_criterion()` function accepts objects of class
139-
#' `root_criterion`, and character values; the latter will be
140-
#' converted to criteria using `has_file`.
141-
#'
142-
#' @rdname root_criterion
143-
#' @export
144-
as_root_criterion.character <- function(x) {
145-
has_file(x)
146-
}
147-
148-
#' @rdname root_criterion
149-
#' @export
150-
as_root_criterion.root_criterion <- identity
151-
152-
#' @export
153-
as_root_criterion.default <- function(x) {
154-
stop("Cannot coerce ", x, " to type root_criterion.", call. = FALSE)
155-
}
156-
157-
#' @export
158-
format.root_criterion <- function(x, ...) {
159-
if (length(x$desc) > 1) {
160-
c("Root criterion: one of", paste0("- ", x$desc))
161-
} else {
162-
paste0("Root criterion: ", x$desc)
163-
}
164-
}
165-
166-
#' @export
167-
print.root_criterion <- function(x, ...) {
168-
cat(format(x), sep = "\n")
169-
invisible(x)
170-
}
171-
172-
#' @export
173-
#' @rdname root_criterion
174-
#' @details Root criteria can be combined with the `|` operator. The result is a
175-
#' composite root criterion that requires either of the original criteria to
176-
#' match.
177-
#' @param y An object
178-
`|.root_criterion` <- function(x, y) {
179-
stopifnot(is_root_criterion(y))
180-
181-
root_criterion(
182-
c(x$testfun, y$testfun),
183-
c(x$desc, y$desc)
184-
)
185-
}

R/root.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
1+
#' @rdname root_criterion
2+
#' @param x An object
3+
#' @export
4+
is_root_criterion <- function(x) {
5+
inherits(x, "root_criterion")
6+
}
7+
8+
#' @rdname root_criterion
9+
#' @export
10+
as_root_criterion <- function(x) UseMethod("as_root_criterion", x)
11+
12+
#' @details
13+
#' The `as_root_criterion()` function accepts objects of class
14+
#' `root_criterion`, and character values; the latter will be
15+
#' converted to criteria using `has_file`.
16+
#'
17+
#' @rdname root_criterion
18+
#' @export
19+
as_root_criterion.character <- function(x) {
20+
has_file(x)
21+
}
22+
23+
#' @rdname root_criterion
24+
#' @export
25+
as_root_criterion.root_criterion <- identity
26+
27+
#' @export
28+
as_root_criterion.default <- function(x) {
29+
stop("Cannot coerce ", x, " to type root_criterion.", call. = FALSE)
30+
}
31+
32+
#' @export
33+
format.root_criterion <- function(x, ...) {
34+
if (length(x$desc) > 1) {
35+
c("Root criterion: one of", paste0("- ", x$desc))
36+
} else {
37+
paste0("Root criterion: ", x$desc)
38+
}
39+
}
40+
41+
#' @export
42+
print.root_criterion <- function(x, ...) {
43+
cat(format(x), sep = "\n")
44+
invisible(x)
45+
}
46+
47+
#' @export
48+
#' @rdname root_criterion
49+
#' @details Root criteria can be combined with the `|` operator. The result is a
50+
#' composite root criterion that requires either of the original criteria to
51+
#' match.
52+
#' @param y An object
53+
`|.root_criterion` <- function(x, y) {
54+
stopifnot(is_root_criterion(y))
55+
56+
root_criterion(
57+
c(x$testfun, y$testfun),
58+
c(x$desc, y$desc)
59+
)
60+
}
61+
162
#' Find the root of a directory hierarchy
263
#'
364
#' A \emph{root} is defined as a directory that contains a regular file
File renamed without changes.

tests/testthat/test-criterion.R

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,9 @@ test_that("root_criterion", {
2929
expect_true(is_root_criterion(root_criterion(function(path) FALSE, "Never")))
3030
})
3131

32-
test_that("is_root_criterion", {
33-
expect_true(is_root_criterion(has_file("DESCRIPTION")))
34-
expect_false(is_root_criterion("DESCRIPTION"))
35-
expect_true(is_root_criterion(as_root_criterion("DESCRIPTION")))
36-
})
37-
38-
test_that("as_root_criterion", {
39-
reset_env <- function(x) {
40-
if (is.function(x)) {
41-
environment(x) <- .GlobalEnv
42-
} else if (is.list(x)) {
43-
x <- lapply(x, reset_env)
44-
}
45-
x
46-
}
47-
48-
expect_equal(
49-
lapply(as_root_criterion("x"), reset_env),
50-
lapply(has_file("x"), reset_env)
51-
)
52-
expect_error(as_root_criterion(5), "Cannot coerce")
53-
})
54-
5532
test_that("Absolute paths are returned", {
5633
expect_equal(
5734
find_root("testthat.R"),
5835
normalizePath(find_root("testthat.R"), winslash = "/")
5936
)
6037
})
61-
62-
test_that("Formatting", {
63-
expect_snapshot(format(is_r_package))
64-
expect_snapshot(is_r_package)
65-
expect_snapshot(is_vcs_root)
66-
expect_snapshot(criteria)
67-
expect_snapshot(str(criteria))
68-
})
69-
70-
test_that("Combining criteria", {
71-
comb_crit <- is_r_package | is_rstudio_project
72-
73-
expect_true(is_root_criterion(comb_crit))
74-
75-
expect_snapshot(comb_crit)
76-
77-
expect_equal(
78-
find_root(comb_crit, "hierarchy"),
79-
find_root(is_rstudio_project, "hierarchy/a")
80-
)
81-
})

tests/testthat/test-root.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
2+
test_that("is_root_criterion", {
3+
expect_true(is_root_criterion(has_file("DESCRIPTION")))
4+
expect_false(is_root_criterion("DESCRIPTION"))
5+
expect_true(is_root_criterion(as_root_criterion("DESCRIPTION")))
6+
})
7+
8+
test_that("as_root_criterion", {
9+
reset_env <- function(x) {
10+
if (is.function(x)) {
11+
environment(x) <- .GlobalEnv
12+
} else if (is.list(x)) {
13+
x <- lapply(x, reset_env)
14+
}
15+
x
16+
}
17+
18+
expect_equal(
19+
lapply(as_root_criterion("x"), reset_env),
20+
lapply(has_file("x"), reset_env)
21+
)
22+
expect_error(as_root_criterion(5), "Cannot coerce")
23+
})
24+
25+
test_that("Formatting", {
26+
expect_snapshot(format(is_r_package))
27+
expect_snapshot(is_r_package)
28+
expect_snapshot(is_vcs_root)
29+
expect_snapshot(criteria)
30+
expect_snapshot(str(criteria))
31+
})
32+
33+
test_that("Combining criteria", {
34+
comb_crit <- is_r_package | is_rstudio_project
35+
36+
expect_true(is_root_criterion(comb_crit))
37+
38+
expect_snapshot(comb_crit)
39+
40+
expect_equal(
41+
find_root(comb_crit, "hierarchy"),
42+
find_root(is_rstudio_project, "hierarchy/a")
43+
)
44+
})
45+
146
test_that("has_file", {
247
wd <- normalizePath(getwd(), winslash = "/")
348
hierarchy <- function(n = 0L) {

0 commit comments

Comments
 (0)