class TestApplicationTester(TestCase): def setUp(self): self.application = Application() self.application.set_auto_exit(False) self.application.register('foo')\ .add_argument('foo')\ .set_code(lambda c: c.line('foo')) self.tester = ApplicationTester(self.application) self.tester.run( [('command', 'foo'), ('foo', 'bar')], { 'interactive': False, 'decorated': False, 'verbosity': Output.VERBOSITY_VERBOSE }) def tearDown(self): self.application = None self.tester = None def test_run(self): """ ApplicationTester.run() behaves properly """ self.assertFalse(self.tester.get_input().is_interactive(), msg='.run() takes an interactive option.') self.assertFalse(self.tester.get_output().is_decorated(), msg='.run() takes a decorated option.') self.assertEqual(Output.VERBOSITY_VERBOSE, self.tester.get_output().get_verbosity(), msg='.run() takes an interactive option.') def test_get_input(self): """ ApplicationTester.get_input() behaves properly """ self.assertEqual( 'bar', self.tester.get_input().get_argument('foo'), msg='.get_input() returns the current input instance.') def test_get_output(self): """ ApplicationTester.get_output() behaves properly """ self.tester.get_output().get_stream().seek(0) self.assertEqual( 'foo\n', self.tester.get_output().get_stream().read().decode('utf-8'), msg='.get_output() returns the current output instance.') def test_get_display(self): """ ApplicationTester.get_display() behaves properly """ self.assertEqual( 'foo\n', self.tester.get_display(), msg='.get_display() returns the display of the last execution.')
class TestApplicationTester(TestCase): def setUp(self): self.application = Application() self.application.set_auto_exit(False) self.application.register('foo')\ .add_argument('foo')\ .set_code(lambda input_, output_: output_.writeln('foo')) self.tester = ApplicationTester(self.application) self.tester.run( [ ('command', 'foo'), ('foo', 'bar') ], { 'interactive': False, 'decorated': False, 'verbosity': Output.VERBOSITY_VERBOSE } ) def tearDown(self): self.application = None self.tester = None def test_run(self): """ ApplicationTester.run() behaves properly """ self.assertFalse(self.tester.get_input().is_interactive(), msg='.run() takes an interactive option.') self.assertFalse(self.tester.get_output().is_decorated(), msg='.run() takes a decorated option.') self.assertEqual(Output.VERBOSITY_VERBOSE, self.tester.get_output().get_verbosity(), msg='.run() takes an interactive option.') def test_get_input(self): """ ApplicationTester.get_input() behaves properly """ self.assertEqual('bar', self.tester.get_input().get_argument('foo'), msg='.get_input() returns the current input instance.') def test_get_output(self): """ ApplicationTester.get_output() behaves properly """ self.tester.get_output().get_stream().seek(0) self.assertEqual('foo\n', self.tester.get_output().get_stream().read().decode('utf-8'), msg='.get_output() returns the current output instance.') def test_get_display(self): """ ApplicationTester.get_display() behaves properly """ self.assertEqual('foo\n', self.tester.get_display(), msg='.get_display() returns the display of the last execution.')
def test_run(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) command = Foo1Command() application.add(command) sys.argv = ['cli.py', 'foo:bar1'] application.run() self.assertEqual( 'ArgvInput', command.input.__class__.__name__ ) self.assertEqual( 'ConsoleOutput', command.output.__class__.__name__ ) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) self.ensure_static_command_help(application) tester = ApplicationTester(application) tester.run([], {'decorated': False}) self.assertEqual( self.open_fixture('application_run1.txt'), tester.get_display() ) tester.run([('--help', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run2.txt'), tester.get_display() ) tester.run([('-h', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run2.txt'), tester.get_display() ) tester.run([('command', 'list'), ('--help', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run3.txt'), tester.get_display() ) tester.run([('command', 'list'), ('-h', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run3.txt'), tester.get_display() ) tester.run([('--ansi', True)]) self.assertTrue(tester.get_output().is_decorated()) tester.run([('--no-ansi', True)]) self.assertFalse(tester.get_output().is_decorated()) tester.run([('--version', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run4.txt'), tester.get_display() ) tester.run([('-V', True)], {'decorated': False}) self.assertEqual( self.open_fixture('application_run4.txt'), tester.get_display() ) tester.run([('command', 'list'), ('--quiet', True)]) self.assertEqual( '', tester.get_display() ) tester.run([('command', 'list'), ('-q', True)]) self.assertEqual( '', tester.get_display() ) tester.run([('command', 'list'), ('--verbose', True)]) self.assertEqual( Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity() ) tester.run([('command', 'list'), ('-v', True)]) self.assertEqual( Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity() ) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.add(FooCommand()) tester = ApplicationTester(application) tester.run([('command', 'foo:bar'), ('--no-interaction', True)], {'decorated': False}) self.assertEqual( 'called\n', tester.get_display() ) tester.run([('command', 'foo:bar'), ('-n', True)], {'decorated': False}) self.assertEqual( 'called\n', tester.get_display() )
def test_run(self): application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) command = Foo1Command() application.add(command) sys.argv = ["cli.py", "foo:bar1"] application.run() self.assertEqual("ArgvInput", command.input.__class__.__name__) self.assertEqual("ConsoleOutput", command.output.__class__.__name__) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) self.ensure_static_command_help(application) tester = ApplicationTester(application) tester.run([], {"decorated": False}) self.assertEqual(self.open_fixture("application_run1.txt"), tester.get_display()) tester.run([("--help", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run2.txt"), tester.get_display()) tester.run([("-h", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run2.txt"), tester.get_display()) tester.run([("command", "list"), ("--help", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run3.txt"), tester.get_display()) tester.run([("command", "list"), ("-h", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run3.txt"), tester.get_display()) tester.run([("--ansi", True)]) self.assertTrue(tester.get_output().is_decorated()) tester.run([("--no-ansi", True)]) self.assertFalse(tester.get_output().is_decorated()) tester.run([("--version", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run4.txt"), tester.get_display()) tester.run([("-V", True)], {"decorated": False}) self.assertEqual(self.open_fixture("application_run4.txt"), tester.get_display()) tester.run([("command", "list"), ("--quiet", True)]) self.assertEqual("", tester.get_display()) tester.run([("command", "list"), ("-q", True)]) self.assertEqual("", tester.get_display()) tester.run([("command", "list"), ("--verbose", True)]) self.assertEqual(Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity()) tester.run([("command", "list"), ("-v", True)]) self.assertEqual(Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity()) application = Application() application.set_auto_exit(False) application.set_catch_exceptions(False) application.add(FooCommand()) tester = ApplicationTester(application) tester.run([("command", "foo:bar"), ("--no-interaction", True)], {"decorated": False}) self.assertEqual("called\n", tester.get_display()) tester.run([("command", "foo:bar"), ("-n", True)], {"decorated": False}) self.assertEqual("called\n", tester.get_display())