File: test-xml_nodes_to_lints.R

package info (click to toggle)
r-cran-lintr 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,396 kB
  • sloc: sh: 13; xml: 10; makefile: 2
file content (160 lines) | stat: -rw-r--r-- 5,143 bytes parent folder | download
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("it creates basic lints", {
  code <- "before   %+%   after"
  tmpfile <- withr::local_tempfile(lines = code)
  expr <- get_source_expressions(tmpfile)$expressions[[2L]]
  xml <- expr$full_xml_parsed_content
  node <- xml2::xml_find_first(xml, "//SPECIAL")

  l <- xml_nodes_to_lints(
    xml = node,
    source_expression = expr,
    lint_message = "lint_msg",
    type = "warning"
  )

  expect_s3_class(l, "lint")
  expect_identical(l$filename, tmpfile)
  expect_identical(l$line_number, 1L)
  expect_identical(l$column_number, as.integer(xml2::xml_attr(node, "col1")))
  expect_identical(l$type, "warning")
  expect_identical(l$message, "lint_msg")
  expect_identical(l$line, code)
  expect_identical(l$ranges, list(as.integer(c(xml2::xml_attr(node, "col1"), xml2::xml_attr(node, "col2")))))

  # location XPaths work
  l_before_col <- xml_nodes_to_lints(
    xml = node,
    source_expression = expr,
    lint_message = "lint_msg",
    column_number_xpath = "number(./preceding-sibling::*[1]/@col2 + 1)",
    range_start_xpath = "number(./preceding-sibling::*[1]/@col1)",
    range_end_xpath = "number(./following-sibling::*[1]/@col2)"
  )
  expect_identical(l_before_col$column_number, nchar("before") + 1L)
  expect_identical(l_before_col$ranges, list(c(1L, nchar(code))))

  # Vectorization works
  ll <- xml_nodes_to_lints(
    xml = xml2::xml_find_all(xml, "//expr"),
    source_expression = expr,
    lint_message = "lint_msg"
  )
  expect_s3_class(ll, "lints")
  expect_length(ll, 3L)
  expect_s3_class(ll[[1L]], "lint")

  # Also for plain lists of xml_nodes, usually obtained by c(nodeset_a, nodeset_b)
  ll_unclassed <- xml_nodes_to_lints(
    xml = unclass(xml2::xml_find_all(xml, "//expr")),
    source_expression = expr,
    lint_message = "lint_msg"
  )
  expect_s3_class(ll_unclassed, "lints")
  expect_length(ll_unclassed, 3L)
  expect_s3_class(ll_unclassed[[1L]], "lint")

  # lint_message as a vector
  ll_msgvec <- xml_nodes_to_lints(
    xml = xml2::xml_find_all(xml, "//SYMBOL"),
    source_expression = expr,
    lint_message = letters[1L:2L]
  )
  expect_identical(ll_msgvec[[1L]]$message, "a")
  expect_identical(ll_msgvec[[2L]]$message, "b")
})

test_that("it handles multi-line lints correctly", {
  code <- c("before %+%", "  after")
  tmpfile <- withr::local_tempfile(lines = code)
  expr <- get_source_expressions(tmpfile)$expressions[[2L]]
  xml <- expr$full_xml_parsed_content
  node <- xml2::xml_find_first(xml, "/exprlist/expr")

  l <- xml_nodes_to_lints(
    xml = node,
    source_expression = expr,
    lint_message = "lint_msg"
  )

  expect_s3_class(l, "lint")
  expect_identical(l$filename, tmpfile)
  expect_identical(l$line_number, 1L)
  expect_identical(l$column_number, as.integer(xml2::xml_attr(node, "col1")))
  expect_identical(l$type, "style")
  expect_identical(l$message, "lint_msg")
  expect_identical(l$line, code[[1L]])
  expect_identical(l$ranges, list(as.integer(c(xml2::xml_attr(node, "col1"), nchar(code[1L])))))
})

test_that("it doesn't produce invalid lints", {
  # Part of #1427
  code <- "before   %+%   after"
  tmpfile <- withr::local_tempfile(lines = code)
  expr <- get_source_expressions(tmpfile)$expressions[[2L]]
  xml <- expr$full_xml_parsed_content
  node <- xml2::xml_find_first(xml, "//SPECIAL")

  # We can produce invalid location xpaths by requiring any non-existent node
  xp_column_number <- "number(./preceding-sibling::*[1]/@col2 + 1)"
  xp_range_start <- "number(./preceding-sibling::*[1]/@col1)"
  xp_range_end <- "number(./following-sibling::*[1]/@col2)"
  xp_invalid <- "number(./DOES-NOT-EXIST/@col1)"

  expect_warning(
    {
      lints1 <- xml_nodes_to_lints(
        xml = node,
        source_expression = expr,
        lint_message = "lint_msg",
        column_number_xpath = xp_column_number,
        range_start_xpath = xp_invalid,
        range_end_xpath = xp_range_end
      )
    },
    "Defaulting to start of line",
    fixed = TRUE
  )

  expect_identical(lints1[["column_number"]], nchar("before") + 1L)
  expect_identical(lints1[["ranges"]], list(c(1L, nchar(code))))

  expect_warning(
    {
      lints2 <- xml_nodes_to_lints(
        xml = node,
        source_expression = expr,
        lint_message = "lint_msg",
        column_number_xpath = xp_column_number,
        range_start_xpath = xp_range_start,
        range_end_xpath = xp_invalid
      )
    },
    "Defaulting to width 1",
    fixed = TRUE
  )

  expect_identical(lints2[["column_number"]], nchar("before") + 1L)
  expect_identical(lints2[["ranges"]], list(c(1L, 1L)))

  expect_warning(
    {
      lints3 <- xml_nodes_to_lints(
        xml = node,
        source_expression = expr,
        lint_message = "lint_msg",
        column_number_xpath = xp_invalid,
        range_start_xpath = xp_range_start,
        range_end_xpath = xp_range_end
      )
    },
    "Defaulting to start of range",
    fixed = TRUE
  )

  expect_identical(lints3[["column_number"]], 1L)
  expect_identical(lints3[["ranges"]], list(c(1L, nchar(code))))
})

test_that("erroneous input errors", {
  expect_error(xml_nodes_to_lints(1L), "Expected an <xml_nodeset>", fixed = TRUE)
})