Back to index

python-cliapp  1.20120630
Public Member Functions | Public Attributes
cliapp.app_tests.ApplicationTests Class Reference

List of all members.

Public Member Functions

def setUp
def test_creates_settings
def test_calls_add_settings_only_in_run
def test_run_uses_string_list_options_only_once
def test_run_sets_up_logging
def test_run_sets_progname_from_sysargv0
def test_run_calls_process_args
def test_run_processes_input_files
def test_run_sets_output_attribute
def test_run_sets_output_to_file_if_output_option_is_set
def test_run_calls_parse_args
def test_makes_envname_correctly
def test_parses_options
def test_calls_setup
def test_calls_cleanup
def test_process_args_calls_process_inputs
def test_process_inputs_calls_process_input_for_each_arg
def test_process_inputs_calls_process_input_with_dash_if_no_inputs
def test_open_input_opens_file
def test_open_input_opens_file_in_binary_mode_when_requested
def test_open_input_opens_stdin_if_dash_given
def test_process_input_calls_open_input
def test_process_input_does_not_close_stdin
def test_processes_input_lines
def test_process_input_line_can_access_counters
def test_run_prints_out_error_for_appexception
def test_run_prints_out_stack_trace_for_not_appexception
def test_run_raises_systemexit_for_systemexit
def test_run_raises_systemexit_for_keyboardint

Public Attributes

 app
 called
 inputs
 output
 log
 setup_called
 cleanup_called
 args
 closed

Detailed Description

Definition at line 41 of file app_tests.py.


Member Function Documentation

Definition at line 43 of file app_tests.py.

00043 
00044     def setUp(self):
00045         self.app = cliapp.Application()

Definition at line 49 of file app_tests.py.

00049 
00050     def test_calls_add_settings_only_in_run(self):
00051     
00052         class Foo(cliapp.Application):
00053             def process_args(self, args):
00054                 pass
00055             def add_settings(self):
00056                 self.settings.string(['foo'], '')
00057         foo = Foo()
00058         self.assertFalse('foo' in foo.settings)
00059         foo.run(args=[])
00060         self.assert_('foo' in foo.settings)

Definition at line 145 of file app_tests.py.

00145 
00146     def test_calls_cleanup(self):
00147         class App(cliapp.Application):
00148             def cleanup(self):
00149                 self.cleanup_called = True
00150             def process_inputs(self, args):
00151                 pass
00152         app = App()
00153         app.run(args=[])
00154         self.assertTrue(app.cleanup_called)

Definition at line 135 of file app_tests.py.

00135 
00136     def test_calls_setup(self):
00137         class App(cliapp.Application):
00138             def setup(self):
00139                 self.setup_called = True
00140             def process_inputs(self, args):
00141                 pass
00142         app = App()
00143         app.run(args=[])
00144         self.assertTrue(app.setup_called)

Definition at line 46 of file app_tests.py.

00046 
00047     def test_creates_settings(self):
00048         self.assert_(isinstance(self.app.settings, cliapp.Settings))
        

Definition at line 120 of file app_tests.py.

00120 
00121     def test_makes_envname_correctly(self):
00122         self.assertEqual(self.app._envname('foo'), 'FOO')
00123         self.assertEqual(self.app._envname('foo.py'), 'FOO')
00124         self.assertEqual(self.app._envname('foo bar'), 'FOO_BAR')
00125         self.assertEqual(self.app._envname('foo-bar'), 'FOO_BAR')
00126         self.assertEqual(self.app._envname('foo/bar'), 'BAR')
00127         self.assertEqual(self.app._envname('foo_bar'), 'FOO_BAR')

Definition at line 179 of file app_tests.py.

00179 
00180     def test_open_input_opens_file(self):
00181         f = self.app.open_input('/dev/null')
00182         self.assert_(isinstance(f, file))
00183         self.assertEqual(f.mode, 'r')
        

Definition at line 184 of file app_tests.py.

00184 
00185     def test_open_input_opens_file_in_binary_mode_when_requested(self):
00186         f = self.app.open_input('/dev/null', mode='rb')
00187         self.assertEqual(f.mode, 'rb')

Definition at line 188 of file app_tests.py.

00188 
00189     def test_open_input_opens_stdin_if_dash_given(self):
00190         self.assertEqual(self.app.open_input('-'), sys.stdin)

