Help us improve
Share bugs, ideas, or general feedback.
From r-skills
Test-driven development workflow for R. Write tests first, then implement. Use for new features, bug fixes, and refactoring.
npx claudepluginhub ab604/claude-code-r-skills --plugin r-skillsHow this command is triggered — by the user, by Claude, or both
Slash command
/r-skills:tddThe summary Claude sees in its command listing — used to decide when to auto-load this command
# /tdd - Test-Driven Development for R Follow the TDD workflow: write tests first, then implement code to make them pass. ## Core Methodology **RED → GREEN → REFACTOR** 1. **RED** - Write a failing test 2. **GREEN** - Write minimal code to pass 3. **REFACTOR** - Improve while keeping tests green ## When to Use - New features or functions - Bug fixes (write test that reproduces bug first) - Refactoring existing code - Adding new model types or algorithms - Creating data processing pipelines ## TDD Workflow Steps ### Step 1: Define Expected Behavior ### Step 2: Write Failing Tests ...
/rust-testEnforces TDD workflow for Rust: write tests first, then implement code. Uses cargo-llvm-cov to verify 80%+ coverage.
/rust-testEnforces TDD workflow for Rust: scaffolds interfaces, writes failing tests first (RED), implements minimally (GREEN), refactors, verifies 80%+ coverage with cargo-llvm-cov.
/tdd-cycleOrchestrates a strict red-green-refactor TDD workflow with phases for test specification, implementation, and refactoring. Supports --incremental, --suite, and --coverage flags.
/tddEnforces test-driven development: scaffolds interfaces, generates failing tests first, implements minimal passing code, refactors, and verifies 80%+ coverage.
/tddStarts TDD workflow: ensures main branch, creates feature branch, follows Red-Green-Refactor cycle, keeps notes, commits changes, pushes to GitHub, and creates PR.
Share bugs, ideas, or general feedback.
Follow the TDD workflow: write tests first, then implement code to make them pass.
RED → GREEN → REFACTOR
# What should the function do?
# rescale01: Rescale a numeric vector to [0, 1] range
# - Minimum value maps to 0
# - Maximum value maps to 1
# - Handle NA values appropriately
# tests/testthat/test-rescale.R
library(testthat)
test_that("rescale01 maps to [0, 1] range", {
expect_equal(rescale01(c(0, 5, 10)), c(0, 0.5, 1))
expect_equal(rescale01(c(-10, 0, 10)), c(0, 0.5, 1))
})
test_that("rescale01 handles edge cases", {
expect_equal(rescale01(c(5, 5, 5)), c(NaN, NaN, NaN))
expect_equal(rescale01(numeric(0)), numeric(0))
})
test_that("rescale01 handles NA values", {
expect_equal(rescale01(c(0, NA, 10)), c(0, NA, 1))
})
devtools::test()
# ✖ rescale01 maps to [0, 1] range
# ✖ rescale01 handles edge cases
# ✖ rescale01 handles NA values
# R/rescale.R
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
devtools::test()
# ✔ rescale01 maps to [0, 1] range
# ✔ rescale01 handles edge cases
# ✔ rescale01 handles NA values
Improve code while keeping tests green:
rescale01 <- function(x, na.rm = TRUE) {
rng <- range(x, na.rm = na.rm, finite = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
covr::package_coverage()
# rescale01: 100% coverage
| Code Type | Minimum Coverage |
|---|---|
| General code | 80% |
| Statistical calculations | 100% |
| Data validation | 100% |
| Security functions | 100% |
| Core business logic | 100% |
test_that("clean_data removes invalid rows", {
input <- tibble(
id = 1:4,
value = c(1, NA, 3, -999)
)
result <- clean_data(input)
expect_equal(nrow(result), 2)
expect_equal(result$id, c(1, 3))
})
test_that("fit_model returns expected structure", {
data <- tibble(x = 1:10, y = 2 * 1:10 + rnorm(10))
model <- fit_model(data, y ~ x)
expect_s3_class(model, "lm")
expect_named(coef(model), c("(Intercept)", "x"))
})
test_that("validate_input throws informative errors", {
expect_error(
validate_input(NULL),
class = "validation_error"
)
expect_snapshot(
validate_input("not numeric"),
error = TRUE
)
})
DON'T:
DO:
/plan - Plan implementation before starting/code-review - Review code after implementation# All tests
devtools::test()
# With coverage
covr::package_coverage()
# Specific file
testthat::test_file("tests/testthat/test-rescale.R")
# Watch mode
testthat::auto_test_package()
Remember: Tests are not optional. Write them FIRST.