File: test-hilight.R

package info (click to toggle)
r-cran-highr 0.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 176 kB
  • sloc: sh: 10; makefile: 7
file content (65 lines) | stat: -rw-r--r-- 2,108 bytes parent folder | download | duplicates (5)
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
library(testit)

assert(
  'hi_latex() works without prompts',
  hi_latex('1+1') == '\\hlnum{1}\\hlopt{+}\\hlnum{1}',
  hi_latex('  1 +    1') == '  \\hlnum{1} \\hlopt{+}    \\hlnum{1}',
  identical(hi_latex(c('  if (TRUE ){', 'foo && bar}')), c(
    '  \\hlkwa{if} \\hlstd{(}\\hlnum{TRUE} \\hlstd{)\\{}',
    '\\hlstd{foo} \\hlopt{&&} \\hlstd{bar\\}}'
  ))
)

assert(
  'hi_latex() works with prompts',
  hi_latex('1+1', prompt=TRUE) == '\\hlstd{> }\\hlnum{1}\\hlopt{+}\\hlnum{1}',
  identical(hi_latex(c('  if (TRUE ){', 'foo && bar}'), prompt = TRUE), paste(
    '\\hlstd{> }  \\hlkwa{if} \\hlstd{(}\\hlnum{TRUE} \\hlstd{)\\{}',
    '\\hlstd{+ }\\hlstd{foo} \\hlopt{&&} \\hlstd{bar\\}}', sep = '\n'
  ))
)

assert(
  'hi_latex() preserves blank lines',
  identical(hi_latex(c('1+1','','foo(x=3) # comm')), c(
    '\\hlnum{1}\\hlopt{+}\\hlnum{1}\n',
    '\\hlkwd{foo}\\hlstd{(}\\hlkwc{x}\\hlstd{=}\\hlnum{3}\\hlstd{)} \\hlcom{# comm}'
  ))
)

assert(
  'the fallback method recognizes comments, functions and strings',
  identical(hi_latex('1+1 # a comment', fallback = TRUE), '1+1 \\hlcom{# a comment}'),
  identical(hi_latex('paste("STRING", \'string\')', fallback = TRUE),
            '\\hlkwd{paste}(\\hlstr{"STRING"}, \\hlstr{\'string\'})')
)

assert(
  'hilight() works even if code only contains comments',
  identical(hi_latex('# only comments'), '\\hlcom{# only comments}')
)

assert(
  'the right arrow -> is preserved',
  identical(hi_latex('1 ->x # foo'), '\\hlnum{1} \\hlkwb{->}\\hlstd{x} \\hlcom{# foo}')
)

assert(
  'blank lines before/after code are preserved',
  hi_latex(c('', '', '1')) %==% c('\n', '\\hlnum{1}'),
  hi_latex(c('', '', '1', '')) %==% c('\n', '\\hlnum{1}', '')
)

# define one's own markup data frame
my_cmd = cmd_html
my_cmd['NUM_CONST', 1] = '<span class="my num">'

assert(
  'custom markup also works',
  hi_html('1+ 1') ==
    '<span class="hl num">1</span><span class="hl opt">+</span> <span class="hl num">1</span>',
  hi_html('1+ 1', markup = my_cmd) ==
    '<span class="my num">1</span><span class="hl opt">+</span> <span class="my num">1</span>'
)

rm(my_cmd)