markvanderloo / tinytest

A lightweight, no-dependency, but full-featured package for unit testing in R
GNU General Public License v3.0
223 stars 20 forks source link

Problems matching messages containing brackets #86

Closed BartJanvanRossum closed 3 years ago

BartJanvanRossum commented 3 years ago

Consider the three example below. Although they appear similar, the behavior of tinytest for the three of them is different.

The test for foo gives this error message: Error in grepl(pattern, e$message) : invalid regular expression '(', reason 'Missing ')'' In addition: Warning message: In grepl(pattern, e$message) : TRE pattern compilation error 'Missing ')''

The test for bar is working fine. The test for foobar however, doesn't give an error message, but the check itself fails with this cryptic message: _----- FAILED[xcpt]: <--> call| expecterror(foobar(), "foobar (fb)") diff| The error message: diff| 'foobar (fb)' diff| does not match pattern 'foobar (fb)'

Especially this last one is worrying since it seems to state that two identical strings are actually different.

foo <- function() {
  stop("(")
}
bar <- function() {
  stop("()")
}
foobar <- function() {
  stop("foobar (fb)")
}
expect_error(foo(),
             "(")
expect_error(bar(),
             "()")
expect_error(foobar(),
             "foobar (fb)")

Not sure if it is relevant, but I'm using the latest CRAN version of tinytest (1.2.4) and R.4.1.0, though the issue was present in earlier versions of R as well.

markvanderloo commented 3 years ago

Well, the bracket is a special character in regular expressions, so you need to escape it if you want to match it. Having said that, I think I should add the ... argument to pass optional arguments like fixed=TRUE to grepl.

BartJanvanRossum commented 3 years ago

I assumed it had something to do with this. Indeed the possibility to pass optional arguments to grepl would be nice.

For me what was most annoying that I had a real life case resembling foobar above. That fails, but it is not clear at first sight why