Show More
@@ -1,121 +1,121 b'' | |||||
1 | #----------------------------------------------------------------------------- |
|
1 | #----------------------------------------------------------------------------- | |
2 | # Copyright (C) 2010-2011, IPython Development Team. |
|
2 | # Copyright (C) 2010-2011, IPython Development Team. | |
3 | # |
|
3 | # | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 | # |
|
5 | # | |
6 | # The full license is in the file COPYING.txt, distributed with this software. |
|
6 | # The full license is in the file COPYING.txt, distributed with this software. | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 |
|
8 | |||
9 | from nose.tools import assert_equal, assert_true |
|
9 | from nose.tools import assert_equal, assert_true | |
10 |
|
10 | |||
11 | from IPython.external import argparse |
|
11 | from IPython.external import argparse | |
12 | from IPython.core.magic_arguments import (argument, argument_group, kwds, |
|
12 | from IPython.core.magic_arguments import (argument, argument_group, kwds, | |
13 | magic_arguments, parse_argstring, real_name) |
|
13 | magic_arguments, parse_argstring, real_name) | |
14 | from IPython.testing.decorators import parametric |
|
14 | from IPython.testing.decorators import parametric | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | @magic_arguments() |
|
17 | @magic_arguments() | |
18 | @argument('-f', '--foo', help="an argument") |
|
18 | @argument('-f', '--foo', help="an argument") | |
19 | def magic_foo1(self, args): |
|
19 | def magic_foo1(self, args): | |
20 | """ A docstring. |
|
20 | """ A docstring. | |
21 | """ |
|
21 | """ | |
22 | return parse_argstring(magic_foo1, args) |
|
22 | return parse_argstring(magic_foo1, args) | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | @magic_arguments() |
|
25 | @magic_arguments() | |
26 | def magic_foo2(self, args): |
|
26 | def magic_foo2(self, args): | |
27 | """ A docstring. |
|
27 | """ A docstring. | |
28 | """ |
|
28 | """ | |
29 | return parse_argstring(magic_foo2, args) |
|
29 | return parse_argstring(magic_foo2, args) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | @magic_arguments() |
|
32 | @magic_arguments() | |
33 | @argument('-f', '--foo', help="an argument") |
|
33 | @argument('-f', '--foo', help="an argument") | |
34 | @argument_group('Group') |
|
34 | @argument_group('Group') | |
35 | @argument('-b', '--bar', help="a grouped argument") |
|
35 | @argument('-b', '--bar', help="a grouped argument") | |
36 | @argument_group('Second Group') |
|
36 | @argument_group('Second Group') | |
37 | @argument('-z', '--baz', help="another grouped argument") |
|
37 | @argument('-z', '--baz', help="another grouped argument") | |
38 | def magic_foo3(self, args): |
|
38 | def magic_foo3(self, args): | |
39 | """ A docstring. |
|
39 | """ A docstring. | |
40 | """ |
|
40 | """ | |
41 | return parse_argstring(magic_foo3, args) |
|
41 | return parse_argstring(magic_foo3, args) | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | @magic_arguments() |
|
44 | @magic_arguments() | |
45 | @kwds(argument_default=argparse.SUPPRESS) |
|
45 | @kwds(argument_default=argparse.SUPPRESS) | |
46 | @argument('-f', '--foo', help="an argument") |
|
46 | @argument('-f', '--foo', help="an argument") | |
47 | def magic_foo4(self, args): |
|
47 | def magic_foo4(self, args): | |
48 | """ A docstring. |
|
48 | """ A docstring. | |
49 | """ |
|
49 | """ | |
50 | return parse_argstring(magic_foo4, args) |
|
50 | return parse_argstring(magic_foo4, args) | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | @magic_arguments('frobnicate') |
|
53 | @magic_arguments('frobnicate') | |
54 | @argument('-f', '--foo', help="an argument") |
|
54 | @argument('-f', '--foo', help="an argument") | |
55 | def magic_foo5(self, args): |
|
55 | def magic_foo5(self, args): | |
56 | """ A docstring. |
|
56 | """ A docstring. | |
57 | """ |
|
57 | """ | |
58 | return parse_argstring(magic_foo5, args) |
|
58 | return parse_argstring(magic_foo5, args) | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | @magic_arguments() |
|
61 | @magic_arguments() | |
62 | @argument('-f', '--foo', help="an argument") |
|
62 | @argument('-f', '--foo', help="an argument") | |
63 | def magic_magic_foo(self, args): |
|
63 | def magic_magic_foo(self, args): | |
64 | """ A docstring. |
|
64 | """ A docstring. | |
65 | """ |
|
65 | """ | |
66 | return parse_argstring(magic_magic_foo, args) |
|
66 | return parse_argstring(magic_magic_foo, args) | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | @magic_arguments() |
|
69 | @magic_arguments() | |
70 | @argument('-f', '--foo', help="an argument") |
|
70 | @argument('-f', '--foo', help="an argument") | |
71 | def foo(self, args): |
|
71 | def foo(self, args): | |
72 | """ A docstring. |
|
72 | """ A docstring. | |
73 | """ |
|
73 | """ | |
74 | return parse_argstring(foo, args) |
|
74 | return parse_argstring(foo, args) | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | @parametric |
|
77 | @parametric | |
78 | def test_magic_arguments(): |
|
78 | def test_magic_arguments(): | |
79 | # Ideally, these would be doctests, but I could not get it to work. |
|
79 | # Ideally, these would be doctests, but I could not get it to work. | |
80 | yield assert_equal(magic_foo1.__doc__, '%foo1 [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') |
|
80 | yield assert_equal(magic_foo1.__doc__, '%foo1 [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
81 | yield assert_equal(getattr(magic_foo1, 'argcmd_name', None), None) |
|
81 | yield assert_equal(getattr(magic_foo1, 'argcmd_name', None), None) | |
82 | yield assert_equal(real_name(magic_foo1), 'foo1') |
|
82 | yield assert_equal(real_name(magic_foo1), 'foo1') | |
83 | yield assert_equal(magic_foo1(None, ''), argparse.Namespace(foo=None)) |
|
83 | yield assert_equal(magic_foo1(None, ''), argparse.Namespace(foo=None)) | |
84 | yield assert_true(hasattr(magic_foo1, 'has_arguments')) |
|
84 | yield assert_true(hasattr(magic_foo1, 'has_arguments')) | |
85 |
|
85 | |||
86 | yield assert_equal(magic_foo2.__doc__, '%foo2\n\nA docstring.\n') |
|
86 | yield assert_equal(magic_foo2.__doc__, '%foo2\n\n A docstring.\n') | |
87 | yield assert_equal(getattr(magic_foo2, 'argcmd_name', None), None) |
|
87 | yield assert_equal(getattr(magic_foo2, 'argcmd_name', None), None) | |
88 | yield assert_equal(real_name(magic_foo2), 'foo2') |
|
88 | yield assert_equal(real_name(magic_foo2), 'foo2') | |
89 | yield assert_equal(magic_foo2(None, ''), argparse.Namespace()) |
|
89 | yield assert_equal(magic_foo2(None, ''), argparse.Namespace()) | |
90 | yield assert_true(hasattr(magic_foo2, 'has_arguments')) |
|
90 | yield assert_true(hasattr(magic_foo2, 'has_arguments')) | |
91 |
|
91 | |||
92 | yield assert_equal(magic_foo3.__doc__, '%foo3 [-f FOO] [-b BAR] [-z BAZ]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n\nGroup:\n -b BAR, --bar BAR a grouped argument\n\nSecond Group:\n -z BAZ, --baz BAZ another grouped argument\n') |
|
92 | yield assert_equal(magic_foo3.__doc__, '%foo3 [-f FOO] [-b BAR] [-z BAZ]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n\nGroup:\n -b BAR, --bar BAR a grouped argument\n\nSecond Group:\n -z BAZ, --baz BAZ another grouped argument\n') | |
93 | yield assert_equal(getattr(magic_foo3, 'argcmd_name', None), None) |
|
93 | yield assert_equal(getattr(magic_foo3, 'argcmd_name', None), None) | |
94 | yield assert_equal(real_name(magic_foo3), 'foo3') |
|
94 | yield assert_equal(real_name(magic_foo3), 'foo3') | |
95 | yield assert_equal(magic_foo3(None, ''), |
|
95 | yield assert_equal(magic_foo3(None, ''), | |
96 | argparse.Namespace(bar=None, baz=None, foo=None)) |
|
96 | argparse.Namespace(bar=None, baz=None, foo=None)) | |
97 | yield assert_true(hasattr(magic_foo3, 'has_arguments')) |
|
97 | yield assert_true(hasattr(magic_foo3, 'has_arguments')) | |
98 |
|
98 | |||
99 | yield assert_equal(magic_foo4.__doc__, '%foo4 [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') |
|
99 | yield assert_equal(magic_foo4.__doc__, '%foo4 [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
100 | yield assert_equal(getattr(magic_foo4, 'argcmd_name', None), None) |
|
100 | yield assert_equal(getattr(magic_foo4, 'argcmd_name', None), None) | |
101 | yield assert_equal(real_name(magic_foo4), 'foo4') |
|
101 | yield assert_equal(real_name(magic_foo4), 'foo4') | |
102 | yield assert_equal(magic_foo4(None, ''), argparse.Namespace()) |
|
102 | yield assert_equal(magic_foo4(None, ''), argparse.Namespace()) | |
103 | yield assert_true(hasattr(magic_foo4, 'has_arguments')) |
|
103 | yield assert_true(hasattr(magic_foo4, 'has_arguments')) | |
104 |
|
104 | |||
105 | yield assert_equal(magic_foo5.__doc__, '%frobnicate [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') |
|
105 | yield assert_equal(magic_foo5.__doc__, '%frobnicate [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
106 | yield assert_equal(getattr(magic_foo5, 'argcmd_name', None), 'frobnicate') |
|
106 | yield assert_equal(getattr(magic_foo5, 'argcmd_name', None), 'frobnicate') | |
107 | yield assert_equal(real_name(magic_foo5), 'frobnicate') |
|
107 | yield assert_equal(real_name(magic_foo5), 'frobnicate') | |
108 | yield assert_equal(magic_foo5(None, ''), argparse.Namespace(foo=None)) |
|
108 | yield assert_equal(magic_foo5(None, ''), argparse.Namespace(foo=None)) | |
109 | yield assert_true(hasattr(magic_foo5, 'has_arguments')) |
|
109 | yield assert_true(hasattr(magic_foo5, 'has_arguments')) | |
110 |
|
110 | |||
111 | yield assert_equal(magic_magic_foo.__doc__, '%magic_foo [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') |
|
111 | yield assert_equal(magic_magic_foo.__doc__, '%magic_foo [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
112 | yield assert_equal(getattr(magic_magic_foo, 'argcmd_name', None), None) |
|
112 | yield assert_equal(getattr(magic_magic_foo, 'argcmd_name', None), None) | |
113 | yield assert_equal(real_name(magic_magic_foo), 'magic_foo') |
|
113 | yield assert_equal(real_name(magic_magic_foo), 'magic_foo') | |
114 | yield assert_equal(magic_magic_foo(None, ''), argparse.Namespace(foo=None)) |
|
114 | yield assert_equal(magic_magic_foo(None, ''), argparse.Namespace(foo=None)) | |
115 | yield assert_true(hasattr(magic_magic_foo, 'has_arguments')) |
|
115 | yield assert_true(hasattr(magic_magic_foo, 'has_arguments')) | |
116 |
|
116 | |||
117 | yield assert_equal(foo.__doc__, '%foo [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') |
|
117 | yield assert_equal(foo.__doc__, '%foo [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
118 | yield assert_equal(getattr(foo, 'argcmd_name', None), None) |
|
118 | yield assert_equal(getattr(foo, 'argcmd_name', None), None) | |
119 | yield assert_equal(real_name(foo), 'foo') |
|
119 | yield assert_equal(real_name(foo), 'foo') | |
120 | yield assert_equal(foo(None, ''), argparse.Namespace(foo=None)) |
|
120 | yield assert_equal(foo(None, ''), argparse.Namespace(foo=None)) | |
121 | yield assert_true(hasattr(foo, 'has_arguments')) |
|
121 | yield assert_true(hasattr(foo, 'has_arguments')) |
General Comments 0
You need to be logged in to leave comments.
Login now