Show More
@@ -1,113 +1,121 b'' | |||||
1 | #----------------------------------------------------------------------------- |
|
1 | #----------------------------------------------------------------------------- | |
2 | # Copyright (c) 2010, IPython Development Team. |
|
2 | # Copyright (c) 2010, 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 |
|
15 | |||
15 |
|
16 | |||
16 | @magic_arguments() |
|
17 | @magic_arguments() | |
17 | @argument('-f', '--foo', help="an argument") |
|
18 | @argument('-f', '--foo', help="an argument") | |
18 | def magic_foo1(self, args): |
|
19 | def magic_foo1(self, args): | |
19 | """ A docstring. |
|
20 | """ A docstring. | |
20 | """ |
|
21 | """ | |
21 | return parse_argstring(magic_foo1, args) |
|
22 | return parse_argstring(magic_foo1, args) | |
22 |
|
23 | |||
|
24 | ||||
23 | @magic_arguments() |
|
25 | @magic_arguments() | |
24 | def magic_foo2(self, args): |
|
26 | def magic_foo2(self, args): | |
25 | """ A docstring. |
|
27 | """ A docstring. | |
26 | """ |
|
28 | """ | |
27 | return parse_argstring(magic_foo2, args) |
|
29 | return parse_argstring(magic_foo2, args) | |
28 |
|
30 | |||
|
31 | ||||
29 | @magic_arguments() |
|
32 | @magic_arguments() | |
30 | @argument('-f', '--foo', help="an argument") |
|
33 | @argument('-f', '--foo', help="an argument") | |
31 | @argument_group('Group') |
|
34 | @argument_group('Group') | |
32 | @argument('-b', '--bar', help="a grouped argument") |
|
35 | @argument('-b', '--bar', help="a grouped argument") | |
33 | @argument_group('Second Group') |
|
36 | @argument_group('Second Group') | |
34 | @argument('-z', '--baz', help="another grouped argument") |
|
37 | @argument('-z', '--baz', help="another grouped argument") | |
35 | def magic_foo3(self, args): |
|
38 | def magic_foo3(self, args): | |
36 | """ A docstring. |
|
39 | """ A docstring. | |
37 | """ |
|
40 | """ | |
38 | return parse_argstring(magic_foo3, args) |
|
41 | return parse_argstring(magic_foo3, args) | |
39 |
|
42 | |||
|
43 | ||||
40 | @magic_arguments() |
|
44 | @magic_arguments() | |
41 | @kwds(argument_default=argparse.SUPPRESS) |
|
45 | @kwds(argument_default=argparse.SUPPRESS) | |
42 | @argument('-f', '--foo', help="an argument") |
|
46 | @argument('-f', '--foo', help="an argument") | |
43 | def magic_foo4(self, args): |
|
47 | def magic_foo4(self, args): | |
44 | """ A docstring. |
|
48 | """ A docstring. | |
45 | """ |
|
49 | """ | |
46 | return parse_argstring(magic_foo4, args) |
|
50 | return parse_argstring(magic_foo4, args) | |
47 |
|
51 | |||
|
52 | ||||
48 | @magic_arguments('frobnicate') |
|
53 | @magic_arguments('frobnicate') | |
49 | @argument('-f', '--foo', help="an argument") |
|
54 | @argument('-f', '--foo', help="an argument") | |
50 | def magic_foo5(self, args): |
|
55 | def magic_foo5(self, args): | |
51 | """ A docstring. |
|
56 | """ A docstring. | |
52 | """ |
|
57 | """ | |
53 | return parse_argstring(magic_foo5, args) |
|
58 | return parse_argstring(magic_foo5, args) | |
54 |
|
59 | |||
|
60 | ||||
55 | @magic_arguments() |
|
61 | @magic_arguments() | |
56 | @argument('-f', '--foo', help="an argument") |
|
62 | @argument('-f', '--foo', help="an argument") | |
57 | def magic_magic_foo(self, args): |
|
63 | def magic_magic_foo(self, args): | |
58 | """ A docstring. |
|
64 | """ A docstring. | |
59 | """ |
|
65 | """ | |
60 | return parse_argstring(magic_magic_foo, args) |
|
66 | return parse_argstring(magic_magic_foo, args) | |
61 |
|
67 | |||
|
68 | ||||
62 | @magic_arguments() |
|
69 | @magic_arguments() | |
63 | @argument('-f', '--foo', help="an argument") |
|
70 | @argument('-f', '--foo', help="an argument") | |
64 | def foo(self, args): |
|
71 | def foo(self, args): | |
65 | """ A docstring. |
|
72 | """ A docstring. | |
66 | """ |
|
73 | """ | |
67 | return parse_argstring(foo, args) |
|
74 | return parse_argstring(foo, args) | |
68 |
|
75 | |||
|
76 | ||||
|
77 | @parametric | |||
69 | def test_magic_arguments(): |
|
78 | def test_magic_arguments(): | |
70 | # 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. | |
71 |
yield assert_equal |
|
80 | yield assert_equal(magic_foo1.__doc__, '%foo1 [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
72 |
yield assert_equal |
|
81 | yield assert_equal(getattr(magic_foo1, 'argcmd_name', None), None) | |
73 |
yield assert_equal |
|
82 | yield assert_equal(real_name(magic_foo1), 'foo1') | |
74 |
yield assert_equal |
|
83 | yield assert_equal(magic_foo1(None, ''), argparse.Namespace(foo=None)) | |
75 |
yield assert_true |
|
84 | yield assert_true(hasattr(magic_foo1, 'has_arguments')) | |
76 |
|
85 | |||
77 |
yield assert_equal |
|
86 | yield assert_equal(magic_foo2.__doc__, '%foo2\n\nA docstring.\n') | |
78 |
yield assert_equal |
|
87 | yield assert_equal(getattr(magic_foo2, 'argcmd_name', None), None) | |
79 |
yield assert_equal |
|
88 | yield assert_equal(real_name(magic_foo2), 'foo2') | |
80 |
yield assert_equal |
|
89 | yield assert_equal(magic_foo2(None, ''), argparse.Namespace()) | |
81 |
yield assert_true |
|
90 | yield assert_true(hasattr(magic_foo2, 'has_arguments')) | |
82 |
|
91 | |||
83 |
yield assert_equal |
|
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') | |
84 |
yield assert_equal |
|
93 | yield assert_equal(getattr(magic_foo3, 'argcmd_name', None), None) | |
85 |
yield assert_equal |
|
94 | yield assert_equal(real_name(magic_foo3), 'foo3') | |
86 |
yield assert_equal |
|
95 | yield assert_equal(magic_foo3(None, ''), | |
87 | yield assert_true, hasattr(magic_foo3, 'has_arguments') |
|
96 | argparse.Namespace(bar=None, baz=None, foo=None)) | |
88 |
|
97 | yield assert_true(hasattr(magic_foo3, 'has_arguments')) | ||
89 | yield assert_equal, magic_foo4.__doc__, '%foo4 [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' |
|
98 | ||
90 | yield assert_equal, getattr(magic_foo4, 'argcmd_name', None), None |
|
99 | yield assert_equal(magic_foo4.__doc__, '%foo4 [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
91 |
yield assert_equal, |
|
100 | yield assert_equal(getattr(magic_foo4, 'argcmd_name', None), None) | |
92 |
yield assert_equal |
|
101 | yield assert_equal(real_name(magic_foo4), 'foo4') | |
93 |
yield assert_ |
|
102 | yield assert_equal(magic_foo4(None, ''), argparse.Namespace()) | |
94 |
|
103 | yield assert_true(hasattr(magic_foo4, 'has_arguments')) | ||
95 | yield assert_equal, magic_foo5.__doc__, '%frobnicate [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' |
|
104 | ||
96 | yield assert_equal, getattr(magic_foo5, 'argcmd_name', None), 'frobnicate' |
|
105 | yield assert_equal(magic_foo5.__doc__, '%frobnicate [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
97 |
yield assert_equal, |
|
106 | yield assert_equal(getattr(magic_foo5, 'argcmd_name', None), 'frobnicate') | |
98 | yield assert_equal, magic_foo5(None, ''), argparse.Namespace(foo=None) |
|
107 | yield assert_equal(real_name(magic_foo5), 'frobnicate') | |
99 |
yield assert_ |
|
108 | yield assert_equal(magic_foo5(None, ''), argparse.Namespace(foo=None)) | |
100 |
|
109 | yield assert_true(hasattr(magic_foo5, 'has_arguments')) | ||
101 | 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' |
|
110 | ||
102 | yield assert_equal, getattr(magic_magic_foo, 'argcmd_name', None), None |
|
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') | |
103 |
yield assert_equal, |
|
112 | yield assert_equal(getattr(magic_magic_foo, 'argcmd_name', None), None) | |
104 |
yield assert_equal |
|
113 | yield assert_equal(real_name(magic_magic_foo), 'magic_foo') | |
105 |
yield assert_ |
|
114 | yield assert_equal(magic_magic_foo(None, ''), argparse.Namespace(foo=None)) | |
106 |
|
115 | yield assert_true(hasattr(magic_magic_foo, 'has_arguments')) | ||
107 | yield assert_equal, foo.__doc__, '%foo [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' |
|
116 | ||
108 | yield assert_equal, getattr(foo, 'argcmd_name', None), None |
|
117 | yield assert_equal(foo.__doc__, '%foo [-f FOO]\n\nA docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n') | |
109 |
yield assert_equal, |
|
118 | yield assert_equal(getattr(foo, 'argcmd_name', None), None) | |
110 | yield assert_equal, foo(None, ''), argparse.Namespace(foo=None) |
|
119 | yield assert_equal(real_name(foo), 'foo') | |
111 | yield assert_true, hasattr(foo, 'has_arguments') |
|
120 | yield assert_equal(foo(None, ''), argparse.Namespace(foo=None)) | |
112 |
|
121 | yield assert_true(hasattr(foo, 'has_arguments')) | ||
113 |
|
General Comments 0
You need to be logged in to leave comments.
Login now