Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/testthat/test-diagnostics-extra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Additional coverage tests for AIC / AICweights diagnostics methods
library(testthat)
library(EnvCpt)

# Minimal envcpt-like object for exercising diagnostics methods directly
fake_envcpt <- list(
summary = matrix(
c(10, 20,
1, 2),
nrow = 2,
byrow = TRUE
)
)
class(fake_envcpt) <- "envcpt"

# AIC.envcpt should respect a user-supplied penalty value
test_that("AIC.envcpt respects custom penalty k", {
out <- AIC(fake_envcpt, k = 3)
expect_equal(out, c(13, 26))
})

# The default AICweights method should return the documented fallback message
test_that("AICweights.default returns fallback message", {
out <- AICweights(1:5)
expect_equal(out, "No default method created for S3 class AICweights.")
})

# AICweights.envcpt should return normalized weights derived from AIC values
test_that("AICweights.envcpt returns normalized weights", {
w <- AICweights(fake_envcpt)
aic <- AIC(fake_envcpt)
expected <- exp(-0.5 * (aic - min(aic)))
expected <- expected / sum(expected)
expect_equal(w, expected)
})
43 changes: 43 additions & 0 deletions tests/testthat/test-plot-extra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Additional coverage tests for plot.envcpt argument handling
library(testthat)
library(EnvCpt)

test_that("plot.envcpt errors when x is not envcpt", {
expect_error(
EnvCpt:::plot.envcpt(list()),
"x must be an object with class envcpt"
)
})

# These plotting branches are run locally / outside CRAN
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
set.seed(99)
x <- c(rnorm(40, 0, 1), rnorm(40, 3, 1))
out <- envcpt(x)
# The function accepts the British spelling "colours", although it currently warns
test_that("plot.envcpt accepts colours alias but warns", {
expect_warning(plot(out, type = "aic", colours = rep("black", 12)))
})

test_that("plot.envcpt errors when colors vector is too short", {
expect_error(
plot(out, type = "aic", colors = rep("black", 3)),
"colors must be a vector of length 12"
)
})

test_that("plot.envcpt errors when a color is invalid", {
bad_cols <- c(rep("black", 11), "not_a_colour")
expect_error(
plot(out, type = "aic", colors = bad_cols),
"Atleast one of your colours is not resolvable by col2rgb."
)
})

test_that("plot.envcpt errors on unsupported plot type", {
expect_error(
plot(out, type = "weird"),
"type supplied can only be 'aic', 'bic' or 'fit'"
)
})
}
Loading