Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.httr-oauth
.quarto
.DS_Store
../.idea/
86 changes: 86 additions & 0 deletions tests/testthat/test-add3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
library(testthat)
# library(withr)

# Testing setup

setup({
cat("Running setup for test\n")
})

teardown({
cat("Cleaning up tests\n")
})

# test blocks
test_that("add3 workds correctly", {
expect_equal(add3(5, 6, 7), 18)
expect_identical(add3(5, 6, 7), 18)
})

test_that("order invariant", {
expect_equal(add3(1, 2, 3), 6)
expect_equal(add3(2, 1, 3), 6)
expect_equal(add3(3, 1, 2), 6)
expect_equal(add3(1, 3, 2), 6)
expect_equal(add3(2, 3, 1), 6)
expect_equal(add3(3, 2, 1), 6)
})

test_that("negatives work", {
expect_equal(add3(-1, -2, -3), -6)
expect_equal(add3(-3, 1, 2), 0)
})

test_that("zero works", {
expect_equal(add3(0, 0, 1), 1)
expect_equal(add3(1, 0, 0), 1)
expect_equal(add3(1, 0, 0), 1)
})

test_that("floats work", {
expect_equal(add3(1.1, 2.2, 3.3), 6.6, tolerance = 1e-8)
})

test_that("warning example", {
warn_func <- function() {
warning("This is a warning")
}

expect_warning(warn_func(), "warning")
})

test_that("message example", {
msg_func <- function() {
message("Sample Message")
}

expect_message(msg_func(), "Message")
})

test_that("NA/NULL", {
expect_null(NULL)
expect_true(is.na(NA))
})

test_that("Length check", {
expect_length(1:10, 10)
})

test_that("Set comparisons", {
expect_setequal(c(1, 2, 3), c(3, 2, 1))
})

test_that("Skipping tests", {
skip("Test skip demo")
expect_true(FALSE)
})

# Custom expects
expect_is_one <- function(object) {
expect_true(object == 1,
info = paste(object, "is not even"))
}

test_that("Custom expect", {
expect_is_one(1)
})