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