1library(testit)
2
3assert('prose_index() works', {
4  x = c('a', '```', 'b', '```', 'c')
5  out = c(1L, 5L)
6  (prose_index(x) %==% out)
7
8  x = c('a', '````', '```r', '1+1', '```', '````', 'c')
9  out = c(1L, 7L)
10  (prose_index(x) %==% out)
11
12  x = c('a', '``', 'b', '``', 'c')
13  out = seq_along(x)
14  (prose_index(x) %==% out)
15
16  # a character vector of length zero
17  x = character()
18  out = integer()
19  (prose_index(x) %==% out)
20
21  # one backbrick
22  x = c('`', 'a', '`')
23  out = seq_along(x)
24  (prose_index(x) %==% out)
25
26  # two backbrick
27  x = c('``', 'a', '``')
28  out = seq_along(x)
29  (prose_index(x) %==% out)
30
31  # no code fences
32  x = c('a', 'b')
33  out = c(1L, 2L)
34  (prose_index(x) %==% out)
35
36  # two code fences
37  x = c('```', 'b', '```', '```', 'd', '```')
38  out = integer()
39  (prose_index(x) %==% out)
40
41  # code fences commented out
42  x = c('```', 'b', '```', '<!--```', 'd', '```-->')
43  (prose_index(x) %==% 4:6)
44
45  # if the code fences are not balanced
46  x = c('a', '```', 'b', '``', 'c')
47  out = seq_along(x)
48  (has_warning(prose_index(x)))
49  (prose_index(x) %==% out)
50})
51
52
53assert('protect_math() puts inline math expressions in backticks', {
54  (protect_math('$x$') %==% '`\\(x\\)`')
55  (protect_math('hi $x$ a') %==% 'hi `\\(x\\)` a')
56  (protect_math('$ a $') %==% '$ a $')
57  (protect_math(' $a$') %==% ' `\\(a\\)`')
58  (protect_math('$ x$') %==% '$ x$')
59  (protect_math('$x $') %==% '$x $')
60  (protect_math('b$a$') %==% 'b$a$')  # no space before $; unlikely to be math
61  (protect_math('`$a$`') %==% '`$a$`')
62  (protect_math('hi $x$9') %==% 'hi $x$9')
63  (protect_math('$500 $600') %==% '$500 $600')
64
65  (protect_math('$$a$$') %==% '`$$a$$`')
66  (protect_math('$$a$') %==% '$$a$')
67  (protect_math('hi $$\alpha$$') %==% 'hi `$$\alpha$$`')
68  (protect_math('hi $$\alpha $$') %==% 'hi $$\alpha $$')
69  (protect_math('hi $$ \alpha$$') %==% 'hi $$ \alpha$$')
70  (protect_math('hi $$\alpha$$ and $$ \alpha$$') %==% 'hi `$$\alpha$$` and $$ \alpha$$')
71})
72
73