Debian Codegraph @

inst/tests/test-helpers.R

raw
1 library(testthat)
2 library(pander)
3 context('has.rownames')
4
5 test_that('has.rownames: TRUE', {
6 expect_true(has.rownames(mtcars))
7 expect_true(has.rownames(mtcars[1:2, ]))
8 expect_true(has.rownames(mtcars[1, ]))
9 expect_true(has.rownames(table(mtcars$am, mtcars$gear)))
10 expect_true(has.rownames(table(mtcars$am, mtcars$gear) + 0.1))
11 expect_true(has.rownames(summary(lm(mtcars$hp~1))$coeff))
12 })
13
14 test_that('has.rownames: FALSE', {
15 expect_false(has.rownames(mtcars$am))
16 expect_false(has.rownames(mtcars[1:2, 5]))
17 expect_false(has.rownames(summary(mtcars$am)))
18 expect_false(has.rownames(table(mtcars$am)))
19 expect_false(has.rownames(table(mtcars$am) + 0.1))
20 })
21
22 context('splitLine')
23
24 test_that('splitLine leaves non-string arguments and empty string unchanged', {
25 expect_equal(1, splitLine(1))
26 expect_equal(1:10, splitLine(1:10))
27 expect_equal(c('string 1', 'string 2', 'string 3'), splitLine(c('string 1', 'string 2', 'string 3')))
28 expect_equal(mtcars, splitLine(mtcars))
29 expect_equal(summary(lm(mtcars$hp~1)), splitLine(summary(lm(mtcars$hp~1))))
30 expect_equal(splitLine('', 10), '')
31 expect_equal(splitLine('', 0), '')
32 expect_equal(splitLine('', Inf), '')
33 })
34
35 test_that('splitLine without hyphening behaves correctly', {
36 widthSpaced <- function(x, max.width, type='max'){
37 if (type == 'max')
38 max(sapply(strsplit(splitLine(x, max.width), '\n'), function(x) nchar(x) * grepl(' ', x)))
39 else if (type == 'min')
40 min(sapply(strsplit(splitLine(x, max.width), '\n'), nchar))
41 }
42 x <- 'foo bar'
43 expect_equal(splitLine(x, 7), 'foo bar')
44 expect_equal(splitLine(x, 6), 'foo\nbar')
45 x <- 'Each character string in the input is first split into paragraphs (or lines containing whitespace only)'
46 expect_equal(splitLine(x, 10),
47 'Each\ncharacter\nstring in\nthe input\nis first\nsplit into\nparagraphs\n(or lines\ncontaining\nwhitespace\nonly)') #nolint
48 expect_equal(splitLine(x, 20),
49 'Each character\nstring in the input\nis first split into\nparagraphs (or lines\ncontaining\nwhitespace only)') #nolint
50 expect_equal(splitLine(x, 0), paste(strwrap(x, 0), collapse = '\n')) # zero width replaces whitespaces by line breaks
51 expect_equal(splitLine(x, Inf), x) # Infinite does not change
52 x <- 'Really do clean up and test your code and think twice before you even start contemplating optimizing the code'
53 expect_true(widthSpaced(x, 5, 'max') <= 5) # max width of line is less than 5 (in case it has spaces)
54 expect_true(widthSpaced(x, 10, 'max') <= 10) # max width of line is less than 10 (in case it has spaces)
55 expect_true(widthSpaced(x, 10, 'min') >= 1) # every line has at least one character
56 expect_equal('character', splitLine('character', 2)) # string without spaces remains unchanged
57 expect_equal('', splitLine('', 2)) # for empty string, returns empty string
58 expect_equal(length(strsplit(splitLine(x, 2), '\n')[[1]]), 19) # when max.width param is small, every word is a line
59 })
60
61 test_that('splitLine with hyphening behaves correctly', {
62 x <- 'Really do clean up and test your code and think twice before you even start contemplating optimizing the code'
63 widthSpaced <- function(x, max.width, type='max'){
64 if (type == 'max')
65 max(sapply(strsplit(splitLine(x, max.width, TRUE), '\n'), function(x) nchar(x) * grepl(' ', x)))
66 else if (type == 'min')
67 min(sapply(strsplit(splitLine(x, max.width, TRUE), '\n'), nchar))
68 }
69 expect_true(widthSpaced(x, 5, 'max') <= 5) # max width of line is less than 5 (in case it has spaces)
70 expect_true(widthSpaced(x, 10, 'max') <= 10) # max width of line is less than 10 (in case it has spaces)
71 expect_true(widthSpaced(x, 10, 'min') >= 1) # every line has at least one character
72 # check correctness for predifined words/phrases
73 expect_equal('char-\nac-\nter', splitLine('character', 2, TRUE))
74 expect_equal(splitLine('start testing', 5, T), 'start\ntest-\ning')
75 expect_equal(splitLine('Pander Package', 5, T), 'Pan-\nder\nPack-\nage')
76 expect_equal(splitLine('Pander Package', 6, T), 'Pander\nPack-\nage')
77 expect_equal(splitLine('Pander Package', 7, T), 'Pander\nPackage')
78 # predifined example
79 # when max.width param is small, every word is a line
80 expect_equal(length(strsplit(splitLine(x, 10, TRUE), '\n')[[1]]), 12)
81 expect_false(grepl('- ', splitLine(x, 10, TRUE))) # no space after hyphen
82 expect_false(grepl(' -', splitLine(x, 10, TRUE))) # no space before hyphen
83 expect_false(grepl('\n ', splitLine(x, 20, TRUE))) # line does not start with a space
84 # infinite width
85 expect_equal(splitLine(x, Inf, TRUE), x)
86 })
87
88 test_that('options', {
89 expect_error(panderOptions('foobar'))
90 expect_error(evalsOptions('foobar'))
91 })
92
93 context('p function')
94 test_that('p', {
95 expect_equal(p(character(0)), '')
96 expect_equal(p(1), '_1_')
97 expect_equal(p(1:3), '_1_, _2_ and _3_')
98 expect_equal(p(1, wrap = ''), '1')
99 expect_equal(p(1:3, wrap = '', sep = '|', copula = '|'), '1|2|3')
100 expect_equal(p(seq(1, 1.2, 0.1)), '_1_, _1.1_ and _1.2_')
101 expect_equal(p(seq(1, 1.2, 0.1), keep.trailing.zeros = TRUE),
102 '_1.0_, _1.1_ and _1.2_')
103 expect_error(p(1:10, limit = 3))
104 })