File: test-utf8_encode.R

package info (click to toggle)
r-cran-utf8 1.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,060 kB
  • sloc: ansic: 25,890; python: 1,616; sh: 13; makefile: 6
file content (160 lines) | stat: -rw-r--r-- 3,883 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
test_that("'utf8_encode' can encode an ASCII string", {
  expect_equal(utf8_encode("hello"), "hello")
})


test_that("'utf8_encode' can encode NULL or an NA string", {
  expect_equal(utf8_encode(NULL), NULL)
  expect_equal(utf8_encode(NA_character_), NA_character_)
})


test_that("'utf8_encode' preserves attributes", {
  x <- matrix(LETTERS, 2, 13)
  x[1, 4] <- "\xa4"
  Encoding(x) <- "latin1"
  dimnames(x) <- list(c("foo", "bar"), as.character(1:13))
  class(x) <- "my_class"

  local_ctype("UTF-8")

  expect_equal(utf8_encode(x), enc2utf8(x))
})


test_that("'utf8_encode' can encode basic Unicode", {
  x <- "\u200b"
  Encoding(x) <- "UTF-8"

  local_ctype("UTF-8")

  expect_equal(utf8_encode(x), x)
})


test_that("'utf8_encode' can encode extended Unicode", {
  skip_on_os("windows") # no extended Unicode

  x <- intToUtf8(0x0001f60d)
  Encoding(x) <- "UTF-8"

  local_ctype("UTF-8")

  expect_equal(utf8_encode(x), x)
})


test_that("'utf8_encode' can handle ASCII escapes", {
  x <- "\x01\a\b\f\n\r\t\v\x7f"
  expect_equal(utf8_encode(x), "\\u0001\\a\\b\\f\\n\\r\\t\\v\\u007f")
})

test_that("'utf8_encode' can handle invalid UTF-8", {
  x <- "\xfe"
  Encoding(x) <- "bytes"
  expect_equal(utf8_encode(x), "\\xfe")
})

test_that("'utf8_encode' can handle bytes", {
  x <- "\x01\a\b\f\n\r\t\v\x7f\x80\xff"
  Encoding(x) <- "bytes"
  expect_equal(
    utf8_encode(x),
    "\\x01\\a\\b\\f\\n\\r\\t\\v\\x7f\\x80\\xff"
  )
})

test_that("'utf8_encode' can handle latin-1", {
  x <- "her \xa320"
  Encoding(x) <- "latin1"

  # https://github.com/r-lib/testthat/issues/1285
  with_ctype("C", {
    latin <- utf8_encode(x)
  })
  expect_equal(latin, "her \\u00a320")

  local_ctype("UTF-8")
  expect_equal(utf8_encode(x), "her \u00a320")
})


test_that("'utf8_encode' can handle bytes", {
  x <- c("fa\u00E7ile", "fa\xE7ile", "fa\xC3\xA7ile")
  Encoding(x) <- c("UTF-8", "UTF-8", "bytes")

  local_ctype("UTF-8")

  y <- c("fa\u00e7ile", "fa\\xe7ile", "fa\\xc3\\xa7ile")
  Encoding(y) <- c("UTF-8", "UTF-8", "bytes")

  expect_equal(utf8_encode(x), y)
})


test_that("'utf8_encode escapes controls in UTF-8 text", {
  local_ctype("UTF-8")

  x <- "\n\u2026"
  Encoding(x) <- "UTF-8"
  y <- "\\n\u2026"
  Encoding(y) <- "UTF-8"
  expect_equal(utf8_encode(x), y)
})


test_that("'utf8_encode' can quote", {
  x <- c("abcde", "x", "123", "\"", "'")
  expect_equal(utf8_encode(x, quote = FALSE), encodeString(x, quote = ""))
  expect_equal(utf8_encode(x, quote = TRUE), encodeString(x, quote = '"'))
})


test_that("'utf8_encode' ignores justify if width = 0", {
  x <- c("abcde", "x", "123", "\"", "'")
  expect_equal(
    utf8_encode(x, width = 0, justify = "none"),
    encodeString(x, width = 0, justify = "none")
  )
  expect_equal(
    utf8_encode(x, width = 0, justify = "left"),
    encodeString(x, width = 0, justify = "left")
  )
  expect_equal(
    utf8_encode(x, width = 0, justify = "centre"),
    encodeString(x, width = 0, justify = "centre")
  )
  expect_equal(
    utf8_encode(x, width = 0, justify = "right"),
    encodeString(x, width = 0, justify = "right")
  )
})


test_that("'utf8_encode' can justify", {
  x <- c("abcde", "x", "123", "\"", "'")
  expect_equal(
    utf8_encode(x, width = NULL, justify = "none"),
    encodeString(x, width = NULL, justify = "none")
  )
  expect_equal(
    utf8_encode(x, width = NULL, justify = "left"),
    encodeString(x, width = NULL, justify = "left")
  )
  expect_equal(
    utf8_encode(x, width = NULL, justify = "centre"),
    encodeString(x, width = NULL, justify = "centre")
  )
  expect_equal(
    utf8_encode(x, width = NULL, justify = "right"),
    encodeString(x, width = NULL, justify = "right")
  )
})


test_that("'utf8_encode' can justify and quote", {
  expect_equal(
    utf8_encode(c("1", "10", "100"), width = NULL, quote = TRUE),
    encodeString(c("1", "10", "100"), width = NULL, quote = '"')
  )
})