Title: | Extend 'tinytest' with 'diffobj' and 'tinysnapshot' |
---|---|
Description: | The 'tinytest' package offers a light-weight zero-dependency unit-testing framework to which this package adds support via the 'diffobj' package for 'diff'-style textual comparison of R objects, as well as via 'tinysnapshot' package for visual differences in plots. |
Authors: | Dirk Eddelbuettel [aut, cre] |
Maintainer: | Dirk Eddelbuettel <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.0.10 |
Built: | 2025-01-24 01:18:19 UTC |
Source: | https://github.com/eddelbuettel/ttdo |
Test for equality with explicit difference
expect_equal_with_diff( current, target, tol = sqrt(.Machine$double.eps), mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... ) expect_equivalent_with_diff( current, target, tol = sqrt(.Machine$double.eps), ... )
expect_equal_with_diff( current, target, tol = sqrt(.Machine$double.eps), mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... ) expect_equivalent_with_diff( current, target, tol = sqrt(.Machine$double.eps), ... )
current |
|
target |
|
tol |
|
mode |
|
format |
|
... |
Passed to |
expect_equivalent_with_diff
calls
expect_equal_with_diff
with the extra arguments
check.attributes=FALSE
and use.names=FALSE
A tinytest
object. A tinytest object is a
logical
with attributes holding information about the
test that was run. The class attribute is set to c("ttdo", "tinytest")
to signal that it is a 'diffobj' result.
library(tinytest) using(ttdo) expect_equal_with_diff(1 + 1, 2) # TRUE expect_equal_with_diff(1 - 1, 2) # FALSE expect_equivalent_with_diff(2, c(x=2)) # TRUE expect_equivalent_with_diff(2, c(x=2)) # TRUE
library(tinytest) using(ttdo) expect_equal_with_diff(1 + 1, 2) # TRUE expect_equal_with_diff(1 - 1, 2) # FALSE expect_equivalent_with_diff(2, c(x=2)) # TRUE expect_equivalent_with_diff(2, c(x=2)) # TRUE
Building on the tinytest functions for testing equality with optional enhanced object diffing and additional test feedback through addtional attributes.
expect_equal_xl( current, target, useDiffObj = TRUE, tol = sqrt(.Machine$double.eps), info = NA_character_, mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... ) expect_identical_xl( current, target, useDiffObj = TRUE, info = NA_character_, mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... ) expect_equivalent_xl( current, target, useDiffObj = TRUE, tol = sqrt(.Machine$double.eps), info = NA_character_, mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... )
expect_equal_xl( current, target, useDiffObj = TRUE, tol = sqrt(.Machine$double.eps), info = NA_character_, mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... ) expect_identical_xl( current, target, useDiffObj = TRUE, info = NA_character_, mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... ) expect_equivalent_xl( current, target, useDiffObj = TRUE, tol = sqrt(.Machine$double.eps), info = NA_character_, mode = getOption("diffobj.mode", "unified"), format = getOption("diffobj.format", "ansi256"), ... )
current |
|
target |
|
useDiffObj |
|
tol |
|
info |
An additional attribute to pass around with the tinytest object |
mode |
|
format |
|
... |
Passed to |
While tinytest does now support the passing of additional information with the info
field in its tests, they are not yet supported in the as.data.frame.tinytests
method.
A tinytest
object. A tinytest object is a
logical
with attributes holding information about the
test that was run
library(tinytest) using(ttdo) expect_equal_xl(1 + 1, 2, score = 3) # TRUE expect_equal_xl(1 - 1, 2, name = "check 1-1==2", score = 1, totalpts = 2) # FALSE
library(tinytest) using(ttdo) expect_equal_xl(1 + 1, 2, score = 3) # TRUE expect_equal_xl(1 - 1, 2, name = "check 1-1==2", score = 1, totalpts = 2) # FALSE
This tinytest
-compatible expectation compares two plots supplied as files (which
should be png files) and returns visual difference in a plot file (for which the
designated file has to be supplied).
The labels “old” (for the reference plot compared against), “new” (for
the candidate plot, and “diff” can be set as value to the global option
tinysnapshot_plot_diff_style
. For example settings c("old", "new", "diff")
shows all three, setting c("new", "diff")
just these two. The default is to only show
the ‘diff’ plot.
The data format used for returning the PNG image created is described in https://en.wikipedia.org/wiki/Data_URI_scheme.
expect_visual_equal_with_diff(proposed, reference, difference, ...)
expect_visual_equal_with_diff(proposed, reference, difference, ...)
proposed |
Character value with filename of proposed solution, should be png |
reference |
Character value with filename of reference plot, should be png |
difference |
Character value with filename for difference plot (if plots differ) |
... |
Passes on to |
The tinytest
result object where the diff
attribute contains the
suitable value that can be passed onto the JSON output, i.e. a character string beginning
with "date:image/png;base64,"
followed with the base64 encoded file. The class
attribute is set to c("ttvd", "tinytest")
to signal that it is a 'visual diff' result.
This method extends the as.data.frame.tinytest
method to handle
arbitrary attributes attached to each tinytest object. You can pass in
the results of a single test (a tinytest object) directly or the results
of one of the run_test_*
functions (a tinytests object).
makeDataFrame(x)
makeDataFrame(x)
x |
a tinytest or tinytests object |
# create a test file in tempdir tests <- " using(ttdo) addOne <- function(x) x + 2 expect_true(addOne(0) > 0) expect_equal(2, addOne(1)) " testfile <- tempfile(pattern = "test_", fileext = ".R") write(tests, testfile) # extract testdir testdir <- dirname(testfile) # run all files starting with 'test' in testdir library(tinytest) out <- run_test_dir(testdir) # # convert results dat <- makeDataFrame(out) dat dat2 <- makeDataFrame(expect_equal_xl(1-1, 2, useDiffObj = FALSE, name = 'subtr', pts = 1))
# create a test file in tempdir tests <- " using(ttdo) addOne <- function(x) x + 2 expect_true(addOne(0) > 0) expect_equal(2, addOne(1)) " testfile <- tempfile(pattern = "test_", fileext = ".R") write(tests, testfile) # extract testdir testdir <- dirname(testfile) # run all files starting with 'test' in testdir library(tinytest) out <- run_test_dir(testdir) # # convert results dat <- makeDataFrame(out) dat dat2 <- makeDataFrame(expect_equal_xl(1-1, 2, useDiffObj = FALSE, name = 'subtr', pts = 1))
Building on the tinytest functions for testing boolean values with additional test feedback through attributes.
expect_true_xl(current, info = NA_character_, ...) expect_false_xl(current, info = NA_character_, ...) expect_null_xl(current, info = NA_character_, ...) expect_silent_xl(current, quiet = TRUE, info = NA_character_, ...) expect_error_xl( current, pattern = ".*", class = "error", info = NA_character_, ... ) expect_warning_xl( current, pattern = ".*", class = "warning", info = NA_character_, strict = FALSE, ... ) expect_message_xl( current, pattern = ".*", class = "message", info = NA_character_, strict = FALSE, ... )
expect_true_xl(current, info = NA_character_, ...) expect_false_xl(current, info = NA_character_, ...) expect_null_xl(current, info = NA_character_, ...) expect_silent_xl(current, quiet = TRUE, info = NA_character_, ...) expect_error_xl( current, pattern = ".*", class = "error", info = NA_character_, ... ) expect_warning_xl( current, pattern = ".*", class = "warning", info = NA_character_, strict = FALSE, ... ) expect_message_xl( current, pattern = ".*", class = "message", info = NA_character_, strict = FALSE, ... )
current |
|
info |
scalar. Optional user-defined message. Must be a single character string. Multiline comments may be separated by "\n". |
... |
Passed to |
quiet |
|
pattern |
|
class |
|
strict |
|
While tinytest does now support the passing of additional information with the info
field in its tests, they are not yet supported in the as.data.frame.tinytests
method.
A tinytest
object. A tinytest object is a
logical
with attributes holding information about the
test that was run
library(tinytest) using(ttdo) expect_true_xl(TRUE, score = 3) # TRUE expect_true_xl(FALSE, name = "check 1-1==2", score = 1, totalpts = 2) # FALSE
library(tinytest) using(ttdo) expect_true_xl(TRUE, score = 3) # TRUE expect_true_xl(FALSE, name = "check 1-1==2", score = 1, totalpts = 2) # FALSE