Skip to content

Commit 1afb7ea

Browse files
turn on testthat linters, fix tests
1 parent 8e58a58 commit 1afb7ea

6 files changed

Lines changed: 199 additions & 119 deletions

File tree

.lintr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
linters: linters_with_defaults(
22
any_is_na_linter(),
3+
expect_comparison_linter(),
4+
expect_identical_linter(),
5+
expect_length_linter(),
6+
expect_named_linter(),
37
expect_not_linter(),
8+
expect_null_linter(),
9+
expect_s3_class_linter(),
10+
expect_s4_class_linter(),
11+
expect_true_false_linter(),
12+
expect_type_linter(),
413
implicit_integer_linter(allow_colon = TRUE),
514
infix_spaces_linter(exclude_operators = c("=", "*", "/")),
615
line_length_linter(120),

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Suggests:
1818
knitr,
1919
rmarkdown,
2020
ggplot2
21-
testthat/config/edition: 3
21+
Config/testthat/edition: 3
2222
VignetteBuilder: knitr
2323
Encoding: UTF-8
2424
RoxygenNote: 7.2.3

tests/testthat/test-decode.R

Lines changed: 136 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,147 @@ test_that('geohash decoder works', {
55
neum = 'srss0'
66

77
# test defaults on scalar input
8-
expect_equal(gh_decode(borobudur),
9-
list(latitude = -7.60528564453125,
10-
longitude = 110.1983642578125))
11-
expect_equal(gh_decode(c(borobudur, akarenga)),
12-
list(latitude = c(-7.60528564453125, 41.7672729492188),
13-
longitude = c(110.198364257812, 140.718383789062)))
8+
expect_identical(
9+
gh_decode(borobudur),
10+
list(latitude = -7.60528564453125, longitude = 110.1983642578125),
11+
tolerance = 1e-8
12+
)
13+
expect_identical(
14+
gh_decode(c(borobudur, akarenga)),
15+
list(latitude = c(-7.60528564453125, 41.7672729492188), longitude = c(110.198364257812, 140.718383789062)),
16+
tolerance = 1e-8
17+
)
1418
## precision can vary
15-
expect_equal(gh_decode(c(borobudur, neum)),
16-
list(latitude = c(-7.60528564453125, 42.91259765625),
17-
longitude = c(110.198364257812, 17.60009765625)))
19+
expect_identical(
20+
gh_decode(c(borobudur, neum)),
21+
list(latitude = c(-7.60528564453125, 42.91259765625), longitude = c(110.198364257812, 17.60009765625)),
22+
tolerance = 1e-8
23+
)
1824

1925
# input is factor, #17
2026
x = gl(4L, 20L, labels = c(borobudur, akarenga, kalakuta, neum))
21-
expect_equal(gh_decode(x), gh_decode(as.character(x)))
27+
expect_identical(gh_decode(x), gh_decode(as.character(x)))
2228

2329
# option: include_delta
24-
expect_equal(gh_decode(borobudur, include_delta = TRUE),
25-
list(latitude = -7.60528564453125,
26-
longitude = 110.198364257812,
27-
delta_latitude = 0.00274658203125,
28-
delta_longitude = 0.0054931640625))
30+
expect_identical(
31+
gh_decode(borobudur, include_delta = TRUE),
32+
list(
33+
latitude = -7.60528564453125,
34+
longitude = 110.198364257812,
35+
delta_latitude = 0.00274658203125,
36+
delta_longitude = 0.0054931640625
37+
),
38+
tolerance = 1e-8
39+
)
2940
## different precision, different delta
30-
expect_equal(gh_decode(c(borobudur, neum), include_delta = TRUE),
31-
list(latitude = c(-7.60528564453125, 42.91259765625),
32-
longitude = c(110.198364257812, 17.60009765625),
33-
delta_latitude = c(0.00274658203125, 0.02197265625),
34-
delta_longitude = c(0.0054931640625, 0.02197265625)))
41+
expect_identical(
42+
gh_decode(c(borobudur, neum), include_delta = TRUE),
43+
list(
44+
latitude = c(-7.60528564453125, 42.91259765625),
45+
longitude = c(110.198364257812, 17.60009765625),
46+
delta_latitude = c(0.00274658203125, 0.02197265625),
47+
delta_longitude = c(0.0054931640625, 0.02197265625)
48+
),
49+
tolerance = 1e-8
50+
)
3551

3652
# option: coord_loc
37-
expect_equal(gh_decode(borobudur, coord_loc = 'se'),
38-
list(latitude = -7.6080322265625, longitude = 110.203857421875))
39-
expect_equal(gh_decode(borobudur, coord_loc = 'southeast'),
40-
list(latitude = -7.6080322265625, longitude = 110.203857421875))
41-
expect_equal(gh_decode(borobudur, coord_loc = 's'),
42-
list(latitude = -7.6080322265625, longitude = 110.198364257812))
43-
expect_equal(gh_decode(borobudur, coord_loc = 'south'),
44-
list(latitude = -7.6080322265625, longitude = 110.198364257812))
45-
expect_equal(gh_decode(borobudur, coord_loc = 'sw'),
46-
list(latitude = -7.6080322265625, longitude = 110.19287109375))
47-
expect_equal(gh_decode(borobudur, coord_loc = 'southwest'),
48-
list(latitude = -7.6080322265625, longitude = 110.19287109375))
49-
expect_equal(gh_decode(borobudur, coord_loc = 'w'),
50-
list(latitude = -7.60528564453125, longitude = 110.19287109375))
51-
expect_equal(gh_decode(borobudur, coord_loc = 'west'),
52-
list(latitude = -7.60528564453125, longitude = 110.19287109375))
53-
expect_equal(gh_decode(borobudur, coord_loc = 'nw'),
54-
list(latitude = -7.6025390625, longitude = 110.19287109375))
55-
expect_equal(gh_decode(borobudur, coord_loc = 'northwest'),
56-
list(latitude = -7.6025390625, longitude = 110.19287109375))
57-
expect_equal(gh_decode(borobudur, coord_loc = 'n'),
58-
list(latitude = -7.6025390625, longitude = 110.198364257812))
59-
expect_equal(gh_decode(borobudur, coord_loc = 'north'),
60-
list(latitude = -7.6025390625, longitude = 110.198364257812))
61-
expect_equal(gh_decode(borobudur, coord_loc = 'ne'),
62-
list(latitude = -7.6025390625, longitude = 110.203857421875))
63-
expect_equal(gh_decode(borobudur, coord_loc = 'northeast'),
64-
list(latitude = -7.6025390625, longitude = 110.203857421875))
65-
expect_equal(gh_decode(borobudur, coord_loc = 'e'),
66-
list(latitude = -7.60528564453125, longitude = 110.203857421875))
67-
expect_equal(gh_decode(borobudur, coord_loc = 'east'),
68-
list(latitude = -7.60528564453125, longitude = 110.203857421875))
69-
53+
expect_identical(
54+
gh_decode(borobudur, coord_loc = 'se'),
55+
list(latitude = -7.6080322265625, longitude = 110.203857421875),
56+
tolerance = 1e-8
57+
)
58+
expect_identical(
59+
gh_decode(borobudur, coord_loc = 'southeast'),
60+
list(latitude = -7.6080322265625, longitude = 110.203857421875),
61+
tolerance = 1e-8
62+
)
63+
expect_identical(
64+
gh_decode(borobudur, coord_loc = 's'),
65+
list(latitude = -7.6080322265625, longitude = 110.198364257812),
66+
tolerance = 1e-8
67+
)
68+
expect_identical(
69+
gh_decode(borobudur, coord_loc = 'south'),
70+
list(latitude = -7.6080322265625, longitude = 110.198364257812),
71+
tolerance = 1e-8
72+
)
73+
expect_identical(
74+
gh_decode(borobudur, coord_loc = 'sw'),
75+
list(latitude = -7.6080322265625, longitude = 110.19287109375),
76+
tolerance = 1e-8
77+
)
78+
expect_identical(
79+
gh_decode(borobudur, coord_loc = 'southwest'),
80+
list(latitude = -7.6080322265625, longitude = 110.19287109375),
81+
tolerance = 1e-8
82+
)
83+
expect_identical(
84+
gh_decode(borobudur, coord_loc = 'w'),
85+
list(latitude = -7.60528564453125, longitude = 110.19287109375),
86+
tolerance = 1e-8
87+
)
88+
expect_identical(
89+
gh_decode(borobudur, coord_loc = 'west'),
90+
list(latitude = -7.60528564453125, longitude = 110.19287109375),
91+
tolerance = 1e-8
92+
)
93+
expect_identical(
94+
gh_decode(borobudur, coord_loc = 'nw'),
95+
list(latitude = -7.6025390625, longitude = 110.19287109375),
96+
tolerance = 1e-8
97+
)
98+
expect_identical(
99+
gh_decode(borobudur, coord_loc = 'northwest'),
100+
list(latitude = -7.6025390625, longitude = 110.19287109375),
101+
tolerance = 1e-8
102+
)
103+
expect_identical(
104+
gh_decode(borobudur, coord_loc = 'n'),
105+
list(latitude = -7.6025390625, longitude = 110.198364257812),
106+
tolerance = 1e-8
107+
)
108+
expect_identical(
109+
gh_decode(borobudur, coord_loc = 'north'),
110+
list(latitude = -7.6025390625, longitude = 110.198364257812),
111+
tolerance = 1e-8
112+
)
113+
expect_identical(
114+
gh_decode(borobudur, coord_loc = 'ne'),
115+
list(latitude = -7.6025390625, longitude = 110.203857421875),
116+
tolerance = 1e-8
117+
)
118+
expect_identical(
119+
gh_decode(borobudur, coord_loc = 'northeast'),
120+
list(latitude = -7.6025390625, longitude = 110.203857421875),
121+
tolerance = 1e-8
122+
)
123+
expect_identical(
124+
gh_decode(borobudur, coord_loc = 'e'),
125+
list(latitude = -7.60528564453125, longitude = 110.203857421875),
126+
tolerance = 1e-8
127+
)
128+
expect_identical(
129+
gh_decode(borobudur, coord_loc = 'east'),
130+
list(latitude = -7.60528564453125, longitude = 110.203857421875),
131+
tolerance = 1e-8
132+
)
70133
# be sure adjacent geohashes interlock
71-
expect_equal(lapply(c('nw', 'n', 'ne'),
134+
expect_identical(lapply(c('nw', 'n', 'ne'),
72135
function(l) gh_decode('m', coord_loc = l)),
73136
lapply(c('sw', 's', 'se'),
74137
function(l) gh_decode('t', coord_loc = l)))
75138

76-
expect_error(gh_decode(c(borobudur, neum), coord_loc = c('n', 's')),
77-
'Please provide only one value', fixed = TRUE)
78-
expect_error(gh_decode(akarenga, coord_loc = 'yo'),
79-
error = 'Unrecognized coordinate location')
139+
expect_error(
140+
gh_decode(c(borobudur, neum), coord_loc = c('n', 's')),
141+
'Please provide only one value',
142+
fixed = TRUE
143+
)
144+
expect_error(
145+
gh_decode(akarenga, coord_loc = 'yo'),
146+
'Unrecognized coordinate location',
147+
fixed = TRUE
148+
)
80149

81150
# invalid geohash characters:
82151
expect_error(gh_decode('a'), fixed = TRUE,
@@ -85,26 +154,28 @@ test_that('geohash decoder works', {
85154
"Invalid geohash; check 'a' at index 2.")
86155

87156
# missing input
88-
expect_equal(gh_decode(c(neum, NA_character_)),
157+
expect_identical(gh_decode(c(neum, NA_character_)),
89158
list(latitude = c(42.91259765625, NA),
90159
longitude = c(17.60009765625, NA)))
91-
expect_equal(gh_decode(c(neum, NA_character_), include_delta = TRUE),
160+
expect_identical(gh_decode(c(neum, NA_character_), include_delta = TRUE),
92161
list(latitude = c(42.91259765625, NA),
93162
longitude = c(17.60009765625, NA),
94163
delta_latitude = c(0.02197265625, NA),
95164
delta_longitude = c(0.02197265625, NA)))
96165

97166
# stress testing
98167
## empty input
99-
expect_equal(gh_decode(character(0L)),
168+
expect_identical(gh_decode(character(0L)),
100169
list(latitude = numeric(0L), longitude = numeric(0L)))
101170
## !nzchar input
102-
expect_equal(gh_decode(''),
171+
expect_identical(gh_decode(''),
103172
list(latitude = NA_real_, longitude = NA_real_))
104173
## long input [intr_length > 8 in geohash_decode_impl]
105-
expect_equal(gh_decode(strrep('1', 26L)),
106-
list(latitude = -84.1935483870968,
107-
longitude = -133.548387117729))
174+
expect_identical(
175+
gh_decode(strrep('1', 26L)),
176+
list(latitude = -84.1935483870968, longitude = -133.548387117729),
177+
tolerance = 1e-8
178+
)
108179

109180
## non-ASCII input #19
110181
## useBytes needed a bit strangely -- that the error returns with _any_

tests/testthat/test-encode.R

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ test_that('geohash encoder works', {
22
y = 0.1234
33
x = 5.6789
44
# test defaults on scalar input
5-
expect_equal(gh_encode(y, x), 's0h09n')
6-
expect_equal(gh_encode(-y, -x), '7zgzqc')
5+
expect_identical(gh_encode(y, x), 's0h09n')
6+
expect_identical(gh_encode(-y, -x), '7zgzqc')
77

88
# longitude wraps around every 360 degrees
9-
expect_equal(gh_encode(y, x), gh_encode(y, x - 360.0))
9+
expect_identical(gh_encode(y, x), gh_encode(y, x - 360.0))
1010

1111
# all level-1 centroids to be sure my manual logic for precision = 1 works
12-
expect_equal(gh_encode(c(-67.5, -67.5, -22.5, -22.5, -67.5, -67.5, -22.5,
12+
expect_identical(gh_encode(c(-67.5, -67.5, -22.5, -22.5, -67.5, -67.5, -22.5,
1313
-22.5, 22.5, 22.5, 67.5, 67.5, 22.5, 22.5, 67.5,
1414
67.5, -67.5, -67.5, -22.5, -22.5, -67.5, -67.5,
1515
-22.5, -22.5, 22.5, 22.5, 67.5, 67.5, 22.5, 22.5,
@@ -26,31 +26,31 @@ test_that('geohash encoder works', {
2626

2727
# geohash cells are _left closed, right open_: [x1, x2) x [y1, y2), see:
2828
# http://geohash.org/s000
29-
expect_equal(gh_encode(0.0, 0.0, 2L), 's0')
29+
expect_identical(gh_encode(0.0, 0.0, 2L), 's0')
3030

3131
# boundary cases
3232
# need to balloon eps so that adding .5 doesn't obliterate sig figs
3333
eps = 1000.0*.Machine$double.eps
34-
expect_equal(gh_encode(c(eps, eps, -eps, -eps,
34+
expect_identical(gh_encode(c(eps, eps, -eps, -eps,
3535
90.0 - eps, 90.0 - eps, eps - 90.0, eps - 90.0),
3636
c(eps, -eps, eps, -eps,
3737
eps - 180.0, 180.0 - eps, eps - 180.0, 180.0 - eps)),
3838
c('s00000', 'ebpbpb', 'kpbpbp', '7zzzzz',
3939
'bpbpbp', 'zzzzzz', '000000', 'pbpbpb'))
4040

4141
# test precision argument
42-
expect_equal(gh_encode(y, x, 12L), 's0h09nrnzgqv')
42+
expect_identical(gh_encode(y, x, 12L), 's0h09nrnzgqv')
4343
# maximum precision
4444
n = 25L
45-
expect_equal(gh_encode(y, x, n),
45+
expect_identical(gh_encode(y, x, n),
4646
substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n))
4747
# truncation beyond there
4848
expect_warning(out <- gh_encode(y, x, n + 5L),
4949
'Precision is limited', fixed = TRUE)
50-
expect_equal(out, substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n))
50+
expect_identical(out, substring('s0h09nrnzgqv8je0f4jpd0000', 1L, n))
5151

5252
# implicit integer truncation
53-
expect_equal(gh_encode(y, x, 1.04), 's')
53+
expect_identical(gh_encode(y, x, 1.04), 's')
5454

5555
# invalid precision
5656
expect_error(gh_encode(y, x, 0.0), 'Precision is measured', fixed = TRUE)
@@ -66,20 +66,20 @@ test_that('geohash encoder works', {
6666
'Inputs must be the same size', fixed = TRUE)
6767

6868
# semi-valid auto-corrected input -- 180 --> -180 by wrapping
69-
expect_equal(gh_encode(y, 180.0), '80008n')
70-
expect_equal(gh_encode(y, 293475908.0), 'db508w')
69+
expect_identical(gh_encode(y, 180.0), '80008n')
70+
expect_identical(gh_encode(y, 293475908.0), 'db508w')
7171

7272
# missing/infinite input
73-
expect_equal(gh_encode(c(y, NA), c(x, NA)), c('s0h09n', NA_character_))
74-
expect_equal(gh_encode(c(NaN, Inf, -Inf, 1:3), c(1:3, NaN, Inf, -Inf)),
73+
expect_identical(gh_encode(c(y, NA), c(x, NA)), c('s0h09n', NA_character_))
74+
expect_identical(gh_encode(c(NaN, Inf, -Inf, 1:3), c(1:3, NaN, Inf, -Inf)),
7575
rep(NA_character_, 6L))
7676

7777
# different branch for precision=1 of the above errors
7878
expect_error(gh_encode(100.0, x, 1L),
7979
'Invalid latitude at index 1', fixed = TRUE)
80-
expect_equal(gh_encode(y, 180.0, 1L), '8')
81-
expect_equal(gh_encode(NA, NA, 1L), NA_character_)
80+
expect_identical(gh_encode(y, 180.0, 1L), '8')
81+
expect_identical(gh_encode(NA, NA, 1L), NA_character_)
8282

8383
# stress testing
84-
expect_equal(gh_encode(numeric(), numeric()), character())
84+
expect_identical(gh_encode(numeric(), numeric()), character())
8585
})

0 commit comments

Comments
 (0)