1test_that("trim works", {
2  expect_identical("", trim(""))
3  expect_identical(character(), trim(character()))
4  expect_identical(" ", trim(" "))
5  expect_identical("test", trim("test"))
6  expect_identical(" test", trim(" test"))
7  expect_identical("test ", trim("test "))
8  expect_identical("test", trim("test"))
9  expect_identical(c("foo", "bar"), trim(c("foo", "bar")))
10  expect_identical(c("foo", "bar"), trim(c("\nfoo", "bar\n")))
11  expect_identical("test",
12    trim(
13      "test"))
14  expect_identical("test",
15    x <- trim(
16      "test
17    "))
18  expect_identical("test",
19    trim("\x20\x20\x20\x20\x20\x20
20      test
21      "))
22  expect_identical("test",
23    trim(
24      "test"))
25  expect_identical("test\n  test2",
26    trim("
27      test
28        test2
29      "))
30  expect_identical("test\n  test2\n    test3",
31    trim("
32      test
33        test2
34          test3
35      "))
36
37  expect_identical("\ntest\n",
38    trim("
39
40      test
41
42      "))
43})
44
45test_that("trim strips escaped newlines", {
46  expect_identical(
47    "foo bar baz",
48    trim("foo bar \\\nbaz"))
49
50  expect_identical(
51    trim("
52      foo bar \\
53      baz"),
54      "foo bar baz")
55
56  expect_identical(
57    trim("
58      foo bar \\
59      baz
60      "),
61      "foo bar baz")
62
63  expect_identical(
64    "foo bar baz\n",
65    trim("foo bar baz\n\n"))
66
67  expect_identical(
68    "\nfoo bar baz",
69    trim("\n\nfoo bar baz"))
70})
71
72test_that("issue#44", {
73  expect_identical(
74    trim("12345678
75            foo
76           bar
77          baz
78           bar
79            baz"),
80          "12345678\n  foo\n bar\nbaz\n bar\n  baz")
81})
82
83test_that("issue#47", {
84  expect_identical(
85    trim("
86      Hello,
87      World.
88    "),
89    "  Hello,\n  World.")
90
91  expect_identical(
92    trim("
93      foo
94              bar
95        123456789"),
96      "foo\n        bar\n  123456789")
97
98  expected <- "The stuff before the bullet list\n  * one bullet"
99
100  expect_identical(
101    trim("The stuff before the bullet list
102            * one bullet
103          "), expected)
104
105  expect_identical(
106    trim("
107      The stuff before the bullet list
108        * one bullet"), expected)
109
110  expect_identical(
111    trim("
112         The stuff before the bullet list
113           * one bullet
114         "), expected)
115})
116
117test_that("lines containing only indentation are handled properly", {
118  # Tabs and spaces are considered indentation. The following examples look
119  # funny because I'm using a tab escape as the last indentation character to
120  # prevent RStudio from removing trailing whitespace on save.
121  expect_identical(
122    trim("
123         \ta
124         \tb
125         \t
126         \tc"),
127    "a\nb\n\nc"
128  )
129  expect_identical(
130    trim("
131         \ta
132       \tb
133         \t
134         \tc"),
135    " \ta\nb\n \t\n \tc"
136  )
137  # A line shorter than min_indent that contains only indentation should not be
138  # trimmed, removed, or prepended to the next line.
139  expect_identical(
140    trim("
141       \ta
142       \tb
143      \t
144       \tc"),
145    "a\nb\n      \t\nc"
146  )
147  # Ensure empty intermedite lines are handled properly
148  expect_identical(
149    trim("
150       \ta
151       \tb
152
153       \tc"),
154    "a\nb\n\nc"
155  )
156})
157
158# https://github.com/tidyverse/glue/issues/238
159test_that("indent counter resets at newline", {
160  # whitespace-only line has 1 space < min_indent (which is 2)
161  # comment in trim_() says:
162  # "if the line consists only of tabs and spaces, and if the line is
163  #  shorter than min_indent, copy the entire line"
164  expect_identical(trim("\n \n  abcd"), " \nabcd")
165
166  # whitespace-only line has n spaces, n >= min_indent
167  expect_identical( trim("\n  \n  abcd"),  "\nabcd")
168  expect_identical(trim("\n   \n  abcd"), " \nabcd")
169})
170
171# https://github.com/tidyverse/glue/issues/247
172test_that("trailing whitespace-only line doesn't goof up indentation", {
173  expect_identical(trim("\n  A\n\n"), "A\n")
174  # comment in trim_() says:
175  # "if the line consists only of tabs and spaces, and if the line is
176  #  shorter than min_indent, copy the entire line"
177  expect_identical(trim("\n  A\n \n"), "A\n ")
178  expect_identical(trim("\n  A\n  \n"), "A\n")
179  expect_identical(trim("\n  A\n   \n"), "A\n ")
180})
181