diff --git a/tests/testthat/test-checkimagetag.R b/tests/testthat/test-checkimagetag.R index a535684..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,6 +32,56 @@ test_that("checkImageTag", " expect_error(res <- checkImageTag(txt.withattr), NA) expect_equal(res, paste0("
", txt.withattr, "
")) +}) + +test_that("checkImageTag: style and width attributes preceding src are preserved, not stripped", +{ + expect_warning(res <- checkImageTag(kTxtStyle), NA) + expect_equal(res, paste0("
", kTxtStyle, "
")) +}) + +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.dq2 <- "" + expect_error(res <- checkImageTag(txt.dq2), NA) + expect_equal(res, paste0("
", txt.dq2, "
")) +}) + +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, "
")) +}) - txt.style <- "" +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 which has been removed: ") + expect_equal(res, "") +}) + +test_that("checkImageTag: surrounding text is preserved, not just the image tag, on the success path", +{ + txt.label <- paste0("Label ", kTxtStyle) + 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 which has been removed: x") + expect_equal(res, "") })