Skip to content

Commit 5e721a3

Browse files
committed
Merge commit '9bfe1314e075a1b3bce3eb32dbd33fa795a869ea'
2 parents 0746701 + 9bfe131 commit 5e721a3

File tree

92 files changed

+1188
-856
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1188
-856
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323
^[\.]?air\.toml$
2424
^\.vscode$
2525
^\.git-blame-ignore-rev$
26+
^CLAUDE\.md$
27+
^\.claude$

.claude/settings.local.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(find:*)"
5+
],
6+
"deny": []
7+
}
8+
}

CLAUDE.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## About This Project
6+
7+
testthat is R's most popular unit testing framework, used by thousands of CRAN packages. It provides functions to make testing R code as fun and addictive as possible, with clear expectations, visual progress indicators, and seamless integration with R package development workflows.
8+
9+
## Key Development Commands
10+
11+
### Testing
12+
- `devtools::test()` or `Ctrl/Cmd+Shift+T` in RStudio - Run all tests
13+
- `devtools::test_file("tests/testthat/test-filename.R")` - Run tests in a specific file
14+
- `testthat::test_local()` - Run tests for local source package
15+
- `testthat::test_package("testthat")` - Run tests for installed package
16+
- `R CMD check` - Full package check including tests
17+
18+
### Building and Installation
19+
- `devtools::load_all()` or `Ctrl/Cmd+Shift+L` - Load package for development
20+
- `devtools::document()` - Generate documentation
21+
- `devtools::check()` - Run R CMD check
22+
- `devtools::install()` - Install package locally
23+
24+
## Core Architecture
25+
26+
### Main Components
27+
28+
1. **Core Testing Functions** (`R/test-that.R`, `R/test-package.R`):
29+
- `test_that()` - The fundamental testing function
30+
- `test_local()`, `test_package()`, `test_check()` - Different ways to run test suites
31+
32+
2. **Expectations** (`R/expect-*.R`):
33+
- Modular expectation functions (equality, conditions, types, etc.)
34+
- Each expectation type has its own file following the pattern `expect-[type].R`
35+
36+
3. **Reporters** (`R/reporter*.R`):
37+
- Different output formats for test results
38+
- Object-oriented design with base `Reporter` class
39+
- Includes check, debug, progress, summary, JUnit, TAP formats
40+
41+
4. **Snapshot Testing** (`R/snapshot*.R`):
42+
- Value snapshots, file snapshots, output snapshots
43+
- Automatic management and comparison of expected outputs
44+
45+
5. **Parallel Testing** (`R/parallel*.R`):
46+
- Multi-core test execution
47+
- Configuration via `Config/testthat/parallel: true` in DESCRIPTION
48+
49+
6. **Mocking** (`R/mock*.R`, `R/mock2*.R`):
50+
- Function mocking capabilities
51+
- Both legacy (`mock.R`) and modern (`mock2*.R`) implementations
52+
53+
### Key Design Patterns
54+
55+
- **Editions**: testthat has different "editions" with varying behavior, controlled by `Config/testthat/edition`
56+
- **Reporters**: Extensible reporting system using R6 classes
57+
- **Lazy Evaluation**: Expectations use substitute() and lazy evaluation for better error messages
58+
- **C++ Integration**: Core functionality implemented in C++ for performance
59+
60+
### File Organization
61+
62+
- `R/` - All R source code, organized by functionality
63+
- `src/` - C++ source code and Makevars
64+
- `inst/include/testthat/` - C++ headers for other packages to use
65+
- `tests/testthat/` - Package's own comprehensive test suite
66+
- `vignettes/` - Documentation on testing concepts and workflows
67+
68+
### Important Configuration
69+
70+
The package uses several DESCRIPTION fields for configuration:
71+
- `Config/testthat/edition: 3` - Sets testthat edition
72+
- `Config/testthat/parallel: true` - Enables parallel testing
73+
- `Config/testthat/start-first` - Tests to run first in parallel mode
74+
75+
### C++ Testing Infrastructure
76+
77+
testthat provides C++ testing capabilities via Catch framework:
78+
- Headers in `inst/include/testthat/`
79+
- Test runner infrastructure in `src/test-runner.cpp`
80+
- Integration with R's testing system
81+
82+
### Snapshot Testing Workflow
83+
84+
- Snapshots stored in `tests/testthat/_snaps/`
85+
- Different snapshot types: values, files, output
86+
- Version-specific snapshots for different R versions
87+
- Use `testthat::snapshot_accept()` to update snapshots
88+
89+
This codebase prioritizes backward compatibility, comprehensive testing, and clear, descriptive error messages to help R developers write better tests.

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export(it)
156156
export(local_edition)
157157
export(local_mock)
158158
export(local_mocked_bindings)
159+
export(local_on_cran)
159160
export(local_reproducible_output)
160161
export(local_snapshotter)
161162
export(local_test_context)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
4+
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
5+
* `expect_no_failures()` and `expect_no_successes()` are now deprecated as `expect_success()` now test for no failures and `expect_failure()` tests for no successes (#)
36
* New `pass()` function to use in place of `succeed()` (#2113).
47
* `expectation()` is now a combination of `new_expectation()` and `exp_signal()` (#2125).
58
* `is_null()`/`matches()` deprecated in 2.0.0 (2017-12-19) and `is_true()`/`is_false()` deprecated in 2.1.0 (2019-04-23) have been removed (#2109).

R/deprec-condition.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ capture_messages <- function(code) {
101101
code,
102102
message = function(condition) {
103103
out$push(condition)
104-
maybe_restart("muffleMessage")
104+
tryInvokeRestart("muffleMessage")
105105
}
106106
)
107107

@@ -121,7 +121,7 @@ capture_warnings <- function(code, ignore_deprecation = FALSE) {
121121
}
122122

123123
out$push(condition)
124-
maybe_restart("muffleWarning")
124+
tryInvokeRestart("muffleWarning")
125125
}
126126
)
127127

R/evaluate-promise.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ evaluate_promise <- function(code, print = FALSE) {
2020
handle_warning <- function(condition) {
2121
if (!is_deprecation(condition)) {
2222
warnings$push(condition)
23-
maybe_restart("muffleWarning")
23+
tryInvokeRestart("muffleWarning")
2424
}
2525
}
2626

2727
messages <- Stack$new()
2828
handle_message <- function(condition) {
2929
messages$push(condition)
30-
maybe_restart("muffleMessage")
30+
tryInvokeRestart("muffleMessage")
3131
}
3232

3333
path <- withr::local_tempfile()
File renamed without changes.

R/expect-comparison.R

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
#' @name comparison-expectations
2020
NULL
2121

22-
expect_compare <- function(operator = c("<", "<=", ">", ">="), act, exp) {
22+
expect_compare_ <- function(
23+
operator = c("<", "<=", ">", ">="),
24+
act,
25+
exp,
26+
trace_env = caller_env()
27+
) {
2328
operator <- match.arg(operator)
2429
op <- match.fun(operator)
2530

@@ -34,26 +39,25 @@ expect_compare <- function(operator = c("<", "<=", ">", ">="), act, exp) {
3439
if (length(cmp) != 1 || !is.logical(cmp)) {
3540
abort("Result of comparison must be a single logical value")
3641
}
37-
expect(
38-
if (!is.na(cmp)) cmp else FALSE,
39-
sprintf(
42+
if (!isTRUE(cmp)) {
43+
msg <- sprintf(
4044
"%s is %s %s. Difference: %.3g",
4145
act$lab,
4246
msg,
4347
exp$lab,
4448
act$val - exp$val
45-
),
46-
trace_env = caller_env()
47-
)
48-
invisible(act$val)
49+
)
50+
return(fail(msg, trace_env = trace_env))
51+
}
52+
pass(act$val)
4953
}
5054
#' @export
5155
#' @rdname comparison-expectations
5256
expect_lt <- function(object, expected, label = NULL, expected.label = NULL) {
5357
act <- quasi_label(enquo(object), label, arg = "object")
5458
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
5559

56-
expect_compare("<", act, exp)
60+
expect_compare_("<", act, exp)
5761
}
5862

5963
#' @export
@@ -62,7 +66,7 @@ expect_lte <- function(object, expected, label = NULL, expected.label = NULL) {
6266
act <- quasi_label(enquo(object), label, arg = "object")
6367
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
6468

65-
expect_compare("<=", act, exp)
69+
expect_compare_("<=", act, exp)
6670
}
6771

6872
#' @export
@@ -71,7 +75,7 @@ expect_gt <- function(object, expected, label = NULL, expected.label = NULL) {
7175
act <- quasi_label(enquo(object), label, arg = "object")
7276
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
7377

74-
expect_compare(">", act, exp)
78+
expect_compare_(">", act, exp)
7579
}
7680

7781
#' @export
@@ -80,7 +84,7 @@ expect_gte <- function(object, expected, label = NULL, expected.label = NULL) {
8084
act <- quasi_label(enquo(object), label, arg = "object")
8185
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
8286

83-
expect_compare(">=", act, exp)
87+
expect_compare_(">=", act, exp)
8488
}
8589

8690

0 commit comments

Comments
 (0)