Skip to content

Commit

Permalink
Merge branch 'fix_print_tests' into master-rok
Browse files Browse the repository at this point in the history
  • Loading branch information
rok-cesnovar committed Mar 6, 2022
2 parents 649d64e + 21be226 commit f462a27
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
30 changes: 17 additions & 13 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ test_that("print.draws_matrix runs without errors", {
)

x <- weight_draws(x, rep(1, ndraws(x)))
expect_output(print(x), "hidden reserved variables \\{'\\.log_weight'\\}")
expect_output(
print(x),
"hidden reserved variables ..\\.log_weight.."
)
})

test_that("print.draws_array runs without errors", {
Expand All @@ -15,7 +18,10 @@ test_that("print.draws_array runs without errors", {
)

x <- weight_draws(x, rep(1, ndraws(x)))
expect_output(print(x), "hidden reserved variables \\{'\\.log_weight'\\}")
expect_output(
print(x),
"hidden reserved variables ..\\.log_weight.."
)
})

test_that("print.draws_df runs without errors", {
Expand All @@ -39,7 +45,10 @@ test_that("print.draws_list runs without errors", {
)

x <- weight_draws(x, rep(1, ndraws(x)))
expect_output(print(x), "hidden reserved variables \\{'\\.log_weight'\\}")
expect_output(
print(x),
"hidden reserved variables ..\\.log_weight.."
)
})

test_that("print.draws_rvars runs without errors", {
Expand All @@ -54,8 +63,7 @@ test_that("print.draws_rvars runs without errors", {
x <- weight_draws(x, rep(1, ndraws(x)))
expect_output(
print(x),
"hidden reserved variables {'.log_weight'}",
fixed = TRUE
"hidden reserved variables ..\\.log_weight.."
)
})

Expand All @@ -65,8 +73,7 @@ test_that("print.draws_array handles reserved variables correctly", {
expect_output(print(x, max_variables = 1), "variable = tau")
expect_output(
print(x),
"hidden reserved variables {'.log_weight'}",
fixed = TRUE
"hidden reserved variables ..\\.log_weight.."
)
})

Expand All @@ -76,8 +83,7 @@ test_that("print.draws_matrix handles reserved variables correctly", {
expect_output(print(x, max_variables = 1), "tau")
expect_output(
print(x),
"hidden reserved variables {'.log_weight'}",
fixed = TRUE
"hidden reserved variables ..\\.log_weight.."
)
})

Expand All @@ -98,8 +104,7 @@ test_that("print.draws_list handles reserved variables correctly", {
expect_output(print(x, max_variables = 1), "tau")
expect_output(
print(x),
"hidden reserved variables {'.log_weight'}",
fixed = TRUE
"hidden reserved variables ..\\.log_weight.."
)
})

Expand All @@ -110,8 +115,7 @@ test_that("print.draws_rvars handles reserved variables correctly", {
expect_output(print(x, max_variables = 1), "tau")
expect_output(
print(x),
"hidden reserved variables {'.log_weight'}",
fixed = TRUE
"hidden reserved variables ..\\.log_weight.."
)
})

Expand Down
70 changes: 31 additions & 39 deletions tests/testthat/test-rvar-print.R
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
test_that("basic print.rvar works", {
skip_on_os("windows")
x <- rvar(array(1:12, dim = c(2,2,3)))
x_with_chains <- rvar(array(1:12, dim = c(2,2,3)), nchains = 2)

expect_output(print(rvar(), color = FALSE),
"rvar<1>[0] mean ± sd:
NULL",
fixed = TRUE
expect_output(
print(rvar(), color = FALSE),
"rvar<1>\\[0\\] mean . sd:\nNULL"
)

expect_output(print(x, color = FALSE),
"rvar<2>[2,3] mean ± sd:\
[,1] [,2] [,3] \
[1,] 1.5 ± 0.71 5.5 ± 0.71 9.5 ± 0.71 \
[2,] 3.5 ± 0.71 7.5 ± 0.71 11.5 ± 0.71 ",
fixed = TRUE
expect_output(
print(x, color = FALSE),
paste0(
"rvar<2>\\[2,3\\] mean . sd:\n",
" \\[,1\\] \\[,2\\] \\[,3\\] \\n",
"\\[1,\\] 1.5 . 0.71 5.5 . 0.71 9.5 . 0.71 \n",
"\\[2,\\] 3.5 . 0.71 7.5 . 0.71 11.5 . 0.71"
)
)

expect_output(print(x_with_chains, color = FALSE),
"rvar<1,2>[2,3] mean ± sd:\
[,1] [,2] [,3] \
[1,] 1.5 ± 0.71 5.5 ± 0.71 9.5 ± 0.71 \
[2,] 3.5 ± 0.71 7.5 ± 0.71 11.5 ± 0.71 ",
fixed = TRUE
expect_output(
print(x_with_chains, color = FALSE),
paste0(
"rvar<1,2>\\[2,3\\] mean . sd:\n",
" \\[,1\\] \\[,2\\] \\[,3\\] \\n",
"\\[1,\\] 1.5 . 0.71 5.5 . 0.71 9.5 . 0.71 \n",
"\\[2,\\] 3.5 . 0.71 7.5 . 0.71 11.5 . 0.71"
)
)
})

test_that("basic str.rvar works", {
skip_on_os("windows")
x <- rvar(array(1:24, dim = c(2,3,4)))
x_with_chains <- rvar(array(1:24, dim = c(2,3,4)), nchains = 2)

expect_output(str(rvar()),
" rvar<1>[0] ",
fixed = TRUE
" rvar<1>\\[0\\] "
)
expect_output(str(rvar(1:3)),
" rvar<3>[1] 2 ± 1",
fixed = TRUE
" rvar<3>\\[1\\] 2 . 1"
)
expect_output(str(x, vec.len = 5),
" rvar<2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ...",
fixed = TRUE
" rvar<2>\\[3,4\\] 1.5 . 0.71 3.5 . 0.71 5.5 . 0.71 7.5 . 0.71 9.5 . 0.71 ..."
)
expect_output(str(x_with_chains, vec.len = 5),
" rvar<1,2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ...",
fixed = TRUE
" rvar<1,2>\\[3,4\\] 1.5 . 0.71 3.5 . 0.71 5.5 . 0.71 7.5 . 0.71 9.5 . 0.71 ..."
)

x_with_attrs <- x
dimnames(x_with_attrs)[1] = list(c("a","b","c"))
attr(draws_of(x_with_attrs), "foo") = list(1,2)
attr(x_with_attrs, "bar") = list(1,2)

expect_output(str(x_with_attrs, vec.len = 5),
' rvar<2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ...
- dimnames(*)=List of 2
..$ : chr [1:3] "a" "b" "c"
..$ : NULL
- attr(draws_of(*), "foo")=List of 2
..$ : num 1
..$ : num 2
- attr(*, "bar")=List of 2
..$ : num 1
..$ : num 2',
fixed = TRUE
expect_output(
str(x_with_attrs, vec.len = 5),
paste0(
" rvar<2>\\[3,4\\] 1.5 . 0.71 3.5 . 0.71 5.5 . 0.71 7.5 . 0.71 9.5 . 0.71 ...\n",
" - dimnames\\(\\*\\)=List of 2\n",
' \\.\\.\\$ : chr \\[1:3\\] "a" "b" "c"'
)
)

})

test_that("glimpse on rvar works", {
skip_on_cran()
x_vec <- rvar(array(1:24, dim = c(6,4)))
x_matrix <- rvar(array(1:24, dim = c(2,3,4)))

Expand Down

0 comments on commit f462a27

Please sign in to comment.