test_that("wid_validate: no args returns list of NULLs", { res <- wid_validate() expect_type(res, "list") expect_named(res, c("series_type","concept","age","pop","years","areas","perc")) }) test_that("wid_validate: returns normalised values", { res <- wid_validate(series_type="s", concept="ptinc", age=992, pop="j") expect_equal(res$series_type, "s") expect_equal(res$concept, "ptinc") expect_equal(res$age, "992") expect_equal(res$pop, "j") }) test_that("wid_validate: table checks error on unknown codes", { expect_silent(wid_validate(series_type=c("s","a"))) expect_error(wid_validate(series_type="Z"), "series_type") expect_silent(wid_validate(concept=c("ptinc","nninc"))) expect_error(wid_validate(concept="zzzzz"), "concept") expect_silent(wid_validate(pop=c("i","j"))) expect_error(wid_validate(pop="z"), "pop") }) test_that("wid_validate: age normalises and validates", { expect_equal(wid_validate(age=c(999,992,14))$age, c("999","992","014")) expect_error(wid_validate(age="abc")) expect_error(wid_validate(age=0)) }) test_that("wid_validate: years validation", { expect_type(wid_validate(years=c(2000,2010))$years, "integer") expect_error(wid_validate(years="abc")) expect_warning(wid_validate(years=1799L)) expect_silent(wid_validate(years=c(1800L,2100L))) }) test_that("wid_validate: areas warns on malformed codes", { expect_silent(wid_validate(areas=c("US","FR"))) expect_warning(wid_validate(areas="us"), "invalid") expect_warning(wid_validate(areas="U"), "invalid") expect_warning(wid_validate(areas="USA"), "invalid") }) test_that("wid_validate: perc validation", { expect_silent(wid_validate(perc=c("p0p100","p99.9p100"))) expect_error(wid_validate(perc="top1"), "percentile") expect_error(wid_validate(perc="p50p10"), "upper") }) test_that("wid_is_valid: TRUE/FALSE, never throws", { expect_true(wid_is_valid(series_type="s", concept="ptinc")) expect_false(wid_is_valid(series_type="Z")) expect_true(wid_is_valid(areas="us")) expect_type(wid_is_valid(concept="ptinc"), "logical") expect_length(wid_is_valid(concept="ptinc"), 1L) }) test_that("wid_decode: full code", { d <- wid_decode("sptinc992j") expect_named(d, c("series_type","concept","age","pop")) expect_equal(d$series_type, "s") expect_equal(d$concept, "ptinc") expect_equal(d$age, "992") expect_equal(d$pop, "j") }) test_that("wid_decode: optional components", { expect_null(wid_decode("mnninc")$age) expect_equal(wid_decode("mnninc999")$age, "999") }) test_that("wid_decode: strict=TRUE errors", { expect_error(wid_decode("Zptinc992j"), "series_type") expect_error(wid_decode("szzzzz992j"), "concept") expect_error(wid_decode("sptinc000j"), "age") expect_error(wid_decode("sptinc992Z"), "pop") }) test_that("wid_decode: strict=FALSE warns but does not error", { expect_warning(out <- wid_decode("Zptinc992j", strict=FALSE)) expect_warning(out <- wid_decode("szzzzz992j", strict=FALSE)) }) test_that("wid_decode: trims whitespace", { expect_equal(wid_decode(" sptinc992j ")$concept, "ptinc") }) test_that("wid_encode: from components", { expect_equal(wid_encode("s","ptinc","992","j"), "sptinc992j") }) test_that("wid_encode: from list", { lst <- list(series_type="s", concept="ptinc", age="992", pop="j") expect_equal(wid_encode(lst), "sptinc992j") }) test_that("wid_encode: round-trip", { expect_equal(wid_encode(wid_decode("sptinc992j")), "sptinc992j") }) test_that("wid_encode: rejects invalid", { expect_error(wid_encode("Z","ptinc")) expect_error(wid_encode("s","zzzzz")) }) test_that("wid_search: structure and matching", { res <- wid_search("income") expect_s3_class(res, "data.frame") expect_named(res, c("table","code","description")) expect_true("ptinc" %in% wid_search("^ptinc$")$code) }) test_that("wid_search: table selection", { expect_equal(wid_search("^US$","countries")$code, "US") res <- wid_search("^i$",c("series_types","pop_types")) expect_true(all(c("series_types","pop_types") %in% res$table)) }) test_that("wid_search: no match", { expect_equal(nrow(wid_search("xyzzy_no_match")), 0L) }) test_that("wid_search: unknown table errors", { expect_error(wid_search("x","nosuch")) })