OILS / doctools / help_gen_test.py View on Github | oils.pub

119 lines, 60 significant
1#!/usr/bin/env python2
2"""help_gen_test.py: Tests for help_gen.py."""
3from __future__ import print_function
4
5import os
6import unittest
7
8from doctools import help_gen # module under test
9
10
11class HelpGenTest(unittest.TestCase):
12
13 def testTopicRe(self):
14 # type: () -> None
15 CASES = [
16 ('hello ', True),
17 ('X hello ', True),
18 ('X hello \n', True),
19 ('X hello\n', True),
20 ('X hello << \n', True),
21 ('hello\n', True),
22 ('X hello\n', True),
23 ]
24 for s, matched in CASES:
25 m = help_gen.TOPIC_RE.match(s)
26 if m:
27 print('%r %s' % (s, m.groups()))
28 print()
29
30 self.assertEqual(matched, bool(m))
31
32 def testTopicHtml(self):
33 # type: () -> None
34 os.environ['OILS_VERSION'] = '0.7.pre5'
35
36 # Three spaces before
37 #
38 # ! for deprecated -- conflicts with ! bang though
39 # X for not implemented
40
41 # Do we need markup here?
42
43 CASES = [
44 # leading space required, at least 2 chars
45 (2, -1, ' aa bb\n'),
46 (3, -1, ' aa bb cc\n'),
47
48 # If end col > linkify_stop_col, then we don't
49 # end col is 1-based, like text editors
50 (3, 12, ' a1 b4 c7\n'), # 11 chars not including newline
51 (2, 11, ' a1 b4 c7\n'), # 11 chars not including newline
52 (3, -1, ' [Overview] hello there X not-impl\n'),
53
54 # Bug fix: 42 was linkified
55 (1, -1, ' int-literal 42 65_536 0xFF 0o755 0b10\n'),
56
57 # Bug fix: echo was linkified
58 (2, -1, ' expr-splice echo @[split(x)] \n'),
59
60 # Bug fix: u was linkified
61 (0, -1, " u'line\\n' b'byte \yff'\n"),
62
63 # Do we support 2 topics like this?
64 (6, -1,
65 ' fork forkwait Replace & and (), and takes a block\n'
66 ),
67 #(2, 20, ' fork forkwait Replace & and (), and takes a block\n'),
68 (6, -1, ' [Primitive] Bool Int Float Str Slice Range\n'
69 ),
70
71 # Trailing space
72 (4, -1, ' [Process State] X BASHPID X PPID UID EUID \n'),
73 (2, -1, ' [Lexing] comment # line-continuation \\\n'),
74 ]
75
76 for expected_topics, linkify_stop_col, line in CASES:
77 debug_out = []
78 r = help_gen.TopicHtmlRenderer('osh', debug_out, linkify_stop_col)
79
80 html = r.Render(line)
81 print(html)
82 record = debug_out[0]
83 print(record)
84 actual_topics = len(record['topics'])
85 print('%d topics' % actual_topics)
86
87 self.assertEqual(
88 expected_topics, actual_topics,
89 'Expected %d topics, got %d: %s' %
90 (expected_topics, actual_topics, line))
91
92 print()
93 print()
94
95 def testSplitIntoCards(self):
96 # type: () -> None
97 contents = """
98<h2>YSH Expression Language</h2>
99
100expr
101
102<h3>Literals</h2>
103
104oil literals
105
106<h4>oil-numbers</h4>
107
10842 1e100
109
110<h4>oil-array</h4>
111
112%(a b)
113"""
114 cards = help_gen.SplitIntoCards(['h2', 'h3', 'h4'], contents)
115 print(list(cards))
116
117
118if __name__ == '__main__':
119 unittest.main()