Definition at line 128 of file app_tests.py.

00128 
00129     def test_parses_options(self):
00130         self.app.settings.string(['foo'], 'foo help')
00131         self.app.settings.boolean(['bar'], 'bar help')
00132         self.app.parse_args(['--foo=foovalue', '--bar'])
00133         self.assertEqual(self.app.settings['foo'], 'foovalue')
00134         self.assertEqual(self.app.settings['bar'], True)

Definition at line 155 of file app_tests.py.

00155 
00156     def test_process_args_calls_process_inputs(self):
00157         self.called = False
00158         def process_inputs(args):
00159             self.called = True
00160         self.app.process_inputs = process_inputs
00161         self.app.process_args([])
00162         self.assert_(self.called)

Definition at line 191 of file app_tests.py.

00191 
00192     def test_process_input_calls_open_input(self):
00193         self.called = None
00194         def open_input(name):
00195             self.called = name
00196             return StringIO.StringIO('')
00197         self.app.open_input = open_input
00198         self.app.process_input('foo')
00199         self.assertEqual(self.called, 'foo')
        

Definition at line 200 of file app_tests.py.

00200 
00201     def test_process_input_does_not_close_stdin(self):
00202         self.closed = False
00203         def close():
00204             self.closed = True
00205         f = StringIO.StringIO('')
00206         f.close = close
00207         def open_input(name):
00208             if name == '-':
00209                 return f
00210         self.app.open_input = open_input
00211         self.app.process_input('-', stdin=f)
00212         self.assertEqual(self.closed, False)

Definition at line 227 of file app_tests.py.

00227 
00228     def test_process_input_line_can_access_counters(self):
00229         counters = []
00230         class Foo(cliapp.Application):
00231             def open_input(self, name):
00232                 return StringIO.StringIO(''.join('%s%d\n' % (name, i)
00233                                                  for i in range(2)))
00234             def process_input_line(self, name, line):
00235                 counters.append((self.fileno, self.global_lineno, self.lineno))
00236 
00237         foo = Foo()
00238         foo.run(args=['foo', 'bar'])
00239         self.assertEqual(counters, 
00240                          [(1, 1, 1), 
00241                           (1, 2, 2), 
00242                           (2, 3, 1), 
00243                           (2, 4, 2)])

Definition at line 163 of file app_tests.py.

00163 
00164     def test_process_inputs_calls_process_input_for_each_arg(self):
00165         self.args = []
00166         def process_input(arg):
00167             self.args.append(arg)
00168         self.app.process_input = process_input
00169         self.app.process_inputs(['foo', 'bar'])
00170         self.assertEqual(self.args, ['foo', 'bar'])

Definition at line 171 of file app_tests.py.

00171 
00172     def test_process_inputs_calls_process_input_with_dash_if_no_inputs(self):
00173         self.args = []
00174         def process_input(arg):
00175             self.args.append(arg)
00176         self.app.process_input = process_input
00177         self.app.process_inputs([])
00178         self.assertEqual(self.args, ['-'])

Definition at line 213 of file app_tests.py.

00213 
00214     def test_processes_input_lines(self):
00215 
00216         lines = []
00217         class Foo(cliapp.Application):
00218             def open_input(self, name):
00219                 return StringIO.StringIO(''.join('%s%d\n' % (name, i)
00220                                                  for i in range(2)))
00221             def process_input_line(self, name, line):
00222                 lines.append(line)
00223 
00224         foo = Foo()
00225         foo.run(args=['foo', 'bar'])
00226         self.assertEqual(lines, ['foo0\n', 'foo1\n', 'bar0\n', 'bar1\n'])

Definition at line 108 of file app_tests.py.

00108 
00109     def test_run_calls_parse_args(self):
00110         class DummyOptions(object):
00111             def __init__(self):
00112                 self.output = None
00113                 self.log = None
00114         self.called = None
00115         self.app.parse_args = lambda args, **kw: setattr(self, 'called', args)
00116         self.app.process_args = lambda args: None
00117         self.app.settings.options = DummyOptions()
00118         self.app.run(args=['foo', 'bar'])
00119         self.assertEqual(self.called, ['foo', 'bar'])

Here is the call graph for this function:

Definition at line 86 of file app_tests.py.

