From 6de2ae0b1e8289fe46f13fe3590d3631cd0d812b Mon Sep 17 00:00:00 2001 From: Surrey Date: Fri, 21 Aug 2026 14:47:45 +1000 Subject: [PATCH 1/2] test: cover checkImageTag style/width attribute ordering and quoting Adds scenarios for style and width attributes preceding src, attribute order independence, double-quoted and unquoted src values, a missing src attribute, surrounding text preservation, and a decoy substring "src=" match that is not silently treated as a valid link. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/testthat/test-checkimagetag.R | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/testthat/test-checkimagetag.R b/tests/testthat/test-checkimagetag.R index a535684..7ec07b5 100644 --- a/tests/testthat/test-checkimagetag.R +++ b/tests/testthat/test-checkimagetag.R @@ -29,3 +29,57 @@ test_that("checkImageTag", txt.style <- "" }) + +test_that("checkImageTag: style and width attributes preceding src are preserved, not stripped", +{ + txt.style <- "" + expect_error(res <- checkImageTag(txt.style), NA) + expect_warning(checkImageTag(txt.style), NA) + expect_equal(res, paste0("
", txt.style, "
")) +}) + +test_that("checkImageTag: src location is independent of attribute order", +{ + txt.order <- "" + expect_error(res <- checkImageTag(txt.order), NA) + expect_equal(res, paste0("
", txt.order, "
")) +}) + +test_that("checkImageTag: a double-quoted src is cleaned identically to a single-quoted src", +{ + txt.dq <- "" + expect_error(res <- checkImageTag(txt.dq), NA) + expect_equal(res, paste0("
", txt.dq, "
")) +}) + +test_that("checkImageTag: an unquoted bare src is cleaned identically", +{ + txt.unquoted <- "" + expect_error(res <- checkImageTag(txt.unquoted), NA) + expect_equal(res, paste0("
", txt.unquoted, "
")) +}) + +test_that("checkImageTag: attributes with no src attribute at all warns with a syntax error and returns an empty string", +{ + txt.nosrc <- "" + expect_warning(res <- checkImageTag(txt.nosrc), "syntax error") + expect_equal(res, "") +}) + +test_that("checkImageTag: surrounding text is preserved, not just the image tag, on the success path", +{ + txt.style <- "" + txt.label <- paste0("Label ", txt.style) + expect_error(res <- checkImageTag(txt.label), NA) + expect_equal(res, paste0("
", txt.label, "
")) +}) + +test_that("checkImageTag: an attribute value containing the substring 'src=' before the real src warns of an invalid link rather than silently succeeding", +{ + # regexpr("src=(\\S+)", ...) is unanchored and matches the embedded "src=" inside + # "mysrc=x" before the real src attribute, producing a broken link; this is not + # asserted as correct behaviour, only that it is not silently treated as valid. + txt.decoy <- "mysrc=x" + expect_warning(res <- checkImageTag(txt.decoy), "invalid link") + expect_false(identical(res, paste0("
", txt.decoy, "
"))) +}) From f9f578b48a0c9faa5a706d2b13e6364794b4bba8 Mon Sep 17 00:00:00 2001 From: Surrey Date: Fri, 21 Aug 2026 14:55:41 +1000 Subject: [PATCH 2/2] fix(test): strengthen checkImageTag decoy/syntax-error assertions Replace unfailable identical() check and network-dependent warning match in the src= decoy block with pinned real values (result "" and warning text including the captured "x"); pin echoed input in the missing-src syntax-error message to distinguish it from the tag-regex-miss branch; drop redundant duplicate expect_warning call; hoist repeated txt.style literal to a file-level constant and remove dangling duplicate txt.dq assignment; rename reused txt.dq in the double-quote block; document network dependency of success-path blocks. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/testthat/test-checkimagetag.R | 30 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/testthat/test-checkimagetag.R b/tests/testthat/test-checkimagetag.R index 7ec07b5..85a16ca 100644 --- a/tests/testthat/test-checkimagetag.R +++ b/tests/testthat/test-checkimagetag.R @@ -1,5 +1,11 @@ context("CreateCustomTable") +# NOTE: the success-path blocks below (style/width attribute preservation, attribute +# order, quoting variants, surrounding text) fetch a real image from +# wiki.q-researchsoftware.com and will fail if run without network access to that host. + +kTxtStyle <- "" + test_that("checkImageTag", { txt.sq <- "" @@ -26,16 +32,12 @@ test_that("checkImageTag", " expect_error(res <- checkImageTag(txt.withattr), NA) expect_equal(res, paste0("
", txt.withattr, "
")) - - txt.style <- "" }) test_that("checkImageTag: style and width attributes preceding src are preserved, not stripped", { - txt.style <- "" - expect_error(res <- checkImageTag(txt.style), NA) - expect_warning(checkImageTag(txt.style), NA) - expect_equal(res, paste0("
", txt.style, "
")) + expect_warning(res <- checkImageTag(kTxtStyle), NA) + expect_equal(res, paste0("
", kTxtStyle, "
")) }) test_that("checkImageTag: src location is independent of attribute order", @@ -47,9 +49,9 @@ test_that("checkImageTag: src location is independent of attribute order", test_that("checkImageTag: a double-quoted src is cleaned identically to a single-quoted src", { - txt.dq <- "" - expect_error(res <- checkImageTag(txt.dq), NA) - expect_equal(res, paste0("
", txt.dq, "
")) + txt.dq2 <- "" + expect_error(res <- checkImageTag(txt.dq2), NA) + expect_equal(res, paste0("
", txt.dq2, "
")) }) test_that("checkImageTag: an unquoted bare src is cleaned identically", @@ -62,14 +64,14 @@ test_that("checkImageTag: an unquoted bare src is cleaned identically", test_that("checkImageTag: attributes with no src attribute at all warns with a syntax error and returns an empty string", { txt.nosrc <- "" - expect_warning(res <- checkImageTag(txt.nosrc), "syntax error") + expect_warning(res <- checkImageTag(txt.nosrc), + "syntax error which has been removed: ") expect_equal(res, "") }) test_that("checkImageTag: surrounding text is preserved, not just the image tag, on the success path", { - txt.style <- "" - txt.label <- paste0("Label ", txt.style) + txt.label <- paste0("Label ", kTxtStyle) expect_error(res <- checkImageTag(txt.label), NA) expect_equal(res, paste0("
", txt.label, "
")) }) @@ -80,6 +82,6 @@ test_that("checkImageTag: an attribute value containing the substring 'src=' bef # "mysrc=x" before the real src attribute, producing a broken link; this is not # asserted as correct behaviour, only that it is not silently treated as valid. txt.decoy <- "mysrc=x" - expect_warning(res <- checkImageTag(txt.decoy), "invalid link") - expect_false(identical(res, paste0("
", txt.decoy, "
"))) + expect_warning(res <- checkImageTag(txt.decoy), "invalid link which has been removed: x") + expect_equal(res, "") })