Skip to content

Commit

Permalink
mutr v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Moravec committed Nov 21, 2024
1 parent 362caa1 commit ee154a8
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 853 deletions.
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# mutr 0.0.2

* added `TEST_NOT_ERROR` to check that the expression doesn't throw error
* fixed incorrect assignment of `call` within `TEST` and `TEST_ERROR`
* enhanced self-testing `TEST__PASS` and `TEST__FAIL` to preserve the expression
* `TEST__PASS` and `TEST__FAIL` now can accept optional arguments
* added more self-tests.
* added versioning and `NEWS.md`
* removed old files (some functionality now in the [mpd](https://github.com/J-Moravec/mpd/) package
* a simple makefile for self-testing and to copy `mutr.r` into examples

# mutr 0.0.1

* initial version
18 changes: 13 additions & 5 deletions examples/helpers/mutr.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mutr: minimal unit-testing framework
#
# a simplified copy-pastable version inspired by https://jera.com/techinfo/jtns/jtn002
# inspired by https://jera.com/techinfo/jtns/jtn002
# version: 0.0.2
# https://github.com/J-Moravec/mutr

TEST_INIT = function(){
env = new.env(parent = emptyenv())
Expand Down Expand Up @@ -31,7 +32,7 @@ TEST_PRINT = function(){


TEST = function(expr, msg = "is not TRUE!", call = NULL){
call = if(is.null(call)) deparse(substitute(expr)) |> paste0(collapse = "")
if(is.null(call)) call = deparse(substitute(expr)) |> paste0(collapse = "")
res = try(expr, silent = TRUE)

env = get(".TESTS", envir = globalenv())
Expand Down Expand Up @@ -81,8 +82,15 @@ not = function(x){
}


TEST_ERROR = function(expr, msg = "does not signal required error!", pattern = "", call = NULL){
call = if(is.null(call)) deparse(substitute(expr)) |> paste0(collapse = "")
TEST_ERROR = function(expr, msg = "does not signal specified error!", pattern = "", call = NULL){
if(is.null(call)) call = deparse(substitute(expr)) |> paste0(collapse = "")
e = tryCatch(expr, error = \(e) e)
(is.error(e) && grepl(pattern, conditionMessage(e))) |> TEST(call = call, msg = msg)
}


TEST_NOT_ERROR = function(expr, msg = "does signal an error!", call = NULL){
if(is.null(call)) call = deparse(substitute(expr)) |> paste0(collapse = "")
e = tryCatch(expr, error = \(e) e)
is.error(e) |> not() |> TEST(call = call, msg = msg)
}
44 changes: 32 additions & 12 deletions examples/helpers/test-mutr.r
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Test for unit-tests
#
# This will test a simplified version of mutr used for self-testing
# test-mutr.r: tests for the unit-testing framework mutr

# helpers
TEST__CHECK = function(fail, total, sets){
env = get(".TESTS", envir = globalenv())
stopifnot(env$FAIL == fail, env$TOTAL == total, env$SETS == sets)
}

TEST__FAIL = function(expr){
msg = capture.output(TEST(expr))
stopifnot(" Error in expr: is not TRUE!" == msg)
TEST__FAIL = function(expr, .msg = NULL, f = TEST, ...){
.expr = substitute(expr)
out = capture.output(eval(as.call(list(f, .expr, ...))))
if(is.null(.msg)) .msg = paste0(" Error in ", deparse(substitute(expr)), ": is not TRUE!")
if(!identical(out, .msg))
stop("Output message differs from expected!\n",
" Output: ", out, "\n",
" Expected: ", .msg)
}

TEST__PASS = function(expr){
msg = capture.output(TEST(expr))
stopifnot(msg == character(0))
TEST__PASS = function(expr, f = TEST, ...){
.expr = substitute(expr)
out = capture.output(eval(as.call(list(f, .expr, ...))))
stopifnot(identical(out, character()))
}

TEST__RESET = function(){
Expand All @@ -27,15 +30,32 @@ TEST__CHECK(0, 0, 0)
TEST__PASS(TRUE)
TEST__CHECK(0, 1, 0)
TEST__FAIL(FALSE)
TEST__CHECK(1, 2, 0)
TEST__FAIL(FALSE, " Error in FALSE: is not TRUE!")
TEST__FAIL(c(1:3), " Error in c(1:3): is not TRUE!")
TEST__CHECK(3, 4, 0)

TEST_SET("", {
TEST__PASS(TRUE)
TEST__FAIL(FALSE)
TEST__FAIL(NA)
TEST__FAIL(1)
})
TEST__CHECK(4, 6, 1)
TEST__CHECK(6, 8, 1)

TEST__PASS(stop("foo"), f = TEST_ERROR)
TEST__PASS(stop("foo"), f = TEST_ERROR, pattern = "foo")
TEST__CHECK(6, 10, 1)
TEST__FAIL(stop("foo"), f = TEST_ERROR, pattern = "bar",
.msg = " Error in stop(\"foo\"): does not signal specified error!")
TEST__FAIL("foo", f = TEST_ERROR,
.msg = " Error in \"foo\": does not signal specified error!")
TEST__CHECK(8, 12, 1)

TEST__PASS("foo", f = TEST_NOT_ERROR)
TEST__PASS(1, f = TEST_NOT_ERROR)
TEST__FAIL(stop("foo"), f = TEST_NOT_ERROR,
.msg = " Error in stop(\"foo\"): does signal an error!")
TEST__CHECK(9, 15, 1)

# Cleanup
TEST__RESET()
Expand Down
11 changes: 11 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.RECIPEPREFIX = +

all: test copy

test:
+ Rscript -e 'source("mutr.r"); source("test-mutr.r")'

copy: test
+ cp mutr.r test-mutr.r examples/helpers/

.PHONY: all test copy
18 changes: 13 additions & 5 deletions mutr.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mutr: minimal unit-testing framework
#
# a simplified copy-pastable version inspired by https://jera.com/techinfo/jtns/jtn002
# inspired by https://jera.com/techinfo/jtns/jtn002
# version: 0.0.2
# https://github.com/J-Moravec/mutr

TEST_INIT = function(){
env = new.env(parent = emptyenv())
Expand Down Expand Up @@ -31,7 +32,7 @@ TEST_PRINT = function(){


TEST = function(expr, msg = "is not TRUE!", call = NULL){
call = if(is.null(call)) deparse(substitute(expr)) |> paste0(collapse = "")
if(is.null(call)) call = deparse(substitute(expr)) |> paste0(collapse = "")
res = try(expr, silent = TRUE)

env = get(".TESTS", envir = globalenv())
Expand Down Expand Up @@ -81,8 +82,15 @@ not = function(x){
}


TEST_ERROR = function(expr, msg = "does not signal required error!", pattern = "", call = NULL){
call = if(is.null(call)) deparse(substitute(expr)) |> paste0(collapse = "")
TEST_ERROR = function(expr, msg = "does not signal specified error!", pattern = "", call = NULL){
if(is.null(call)) call = deparse(substitute(expr)) |> paste0(collapse = "")
e = tryCatch(expr, error = \(e) e)
(is.error(e) && grepl(pattern, conditionMessage(e))) |> TEST(call = call, msg = msg)
}


TEST_NOT_ERROR = function(expr, msg = "does signal an error!", call = NULL){
if(is.null(call)) call = deparse(substitute(expr)) |> paste0(collapse = "")
e = tryCatch(expr, error = \(e) e)
is.error(e) |> not() |> TEST(call = call, msg = msg)
}
24 changes: 0 additions & 24 deletions old/DESCRIPTION

This file was deleted.

2 changes: 0 additions & 2 deletions old/LICENSE

This file was deleted.

21 changes: 0 additions & 21 deletions old/LICENSE.md

This file was deleted.

17 changes: 0 additions & 17 deletions old/NAMESPACE

This file was deleted.

Loading

0 comments on commit ee154a8

Please sign in to comment.