| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2006 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 """ | |
| 5 Test cases for twisted.python.zshcomp | |
| 6 """ | |
| 7 | |
| 8 import os, os.path | |
| 9 from cStringIO import StringIO | |
| 10 | |
| 11 from twisted.trial import unittest | |
| 12 from twisted.python import zshcomp, usage | |
| 13 | |
| 14 class ZshcompTestCase(unittest.TestCase): | |
| 15 """ | |
| 16 Tests for the zsh completion function builder in twisted/python/zshcomp.py | |
| 17 """ | |
| 18 def test_buildAll(self): | |
| 19 """ | |
| 20 Build all the completion functions for twisted commands - no errors | |
| 21 should be raised | |
| 22 """ | |
| 23 dirname = self.mktemp() | |
| 24 os.mkdir(dirname) | |
| 25 skippedCmds = [x[0] for x in zshcomp.makeCompFunctionFiles(dirname)] | |
| 26 | |
| 27 # verify a zsh function was created for each twisted command | |
| 28 for info in zshcomp.generateFor: | |
| 29 if info[0] in skippedCmds: | |
| 30 continue | |
| 31 funcPath = os.path.join(dirname, '_' + info[0]) | |
| 32 self.failUnless(os.path.exists(funcPath)) | |
| 33 | |
| 34 def test_accumulateMetadata(self): | |
| 35 """ | |
| 36 Test that the zsh_* variables you can place on Option classes gets | |
| 37 picked up correctly | |
| 38 """ | |
| 39 opts = TestOptions2() | |
| 40 ag = zshcomp.ArgumentsGenerator('dummy_cmd', opts, 'dummy_value') | |
| 41 | |
| 42 altArgDescr = TestOptions.zsh_altArgDescr.copy() | |
| 43 altArgDescr.update(TestOptions2.zsh_altArgDescr) | |
| 44 | |
| 45 actionDescr = TestOptions.zsh_actionDescr.copy() | |
| 46 actionDescr.update(TestOptions2.zsh_actionDescr) | |
| 47 | |
| 48 self.failUnlessEquals(ag.altArgDescr, altArgDescr) | |
| 49 self.failUnlessEquals(ag.actionDescr, actionDescr) | |
| 50 self.failUnlessEquals(ag.multiUse, TestOptions.zsh_multiUse) | |
| 51 self.failUnlessEquals(ag.mutuallyExclusive, | |
| 52 TestOptions.zsh_mutuallyExclusive) | |
| 53 self.failUnlessEquals(ag.actions, TestOptions.zsh_actions) | |
| 54 self.failUnlessEquals(ag.extras, TestOptions.zsh_extras) | |
| 55 | |
| 56 def test_accumulateAdditionalOptions(self): | |
| 57 """ | |
| 58 Test that we pick up options that are only defined by having an | |
| 59 appropriately named method on your Options class, | |
| 60 e.g. def opt_foo(self, foo) | |
| 61 """ | |
| 62 opts = TestOptions2() | |
| 63 ag = zshcomp.ArgumentsGenerator('dummy_cmd', opts, 'dummy_value') | |
| 64 | |
| 65 self.failUnless('nocrash' in ag.optFlags_d and \ | |
| 66 'nocrash' in ag.optAll_d) | |
| 67 self.failUnless('difficulty' in ag.optParams_d and \ | |
| 68 'difficulty' in ag.optAll_d) | |
| 69 | |
| 70 def test_verifyZshNames(self): | |
| 71 """ | |
| 72 Test that using a parameter/flag name that doesn't exist | |
| 73 will raise an error | |
| 74 """ | |
| 75 class TmpOptions(TestOptions2): | |
| 76 zsh_actions = {'detaill' : 'foo'} # Note typo of detail | |
| 77 | |
| 78 opts = TmpOptions() | |
| 79 self.failUnlessRaises(ValueError, zshcomp.ArgumentsGenerator, | |
| 80 'dummy_cmd', opts, 'dummy_value') | |
| 81 | |
| 82 def test_zshCode(self): | |
| 83 """ | |
| 84 Generate a completion function, and test the textual output | |
| 85 against a known correct output | |
| 86 """ | |
| 87 cmd_name = 'testprog' | |
| 88 opts = CodeTestOptions() | |
| 89 f = StringIO() | |
| 90 b = zshcomp.Builder(cmd_name, opts, f) | |
| 91 b.write() | |
| 92 f.reset() | |
| 93 self.failUnlessEquals(f.read(), testOutput1) | |
| 94 | |
| 95 def test_skipBuild(self): | |
| 96 """ | |
| 97 Test that makeCompFunctionFiles skips building for commands whos | |
| 98 script module cannot be imported | |
| 99 """ | |
| 100 generateFor = [('test_cmd', 'no.way.your.gonna.import.this', 'Foo')] | |
| 101 skips = zshcomp.makeCompFunctionFiles('out_dir', generateFor, {}) | |
| 102 # no exceptions should be raised. hooray. | |
| 103 self.failUnlessEqual(len(skips), 1) | |
| 104 self.failUnlessEqual(len(skips[0]), 2) | |
| 105 self.failUnlessEqual(skips[0][0], 'test_cmd') | |
| 106 self.failUnless(isinstance(skips[0][1], ImportError)) | |
| 107 self.flushLoggedErrors(self, ImportError) | |
| 108 | |
| 109 class TestOptions(usage.Options): | |
| 110 """ | |
| 111 Command-line options for an imaginary game | |
| 112 """ | |
| 113 optFlags = [['fokker', 'f', | |
| 114 'Select the Fokker Dr.I as your dogfighter aircraft'], | |
| 115 ['albatros', 'a', | |
| 116 'Select the Albatros D-III as your dogfighter aircraft'], | |
| 117 ['spad', 's', | |
| 118 'Select the SPAD S.VII as your dogfighter aircraft'], | |
| 119 ['bristol', 'b', | |
| 120 'Select the Bristol Scout as your dogfighter aircraft'], | |
| 121 ['physics', 'p', | |
| 122 'Enable secret Twisted physics engine'], | |
| 123 ['jam', 'j', | |
| 124 'Enable a small chance that your machine guns will jam!'], | |
| 125 ['verbose', 'v', | |
| 126 'Verbose logging (may be specified more than once)'], | |
| 127 ] | |
| 128 | |
| 129 optParameters = [['pilot-name', None, "What's your name, Ace?", | |
| 130 'Manfred von Richthofen'], | |
| 131 ['detail', 'd', | |
| 132 'Select the level of rendering detail (1-5)', '3'], | |
| 133 ] | |
| 134 | |
| 135 | |
| 136 zsh_altArgDescr = {'physics' : 'Twisted-Physics', | |
| 137 'detail' : 'Rendering detail level'} | |
| 138 zsh_actionDescr = {'detail' : 'Pick your detail'} | |
| 139 zsh_multiUse = ['verbose'] | |
| 140 zsh_mutuallyExclusive = [['fokker', 'albatros', 'spad', 'bristol']] | |
| 141 zsh_actions = {'detail' : '(1 2 3 4 5)'} | |
| 142 zsh_extras = [':saved game file to load:_files'] | |
| 143 | |
| 144 class TestOptions2(TestOptions): | |
| 145 """ | |
| 146 Extend the options and zsh metadata provided by TestOptions. zshcomp must | |
| 147 accumulate options and metadata from all classes in the hiearchy so this | |
| 148 is important for testing | |
| 149 """ | |
| 150 optFlags = [['no-stalls', None, | |
| 151 'Turn off the ability to stall your aircraft']] | |
| 152 optParameters = [['reality-level', None, | |
| 153 'Select the level of physics reality (1-5)', '5']] | |
| 154 | |
| 155 zsh_altArgDescr = {'no-stalls' : 'Can\'t stall your plane'} | |
| 156 zsh_actionDescr = {'reality-level' : 'Physics reality level'} | |
| 157 | |
| 158 def opt_nocrash(self): | |
| 159 """Select that you can't crash your plane""" | |
| 160 | |
| 161 def opt_difficulty(self, difficulty): | |
| 162 """How tough are you? (1-10)""" | |
| 163 | |
| 164 def _accuracyAction(): | |
| 165 return '(1 2 3)' | |
| 166 | |
| 167 class CodeTestOptions(usage.Options): | |
| 168 """ | |
| 169 Command-line options for an imaginary program | |
| 170 """ | |
| 171 optFlags = [['color', 'c', 'Turn on color output'], | |
| 172 ['gray', 'g', 'Turn on gray-scale output'], | |
| 173 ['verbose', 'v', | |
| 174 'Verbose logging (may be specified more than once)'], | |
| 175 ] | |
| 176 | |
| 177 optParameters = [['optimization', None, | |
| 178 'Select the level of optimization (1-5)', '5'], | |
| 179 ['accuracy', 'a', | |
| 180 'Select the level of accuracy (1-3)', '3'], | |
| 181 ] | |
| 182 | |
| 183 | |
| 184 zsh_altArgDescr = {'color' : 'Color on', | |
| 185 'optimization' : 'Optimization level'} | |
| 186 zsh_actionDescr = {'optimization' : 'Optimization?', | |
| 187 'accuracy' : 'Accuracy?'} | |
| 188 zsh_multiUse = ['verbose'] | |
| 189 zsh_mutuallyExclusive = [['color', 'gray']] | |
| 190 zsh_actions = {'optimization' : '(1 2 3 4 5)', | |
| 191 'accuracy' : _accuracyAction} | |
| 192 zsh_extras = [':output file:_files'] | |
| 193 | |
| 194 testOutput1 = """#compdef testprog | |
| 195 _arguments -s -A "-*" \\ | |
| 196 ':output file:_files' \\ | |
| 197 '(--accuracy)-a[3]:Accuracy?:(1 2 3)' \\ | |
| 198 '(-a)--accuracy=[3]:Accuracy?:(1 2 3)' \\ | |
| 199 '(--gray -g --color)-c[Color on]' \\ | |
| 200 '(--gray -g -c)--color[Color on]' \\ | |
| 201 '(--color -c --gray)-g[Turn on gray-scale output]' \\ | |
| 202 '(--color -c -g)--gray[Turn on gray-scale output]' \\ | |
| 203 '--help[Display this help and exit.]' \\ | |
| 204 '--optimization=[Optimization level]:Optimization?:(1 2 3 4 5)' \\ | |
| 205 '*-v[Verbose logging (may be specified more than once)]' \\ | |
| 206 '*--verbose[Verbose logging (may be specified more than once)]' \\ | |
| 207 '--version[version]' \\ | |
| 208 && return 0 | |
| 209 """ | |
| 210 | |
| OLD | NEW |