From f7a326eca24bb516eae9009cba44ce9f74ee2fd2 Mon Sep 17 00:00:00 2001 From: "David J. Bosak" Date: Sun, 29 Oct 2023 09:02:44 -0400 Subject: [PATCH] Still more tests. --- R/utilities.R | 20 +++++++++---------- tests/testthat/test-utilities.R | 35 +++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/R/utilities.R b/R/utilities.R index c279a2e..1206fa5 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -550,22 +550,22 @@ split_strings <- function(strng, width, units, multiplier = 1.03) { strwdth <- Vectorize(function(wrd, un) { - if (is.na(wrd)) { - ret <- 0 - } else { - - ret <- strwidth("a", units = un) * nchar(wrd) - - } + if (is.na(wrd)) { + ret <- 0 + } else { - tryCatch({ + ret <- tryCatch({ - ret <- suppressWarnings(strwidth(wrd, units = un)) + suppressWarnings(strwidth(wrd, units = un)) + + }, error = function(cond) { - + suppressWarnings(strwidth("a", units = un)) * nchar(wrd) + }) + } return(ret) }, USE.NAMES = FALSE, SIMPLIFY = TRUE) diff --git a/tests/testthat/test-utilities.R b/tests/testthat/test-utilities.R index d26e3d0..91fdf9f 100644 --- a/tests/testthat/test-utilities.R +++ b/tests/testthat/test-utilities.R @@ -663,14 +663,14 @@ test_that("utils31: strwdth() works as expected.", { - pdf(NULL) - par(family = 'mono', ps = 10) - - res <- strwdth(strngs, un = "inches") - - dev.off() + # pdf(NULL) + # par(family = 'mono', ps = 10) + # + # res <- strwidth(strngs, un = "inches") + # + # dev.off() - res + res <- c(0.4166667, 0.5833333, 1.5000000) @@ -683,24 +683,33 @@ test_that("utils31: strwdth() works as expected.", { res1 - expect_equal(all(res == res1), TRUE) + expect_equal(all(res == round(res1, 7)), TRUE) + # pdf(NULL) + # par(family = 'sans', ps = 10) + # + # res3 <- strwidth(strngs, un = "inches") + # + # dev.off() + + res3 <- c(0.2933333, 0.5272222, 1.1733333) + pdf(NULL) par(family = 'sans', ps = 10) - res2 <- strwdth(strngs, un = "inches") + res4 <- strwdth(strngs, un = "inches") dev.off() - res2 + res4 # print(res1) # print(res2) - expect_equal(all(res1 != res2) , TRUE) - - + expect_equal(all(res != res4), TRUE) + expect_equal(res3[1] == round(res4[1], 7), TRUE) + expect_equal(res3[2] == round(res4[2], 7), TRUE) })