Package 'tinyexpect'

Title: Additional Expecation Predicates for the 'tinytest' Testing Framework
Description: Ports of 'testthat' expectation predicates to the 'tinytest' testing framework. These ports allow easier transitioning from 'testthat' to 'tinytest' as well as provide more explicit expectation predicates for 'tinytest' users.
Authors: Paul Hoffman [aut, cre] (ORCID: <https://orcid.org/0000-0002-7693-8957>)
Maintainer: Paul Hoffman <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9010
Built: 2026-05-21 07:18:39 UTC
Source: https://github.com/mojaveazure/tinyexpect

Help Index


Expect a Comparison to Hold

Description

Expectation predicate for testing based on comparisons

Usage

expect_lt(current, target, ..., info = NA_character_)

expect_lte(current, target, ..., info = NA_character_)

expect_gt(current, target, ..., info = NA_character_)

expect_gte(current, target, ..., info = NA_character_)

Arguments

current

[R object or expression] Object or expression to be tested

target

[R object or expression] Expected outcome

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalents: expect_lt(), expect_lte(), expect_gt(), and expect_gte()

Examples

# Expect strictly less than
expect_lt(1L, 3L) # Pass
expect_lt(3L, 1L) # Fail
expect_lt(1L, 1L) # Fail

# Expect less than or equal to
expect_lte(1L, 3L) # Pass
expect_lte(3L, 1L) # Fail
expect_lte(1L, 1L) # Pass

# Expect strictly greater than
expect_gt(1L, 3L) # Fail
expect_gt(3L, 1L) # Pass
expect_gt(1L, 1L) # Fail

# Expect greater than or equal to
expect_gte(1L, 3L) # Fail
expect_gte(3L, 1L) # Pass
expect_gte(1L, 1L) # Pass

Expect a Condition to be Thrown

Description

Expectation predicate for testing that a given function call throws a condition

Usage

expect_condition(
  current,
  pattern = NULL,
  class = NULL,
  ...,
  info = NA_character_
)

Arguments

current

[R object or expression] Object or expression to be tested

pattern

[character(1L)] A regular expression to match the condition message; any condition thrown that does not match pattern will trigger a failure. Must be a single character string

class

[character] Optional vector of classes from which the condition should inherit; any conditions thrown that do no inherit from class will trigger a failure

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_condition()

Other condition expectation predicates: expect_no_condition()

Examples

expect_condition(message("test message")) # Pass
expect_condition(warning("test warning")) # Pass
expect_condition(stop("test error")) # Pass

expect_condition("tomato") # Fail
expect_condition(message("test message"), class = "specialMessage") # Fail
expect_condition(message("test message"), pattern = "tomato") # Fail

Expect a Superset

Description

Expectation predicate for testing that a given vector contains all values present in another vector

Usage

expect_contains(current, target, ..., info = NA_character_)

Arguments

current

[R vector] A vector to be tested

target

[R vector] A vector to be compared against

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_contains()

Other in-condition expectation predicates: expect_in(), expect_mapequal(), expect_setequal()

Examples

expect_contains(letters, target = rev(letters)) # Pass
expect_contains(letters, target = "b") # Pass
expect_contains("b", target = letters) # Fail

Expect a Subset

Description

Expectation predicate for testing that a vector is fully contained within another vector

Usage

expect_in(current, target, ..., info = NA_character_)

Arguments

current

[R vector] A vector to be tested

target

[R vector] A vector to be compared against

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_in()

Other in-condition expectation predicates: expect_contains(), expect_mapequal(), expect_setequal()

Examples

expect_in(letters, target = rev(letters)) # Pass
expect_in(letters, target = "b") # Fail
expect_in("b", target = letters) # Pass

Expect Invisible

Description

Expect Invisible

Usage

expect_invisible(current, ..., info = NA_character_)

Arguments

current

[R object or expression] Object or expression to be tested

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_invisible()

withVisible(), invisible()

Other visiblity expectation predicate: expect_visible()

Examples

f <- function() invisible()
expect_invisible(f()) # Pass

g <- function() NULL
expect_invisible(g()) # Fail

Expect Map Equality

Description

Expectation predicate for testing that the named sets of two vectors are equal

Usage

expect_mapequal(
  current,
  target,
  ...,
  info = NA_character_,
  tolerance = sqrt(x = .Machine$double.eps)
)

Arguments

current

[R vector] A vector to be tested

target

[R vector] A vector to be compared against

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

tolerance

[numeric] Test equality to machine rounding; passed to all.equal()

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_mapequal()

Other in-condition expectation predicates: expect_contains(), expect_in(), expect_setequal()

Examples

(x <- `names<-`(letters, letters))
expect_mapequal(x, target = rev(x)) # Pass
expect_mapequal(letters, target = c(b = "b")) # Fail
expect_mapequal(c(b = "b"), target = x) # Fail

Expect Names

Description

Expectation predicate for testing the names of an object

Usage

expect_named(
  current,
  target,
  ...,
  ignore.order = FALSE,
  ignore.case = FALSE,
  info = NA_character_
)

Arguments

current

[R object or expression] Object or expression to be tested

target

[missing, character, or NULL] One of the following:

  • Left missing to test that current has names

  • A character vector giving the expected names of current

  • NULL to test that current is unnamed

...

Ignored

ignore.order

[logical(1L)] Ignore the order of names(current) with respect to target

ignore.case

[logical(1L)] Ignore upper- vs lower-case discrepancies between names(current) and target

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_named()

Examples

(x <- c(a = 1L, b = 2L, c = 3L))
(y <- c(4L, 5L, 6L))

expect_named(x) # Pass
expect_named(y) # Fail

expect_named(x, target = NULL) # Fail
expect_named(y, target = NULL) # Pass

expect_named(x, target = c("a", "b", "c")) # Pass
expect_named(y, target = c("a", "b", "c")) # Fail

expect_named(x, target = c("b", "a", "c")) # Fail
expect_named(x, target = c("b", "a", "c"), ignore.order = TRUE) # Pass

expect_named(x, target = c("A", "B", "C")) # Fail
expect_named(x, target = c("A", "B", "C"), ignore.case = TRUE) # Pass

Expect No Conditions to be Thrown

Description

Expectation predicate for testing that a given function does not raise a condition

Usage

expect_no_condition(
  current,
  pattern = NULL,
  class = NULL,
  ...,
  info = NA_character_
)

Arguments

current

[R object or expression] Object or expression to be tested

pattern

[character(1L)] A regular expression to match the condition message; any conditions thrown with a message matching pattern will trigger a failure while all other conditions thrown will trigger a success. Must be a single character string

class

[character] Optional vector of condition classes that should not be thrown; conditions thrown that inherit from class will trigger a failure while all other conditions thrown will trigger a success. If NULL, any condition thrown will trigger a failure

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_no_condition()

Other condition expectation predicates: expect_condition()

Examples

expect_no_condition("tomato") # Pass

expect_no_condition(message("test")) # Fail

expect_no_condition(message("test"), pattern = "test") # Fail
expect_no_condition(message("test"), pattern = "tomato") # Pass

expect_no_condition(message("test"), class = "message") # Fail
expect_no_condition(message("test"), class = "error") # Pass

expect_no_condition(message("test"), pattern = "test", class = "message") # Fail
expect_no_condition(message("test"), pattern = "tomato", class = "message") # Pass
expect_no_condition(message("test"), pattern = "test", class = "error") # Pass
expect_no_condition(message("test"), pattern = "tomato", class = "error") # Pass

Expect S3 Object

Description

Check that current is an S3 object that inherits from class

Usage

expect_s3_class(current, class, ..., info = NA_character_, exact = FALSE)

Arguments

current

[R object or expression] Object or expression to be tested

class

[character] Expected class of current; may optionally pass NA to assert that current is not an object

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

exact

[logical(1L)] If TRUE, check that class(current) is identical to class; otherwise, check that current inherits class

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_s3_class()

Other typing expectation predicates: expect_s4_class(), expect_type()

Examples

expect_s3_class(data.frame(), "data.frame") # Pass
expect_s3_class(1L, "integer") # Fail
expect_s3_class(1L, NA) # Pass


expect_s3_class(Matrix::Matrix(), "Matrix") # Fail
expect_s3_class(Matrix::Matrix(), NA) # Pass

Expect S4 Object

Description

Check that current is an S4 object that is of class class

Usage

expect_s4_class(current, class, ..., info = NA_character_)

Arguments

current

[R object or expression] Object or expression to be tested

class

[character] Expected class of current; may optionally pass NA to assert that current is not an object

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_s4_class()

Other typing expectation predicates: expect_s3_class(), expect_type()

Examples

expect_s4_class(Matrix::Matrix(), "Matrix") # Pass

expect_s4_class(data.frame(), "data.frame") # Fail
expect_s4_class(data.frame(), NA) # Pass

Expect Set Equality

Description

Expectation predicate for testing that the sets of two vectors are equal

Usage

expect_setequal(current, target, ..., info = NA_character_)

Arguments

current

[R vector] A vector to be tested

target

[R vector] A vector to be compared against

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_setequal()

Other in-condition expectation predicates: expect_contains(), expect_in(), expect_mapequal()

Examples

expect_setequal(letters, target = rev(letters)) # Pass
expect_setequal(letters, target = "b") # Fail
expect_setequal("b", target = letters) # Fail

Expect Object Type

Description

Check that typeof(current) is type

Usage

expect_type(current, type, ..., info = NA_character_)

Arguments

current

[R object or expression] Object or expression to be tested

type

[character(1L)] Expected base type (as given by typeof())

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_type()

Other typing expectation predicates: expect_s3_class(), expect_s4_class()

Examples

expect_type(1L, "integer") # Pass
expect_type(1.0, "integer") # Fail

Expect Visible

Description

Expect Visible

Usage

expect_visible(current, ..., info = NA_character_)

Arguments

current

[R object or expression] Object or expression to be tested

...

Ignored

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See Also

testthat equivalent: testthat::expect_visible()

withVisible()

Other visiblity expectation predicate: expect_invisible()

Examples

f <- function() invisible()
expect_visible(f()) # Fail

g <- function() NULL
expect_visible(g()) # Pass

Stop Testing if a Required Package is Not Installed

Description

Conditionally stop testing a tinytest test file if a required package is not available or not of a minimum required version

Usage

skip_if_not_installed(pkg, minimum_version = NULL, quietly = TRUE)

exit_if_not_installed(pkg, minimum_version = NULL, quietly = TRUE)

Arguments

pkg

Name of package to check for

minimum_version

Optional minimum required version of pkg

quietly

Attempt to find the package quietly; passed to requireNamespace()

Value

If called within a tinytest test and pkg is either not installed or not at least minimum_version, triggers an exit condition; otherwise, returns one of

  • A string saying that pkg is not installed

  • A string saying that pkg is installed, but not at least minimum_version

  • NULL invisibly

“Skip” vs “Exit”

tinyexpect provides both “skip_” and “exit_” versions of “stop testing” functions due to the different philosophies of tinytest and testthat; in testthat, tests are encapsulated by test_that() to create smaller testing units within a single test file. As such, if a series of tests need to be passed over for some reason, it makes sense to “skip” a test_that() block and move on to the next block

tinytest, however, treats each test file as a testing unit. Each file in inst/tinytest is equivalent to a testthat test_that() block; as such, if a series of tests need to be passed over for some reason, it makes sense to “exit” a test file and move on to the next file in inst/tinytest

In order to provide compatibility with users transitioning from testthat to tinytest, and to provide continuity with the tinytest philosophy, tinyexpect provides both skip_- and exit_- prefixed “stop testing” functions that work identically to one another

See Also

testthat equivalent: testthat::skip_if_not_installed()

Other "stop testing" functions: skip(), skip_on_bioc(), skip_on_ci(), skip_on_covr(), skip_on_cran(), skip_on_os()

Examples

(pkg <- paste(sample(letters, size = 7L, replace = TRUE), collapse = ""))
skip_if_not_installed(pkg)

skip_if_not_installed("tinyexpect", minimum_version = "99.0.1")

Stop Testing on the Bioconductor Build System

Description

Conditionally stop testing a tinytest test file if running on the Bioconductor Build System. This is determined by checking if the environment variable “IS_BIOC_BUILD_MACHINE” set and is a non-FALSE value

Usage

skip_on_bioc()

exit_on_bioc()

Value

If called within a tinytest test running on the Bioconductor Build System, triggers an exit condition; otherwise, either a string saying “On Bioconductor” or NULL invisibly

See Also

testthat equivalent: testthat::skip_on_bioc()

Other "stop testing" functions: skip(), skip_if_not_installed(), skip_on_ci(), skip_on_covr(), skip_on_cran(), skip_on_os()

Examples

withr::with_envvar(c(IS_BIOC_BUILD_MACHINE = "true"), skip_on_bioc())

Stop Testing on CI

Description

Conditionally stop testing a tinytest test file if running on CI. This is determined by checking if the environment variable “CI” set and is a non-FALSE value

Usage

skip_on_ci()

exit_on_ci()

Value

If called within a tinytest test running on CI, triggers an exit condition; otherwise, either a string saying “On CI” or NULL invisibly

“Skip” vs “Exit”

tinyexpect provides both “skip_” and “exit_” versions of “stop testing” functions due to the different philosophies of tinytest and testthat; in testthat, tests are encapsulated by test_that() to create smaller testing units within a single test file. As such, if a series of tests need to be passed over for some reason, it makes sense to “skip” a test_that() block and move on to the next block

tinytest, however, treats each test file as a testing unit. Each file in inst/tinytest is equivalent to a testthat test_that() block; as such, if a series of tests need to be passed over for some reason, it makes sense to “exit” a test file and move on to the next file in inst/tinytest

In order to provide compatibility with users transitioning from testthat to tinytest, and to provide continuity with the tinytest philosophy, tinyexpect provides both skip_- and exit_- prefixed “stop testing” functions that work identically to one another

See Also

testthat equivalent: testthat::skip_on_ci()

Other "stop testing" functions: skip(), skip_if_not_installed(), skip_on_bioc(), skip_on_covr(), skip_on_cran(), skip_on_os()

Examples

withr::with_envvar(c(CI = "true"), skip_on_ci())

Stop Testing Under covr

Description

Conditionally stop testing a tinytest test file if running under covr. This is determined by checking if the environment variable “R_COVR” set and is a non-FALSE value

Usage

skip_on_covr()

exit_on_covr()

Value

If called within a tinytest test running under covr, triggers an exit condition; otherwise, either a string saying “On covr” or NULL invisibly

“Skip” vs “Exit”

tinyexpect provides both “skip_” and “exit_” versions of “stop testing” functions due to the different philosophies of tinytest and testthat; in testthat, tests are encapsulated by test_that() to create smaller testing units within a single test file. As such, if a series of tests need to be passed over for some reason, it makes sense to “skip” a test_that() block and move on to the next block

tinytest, however, treats each test file as a testing unit. Each file in inst/tinytest is equivalent to a testthat test_that() block; as such, if a series of tests need to be passed over for some reason, it makes sense to “exit” a test file and move on to the next file in inst/tinytest

In order to provide compatibility with users transitioning from testthat to tinytest, and to provide continuity with the tinytest philosophy, tinyexpect provides both skip_- and exit_- prefixed “stop testing” functions that work identically to one another

See Also

testthat equivalent: testthat::skip_on_covr()

Other "stop testing" functions: skip(), skip_if_not_installed(), skip_on_bioc(), skip_on_ci(), skip_on_cran(), skip_on_os()

Examples

withr::with_envvar(c(R_COVR = "true"), skip_on_covr())

Stop Testing on CRAN

Description

Conditionally stop testing a tinytest test file if running under covr. This is determined by checking if

  • R is in a non-interactive session, and

  • the environment variable “NOT_CRAN” is unset or is not a TRUE value

Usage

skip_on_cran()

exit_on_cran()

Value

If called within a tinytest test running on CRAN in a non-interactive session, triggers an exit condition; otherwise, either a string saying “On CRAN” or NULL invisibly

“Skip” vs “Exit”

tinyexpect provides both “skip_” and “exit_” versions of “stop testing” functions due to the different philosophies of tinytest and testthat; in testthat, tests are encapsulated by test_that() to create smaller testing units within a single test file. As such, if a series of tests need to be passed over for some reason, it makes sense to “skip” a test_that() block and move on to the next block

tinytest, however, treats each test file as a testing unit. Each file in inst/tinytest is equivalent to a testthat test_that() block; as such, if a series of tests need to be passed over for some reason, it makes sense to “exit” a test file and move on to the next file in inst/tinytest

In order to provide compatibility with users transitioning from testthat to tinytest, and to provide continuity with the tinytest philosophy, tinyexpect provides both skip_- and exit_- prefixed “stop testing” functions that work identically to one another

See Also

testthat equivalent: testthat::skip_on_cran()

Other "stop testing" functions: skip(), skip_if_not_installed(), skip_on_bioc(), skip_on_ci(), skip_on_covr(), skip_on_os()

Examples

withr::with_envvar(c(NOT_CRAN = "false"), skip_on_cran())

Stop Testing on Specific Operating Systems and Architectures

Description

Conditionally stop testing a tinytest test file if running under a given operating system and/or architecture

Usage

skip_on_os(os, arch = NULL)

exit_on_os(os, arch = NULL)

Arguments

os

Operating system to not test on; choose one or more from:

  • windows

  • mac

  • linux

  • solaris

The following OS designations are accepted as synonyms:

  • darwin”: accepted in place of “mac

  • sunos”: accepted in place of “solaris

Pass TRUE to set os to all of the above; this is useful for limiting tests by architecture rather than OS/architecture combos

arch

Optional system architectures to not test on; note that this only applies to operating systems present in os

Value

If called within a tinytest test running under os and potentially on an arch system, triggers an exit condition; otherwise, returns one of

  • A string saying that the code is running under os

  • A string saying that the code is running under os on an arch system

  • NULL invisibly

“Skip” vs “Exit”

tinyexpect provides both “skip_” and “exit_” versions of “stop testing” functions due to the different philosophies of tinytest and testthat; in testthat, tests are encapsulated by test_that() to create smaller testing units within a single test file. As such, if a series of tests need to be passed over for some reason, it makes sense to “skip” a test_that() block and move on to the next block

tinytest, however, treats each test file as a testing unit. Each file in inst/tinytest is equivalent to a testthat test_that() block; as such, if a series of tests need to be passed over for some reason, it makes sense to “exit” a test file and move on to the next file in inst/tinytest

In order to provide compatibility with users transitioning from testthat to tinytest, and to provide continuity with the tinytest philosophy, tinyexpect provides both skip_- and exit_- prefixed “stop testing” functions that work identically to one another

See Also

testthat equivalent: testthat::skip_on_os()

Tools for querying system OS and architecture: Sys.info(), R.version[["arch"]]

Other "stop testing" functions: skip(), skip_if_not_installed(), skip_on_bioc(), skip_on_ci(), skip_on_covr(), skip_on_cran()

Examples

(system <- tolower(Sys.info()[["sysname"]]))
skip_on_os(system)

# Nothing happens if on a different OS
(other <- sample(setdiff(c("windows", "mac", "linux", "solaris"), system), 1L))
skip_on_os(other)

# System architectures can be used to fine-tune skips
(sysarch <- R.version$arch)
skip_on_os(system, arch = sysarch)