Mercurial > treepkg
comparison test/test_cmdexpand.py @ 44:a2ce575ce82b
add cmdexpand function and tests
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Mon, 19 Mar 2007 20:14:07 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
43:3c5ab7a65384 | 44:a2ce575ce82b |
---|---|
1 # Copyright (C) 2007 by Intevation GmbH | |
2 # Authors: | |
3 # Bernhard Herzog <bh@intevation.de> | |
4 # | |
5 # This program is free software under the GPL (>=v2) | |
6 # Read the file COPYING coming with the software for details. | |
7 | |
8 """Tests for the cmdexpand function""" | |
9 | |
10 import unittest | |
11 | |
12 from treepkg.cmdexpand import cmdexpand | |
13 | |
14 | |
15 class TestCMDExpand(unittest.TestCase): | |
16 | |
17 def test_words(self): | |
18 """Test cmdexpand with simple whitespace separated words""" | |
19 self.assertEquals(cmdexpand("abc defg xyz zy"), | |
20 ['abc', 'defg', 'xyz', 'zy']) | |
21 | |
22 def test_single_quoted(self): | |
23 """Test cmdexpand with some single quoted words""" | |
24 self.assertEquals(cmdexpand("abc 'defg xyz' zy"), | |
25 ['abc', 'defg xyz', 'zy']) | |
26 | |
27 def test_double_quoted(self): | |
28 """Test cmdexpand with some double quoted words""" | |
29 self.assertEquals(cmdexpand('abc "defg xyz" zy'), | |
30 ['abc', 'defg xyz', 'zy']) | |
31 | |
32 def test_word_expansion(self): | |
33 """Test cmdexpand with simple word expansion""" | |
34 self.assertEquals(cmdexpand('abc $foo ghi', foo="def"), | |
35 ['abc', 'def', 'ghi']) | |
36 self.assertEquals(cmdexpand('abc $foo ghi $bar', foo="def", bar="X"), | |
37 ['abc', 'def', 'ghi', 'X']) | |
38 | |
39 def test_word_expansion_braced_name(self): | |
40 """Test cmdexpand with word expansion using braced names""" | |
41 self.assertEquals(cmdexpand('abc ${foo} x${foo}y ghi', foo="def"), | |
42 ['abc', 'def', 'xdefy', 'ghi']) | |
43 | |
44 def test_word_expansion_non_byte_string(self): | |
45 """Test cmdexpand quoting of dollar signs""" | |
46 self.assertEquals(cmdexpand('abc $foo $bar ghi', foo=123, bar=u"1 2 3"), | |
47 ['abc', '123', '1 2 3', 'ghi']) | |
48 | |
49 def test_word_expansion_non_identifier(self): | |
50 """Test cmdexpand word expansion if dollar not followed by identifier""" | |
51 # $ immediately followed by a non-identifier character | |
52 self.assertRaises(ValueError, cmdexpand, 'abc $#foo bar', foo="def") | |
53 | |
54 def test_word_expansion_inside_words(self): | |
55 """Test cmdexpand word expansions in parts of words""" | |
56 self.assertEquals(cmdexpand("$foo x$bar y$baz.", | |
57 foo="abc", bar="yz", baz="zx"), | |
58 ["abc", "xyz", "yzx."]) | |
59 self.assertEquals(cmdexpand("$foo x$bar-$baz.", | |
60 foo="abc", bar="yz", baz="zx"), | |
61 ["abc", "xyz-zx."]) | |
62 | |
63 def test_case_sensitivity(self): | |
64 """Test case sensitivity of expansion keys""" | |
65 self.assertEquals(cmdexpand('abc $foo $Foo $FOO', | |
66 foo="def", Foo="DEF", FOO="Def"), | |
67 ['abc', 'def', 'DEF', 'Def']) | |
68 | |
69 def test_list_expansion(self): | |
70 """Test cmdexpand with list expansion""" | |
71 self.assertEquals(cmdexpand('abc @foo ghi', foo=["d", "e", "f"]), | |
72 ['abc', 'd', 'e', 'f', 'ghi']) | |
73 | |
74 def test_list_expansion_non_string(self): | |
75 """Test cmdexpand with list expansion""" | |
76 self.assertEquals(cmdexpand('abc @foo ghi', foo=[1, 1.0, None]), | |
77 ['abc', '1', '1.0', 'None', 'ghi']) | |
78 | |
79 def test_list_expansion_with_iterators(self): | |
80 """Test cmdexpand with list expansion using an iterator""" | |
81 self.assertEquals(cmdexpand('abc @foo ghi', | |
82 foo=(i**2 for i in range(3))), | |
83 ['abc', '0', '1', '4', 'ghi']) | |
84 | |
85 def test_list_expansion_non_identifier(self): | |
86 """Test cmdexpand with at-sign not followed by identifier""" | |
87 # @+identifier do not cover entire word | |
88 self.assertRaises(ValueError, cmdexpand, 'abc @foo, ghi', | |
89 foo=["d", "e", "f"]) | |
90 | |
91 # @ immediately followed by a non-identifier character | |
92 self.assertRaises(ValueError, cmdexpand, 'abc @. z') | |
93 | |
94 def test_list_expansion_inside_word(self): | |
95 """Test whether cmdexpand raises ValueError for at-signs inside words""" | |
96 self.assertRaises(ValueError, cmdexpand, 'abc x@foo ghi', | |
97 foo=["d", "e", "f"]) | |
98 | |
99 | |
100 def test_dollar_quoting(self): | |
101 """Test cmdexpand quoting of dollar signs""" | |
102 self.assertEquals(cmdexpand('abc $$foo $foo g$$hi', foo="def"), | |
103 ['abc', '$foo', 'def', 'g$hi']) | |
104 | |
105 def test_atsign_quoting(self): | |
106 """Test cmdexpand quoting of at-signs""" | |
107 self.assertEquals(cmdexpand('abc @foo $@foo g$@i', foo=["d", "e", "f"]), | |
108 ['abc', 'd', 'e', 'f', '@foo', 'g@i']) | |
109 | |
110 def test_interaction_with_shlex_quoting(self): | |
111 """Test cmdexpand's interaction with shlex's quoting""" | |
112 # Unlike unix-shells the expansion isn't influenced much by | |
113 # shell quoting as supported by shlex. | |
114 self.assertEquals(cmdexpand('abc "@foo" \'@foo\' ghi', | |
115 foo=["d", "e", "f"]), | |
116 ['abc', 'd', 'e', 'f', 'd', 'e', 'f', 'ghi']) | |
117 self.assertEquals(cmdexpand('abc "$foo" \'$foo\' ghi', foo="def"), | |
118 ['abc', 'def', 'def', 'ghi']) | |
119 self.assertEquals(cmdexpand('abc " $foo" \'a $foo\' ghi', foo="def"), | |
120 ['abc', ' def', 'a def', 'ghi']) | |
121 | |
122 | |
123 if __name__ == "__main__": | |
124 unittest.main() |