00086 
00087     def test_run_calls_process_args(self):
00088         self.called = None
00089         self.app.process_args = lambda args: setattr(self, 'called', args)
00090         self.app.run(args=['foo', 'bar'])
00091         self.assertEqual(self.called, ['foo', 'bar'])
    

Definition at line 244 of file app_tests.py.

00244 
00245     def test_run_prints_out_error_for_appexception(self):
00246         def raise_error(args):
00247             raise cliapp.AppException('xxx')
00248         self.app.process_args = raise_error
00249         f = StringIO.StringIO()
00250         self.assertRaises(SystemExit, self.app.run, [], stderr=f, log=devnull)
00251         self.assert_('xxx' in f.getvalue())

Definition at line 252 of file app_tests.py.

00252 
00253     def test_run_prints_out_stack_trace_for_not_appexception(self):
00254         def raise_error(args):
00255             raise Exception('xxx')
00256         self.app.process_args = raise_error
00257         f = StringIO.StringIO()
00258         self.assertRaises(SystemExit, self.app.run, [], stderr=f, log=devnull)
00259         self.assert_('Traceback' in f.getvalue())

Definition at line 92 of file app_tests.py.

00092 
00093     def test_run_processes_input_files(self):
00094         self.inputs = []
00095         self.app.process_input = lambda name: self.inputs.append(name)
00096         self.app.run(args=['foo', 'bar'])
00097         self.assertEqual(self.inputs, ['foo', 'bar'])
        

Definition at line 267 of file app_tests.py.

00267 
00268     def test_run_raises_systemexit_for_keyboardint(self):
00269         def raise_error(args):
00270             raise KeyboardInterrupt()
00271         self.app.process_args = raise_error
00272         f = StringIO.StringIO()
00273         self.assertRaises(SystemExit, self.app.run, [], stderr=f, log=devnull)
00274     

Definition at line 260 of file app_tests.py.

00260 
00261     def test_run_raises_systemexit_for_systemexit(self):
00262         def raise_error(args):
00263             raise SystemExit(123)
00264         self.app.process_args = raise_error
00265         f = StringIO.StringIO()
00266         self.assertRaises(SystemExit, self.app.run, [], stderr=f, log=devnull)

Definition at line 98 of file app_tests.py.

00098 
00099     def test_run_sets_output_attribute(self):
00100         self.app.process_args = lambda args: None
00101         self.app.run(args=[])
00102         self.assertEqual(self.app.output, sys.stdout)
        

Definition at line 103 of file app_tests.py.

00103 
00104     def test_run_sets_output_to_file_if_output_option_is_set(self):
00105         self.app.process_args = lambda args: None
00106         self.app.run(args=['--output=/dev/null'])
00107         self.assertEqual(self.app.output.name, '/dev/null')
    

Definition at line 81 of file app_tests.py.

00081 
00082     def test_run_sets_progname_from_sysargv0(self):
00083         self.app.process_args = lambda args: None
00084         self.app.run(args=[], sysargv=['foo'])
00085         self.assertEqual(self.app.settings.progname, 'foo')
    

Definition at line 72 of file app_tests.py.

00072 
00073     def test_run_sets_up_logging(self):
00074         self.called = False
00075         def setup():
00076             self.called = True
00077         self.app.setup_logging = setup
00078         self.app.process_args = lambda args: None
00079         self.app.run([])
00080         self.assert_(self.called)
    

Definition at line 61 of file app_tests.py.

00061 
00062     def test_run_uses_string_list_options_only_once(self):
00063         
00064         class Foo(cliapp.Application):
00065             def add_settings(self):
00066                 self.settings.string_list(['foo'], '')
00067             def process_args(self, args):
00068                 pass
00069         foo = Foo()
00070         foo.run(args=['--foo=yo'])
00071         self.assertEqual(foo.settings['foo'], ['yo'])


Member Data Documentation

Definition at line 44 of file app_tests.py.

Definition at line 164 of file app_tests.py.

Definition at line 73 of file app_tests.py.

Definition at line 148 of file app_tests.py.

Definition at line 201 of file app_tests.py.

Definition at line 93 of file app_tests.py.

Definition at line 112 of file app_tests.py.

Definition at line 111 of file app_tests.py.

Definition at line 138 of file app_tests.py.


The documentation for this class was generated from the following file: