1 | #!/usr/bin/env python2
|
2 | """
|
3 | prompt_test.py: Tests for prompt.py
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import unittest
|
8 |
|
9 | from _devbuild.gen.value_asdl import value
|
10 | from core import state
|
11 | from core import test_lib
|
12 | from frontend import match
|
13 | from osh import prompt # module under test
|
14 |
|
15 |
|
16 | class PromptTest(unittest.TestCase):
|
17 |
|
18 | def setUp(self):
|
19 | arena = test_lib.MakeArena('<ui_test.py>')
|
20 | mem = state.Mem('', [], arena, [], {})
|
21 | parse_ctx = test_lib.InitParseContext()
|
22 | self.p = prompt.Evaluator('osh', '0.0.0', parse_ctx, mem)
|
23 | # note: this has a separate 'mem' object
|
24 | self.p.word_ev = test_lib.InitWordEvaluator()
|
25 |
|
26 | def testEvaluator(self):
|
27 | # Regression for caching bug!
|
28 | self.assertEqual('foo', self.p.EvalPrompt(value.Str('foo')))
|
29 | self.assertEqual('foo', self.p.EvalPrompt(value.Str('foo')))
|
30 |
|
31 | def testNoEscapes(self):
|
32 | for prompt_str in ["> ", "osh>", "[[]][[]][][]]][["]:
|
33 | self.assertEqual(self.p.EvalPrompt(value.Str(prompt_str)),
|
34 | prompt_str)
|
35 |
|
36 | def testValidEscapes(self):
|
37 | for prompt_str in [
|
38 | "\[\033[01;34m\]user\[\033[00m\] >", r"\[\]\[\]\[\]",
|
39 | r"\[\] hi \[hi\] \[\] hello"
|
40 | ]:
|
41 | self.assertEqual(
|
42 | self.p.EvalPrompt(value.Str(prompt_str)),
|
43 | prompt_str.replace(r"\[", "\x01").replace(r"\]", "\x02"))
|
44 |
|
45 | def testInvalidEscapes(self):
|
46 | for invalid_prompt in [
|
47 | r"\[\[",
|
48 | r"\[\]\[\]\]",
|
49 | r"\]\]",
|
50 | r"almost valid \]",
|
51 | r"\[almost valid",
|
52 | r"\]\[", # goes negative!
|
53 | ]:
|
54 | tokens = match.Ps1Tokens(invalid_prompt)
|
55 | self.assertEqual(r'<Error: Unbalanced \[ and \]> ',
|
56 | self.p._ReplaceBackslashCodes(tokens))
|
57 |
|
58 |
|
59 | if __name__ == '__main__':
|
60 | unittest.main()
|