def testParseTree(self): COMMAND = squires.CommandDefinition OPTION = squires.OptionDefinition cmd = COMMAND('foo', help='bar', method=squires) self.assertEqual(('foo', ), cmd.args) self.assertEqual({'help': 'bar', 'method': squires}, cmd.kwargs) root = squires.Command('one') tree = {COMMAND('two', help='Number two', method=squires): {}} squires.ParseTree(root, tree) self.assertEqual('Number two', root['two'].help) root = squires.Command('<root>') tree = { COMMAND('one', help='Number one', method=squires): { OPTION('fast', boolean=True, helptext='Quickly'): {}, OPTION('slow', boolean=True, helptext='Slowly'): {}, }, COMMAND('two', help='Number two', method=squires): { COMMAND('three', help='Number three', method=squires): {} } } squires.ParseTree(root, tree) self.assertEqual('Number two', root['two'].help) self.assertEqual('Number three', root['two']['three'].help) self.assertTrue(root['one'].options[0].helptext in ('Quickly', 'Slowly')) self.assertTrue(root['one'].options[1].helptext in ('Quickly', 'Slowly'))
def testAttach(self): """Verify that commands get attached in the right place.""" self.failUnlessEqual(self.cmd['show'].name, 'show') self.failUnlessEqual(self.cmd['show']['interface'].name, 'interface') self.failUnlessEqual(self.cmd['show']['interface']['terse'].name, 'terse') self.failUnlessEqual(self.cmd['show'].root, self.cmd) self.failUnlessEqual(self.cmd['show']['interface'].root, self.cmd) self.failUnlessEqual(self.cmd['show']['interface']['terse'].root, self.cmd) self.failUnlessEqual(['show', 'interface', 'terse'], self.cmd['show']['interface']['terse'].path) # Verify that _AddAncestors() has worked. self.failUnlessEqual('write', self.cmd['write'].name) self.failUnlessEqual('file', self.cmd['write']['file'].name) self.failUnlessEqual('logs', self.cmd['write']['file']['logs'].name) command = squires.Command() command.name = 'write' command.help = 'Write something' command.ancestors = [] self.cmd.Attach(command) # Merge a new Command() over an existing. self.failUnlessEqual('write', self.cmd['write'].name) self.failUnlessEqual('file', self.cmd['write']['file'].name) self.failUnlessEqual('logs', self.cmd['write']['file']['logs'].name) command = squires.Command() command.name = 'file' command.help = 'Write file' command.ancestors = ['write'] command.AddOption('now', helptext='Write it now') self.cmd.Attach(command) self.failUnlessEqual('now', command.options.GetOptionObject('now').name) self.failUnlessEqual(None, command.options.GetOptionObject('unknown')) self.failUnlessEqual('write', self.cmd['write'].name) self.failUnlessEqual('file', self.cmd['write']['file'].name) self.failUnlessEqual('Write file', self.cmd['write']['file'].help) self.failUnlessEqual('logs', self.cmd['write']['file']['logs'].name) self.assertEqual(1, len(self.cmd['write']['file'].options))
def setUp(self): """First thing we do is build a command tree.""" self.cmd = squires.Command() self.cmd.name = '<root>' self.cmd.AddCommand('show', help='show help') interface = squires.Command() interface.name = 'interface' interface.help = 'interface help' interface.ancestors = ['show'] self.cmd.Attach(interface) command = squires.Command() command.name = 'xe10' command.help = 'xe10 help' command.ancestors = ['show', 'interface'] self.cmd.Attach(command) command = squires.Command() command.name = 'xe1' command.help = 'xe1 help' command.ancestors = ['show', 'interface'] self.cmd.Attach(command) command = squires.Command() command.name = 'terse' command.help = 'terse help' command.ancestors = ['show', 'interface'] self.cmd.Attach(command) command = squires.Command() command.name = 'version' command.help = 'version help' command.ancestors = ['show'] self.cmd.Attach(command) interface.AddSubCommand('teal', help='teal help') command = squires.Command() command.name = 'invisible' command.help = 'invisible command' command.hidden = True command.ancestors = ['show'] self.cmd.Attach(command) command = squires.Command() command.name = 'logs' command.help = 'write file logs' command.ancestors = ['write', 'file'] self.cmd.Attach(command)
def testReadlineFuncs(self): root = squires.Command('<root>') root.AddCommand('one', help='ONE', method=squires) root.AddCommand('two', help='TWO', method=squires).AddSubCommand('four', help='FOUR', method=squires) root.AddCommand('three', help='THREE', method=squires) # Test 'preparereadline' self.assertEqual(None, squires.readline.get_completer()) self.assertFalse(' ' == squires.readline.get_completer_delims()) root._ReadlinePrepare() self.assertEqual(root.ReadlineCompleter, squires.readline.get_completer()) self.assertEqual(' ', squires.readline.get_completer_delims()) root._ReadlineUnprepare() self.assertEqual(None, squires.readline.get_completer()) self.assertFalse(' ' == squires.readline.get_completer_delims()) get_line_buffer = squires.readline.get_line_buffer squires.readline.get_line_buffer = lambda: '' # Test 'FindCurrentCandidates' self.assertEqual({ 'one': 'ONE', 'two': 'TWO', 'three': 'THREE' }, root.FindCurrentCandidates()) squires.readline.get_line_buffer = get_line_buffer squires.readline.insert_text('t') self.assertEqual({ 'two': 'TWO', 'three': 'THREE' }, root.FindCurrentCandidates()) # Test completion formatter buf = cStringIO.StringIO() sys.stdout = buf root.FormatCompleterOptions('t', ['two', 'three'], 1) sys.stdout = sys.__stdout__ self.assertEqual([ '', 'Valid completions:', ' three THREE', ' two TWO', '> t' ], buf.getvalue().splitlines()) self.assertEqual('three' + squires.COMPLETE_SUFFIX, root.ReadlineCompleter('', 0)) self.assertEqual('two' + squires.COMPLETE_SUFFIX, root.ReadlineCompleter('', 1)) self.assertEqual(None, root.ReadlineCompleter('', 2))
def testReadlineHistorySaving(self): for _ in xrange(squires.readline.get_current_history_length()): squires.readline.remove_history_item(0) cmd = squires.Command() cmd._SaveHistory() self.assertEqual([], cmd._saved_history) cmd._RestoreHistory() self.assertEqual(0, squires.readline.get_current_history_length()) squires.readline.add_history('command one') squires.readline.add_history('command two') squires.readline.add_history('command three') cmd._SaveHistory() self.assertEqual(['command one', 'command two', 'command three'], cmd._saved_history) self.assertEqual(0, squires.readline.get_current_history_length()) # Child command history item, should be removed. squires.readline.add_history('command four') cmd._RestoreHistory() self.assertEqual(3, squires.readline.get_current_history_length()) self.assertEqual('command one', squires.readline.get_history_item(1)) self.assertEqual('command two', squires.readline.get_history_item(2)) self.assertEqual('command three', squires.readline.get_history_item(3))
def testPipe(self): class testPipe(squires.pipe.Pipe): pass COMMAND = squires.CommandDefinition PIPE = squires.PipeDefinition PIPETREE = squires.PipeTreeDefinition OPTION = squires.OptionDefinition pipe1 = {PIPE('grep', pipe=testPipe()): {}} tree = { COMMAND('one', method=squires, help='ONE'): { OPTION('slow', boolean=True): {}, PIPETREE(tree=pipe1): {}, }, COMMAND('two', method=squires, help='TWO'): { PIPETREE(tree=pipe1): {}, COMMAND('four', method=squires, help='FOUR'): {}, }, COMMAND('three', method=squires, help='THREE'): {} } # Test that pipes get added to the tree, and can be found. root = squires.Command('<root>') squires.ParseTree(root, tree) self.assertEqual(None, root['three'].GetPipeTree()) self.assertEqual('grep', root['one'].GetPipeTree()['grep'].name) self.assertTrue( isinstance(root['one'].GetPipeTree()['grep'].pipe, testPipe)) root['one'].command_line = ['slow', '|', 'grep', 'bar'] self.assertTrue(root['one'].GetOption('slow')) root['one'].command_line = ['|', 'grep', 'slow'] self.assertIsNone(root['one'].GetOption('slow')) # Test 'WillPipe()' self.assertTrue(root['two']['four'].WillPipe( ['two', 'four', squires.PIPE_CHAR, 'grep'])) self.assertFalse(root['two']['four'].WillPipe(['two', 'four', 'grep']))
def testWithMethod(self): def MyMethod(*args): """test docstring.""" cmd = squires.Command(name='foo', method=MyMethod) self.failUnlessEqual('test docstring.', cmd.help)