1tag_df <- function(tag, start, end, argend = NULL) {
2  df <- data.frame(
3    stringsAsFactors = FALSE,
4    tag = tag, start = start, end = end
5  )
6  if (!is.null(argend)) df$argend <- argend
7  df
8}
9
10test_that("find_all_tag_names", {
11
12  text <- "blah blah \\mytag blah blah"
13  expect_equal(
14    find_all_tag_names(text),
15    tag_df("\\mytag", 11, 16)
16  )
17})
18
19test_that("find_all_rd_tags", {
20
21  cases <- list(
22    ## No tags
23    list("", character(), numeric(), numeric(), numeric()),
24    list("nothing to see here",
25         character(), numeric(), numeric(), numeric()),
26    list("\nstill\nnothing\n",
27         character(), numeric(), numeric(), numeric()),
28
29    ## One tag
30    list("blah blah \\mytag blah blah", "\\mytag", 11, 16, 16),
31    list("blah blah \\mytag{arg1} blah blah",
32         "\\mytag", 11, 16, 22),
33    list("blah blah \\mytag{arg1}{arg2} blah blah",
34         "\\mytag", 11, 16, 28),
35    list("blah\\mytag", "\\mytag", 5, 10, 10),
36    list("blah \\mytag", "\\mytag", 6, 11, 11),
37    list("blah\\mytag{arg}", "\\mytag", 5, 10, 15),
38    list("\\mytag hoohoo", "\\mytag", 1, 6, 6),
39    list("\\mytag", "\\mytag", 1, 6, 6),
40    list("\\mytag{arg}", "\\mytag", 1, 6, 11),
41    list("blah \\mytag\nblah blah", "\\mytag", 6, 11, 11),
42
43    ## Multiple tags
44    list("blah \\tag1 \\tag2{arg} blah", c("\\tag1", "\\tag2"),
45         c(6, 12), c(10, 16), c(10, 21)),
46    list("blah \\tag1{ \\tag2{arg} } blah", c("\\tag1", "\\tag2"),
47         c(6, 13), c(10, 17), c(24, 22)),
48    list("blah \\tag1{\n\\tag2{arg}\n} blah", c("\\tag1", "\\tag2"),
49         c(6, 13), c(10, 17), c(24, 22))
50  )
51
52  for (case in cases) {
53    expect_equal(
54      find_all_rd_tags(case[[1]]),
55      do.call(tag_df, case[-1]),
56      info = case[[1]]
57    )
58  }
59
60})
61
62test_that("find_fragile_rd_tags", {
63
64  fragile <- c("\\frag", "\\frag1", "\\frag2")
65
66  cases <- list(
67    list("This is \\frag{here}, \\this{arg} not", "\\frag"),
68    list("Embedded \\frag{ into \\frag1{arg} plus }", "\\frag"),
69    list(
70      "blah \\cmd{ \\frag{arg} \\frag{arg} } \\frag2 blah",
71      c("\\frag", "\\frag", "\\frag2")
72    )
73  )
74
75  for (case in cases) {
76    expect_equal(
77      find_fragile_rd_tags(case[[1]], fragile)$tag,
78      case[[2]],
79      info = case[[1]]
80    )
81  }
82
83})
84
85
86test_that("str_sub_same", {
87
88  expect_equal(
89    str_sub_same(
90      "123456789ab",
91      data.frame(start = c(1,6), end = c(2,10), argend = c(2,10)),
92      "xxx"
93    ),
94    "xxx-1-345xxx-2-b"
95  )
96
97  expect_equal(
98    str_sub_same(
99      "123456789ab",
100      data.frame(start = c(1,8), end = c(7,10), argend = c(7,10)),
101      "xxx"
102    ),
103    "xxx-1-xxx-2-b",
104  )
105
106  expect_equal(
107    str_sub_same(
108      "123456789ab",
109      data.frame(start = numeric(), end = numeric(),
110                 argend = numeric()),
111      "xxx"
112    ),
113    "123456789ab"
114  )
115
116})
117