From 81e409e1c558a2c40808ba0a76a27e10ccb0ef5b Mon Sep 17 00:00:00 2001 From: Leonidas Zhak <70497898+LeonidasZhak@users.noreply.github.com> Date: Sun, 7 Jun 2026 00:12:38 +0800 Subject: [PATCH] test: expand zap_widths() test coverage - Add test for labelled vectors (verifies class and labels preserved) - Add test for labelled_spss vectors (verifies na_values preserved) - Add test that other attributes (format.spss, label) are preserved - Add test for data frames with multiple columns - Add test that existing NAs are preserved - Total: 7 tests (was 2) --- tests/testthat/test-zap_widths.R | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/testthat/test-zap_widths.R b/tests/testthat/test-zap_widths.R index 102443ae..893cbe75 100644 --- a/tests/testthat/test-zap_widths.R +++ b/tests/testthat/test-zap_widths.R @@ -12,3 +12,54 @@ test_that("can zap width attribute from vector in data frame", { expect_null(attributes(out$x)) }) + +test_that("zap_widths removes display_width from labelled vector", { + x <- labelled(1:5, c(good = 1, bad = 5)) + attr(x, "display_width") <- 10 + out <- zap_widths(x) + + expect_null(attr(out, "display_width")) + expect_s3_class(out, "haven_labelled") + expect_equal(attr(out, "labels"), c(good = 1, bad = 5)) +}) + +test_that("zap_widths removes display_width from labelled_spss vector", { + x <- labelled_spss(1:5, c(good = 1), na_values = 5) + attr(x, "display_width") <- 10 + out <- zap_widths(x) + + expect_null(attr(out, "display_width")) + expect_s3_class(out, "haven_labelled_spss") + expect_equal(attr(out, "na_values"), 5) +}) + +test_that("zap_widths preserves other attributes", { + x <- structure(1:5, display_width = 10, format.spss = "F8.2", label = "myvar") + out <- zap_widths(x) + + expect_null(attr(out, "display_width")) + expect_equal(attr(out, "format.spss"), "F8.2") + expect_equal(attr(out, "label"), "myvar") +}) + +test_that("zap_widths works with data frame containing multiple columns", { + df <- tibble::tibble( + a = structure(1:3, display_width = 10), + b = structure(4:6, display_width = 20, label = "var b"), + c = 7:9 + ) + out <- zap_widths(df) + + expect_null(attr(out$a, "display_width")) + expect_null(attr(out$b, "display_width")) + expect_equal(attr(out$b, "label"), "var b") + expect_equal(out$c, 7:9) +}) + +test_that("zap_widths preserves existing NAs", { + x <- structure(c(1, NA, 3), display_width = 10) + out <- zap_widths(x) + + expect_null(attr(out, "display_width")) + expect_equal(out, c(1, NA, 3)) +})