brodieG / unitizer

Easy R Unit Tests
Other
39 stars 6 forks source link

Failure to recognize assignment as ignored #272

Closed brodieG closed 2 years ago

brodieG commented 3 years ago

The assignment to error in addition to the display of it get both treated as tests, instead of only the latter of the two.

library(svgchop)

unitizer_sect("pixel match", 
  compare=function(ref, new) {
    if(length(ref) != length(new)) {
      sprintf("length mismatch (ref: %d new: %d)", length(ref), length(new))
    } else if (!identical(names(ref), names(new))) {
      "name mismatch"
    } else if (anyNA(new)) {
      "NA values in new"
    } else {
      thresh <- new - ref < -.01
      if(any(thresh))
        paste0(
          c("elements ", deparse(which(thresh)) ," exceed error threshold."),
          collapse=""
        )
      else TRUE
    }
  },
  {
    error <- local({
      page <- compare_rsvg(display=0, steps=50, quietly=TRUE)
      diffs <-
        list.files(dirname(page), full.names=TRUE, pattern="^diff.*\\.png$")
      setNames(
        vapply(diffs, function(x) mean(png::readPNG(x)), 0),
        sub(".*[0-9]{2}-(.*)\\.png$", "\\1", basename(diffs))
      )
    })
    error
  }
)
brodieG commented 2 years ago

This is because the code in question emits uncaught, undisplayed conditions:

$conditions
$conditions[[1]]
<svgchop_unsupported: unit (cm) in <svg width='...'>.>

$conditions[[2]]
<svgchop_unsupported: unit (cm) in <svg height='...'>.>

$conditions[[3]]
<svgchop_unsupported: 'preserveAspectRatio' value 'xMidYMid' unsupported>

So the fix here is to display the conditions, although current behavior at the R prompt is to simply ignore these:

> res <- signalCondition(simpleCondition('hello'))
> 

I.e. without a handler, these might as well not exist. It really feels like unitizer should say something. The tension is that many conditions do usually manifest, e.g. by outputting to standard err or some other way. Perhaps what we need to do is only show the "additionally, 3 conditions were emitted, check .NEW$conditions" if there is no stderr output.

We can get to the conditions via .NEW, but the display is not ideal there either:

~~~ New Test ~~~
page <- compare_rsvg(display = 0, steps = 50, quietly = TRUE)
* value: chr[1]
* conditions:  

Access components with `$`, e.g. `.NEW$value`; see `help("$", "unitizer")`
unitizer> .NEW$conditions
Condition list with 3 conditions:
1. SimpleCondition: unit (cm) in <svg width='...'>.
2. SimpleCondition: unit (cm) in <svg height='...'>.
3. SimpleCondition: 'preserveAspectRatio' value 'xMidYMid' unsupported

Note class display for conditionList is bad, it should be showing "svgchop_unsupported", and the .NEW display is not telling us anything about the condition list.

brodieG commented 2 years ago

Loosely related to #229