Skip to content

Commit edbae6b

Browse files
adding simple test for highlighted region via with_mock.
1 parent a6f1b4a commit edbae6b

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

R/addins.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ style_region <- function(text, context) {
5050
lines_to_style <- text[ind_to_style]
5151
last <- length(lines_to_style)
5252
lines_to_style[1] <- substring(lines_to_style[1], context$start[2])
53-
start_if_on_same_line <- ifelse(context$start[1] == context$end[1], context$start[2] - 1, 0)
53+
start_if_on_same_line <- ifelse(context$start[1] == context$end[1], context$start[2], 0)
5454
lines_to_style[last] <- substring(
5555
lines_to_style[last],
5656
1,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fjkdsfa 2jy+wj/ 1+1 <?+d
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fjkdsfa 2jy+wj/ 1 + 1<?+d

tests/testthat/test-public_api.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,37 @@ test_that("styler can style file", {
1717
})
1818

1919
# style_active_file() must be tested manually.
20+
test_that("...", {
21+
before_styling <- utf8::read_lines_enc(paste0(base, "/xyzaddin/addin_region-in.R"))
22+
reference <- utf8::read_lines_enc(paste0(base, "/xyzaddin/addin_region-out.R"))
23+
result <- expect_error(
24+
testthat::with_mock(
25+
find_active_context = function() {
26+
context <- structure(list(
27+
id = "C533793B",
28+
path = paste0(base, "/xyzaddin/addin_region-in.R"),
29+
contents = c("fjkdsfa 2jy+wj/ 1+1 <?+d", ""),
30+
selection = structure(list(structure(list(range = structure(list(
31+
start = structure(c(1, 17), .Names = c("row", "column"), class = "document_position"),
32+
end = structure(c(1, 21), .Names = c("row", "column"),
33+
class = "document_position")), .Names = c("start", "end"),
34+
class = "document_range"), text = "1+1 "), .Names = c("range", "text"))),
35+
.Names = "", class = "document_selection")),
36+
.Names = c("id", "path", "contents", "selection"), class = "document_context"
37+
)
38+
path <- context$path
39+
start <- context$selection[[1]]$range$start
40+
end <- context$selection[[1]]$range$end
41+
if (all(start == end)) return()
42+
if (end[2] == 1) {
43+
end[1] <- end[1] - 1
44+
end[2] <- 1000000L # because of range constraint in substr()
45+
}
46+
list(start = start, end = end, path = path)
47+
},
48+
style_active_region()
49+
), NA)
50+
after_styling <- utf8::read_lines_enc(paste0(base, "/xyzaddin/addin_region-in.R"))
51+
expect_equivalent(after_styling, reference)
52+
utf8::write_lines_enc(before_styling, paste0(base, "/xyzaddin/addin_region-in.R"))
53+
})

0 commit comments

Comments
 (0)