| 1 | #!/usr/bin/env python
 | 
| 2 | """
 | 
| 3 | osh2oil_test.py: Tests for osh2oil.py
 | 
| 4 | """
 | 
| 5 | 
 | 
| 6 | import unittest
 | 
| 7 | 
 | 
| 8 | from core import word
 | 
| 9 | from tools import osh2oil  # module under test
 | 
| 10 | 
 | 
| 11 | from _devbuild.gen import runtime_asdl
 | 
| 12 | 
 | 
| 13 | from osh.word_parse_test import _assertReadWord
 | 
| 14 | 
 | 
| 15 | word_style_e = runtime_asdl.word_style_e
 | 
| 16 | 
 | 
| 17 | 
 | 
| 18 | def assertStyle(test, expected_style, word_str):
 | 
| 19 |   w = _assertReadWord(test, word_str)
 | 
| 20 | 
 | 
| 21 |   new_word = word.TildeDetect(w)
 | 
| 22 |   if new_word is not None:
 | 
| 23 |     w = new_word
 | 
| 24 | 
 | 
| 25 |   actual = osh2oil._GetRhsStyle(w)
 | 
| 26 |   test.assertEqual(expected_style, actual)
 | 
| 27 | 
 | 
| 28 | 
 | 
| 29 | class FixTest(unittest.TestCase):
 | 
| 30 | 
 | 
| 31 |   def testGetRhsStyle(self):
 | 
| 32 |     w = assertStyle(self, word_style_e.SQ, 'foo')
 | 
| 33 |     w = assertStyle(self, word_style_e.SQ, "'hi'")
 | 
| 34 | 
 | 
| 35 |     w = assertStyle(self, word_style_e.Expr, '$var')
 | 
| 36 |     w = assertStyle(self, word_style_e.Expr, '${var}')
 | 
| 37 | 
 | 
| 38 |     w = assertStyle(self, word_style_e.Expr, ' "$var" ')
 | 
| 39 |     w = assertStyle(self, word_style_e.Expr, ' "${var}" ')
 | 
| 40 | 
 | 
| 41 |     w = assertStyle(self, word_style_e.Unquoted, ' $((1+2)) ')
 | 
| 42 |     w = assertStyle(self, word_style_e.Unquoted, ' $(echo hi) ')
 | 
| 43 | 
 | 
| 44 |     w = assertStyle(self, word_style_e.Unquoted, ' "$((1+2))" ')
 | 
| 45 |     w = assertStyle(self, word_style_e.Unquoted, ' "$(echo hi)" ')
 | 
| 46 | 
 | 
| 47 |     w = assertStyle(self, word_style_e.DQ, ' $src/file ')
 | 
| 48 |     w = assertStyle(self, word_style_e.DQ, ' ${src}/file ')
 | 
| 49 | 
 | 
| 50 |     w = assertStyle(self, word_style_e.DQ, ' "$src/file" ')
 | 
| 51 |     w = assertStyle(self, word_style_e.DQ, ' "${src}/file" ')
 | 
| 52 | 
 | 
| 53 |     w = assertStyle(self, word_style_e.DQ, ' $((1+2))$(echo hi) ')
 | 
| 54 | 
 | 
| 55 |     # PROBLEM: How do you express it quoted?
 | 
| 56 |     # "$~/src"
 | 
| 57 |     # "$~bob/src"
 | 
| 58 |     # Hm I guess this is OK ?
 | 
| 59 | 
 | 
| 60 |     # I think you need concatenation operator!    In expression mode
 | 
| 61 | 
 | 
| 62 |     # x = tilde() + "/src"
 | 
| 63 |     # x = tilde('andy') + "/src"  # ~/src/
 | 
| 64 | 
 | 
| 65 |     # x = "$HOME/src"  # but this isn't the same -- $HOME might not be set!
 | 
| 66 | 
 | 
| 67 |     # x = "$tilde()/src"
 | 
| 68 |     # x = "$tilde('andy')/src"  # Does this make sense?  A little ugly.
 | 
| 69 | 
 | 
| 70 | 
 | 
| 71 | 
 | 
| 72 |     w = assertStyle(self, word_style_e.DQ, ' ~/src ')
 | 
| 73 |     w = assertStyle(self, word_style_e.DQ, ' ~bob/foo ')
 | 
| 74 |     w = assertStyle(self, word_style_e.SQ, 'notleading~')
 | 
| 75 | 
 | 
| 76 |     # These tildes are quoted
 | 
| 77 |     w = assertStyle(self, word_style_e.SQ, ' "~/src" ')
 | 
| 78 |     w = assertStyle(self, word_style_e.SQ, ' "~bob/foo" ')
 | 
| 79 | 
 | 
| 80 | 
 | 
| 81 | if __name__ == '__main__':
 | 
| 82 |   unittest.main()
 |