Skip to content

Commit d440d45

Browse files
authored
Lints to tests (#430)
* lints * Use expect_true / false length * expect_named * depend on testthat 3.2.0
1 parent 6f4612f commit d440d45

13 files changed

+46
-51
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Authors@R: c(
1212
Description: Work with XML files using a simple, consistent interface.
1313
Built on top of the 'libxml2' C library.
1414
License: MIT + file LICENSE
15-
URL: https://xml2.r-lib.org/, https://github.com/r-lib/xml2
15+
URL: https://xml2.r-lib.org, https://github.com/r-lib/xml2
1616
BugReports: https://github.com/r-lib/xml2/issues
1717
Depends:
1818
R (>= 3.6.0)
@@ -28,7 +28,7 @@ Suggests:
2828
magrittr,
2929
mockery,
3030
rmarkdown,
31-
testthat (>= 3.0.0)
31+
testthat (>= 3.2.0)
3232
VignetteBuilder:
3333
knitr
3434
Config/Needs/website: tidyverse/tidytemplate

R/format.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#' @export
22
format.xml_node <- function(x, ...) {
33
attrs <- xml_attrs(x)
4-
paste("<",
4+
paste0("<",
55
paste(
66
c(
77
xml_name(x),
88
format_attributes(attrs)
99
),
1010
collapse = " "
1111
),
12-
">",
13-
sep = ""
12+
">"
1413
)
1514
}
1615

R/paths.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ path_to_connection <- function(path, check = c("file", "dir")) {
66
}
77

88
if (is_url(path)) {
9-
if (requireNamespace("curl", quietly = TRUE)) {
9+
if (is_installed("curl")) {
1010
return(curl::curl(path))
1111
} else {
1212
return(url(path))

R/utils.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ quote_str <- function(x, quote = "\"") {
2222
paste0(quote, x, quote)
2323
}
2424

25-
is_installed <- function(pkg) {
26-
requireNamespace(pkg, quietly = TRUE)
27-
}
28-
2925
# Format the C bitwise flags for display in Rd. The input object is a named
3026
# integer vector with a 'descriptions' character vector attribute that
3127
# corresponds to each flag.

man/xml2-package.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-xml_attr.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ test_that("qualified names returned when ns given", {
3535
bars <- xml_children(xml_children(x))
3636
attr <- xml_attrs(bars, ns)
3737

38-
expect_equal(names(attr[[1]]), "f:id")
39-
expect_equal(names(attr[[2]]), "g:id")
38+
expect_named(attr[[1]], "f:id")
39+
expect_named(attr[[2]], "g:id")
4040
})
4141

4242

@@ -150,21 +150,21 @@ test_that("xml_attr<- removes namespaces if desired", {
150150
x <- read_xml("<a xmlns = 'tag:foo'><b/></a>")
151151

152152
# cannot find //b with a default namespace
153-
expect_equal(length(xml_find_all(x, "//b")), 0)
153+
expect_length(xml_find_all(x, "//b"), 0)
154154

155155
# unless we specify it explicitly
156-
expect_equal(length(xml_find_all(x, "//b")), 0)
157-
expect_equal(length(xml_find_all(x, "//d1:b", xml_ns(x))), 1)
156+
expect_length(xml_find_all(x, "//b"), 0)
157+
expect_length(xml_find_all(x, "//d1:b", xml_ns(x)), 1)
158158

159159
# but can find it once we remove the namespace
160160
xml_attr(x, "xmlns") <- NULL
161-
expect_equal(length(xml_find_all(x, "//b")), 1)
161+
expect_length(xml_find_all(x, "//b"), 1)
162162

163163
# and add the old namespace back
164164
xml_attr(x, "xmlns") <- "tag:foo"
165165
expect_equal(xml_attr(x, "xmlns"), "tag:foo")
166-
expect_equal(length(xml_find_all(x, "//b")), 0)
167-
expect_equal(length(xml_find_all(x, "//d1:b", xml_ns(x))), 1)
166+
expect_length(xml_find_all(x, "//b"), 0)
167+
expect_length(xml_find_all(x, "//d1:b", xml_ns(x)), 1)
168168

169169
expect_equal(xml_attr(x, "xmlns"), "tag:foo")
170170
})
@@ -173,22 +173,22 @@ test_that("xml_attr<- removes prefixed namespaces if desired", {
173173
x <- read_xml("<a xmlns:pre = 'tag:foo'><pre:b/></a>")
174174

175175
# cannot find //b with a prefixed namespace
176-
expect_equal(length(xml_find_all(x, "//b")), 0)
176+
expect_length(xml_find_all(x, "//b"), 0)
177177

178178
# unless we specify it explicitly
179-
expect_equal(length(xml_find_all(x, "//b")), 0)
180-
expect_equal(length(xml_find_all(x, "//pre:b", xml_ns(x))), 1)
179+
expect_length(xml_find_all(x, "//b"), 0)
180+
expect_length(xml_find_all(x, "//pre:b", xml_ns(x)), 1)
181181

182182
# but can find it once we remove the namespace
183183
xml_attr(x, "xmlns:pre") <- NULL
184-
expect_equal(length(xml_find_all(x, "//b")), 1)
184+
expect_length(xml_find_all(x, "//b"), 1)
185185

186186
# and add the old namespace back
187187
xml_attr(x, "xmlns:pre") <- "tag:foo"
188188
xml_set_namespace(xml_children(x)[[1]], "pre")
189189
expect_equal(xml_attr(x, "xmlns:pre"), "tag:foo")
190-
expect_equal(length(xml_find_all(x, "//b")), 0)
191-
expect_equal(length(xml_find_all(x, "//pre:b", xml_ns(x))), 1)
190+
expect_length(xml_find_all(x, "//b"), 0)
191+
expect_length(xml_find_all(x, "//pre:b", xml_ns(x)), 1)
192192

193193
expect_equal(xml_attr(x, "xmlns:pre"), "tag:foo")
194194
})
@@ -213,8 +213,8 @@ test_that("xml_set_attr works identically to xml_attr<-", {
213213

214214
# No errors for xml_missing
215215
mss <- xml_find_first(bx, "./c")
216-
expect_error(xml_attr(mss[[2]], "b") <- "blah", NA)
217-
expect_error(xml_set_attr(mss[[2]], "b", "blah"), NA)
216+
expect_no_error(xml_attr(mss[[2]], "b") <- "blah")
217+
expect_no_error(xml_set_attr(mss[[2]], "b", "blah"))
218218
})
219219

220220
test_that("xml_set_attrs works identically to xml_attrs<-", {
@@ -237,8 +237,8 @@ test_that("xml_set_attrs works identically to xml_attrs<-", {
237237

238238
# No errors for xml_missing
239239
mss <- xml_find_first(bx, "./c")
240-
expect_error(xml_attrs(mss[[2]]) <- c("b" = "blah"), NA)
241-
expect_error(xml_set_attrs(mss[[2]], c("b" = "blah")), NA)
240+
expect_no_error(xml_attrs(mss[[2]]) <- c("b" = "blah"))
241+
expect_no_error(xml_set_attrs(mss[[2]], c("b" = "blah")))
242242
})
243243

244244
test_that("xml_set_attr can set the same namespace multiple times", {

tests/testthat/test-xml_find.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ test_that("xml_find_first does not deduplicate identical results", {
1616
y <- xml_find_all(x, ".//y")
1717
z <- xml_find_first(y, "..")
1818
expect_s3_class(z, "xml_nodeset")
19-
expect_equal(length(z), 2)
19+
expect_length(z, 2)
2020
})
2121

2222
# Find all ---------------------------------------------------------------------
2323

2424
test_that("unqualified names don't look in default ns", {
2525
x <- read_xml(test_path("ns-multiple-default.xml"))
2626

27-
expect_equal(length(xml_find_all(x, "//bar")), 0)
27+
expect_length(xml_find_all(x, "//bar"), 0)
2828
})
2929

3030
test_that("qualified names matches to namespace", {
3131
x <- read_xml(test_path("ns-multiple-default.xml"))
3232
ns <- xml_ns(x)
3333

34-
expect_equal(length(xml_find_all(x, "//d1:bar", ns)), 1)
35-
expect_equal(length(xml_find_all(x, "//d2:bar", ns)), 1)
34+
expect_length(xml_find_all(x, "//d1:bar", ns), 1)
35+
expect_length(xml_find_all(x, "//d2:bar", ns), 1)
3636
})
3737

3838
test_that("warning if unknown namespace", {
@@ -45,7 +45,7 @@ test_that("warning if unknown namespace", {
4545

4646
test_that("no matches returns empty nodeset", {
4747
x <- read_xml("<foo><bar /></foo>")
48-
expect_equal(length(xml_find_all(x, "//baz")), 0)
48+
expect_length(xml_find_all(x, "//baz"), 0)
4949
})
5050

5151
test_that("xml_find_all returns nodeset or list of nodesets based on flatten", {
@@ -145,15 +145,15 @@ test_that("xml_find_lgl errors with non logical results", {
145145
test_that("xml_find_lgl returns a logical result", {
146146
x <- read_xml("<x><y>1</y><y/></x>")
147147

148-
expect_equal(xml_find_lgl(x, "1=1"), TRUE)
148+
expect_true(xml_find_lgl(x, "1=1"))
149149

150-
expect_equal(xml_find_lgl(x, "1!=1"), FALSE)
150+
expect_false(xml_find_lgl(x, "1!=1"))
151151

152-
expect_equal(xml_find_lgl(x, "true()=true()"), TRUE)
152+
expect_true(xml_find_lgl(x, "true()=true()"))
153153

154-
expect_equal(xml_find_lgl(x, "true()=false()"), FALSE)
154+
expect_false(xml_find_lgl(x, "true()=false()"))
155155

156-
expect_equal(xml_find_lgl(x, "'test'='test'"), TRUE)
156+
expect_true(xml_find_lgl(x, "'test'='test'"))
157157
})
158158

159159
test_that("Searches with empty inputs retain type stability", {

tests/testthat/test-xml_missing.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ b <- xml_find_first(para, ".//b")
88
mss <- b[[3]]
99

1010
test_that("xml_find returns nodes of class 'xml_missing' for missing nodes", {
11-
expect_equal(length(b), 3L)
12-
expect_equal(vapply(b, length, integer(1)), c(2L, 2L, 0L))
11+
expect_length(b, 3L)
12+
expect_equal(lengths(b), c(2L, 2L, 0L))
1313
expect_s3_class(mss, "xml_missing")
1414
})
1515

tests/testthat/test-xml_namespaces.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ test_that("unique prefix-url combo unchanged", {
2525

2626
test_that("all prefixs kept", {
2727
x <- c(blah = "http://blah.com", rah = "http://blah.com")
28-
expect_equal(names(.Call(unique_ns, x)), c("blah", "rah"))
28+
expect_named(.Call(unique_ns, x), c("blah", "rah"))
2929
})
3030

3131
test_that("multiple default namespaces can be stripped", {
3232
x <- read_xml(test_path("ns-multiple-default.xml"))
3333
ns <- unclass(xml_ns(x))
3434
expect_equal(ns, c(d1 = "http://foo.com", d2 = "http://bar.com"))
35-
expect_equal(length(xml_find_all(x, "//bar")), 0)
35+
expect_length(xml_find_all(x, "//bar"), 0)
3636

3737
xml_ns_strip(x)
3838
ns <- unclass(xml_ns(x))
3939

4040
expect_equal(unname(ns), character())
41-
expect_equal(length(xml_find_all(x, "//bar")), 2)
41+
expect_length(xml_find_all(x, "//bar"), 2)
4242
})

tests/testthat/test-xml_nodeset.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test_that("methods work on empty nodesets", {
2222
expect_identical(as_list(empty), list())
2323
expect_identical(nodeset_apply(empty, identical), empty)
2424
expect_output(print(empty), "\\{xml_nodeset \\(0\\)\\}")
25-
expect_output(tree_structure(empty), NA)
25+
expect_silent(tree_structure(empty))
2626

2727
xml_add_child(test, "test")
2828
expect_identical(test, empty)
@@ -64,7 +64,7 @@ test_that("methods work on empty nodesets", {
6464
expect_identical(test, empty)
6565

6666
expect_identical(xml_siblings(empty), empty)
67-
expect_output(xml_structure(empty), NA)
67+
expect_silent(xml_structure(empty))
6868

6969
expect_identical(xml_text(empty), character())
7070
expect_identical(xml_url(empty), character())

tests/testthat/test-xml_parse.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_that("read_xml errors with an empty document", {
2020

2121
test_that("read_html correctly parses malformed document", {
2222
lego <- read_html(test_path("lego.html.bz2"))
23-
expect_equal(length(xml_find_all(lego, ".//p")), 39)
23+
expect_length(xml_find_all(lego, ".//p"), 39)
2424
})
2525

2626
test_that("parse_options errors when given an invalid option", {
@@ -68,7 +68,7 @@ test_that("read_xml works with httr response objects", {
6868

6969
expect_s3_class(x, "xml_document")
7070

71-
expect_equal(length(xml_find_all(x, "//slide")), 2)
71+
expect_length(xml_find_all(x, "//slide"), 2)
7272
})
7373

7474
test_that("read_xml and read_html fail for bad status codes", {

tests/testthat/test-xml_serialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ x <- read_xml("<a>
22
<b><c>123</c></b>
33
<b><c>456</c></b>
44
</a>")
5-
5+
66
test_that("xml_serialize and xml_unserialize work with xml_document input", {
77
out <- xml_unserialize(xml_serialize(x, NULL))
88
expect_identical(as.character(x), as.character(out))

tests/testthat/test-xml_write.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ test_that("write_xml returns invisibly", {
134134

135135
res <- withVisible(write_xml(x, tf))
136136

137-
expect_equal(res$value, NULL)
138-
expect_equal(res$visible, FALSE)
137+
expect_null(res$value)
138+
expect_false(res$visible)
139139
})

0 commit comments

Comments
 (0)