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