You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_usage.py 23KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.usage}, a command line option parsing library.
  5. """
  6. from __future__ import division, absolute_import
  7. from twisted.trial import unittest
  8. from twisted.python import usage
  9. class WellBehaved(usage.Options):
  10. optParameters = [['long', 'w', 'default', 'and a docstring'],
  11. ['another', 'n', 'no docstring'],
  12. ['longonly', None, 'noshort'],
  13. ['shortless', None, 'except',
  14. 'this one got docstring'],
  15. ]
  16. optFlags = [['aflag', 'f',
  17. """
  18. flagallicious docstringness for this here
  19. """],
  20. ['flout', 'o'],
  21. ]
  22. def opt_myflag(self):
  23. self.opts['myflag'] = "PONY!"
  24. def opt_myparam(self, value):
  25. self.opts['myparam'] = "%s WITH A PONY!" % (value,)
  26. class ParseCorrectnessTests(unittest.TestCase):
  27. """
  28. Test L{usage.Options.parseOptions} for correct values under
  29. good conditions.
  30. """
  31. def setUp(self):
  32. """
  33. Instantiate and parseOptions a well-behaved Options class.
  34. """
  35. self.niceArgV = ("--long Alpha -n Beta "
  36. "--shortless Gamma -f --myflag "
  37. "--myparam Tofu").split()
  38. self.nice = WellBehaved()
  39. self.nice.parseOptions(self.niceArgV)
  40. def test_checkParameters(self):
  41. """
  42. Parameters have correct values.
  43. """
  44. self.assertEqual(self.nice.opts['long'], "Alpha")
  45. self.assertEqual(self.nice.opts['another'], "Beta")
  46. self.assertEqual(self.nice.opts['longonly'], "noshort")
  47. self.assertEqual(self.nice.opts['shortless'], "Gamma")
  48. def test_checkFlags(self):
  49. """
  50. Flags have correct values.
  51. """
  52. self.assertEqual(self.nice.opts['aflag'], 1)
  53. self.assertEqual(self.nice.opts['flout'], 0)
  54. def test_checkCustoms(self):
  55. """
  56. Custom flags and parameters have correct values.
  57. """
  58. self.assertEqual(self.nice.opts['myflag'], "PONY!")
  59. self.assertEqual(self.nice.opts['myparam'], "Tofu WITH A PONY!")
  60. class TypedOptions(usage.Options):
  61. optParameters = [
  62. ['fooint', None, 392, 'Foo int', int],
  63. ['foofloat', None, 4.23, 'Foo float', float],
  64. ['eggint', None, None, 'Egg int without default', int],
  65. ['eggfloat', None, None, 'Egg float without default', float],
  66. ]
  67. def opt_under_score(self, value):
  68. """
  69. This option has an underscore in its name to exercise the _ to -
  70. translation.
  71. """
  72. self.underscoreValue = value
  73. opt_u = opt_under_score
  74. class TypedTests(unittest.TestCase):
  75. """
  76. Test L{usage.Options.parseOptions} for options with forced types.
  77. """
  78. def setUp(self):
  79. self.usage = TypedOptions()
  80. def test_defaultValues(self):
  81. """
  82. Default values are parsed.
  83. """
  84. argV = []
  85. self.usage.parseOptions(argV)
  86. self.assertEqual(self.usage.opts['fooint'], 392)
  87. self.assertIsInstance(self.usage.opts['fooint'], int)
  88. self.assertEqual(self.usage.opts['foofloat'], 4.23)
  89. self.assertIsInstance(self.usage.opts['foofloat'], float)
  90. self.assertIsNone(self.usage.opts['eggint'])
  91. self.assertIsNone(self.usage.opts['eggfloat'])
  92. def test_parsingValues(self):
  93. """
  94. int and float values are parsed.
  95. """
  96. argV = ("--fooint 912 --foofloat -823.1 "
  97. "--eggint 32 --eggfloat 21").split()
  98. self.usage.parseOptions(argV)
  99. self.assertEqual(self.usage.opts['fooint'], 912)
  100. self.assertIsInstance(self.usage.opts['fooint'], int)
  101. self.assertEqual(self.usage.opts['foofloat'], -823.1)
  102. self.assertIsInstance(self.usage.opts['foofloat'], float)
  103. self.assertEqual(self.usage.opts['eggint'], 32)
  104. self.assertIsInstance(self.usage.opts['eggint'], int)
  105. self.assertEqual(self.usage.opts['eggfloat'], 21.)
  106. self.assertIsInstance(self.usage.opts['eggfloat'], float)
  107. def test_underscoreOption(self):
  108. """
  109. A dash in an option name is translated to an underscore before being
  110. dispatched to a handler.
  111. """
  112. self.usage.parseOptions(['--under-score', 'foo'])
  113. self.assertEqual(self.usage.underscoreValue, 'foo')
  114. def test_underscoreOptionAlias(self):
  115. """
  116. An option name with a dash in it can have an alias.
  117. """
  118. self.usage.parseOptions(['-u', 'bar'])
  119. self.assertEqual(self.usage.underscoreValue, 'bar')
  120. def test_invalidValues(self):
  121. """
  122. Passing wrong values raises an error.
  123. """
  124. argV = "--fooint egg".split()
  125. self.assertRaises(usage.UsageError, self.usage.parseOptions, argV)
  126. class WrongTypedOptions(usage.Options):
  127. optParameters = [
  128. ['barwrong', None, None, 'Bar with wrong coerce', 'he']
  129. ]
  130. class WeirdCallableOptions(usage.Options):
  131. def _bar(value):
  132. raise RuntimeError("Ouch")
  133. def _foo(value):
  134. raise ValueError("Yay")
  135. optParameters = [
  136. ['barwrong', None, None, 'Bar with strange callable', _bar],
  137. ['foowrong', None, None, 'Foo with strange callable', _foo]
  138. ]
  139. class WrongTypedTests(unittest.TestCase):
  140. """
  141. Test L{usage.Options.parseOptions} for wrong coerce options.
  142. """
  143. def test_nonCallable(self):
  144. """
  145. Using a non-callable type fails.
  146. """
  147. us = WrongTypedOptions()
  148. argV = "--barwrong egg".split()
  149. self.assertRaises(TypeError, us.parseOptions, argV)
  150. def test_notCalledInDefault(self):
  151. """
  152. The coerce functions are not called if no values are provided.
  153. """
  154. us = WeirdCallableOptions()
  155. argV = []
  156. us.parseOptions(argV)
  157. def test_weirdCallable(self):
  158. """
  159. Errors raised by coerce functions are handled properly.
  160. """
  161. us = WeirdCallableOptions()
  162. argV = "--foowrong blah".split()
  163. # ValueError is swallowed as UsageError
  164. e = self.assertRaises(usage.UsageError, us.parseOptions, argV)
  165. self.assertEqual(str(e), "Parameter type enforcement failed: Yay")
  166. us = WeirdCallableOptions()
  167. argV = "--barwrong blah".split()
  168. # RuntimeError is not swallowed
  169. self.assertRaises(RuntimeError, us.parseOptions, argV)
  170. class OutputTests(unittest.TestCase):
  171. def test_uppercasing(self):
  172. """
  173. Error output case adjustment does not mangle options
  174. """
  175. opt = WellBehaved()
  176. e = self.assertRaises(usage.UsageError,
  177. opt.parseOptions, ['-Z'])
  178. self.assertEqual(str(e), 'option -Z not recognized')
  179. class InquisitionOptions(usage.Options):
  180. optFlags = [
  181. ('expect', 'e'),
  182. ]
  183. optParameters = [
  184. ('torture-device', 't',
  185. 'comfy-chair',
  186. 'set preferred torture device'),
  187. ]
  188. class HolyQuestOptions(usage.Options):
  189. optFlags = [('horseback', 'h',
  190. 'use a horse'),
  191. ('for-grail', 'g'),
  192. ]
  193. class SubCommandOptions(usage.Options):
  194. optFlags = [('europian-swallow', None,
  195. 'set default swallow type to Europian'),
  196. ]
  197. subCommands = [
  198. ('inquisition', 'inquest', InquisitionOptions,
  199. 'Perform an inquisition'),
  200. ('holyquest', 'quest', HolyQuestOptions,
  201. 'Embark upon a holy quest'),
  202. ]
  203. class SubCommandTests(unittest.TestCase):
  204. """
  205. Test L{usage.Options.parseOptions} for options with subcommands.
  206. """
  207. def test_simpleSubcommand(self):
  208. """
  209. A subcommand is recognized.
  210. """
  211. o = SubCommandOptions()
  212. o.parseOptions(['--europian-swallow', 'inquisition'])
  213. self.assertTrue(o['europian-swallow'])
  214. self.assertEqual(o.subCommand, 'inquisition')
  215. self.assertIsInstance(o.subOptions, InquisitionOptions)
  216. self.assertFalse(o.subOptions['expect'])
  217. self.assertEqual(o.subOptions['torture-device'], 'comfy-chair')
  218. def test_subcommandWithFlagsAndOptions(self):
  219. """
  220. Flags and options of a subcommand are assigned.
  221. """
  222. o = SubCommandOptions()
  223. o.parseOptions(['inquisition', '--expect', '--torture-device=feather'])
  224. self.assertFalse(o['europian-swallow'])
  225. self.assertEqual(o.subCommand, 'inquisition')
  226. self.assertIsInstance(o.subOptions, InquisitionOptions)
  227. self.assertTrue(o.subOptions['expect'])
  228. self.assertEqual(o.subOptions['torture-device'], 'feather')
  229. def test_subcommandAliasWithFlagsAndOptions(self):
  230. """
  231. Flags and options of a subcommand alias are assigned.
  232. """
  233. o = SubCommandOptions()
  234. o.parseOptions(['inquest', '--expect', '--torture-device=feather'])
  235. self.assertFalse(o['europian-swallow'])
  236. self.assertEqual(o.subCommand, 'inquisition')
  237. self.assertIsInstance(o.subOptions, InquisitionOptions)
  238. self.assertTrue(o.subOptions['expect'])
  239. self.assertEqual(o.subOptions['torture-device'], 'feather')
  240. def test_anotherSubcommandWithFlagsAndOptions(self):
  241. """
  242. Flags and options of another subcommand are assigned.
  243. """
  244. o = SubCommandOptions()
  245. o.parseOptions(['holyquest', '--for-grail'])
  246. self.assertFalse(o['europian-swallow'])
  247. self.assertEqual(o.subCommand, 'holyquest')
  248. self.assertIsInstance(o.subOptions, HolyQuestOptions)
  249. self.assertFalse(o.subOptions['horseback'])
  250. self.assertTrue(o.subOptions['for-grail'])
  251. def test_noSubcommand(self):
  252. """
  253. If no subcommand is specified and no default subcommand is assigned,
  254. a subcommand will not be implied.
  255. """
  256. o = SubCommandOptions()
  257. o.parseOptions(['--europian-swallow'])
  258. self.assertTrue(o['europian-swallow'])
  259. self.assertIsNone(o.subCommand)
  260. self.assertFalse(hasattr(o, 'subOptions'))
  261. def test_defaultSubcommand(self):
  262. """
  263. Flags and options in the default subcommand are assigned.
  264. """
  265. o = SubCommandOptions()
  266. o.defaultSubCommand = 'inquest'
  267. o.parseOptions(['--europian-swallow'])
  268. self.assertTrue(o['europian-swallow'])
  269. self.assertEqual(o.subCommand, 'inquisition')
  270. self.assertIsInstance(o.subOptions, InquisitionOptions)
  271. self.assertFalse(o.subOptions['expect'])
  272. self.assertEqual(o.subOptions['torture-device'], 'comfy-chair')
  273. def test_subCommandParseOptionsHasParent(self):
  274. """
  275. The parseOptions method from the Options object specified for the
  276. given subcommand is called.
  277. """
  278. class SubOpt(usage.Options):
  279. def parseOptions(self, *a, **kw):
  280. self.sawParent = self.parent
  281. usage.Options.parseOptions(self, *a, **kw)
  282. class Opt(usage.Options):
  283. subCommands = [
  284. ('foo', 'f', SubOpt, 'bar'),
  285. ]
  286. o = Opt()
  287. o.parseOptions(['foo'])
  288. self.assertTrue(hasattr(o.subOptions, 'sawParent'))
  289. self.assertEqual(o.subOptions.sawParent , o)
  290. def test_subCommandInTwoPlaces(self):
  291. """
  292. The .parent pointer is correct even when the same Options class is
  293. used twice.
  294. """
  295. class SubOpt(usage.Options):
  296. pass
  297. class OptFoo(usage.Options):
  298. subCommands = [
  299. ('foo', 'f', SubOpt, 'quux'),
  300. ]
  301. class OptBar(usage.Options):
  302. subCommands = [
  303. ('bar', 'b', SubOpt, 'quux'),
  304. ]
  305. oFoo = OptFoo()
  306. oFoo.parseOptions(['foo'])
  307. oBar=OptBar()
  308. oBar.parseOptions(['bar'])
  309. self.assertTrue(hasattr(oFoo.subOptions, 'parent'))
  310. self.assertTrue(hasattr(oBar.subOptions, 'parent'))
  311. self.failUnlessIdentical(oFoo.subOptions.parent, oFoo)
  312. self.failUnlessIdentical(oBar.subOptions.parent, oBar)
  313. class HelpStringTests(unittest.TestCase):
  314. """
  315. Test generated help strings.
  316. """
  317. def setUp(self):
  318. """
  319. Instantiate a well-behaved Options class.
  320. """
  321. self.niceArgV = ("--long Alpha -n Beta "
  322. "--shortless Gamma -f --myflag "
  323. "--myparam Tofu").split()
  324. self.nice = WellBehaved()
  325. def test_noGoBoom(self):
  326. """
  327. __str__ shouldn't go boom.
  328. """
  329. try:
  330. self.nice.__str__()
  331. except Exception as e:
  332. self.fail(e)
  333. def test_whitespaceStripFlagsAndParameters(self):
  334. """
  335. Extra whitespace in flag and parameters docs is stripped.
  336. """
  337. # We test this by making sure aflag and it's help string are on the
  338. # same line.
  339. lines = [s for s in str(self.nice).splitlines() if s.find("aflag")>=0]
  340. self.assertTrue(len(lines) > 0)
  341. self.assertTrue(lines[0].find("flagallicious") >= 0)
  342. class PortCoerceTests(unittest.TestCase):
  343. """
  344. Test the behavior of L{usage.portCoerce}.
  345. """
  346. def test_validCoerce(self):
  347. """
  348. Test the answers with valid input.
  349. """
  350. self.assertEqual(0, usage.portCoerce("0"))
  351. self.assertEqual(3210, usage.portCoerce("3210"))
  352. self.assertEqual(65535, usage.portCoerce("65535"))
  353. def test_errorCoerce(self):
  354. """
  355. Test error path.
  356. """
  357. self.assertRaises(ValueError, usage.portCoerce, "")
  358. self.assertRaises(ValueError, usage.portCoerce, "-21")
  359. self.assertRaises(ValueError, usage.portCoerce, "212189")
  360. self.assertRaises(ValueError, usage.portCoerce, "foo")
  361. class ZshCompleterTests(unittest.TestCase):
  362. """
  363. Test the behavior of the various L{twisted.usage.Completer} classes
  364. for producing output usable by zsh tab-completion system.
  365. """
  366. def test_completer(self):
  367. """
  368. Completer produces zsh shell-code that produces no completion matches.
  369. """
  370. c = usage.Completer()
  371. got = c._shellCode('some-option', usage._ZSH)
  372. self.assertEqual(got, ':some-option:')
  373. c = usage.Completer(descr='some action', repeat=True)
  374. got = c._shellCode('some-option', usage._ZSH)
  375. self.assertEqual(got, '*:some action:')
  376. def test_files(self):
  377. """
  378. CompleteFiles produces zsh shell-code that completes file names
  379. according to a glob.
  380. """
  381. c = usage.CompleteFiles()
  382. got = c._shellCode('some-option', usage._ZSH)
  383. self.assertEqual(got, ':some-option (*):_files -g "*"')
  384. c = usage.CompleteFiles('*.py')
  385. got = c._shellCode('some-option', usage._ZSH)
  386. self.assertEqual(got, ':some-option (*.py):_files -g "*.py"')
  387. c = usage.CompleteFiles('*.py', descr="some action", repeat=True)
  388. got = c._shellCode('some-option', usage._ZSH)
  389. self.assertEqual(got, '*:some action (*.py):_files -g "*.py"')
  390. def test_dirs(self):
  391. """
  392. CompleteDirs produces zsh shell-code that completes directory names.
  393. """
  394. c = usage.CompleteDirs()
  395. got = c._shellCode('some-option', usage._ZSH)
  396. self.assertEqual(got, ':some-option:_directories')
  397. c = usage.CompleteDirs(descr="some action", repeat=True)
  398. got = c._shellCode('some-option', usage._ZSH)
  399. self.assertEqual(got, '*:some action:_directories')
  400. def test_list(self):
  401. """
  402. CompleteList produces zsh shell-code that completes words from a fixed
  403. list of possibilities.
  404. """
  405. c = usage.CompleteList('ABC')
  406. got = c._shellCode('some-option', usage._ZSH)
  407. self.assertEqual(got, ':some-option:(A B C)')
  408. c = usage.CompleteList(['1', '2', '3'])
  409. got = c._shellCode('some-option', usage._ZSH)
  410. self.assertEqual(got, ':some-option:(1 2 3)')
  411. c = usage.CompleteList(['1', '2', '3'], descr='some action',
  412. repeat=True)
  413. got = c._shellCode('some-option', usage._ZSH)
  414. self.assertEqual(got, '*:some action:(1 2 3)')
  415. def test_multiList(self):
  416. """
  417. CompleteMultiList produces zsh shell-code that completes multiple
  418. comma-separated words from a fixed list of possibilities.
  419. """
  420. c = usage.CompleteMultiList('ABC')
  421. got = c._shellCode('some-option', usage._ZSH)
  422. self.assertEqual(got, ':some-option:_values -s , \'some-option\' A B C')
  423. c = usage.CompleteMultiList(['1','2','3'])
  424. got = c._shellCode('some-option', usage._ZSH)
  425. self.assertEqual(got, ':some-option:_values -s , \'some-option\' 1 2 3')
  426. c = usage.CompleteMultiList(['1','2','3'], descr='some action',
  427. repeat=True)
  428. got = c._shellCode('some-option', usage._ZSH)
  429. expected = '*:some action:_values -s , \'some action\' 1 2 3'
  430. self.assertEqual(got, expected)
  431. def test_usernames(self):
  432. """
  433. CompleteUsernames produces zsh shell-code that completes system
  434. usernames.
  435. """
  436. c = usage.CompleteUsernames()
  437. out = c._shellCode('some-option', usage._ZSH)
  438. self.assertEqual(out, ':some-option:_users')
  439. c = usage.CompleteUsernames(descr='some action', repeat=True)
  440. out = c._shellCode('some-option', usage._ZSH)
  441. self.assertEqual(out, '*:some action:_users')
  442. def test_groups(self):
  443. """
  444. CompleteGroups produces zsh shell-code that completes system group
  445. names.
  446. """
  447. c = usage.CompleteGroups()
  448. out = c._shellCode('some-option', usage._ZSH)
  449. self.assertEqual(out, ':group:_groups')
  450. c = usage.CompleteGroups(descr='some action', repeat=True)
  451. out = c._shellCode('some-option', usage._ZSH)
  452. self.assertEqual(out, '*:some action:_groups')
  453. def test_hostnames(self):
  454. """
  455. CompleteHostnames produces zsh shell-code that completes hostnames.
  456. """
  457. c = usage.CompleteHostnames()
  458. out = c._shellCode('some-option', usage._ZSH)
  459. self.assertEqual(out, ':some-option:_hosts')
  460. c = usage.CompleteHostnames(descr='some action', repeat=True)
  461. out = c._shellCode('some-option', usage._ZSH)
  462. self.assertEqual(out, '*:some action:_hosts')
  463. def test_userAtHost(self):
  464. """
  465. CompleteUserAtHost produces zsh shell-code that completes hostnames or
  466. a word of the form <username>@<hostname>.
  467. """
  468. c = usage.CompleteUserAtHost()
  469. out = c._shellCode('some-option', usage._ZSH)
  470. self.assertTrue(out.startswith(':host | user@host:'))
  471. c = usage.CompleteUserAtHost(descr='some action', repeat=True)
  472. out = c._shellCode('some-option', usage._ZSH)
  473. self.assertTrue(out.startswith('*:some action:'))
  474. def test_netInterfaces(self):
  475. """
  476. CompleteNetInterfaces produces zsh shell-code that completes system
  477. network interface names.
  478. """
  479. c = usage.CompleteNetInterfaces()
  480. out = c._shellCode('some-option', usage._ZSH)
  481. self.assertEqual(out, ':some-option:_net_interfaces')
  482. c = usage.CompleteNetInterfaces(descr='some action', repeat=True)
  483. out = c._shellCode('some-option', usage._ZSH)
  484. self.assertEqual(out, '*:some action:_net_interfaces')
  485. class CompleterNotImplementedTests(unittest.TestCase):
  486. """
  487. Using an unknown shell constant with the various Completer() classes
  488. should raise NotImplementedError
  489. """
  490. def test_unknownShell(self):
  491. """
  492. Using an unknown shellType should raise NotImplementedError
  493. """
  494. classes = [usage.Completer, usage.CompleteFiles,
  495. usage.CompleteDirs, usage.CompleteList,
  496. usage.CompleteMultiList, usage.CompleteUsernames,
  497. usage.CompleteGroups, usage.CompleteHostnames,
  498. usage.CompleteUserAtHost, usage.CompleteNetInterfaces]
  499. for cls in classes:
  500. try:
  501. action = cls()
  502. except:
  503. action = cls(None)
  504. self.assertRaises(NotImplementedError, action._shellCode,
  505. None, "bad_shell_type")
  506. class FlagFunctionTests(unittest.TestCase):
  507. """
  508. Tests for L{usage.flagFunction}.
  509. """
  510. class SomeClass(object):
  511. """
  512. Dummy class for L{usage.flagFunction} tests.
  513. """
  514. def oneArg(self, a):
  515. """
  516. A one argument method to be tested by L{usage.flagFunction}.
  517. @param a: a useless argument to satisfy the function's signature.
  518. """
  519. def noArg(self):
  520. """
  521. A no argument method to be tested by L{usage.flagFunction}.
  522. """
  523. def manyArgs(self, a, b, c):
  524. """
  525. A multiple arguments method to be tested by L{usage.flagFunction}.
  526. @param a: a useless argument to satisfy the function's signature.
  527. @param b: a useless argument to satisfy the function's signature.
  528. @param c: a useless argument to satisfy the function's signature.
  529. """
  530. def test_hasArg(self):
  531. """
  532. L{usage.flagFunction} returns C{False} if the method checked allows
  533. exactly one argument.
  534. """
  535. self.assertIs(False, usage.flagFunction(self.SomeClass().oneArg))
  536. def test_noArg(self):
  537. """
  538. L{usage.flagFunction} returns C{True} if the method checked allows
  539. exactly no argument.
  540. """
  541. self.assertIs(True, usage.flagFunction(self.SomeClass().noArg))
  542. def test_tooManyArguments(self):
  543. """
  544. L{usage.flagFunction} raises L{usage.UsageError} if the method checked
  545. allows more than one argument.
  546. """
  547. exc = self.assertRaises(
  548. usage.UsageError, usage.flagFunction, self.SomeClass().manyArgs)
  549. self.assertEqual("Invalid Option function for manyArgs", str(exc))
  550. def test_tooManyArgumentsAndSpecificErrorMessage(self):
  551. """
  552. L{usage.flagFunction} uses the given method name in the error message
  553. raised when the method allows too many arguments.
  554. """
  555. exc = self.assertRaises(
  556. usage.UsageError,
  557. usage.flagFunction, self.SomeClass().manyArgs, "flubuduf")
  558. self.assertEqual("Invalid Option function for flubuduf", str(exc))
  559. class OptionsInternalTests(unittest.TestCase):
  560. """
  561. Tests internal behavior of C{usage.Options}.
  562. """
  563. def test_optionsAliasesOrder(self):
  564. """
  565. Options which are synonyms to another option are aliases towards the
  566. longest option name.
  567. """
  568. class Opts(usage.Options):
  569. def opt_very_very_long(self):
  570. """
  571. This is an option method with a very long name, that is going to
  572. be aliased.
  573. """
  574. opt_short = opt_very_very_long
  575. opt_s = opt_very_very_long
  576. opts = Opts()
  577. self.assertEqual(
  578. dict.fromkeys(
  579. ["s", "short", "very-very-long"], "very-very-long"), {
  580. "s": opts.synonyms["s"],
  581. "short": opts.synonyms["short"],
  582. "very-very-long": opts.synonyms["very-very-long"],
  583. })