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