-
Notifications
You must be signed in to change notification settings - Fork 198
/
test-l_fmt_currency.R
235 lines (216 loc) · 9.48 KB
/
test-l_fmt_currency.R
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
test_that("The `fmt_currency()` function works correctly", {
# Create an input data frame four columns: two
# character-based and two that are numeric
data_tbl <-
data.frame(
char_1 = c("saturday", "sunday", "monday", "tuesday",
"wednesday", "thursday", "friday"),
char_2 = c("june", "july", "august", "september",
"october", "november", "december"),
num_1 = c(1836.23, 2763.39, 937.29, 643.00, 212.232, 0, -23.24),
num_2 = c(34, 74, 23, 93, 35, 76, 57),
stringsAsFactors = FALSE
)
# Create a `tbl_latex` object with `gt()` and the
# `data_tbl` dataset
tbl_latex <- gt(data_tbl)
# Format the `num_1` column as USD, use all other defaults;
# extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(columns = "num_1", currency = "USD") %>%
render_formats_test(context = "latex"))[["num_1"]],
c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$",
"$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$",
"$-\\text{\\$}23.24$")
)
# Format the `num_1` column as USD to 5 decimal places, use all
# other defaults; extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(columns = "num_1", currency = "USD", decimals = 5) %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1,836.23000$", "$\\text{\\$}2,763.39000$",
"$\\text{\\$}937.29000$", "$\\text{\\$}643.00000$",
"$\\text{\\$}212.23200$", "$\\text{\\$}0.00000$",
"$-\\text{\\$}23.24000$")
)
# Format the `num_1` column as USD, and don't include the subunits;
# use all other defaults; extract `output_df` and compare to
# expected values
expect_equal(
(tbl_latex %>%
fmt_currency(columns = "num_1", currency = "USD", use_subunits = FALSE) %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1,836$", "$\\text{\\$}2,763$", "$\\text{\\$}937$",
"$\\text{\\$}643$", "$\\text{\\$}212$", "$\\text{\\$}0$",
"$-\\text{\\$}23$")
)
# Format the `num_1` column as USD, don't use digit
# grouping separators, use all other defaults; extract `output_df`
# and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(columns = "num_1", currency = "USD", use_seps = FALSE) %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1836.23$", "$\\text{\\$}2763.39$", "$\\text{\\$}937.29$",
"$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$",
"$-\\text{\\$}23.24$")
)
# Format the `num_1` column to 2 decimal places, use a single space
# character as digit grouping separators, use all other defaults;
# extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(columns = "num_1", currency = "USD", sep_mark = " ") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1 836.23$", "$\\text{\\$}2 763.39$", "$\\text{\\$}937.29$",
"$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$",
"$-\\text{\\$}23.24$")
)
# Format the `num_1` column as USD, use a period for the digit grouping
# separators and a comma for the decimal mark, use all other defaults;
# extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD",
sep_mark = ".", dec_mark = ",") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1.836,23$", "$\\text{\\$}2.763,39$", "$\\text{\\$}937,29$",
"$\\text{\\$}643,00$", "$\\text{\\$}212,23$", "$\\text{\\$}0,00$",
"$-\\text{\\$}23,24$")
)
# Format the `num_1` column as USD, apply parentheses to all negative
# values, use all other defaults; extract `output_df` and compare
# to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD", accounting = TRUE) %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$",
"$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$",
"$(\\text{\\$}23.24)$")
)
# Format the `num_1` column as USD, apply parentheses to all negative
# values, use all other defaults; apply the default context, extract
# `output_df`, and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD", accounting = TRUE) %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$",
"$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$",
"$(\\text{\\$}23.24)$")
)
# Format the `num_1` column as USD to 4 decimal places, scale all values by
# 1/1000, use all other defaults; extract `output_df` and compare
# to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD",
decimals = 4, scale_by = 1/1000) %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1.8362$", "$\\text{\\$}2.7634$", "$\\text{\\$}0.9373$",
"$\\text{\\$}0.6430$", "$\\text{\\$}0.2122$", "$\\text{\\$}0.0000$",
"$-\\text{\\$}0.0232$")
)
# Format the `num_1` column as USD, prepend and append all values by 2
# different literals, use all other defaults; extract `output_df` and
# compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD", pattern = "a {x} b") %>%
render_formats_test("latex"))[["num_1"]],
c("a $\\text{\\$}1,836.23$ b", "a $\\text{\\$}2,763.39$ b",
"a $\\text{\\$}937.29$ b", "a $\\text{\\$}643.00$ b",
"a $\\text{\\$}212.23$ b", "a $\\text{\\$}0.00$ b",
"a $-\\text{\\$}23.24$ b")
)
# Format the `num_1` column as USD to 4 decimal places, scale all values
# by 1/1000 and append a `K` character to the resultant values, use
# all other defaults; extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD", decimals = 4,
scale_by = 1/1000, pattern = "{x}K") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1.8362$K", "$\\text{\\$}2.7634$K", "$\\text{\\$}0.9373$K",
"$\\text{\\$}0.6430$K", "$\\text{\\$}0.2122$K", "$\\text{\\$}0.0000$K",
"$-\\text{\\$}0.0232$K")
)
# Format the `num_1` column as USD, apply the `en_US` locale and use all
# other defaults; extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "USD", locale = "en_US") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{\\$}1,836.23$", "$\\text{\\$}2,763.39$", "$\\text{\\$}937.29$",
"$\\text{\\$}643.00$", "$\\text{\\$}212.23$", "$\\text{\\$}0.00$",
"$-\\text{\\$}23.24$")
)
# Format the `num_1` column as DKK, apply the `da_DK` locale and use all
# other defaults; extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "DKK", locale = "da_DK",
placement = "right", incl_space = TRUE) %>%
render_formats_test("latex"))[["num_1"]],
c("$1.836,23 \\text{kr.}$", "$2.763,39 \\text{kr.}$",
"$937,29 \\text{kr.}$", "$643,00 \\text{kr.}$", "$212,23 \\text{kr.}$",
"$0,00 \\text{kr.}$", "$-23,24 \\text{kr.}$")
)
# Format the `num_1` column as DKK, apply the `da_DK` locale and use all
# other defaults; extract `output_df`, and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "DKK", locale = "da_DK") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{kr.}1.836,23$", "$\\text{kr.}2.763,39$", "$\\text{kr.}937,29$",
"$\\text{kr.}643,00$", "$\\text{kr.}212,23$", "$\\text{kr.}0,00$",
"$-\\text{kr.}23,24$" )
)
# Format the `num_1` column as EUR, apply the `de_AT` locale and use all
# other defaults; extract `output_df` and compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "EUR", locale = "de_AT") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{EUR}1 836,23$", "$\\text{EUR}2 763,39$", "$\\text{EUR}937,29$",
"$\\text{EUR}643,00$", "$\\text{EUR}212,23$", "$\\text{EUR}0,00$",
"$-\\text{EUR}23,24$")
)
# Format the `num_1` column as EUR, apply the `de_AT` locale and use all
# other defaults; use the default context, extract `output_df`, and
# compare to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "EUR", locale = "de_AT") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{EUR}1 836,23$", "$\\text{EUR}2 763,39$", "$\\text{EUR}937,29$",
"$\\text{EUR}643,00$", "$\\text{EUR}212,23$", "$\\text{EUR}0,00$",
"$-\\text{EUR}23,24$")
)
# Format the `num_1` column to 2 decimal places, apply the `et_EE`
# locale and use all other defaults; extract `output_df` and compare
# to expected values
expect_equal(
(tbl_latex %>%
fmt_currency(
columns = "num_1", currency = "EUR", locale = "et_EE") %>%
render_formats_test("latex"))[["num_1"]],
c("$\\text{EUR}1 836,23$", "$\\text{EUR}2 763,39$", "$\\text{EUR}937,29$",
"$\\text{EUR}643,00$", "$\\text{EUR}212,23$", "$\\text{EUR}0,00$",
"$-\\text{EUR}23,24$")
)
})