File: test-rowid.R

package info (click to toggle)
r-cran-pillar 1.8.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,840 kB
  • sloc: sh: 13; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 1,902 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
test_that("rowids for line-style", {
  lines <- function(char = "-") {
    stopifnot(nchar(char) == 1)
    structure(char, class = "lines")
  }

  format_lines <- function(x, width, ...) {
    paste(rep(x, width), collapse = "")
  }

  ctl_new_pillar_line_tbl <- function(controller, x, width, ..., title = NULL) {
    out <- NextMethod()
    new_pillar(list(
      title = out$title,
      type = out$type,
      lines = new_pillar_component(list(lines("=")), width = 1),
      data = out$data
    ))
  }

  ctl_new_rowid_pillar_line_tbl <- function(controller, x, width, ...) {
    out <- NextMethod()
    new_pillar(
      list(
        title = out$title,
        type = out$type,
        lines = new_pillar_component(list(lines("=")), width = 1),
        data = out$data
      ),
      width = as.integer(floor(log10(max(nrow(x), 1))) + 1)
    )
  }

  local_methods(
    format.lines = format_lines,
    ctl_new_pillar.line_tbl = ctl_new_pillar_line_tbl,
    ctl_new_rowid_pillar.line_tbl = ctl_new_rowid_pillar_line_tbl
  )

  expect_snapshot({
    vctrs::new_data_frame(
      list(a = 1:3, b = letters[1:3]),
      class = c("line_tbl", "tbl")
    )
  })
})

test_that("roman rowids", {
  ctl_new_rowid_pillar_roman_tbl <- function(controller, x, width, ...) {
    out <- NextMethod()
    rowid <- utils::as.roman(seq_len(nrow(x)))
    width <- max(nchar(as.character(rowid)))
    new_pillar(
      list(
        title = out$title,
        type = out$type,
        data = pillar_component(
          new_pillar_shaft(list(row_ids = rowid),
            width = width,
            class = "pillar_rif_shaft"
          )
        )
      ),
      width = width
    )
  }

  local_methods(
    ctl_new_rowid_pillar.roman_tbl = ctl_new_rowid_pillar_roman_tbl
  )

  expect_snapshot({
    vctrs::new_data_frame(
      list(a = 1:3, b = letters[1:3]),
      class = c("roman_tbl", "tbl")
    )
  })
})