bh@44: # Copyright (C) 2007 by Intevation GmbH
bh@44: # Authors:
bh@44: # Bernhard Herzog <bh@intevation.de>
bh@44: #
bh@44: # This program is free software under the GPL (>=v2)
bh@44: # Read the file COPYING coming with the software for details.
bh@44: 
bh@44: """Tests for the cmdexpand function"""
bh@44: 
bh@44: import unittest
bh@44: 
bh@44: from treepkg.cmdexpand import cmdexpand
bh@44: 
bh@44: 
bh@44: class TestCMDExpand(unittest.TestCase):
bh@44: 
bh@44:     def test_words(self):
bh@44:         """Test cmdexpand with simple whitespace separated words"""
bh@44:         self.assertEquals(cmdexpand("abc defg xyz zy"),
bh@44:                           ['abc', 'defg', 'xyz', 'zy'])
bh@44: 
bh@44:     def test_single_quoted(self):
bh@44:         """Test cmdexpand with some single quoted words"""
bh@44:         self.assertEquals(cmdexpand("abc 'defg xyz' zy"),
bh@44:                           ['abc', 'defg xyz', 'zy'])
bh@44: 
bh@44:     def test_double_quoted(self):
bh@44:         """Test cmdexpand with some double quoted words"""
bh@44:         self.assertEquals(cmdexpand('abc "defg  xyz" zy'),
bh@44:                           ['abc', 'defg  xyz', 'zy'])
bh@44: 
bh@44:     def test_word_expansion(self):
bh@44:         """Test cmdexpand with simple word expansion"""
bh@44:         self.assertEquals(cmdexpand('abc $foo ghi', foo="def"),
bh@44:                           ['abc', 'def', 'ghi'])
bh@44:         self.assertEquals(cmdexpand('abc $foo ghi $bar', foo="def", bar="X"),
bh@44:                           ['abc', 'def', 'ghi', 'X'])
bh@44: 
bh@44:     def test_word_expansion_braced_name(self):
bh@44:         """Test cmdexpand with word expansion using braced names"""
bh@44:         self.assertEquals(cmdexpand('abc ${foo} x${foo}y ghi', foo="def"),
bh@44:                           ['abc', 'def', 'xdefy', 'ghi'])
bh@44: 
bh@44:     def test_word_expansion_non_byte_string(self):
bh@44:         """Test cmdexpand quoting of dollar signs"""
bh@44:         self.assertEquals(cmdexpand('abc $foo $bar ghi', foo=123, bar=u"1 2 3"),
bh@44:                           ['abc', '123', '1 2 3', 'ghi'])
bh@44: 
bh@44:     def test_word_expansion_non_identifier(self):
bh@44:         """Test cmdexpand word expansion if dollar not followed by identifier"""
bh@44:         # $ immediately followed by a non-identifier character
bh@44:         self.assertRaises(ValueError, cmdexpand, 'abc $#foo bar', foo="def")
bh@44: 
bh@44:     def test_word_expansion_inside_words(self):
bh@44:         """Test cmdexpand word expansions in parts of words"""
bh@44:         self.assertEquals(cmdexpand("$foo x$bar y$baz.",
bh@44:                                     foo="abc", bar="yz", baz="zx"),
bh@44:                           ["abc", "xyz", "yzx."])
bh@44:         self.assertEquals(cmdexpand("$foo x$bar-$baz.",
bh@44:                                     foo="abc", bar="yz", baz="zx"),
bh@44:                           ["abc", "xyz-zx."])
bh@44: 
bh@44:     def test_case_sensitivity(self):
bh@44:         """Test case sensitivity of expansion keys"""
bh@44:         self.assertEquals(cmdexpand('abc $foo $Foo $FOO',
bh@44:                                     foo="def", Foo="DEF", FOO="Def"),
bh@44:                           ['abc', 'def', 'DEF', 'Def'])
bh@44: 
bh@44:     def test_list_expansion(self):
bh@44:         """Test cmdexpand with list expansion"""
bh@44:         self.assertEquals(cmdexpand('abc @foo ghi', foo=["d", "e", "f"]),
bh@44:                           ['abc', 'd', 'e', 'f', 'ghi'])
bh@44: 
bh@44:     def test_list_expansion_non_string(self):
bh@44:         """Test cmdexpand with list expansion"""
bh@44:         self.assertEquals(cmdexpand('abc @foo ghi', foo=[1, 1.0, None]),
bh@44:                           ['abc', '1', '1.0', 'None', 'ghi'])
bh@44: 
bh@44:     def test_list_expansion_with_iterators(self):
bh@44:         """Test cmdexpand with list expansion using an iterator"""
bh@44:         self.assertEquals(cmdexpand('abc @foo ghi',
bh@44:                                     foo=(i**2 for i in range(3))),
bh@44:                           ['abc', '0', '1', '4', 'ghi'])
bh@44: 
bh@44:     def test_list_expansion_non_identifier(self):
bh@44:         """Test cmdexpand with at-sign not followed by identifier"""
bh@44:         # @+identifier do not cover entire word
bh@44:         self.assertRaises(ValueError, cmdexpand, 'abc @foo, ghi',
bh@44:                           foo=["d", "e", "f"])
bh@44: 
bh@44:         # @ immediately followed by a non-identifier character
bh@44:         self.assertRaises(ValueError, cmdexpand, 'abc @. z')
bh@44: 
bh@44:     def test_list_expansion_inside_word(self):
bh@44:         """Test whether cmdexpand raises ValueError for at-signs inside words"""
bh@44:         self.assertRaises(ValueError, cmdexpand, 'abc x@foo ghi',
bh@44:                           foo=["d", "e", "f"])
bh@44: 
bh@44: 
bh@44:     def test_dollar_quoting(self):
bh@44:         """Test cmdexpand quoting of dollar signs"""
bh@44:         self.assertEquals(cmdexpand('abc $$foo $foo g$$hi', foo="def"),
bh@44:                           ['abc', '$foo', 'def', 'g$hi'])
bh@44: 
bh@44:     def test_atsign_quoting(self):
bh@44:         """Test cmdexpand quoting of at-signs"""
bh@44:         self.assertEquals(cmdexpand('abc @foo $@foo g$@i', foo=["d", "e", "f"]),
bh@44:                           ['abc', 'd', 'e', 'f', '@foo', 'g@i'])
bh@44: 
bh@44:     def test_interaction_with_shlex_quoting(self):
bh@44:         """Test cmdexpand's interaction with shlex's quoting"""
bh@44:         # Unlike unix-shells the expansion isn't influenced much by
bh@44:         # shell quoting as supported by shlex.
bh@44:         self.assertEquals(cmdexpand('abc "@foo" \'@foo\' ghi',
bh@44:                                     foo=["d", "e", "f"]),
bh@44:                           ['abc', 'd', 'e', 'f', 'd', 'e', 'f', 'ghi'])
bh@44:         self.assertEquals(cmdexpand('abc "$foo" \'$foo\' ghi', foo="def"),
bh@44:                           ['abc', 'def', 'def', 'ghi'])
bh@44:         self.assertEquals(cmdexpand('abc " $foo" \'a $foo\' ghi', foo="def"),
bh@44:                           ['abc', ' def', 'a def', 'ghi'])
bh@44: 
bh@44: 
bh@44: if __name__ == "__main__":
bh@44:     unittest.main()