Show More
@@ -1,105 +1,105 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Tests for IPython.config.application.Application |
|
2 | Tests for IPython.config.application.Application | |
3 |
|
3 | |||
4 | Authors: |
|
4 | Authors: | |
5 |
|
5 | |||
6 | * Brian Granger |
|
6 | * Brian Granger | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | # Copyright (C) 2008-2011 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | from unittest import TestCase |
|
20 | from unittest import TestCase | |
21 |
|
21 | |||
22 | from IPython.config.configurable import Configurable |
|
22 | from IPython.config.configurable import Configurable | |
23 |
|
23 | |||
24 | from IPython.config.application import ( |
|
24 | from IPython.config.application import ( | |
25 | Application |
|
25 | Application | |
26 | ) |
|
26 | ) | |
27 |
|
27 | |||
28 | from IPython.utils.traitlets import ( |
|
28 | from IPython.utils.traitlets import ( | |
29 | Bool, Unicode, Int, Float, List, Dict |
|
29 | Bool, Unicode, Int, Float, List, Dict | |
30 | ) |
|
30 | ) | |
31 |
|
31 | |||
32 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
33 | # Code |
|
33 | # Code | |
34 | #----------------------------------------------------------------------------- |
|
34 | #----------------------------------------------------------------------------- | |
35 |
|
35 | |||
36 | class Foo(Configurable): |
|
36 | class Foo(Configurable): | |
37 |
|
37 | |||
38 | i = Int(0, config=True, help="The integer i.") |
|
38 | i = Int(0, config=True, help="The integer i.") | |
39 | j = Int(1, config=True, help="The integer j.") |
|
39 | j = Int(1, config=True, help="The integer j.") | |
40 | name = Unicode(u'Brian', config=True, help="First name.") |
|
40 | name = Unicode(u'Brian', config=True, help="First name.") | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | class Bar(Configurable): |
|
43 | class Bar(Configurable): | |
44 |
|
44 | |||
45 | enabled = Bool(True, config=True, help="Enable bar.") |
|
45 | enabled = Bool(True, config=True, help="Enable bar.") | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | class MyApp(Application): |
|
48 | class MyApp(Application): | |
49 |
|
49 | |||
50 | name = Unicode(u'myapp') |
|
50 | name = Unicode(u'myapp') | |
51 | running = Bool(False, config=True, |
|
51 | running = Bool(False, config=True, | |
52 | help="Is the app running?") |
|
52 | help="Is the app running?") | |
53 | classes = List([Bar, Foo]) |
|
53 | classes = List([Bar, Foo]) | |
54 | config_file = Unicode(u'', config=True, |
|
54 | config_file = Unicode(u'', config=True, | |
55 | help="Load this config file") |
|
55 | help="Load this config file") | |
56 |
|
56 | |||
57 | aliases = Dict(dict(i='Foo.i',j='Foo.j',name='Foo.name', |
|
57 | aliases = Dict(dict(i='Foo.i',j='Foo.j',name='Foo.name', | |
58 | enabled='Bar.enabled', log_level='MyApp.log_level')) |
|
58 | enabled='Bar.enabled', log_level='MyApp.log_level')) | |
59 |
|
59 | |||
60 | flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Set Bar.enabled to True"), |
|
60 | flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Set Bar.enabled to True"), | |
61 | disable=({'Bar': {'enabled' : False}}, "Set Bar.enabled to False"))) |
|
61 | disable=({'Bar': {'enabled' : False}}, "Set Bar.enabled to False"))) | |
62 |
|
62 | |||
63 | def init_foo(self): |
|
63 | def init_foo(self): | |
64 | self.foo = Foo(config=self.config) |
|
64 | self.foo = Foo(config=self.config) | |
65 |
|
65 | |||
66 | def init_bar(self): |
|
66 | def init_bar(self): | |
67 | self.bar = Bar(config=self.config) |
|
67 | self.bar = Bar(config=self.config) | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | class TestApplication(TestCase): |
|
70 | class TestApplication(TestCase): | |
71 |
|
71 | |||
72 | def test_basic(self): |
|
72 | def test_basic(self): | |
73 | app = MyApp() |
|
73 | app = MyApp() | |
74 | self.assertEquals(app.name, u'myapp') |
|
74 | self.assertEquals(app.name, u'myapp') | |
75 | self.assertEquals(app.running, False) |
|
75 | self.assertEquals(app.running, False) | |
76 | self.assertEquals(app.classes, [MyApp,Bar,Foo]) |
|
76 | self.assertEquals(app.classes, [MyApp,Bar,Foo]) | |
77 | self.assertEquals(app.config_file, u'') |
|
77 | self.assertEquals(app.config_file, u'') | |
78 |
|
78 | |||
79 | def test_config(self): |
|
79 | def test_config(self): | |
80 | app = MyApp() |
|
80 | app = MyApp() | |
81 | app.parse_command_line(["i=10","Foo.j=10","enabled=False","log_level=0"]) |
|
81 | app.parse_command_line(["i=10","Foo.j=10","enabled=False","log_level=50"]) | |
82 | config = app.config |
|
82 | config = app.config | |
83 | self.assertEquals(config.Foo.i, 10) |
|
83 | self.assertEquals(config.Foo.i, 10) | |
84 | self.assertEquals(config.Foo.j, 10) |
|
84 | self.assertEquals(config.Foo.j, 10) | |
85 | self.assertEquals(config.Bar.enabled, False) |
|
85 | self.assertEquals(config.Bar.enabled, False) | |
86 | self.assertEquals(config.MyApp.log_level,0) |
|
86 | self.assertEquals(config.MyApp.log_level,50) | |
87 |
|
87 | |||
88 | def test_config_propagation(self): |
|
88 | def test_config_propagation(self): | |
89 | app = MyApp() |
|
89 | app = MyApp() | |
90 | app.parse_command_line(["i=10","Foo.j=10","enabled=False","log_level=0"]) |
|
90 | app.parse_command_line(["i=10","Foo.j=10","enabled=False","log_level=50"]) | |
91 | app.init_foo() |
|
91 | app.init_foo() | |
92 | app.init_bar() |
|
92 | app.init_bar() | |
93 | self.assertEquals(app.foo.i, 10) |
|
93 | self.assertEquals(app.foo.i, 10) | |
94 | self.assertEquals(app.foo.j, 10) |
|
94 | self.assertEquals(app.foo.j, 10) | |
95 | self.assertEquals(app.bar.enabled, False) |
|
95 | self.assertEquals(app.bar.enabled, False) | |
96 |
|
96 | |||
97 | def test_alias(self): |
|
97 | def test_alias(self): | |
98 | app = MyApp() |
|
98 | app = MyApp() | |
99 | app.parse_command_line(["--disable"]) |
|
99 | app.parse_command_line(["--disable"]) | |
100 | app.init_bar() |
|
100 | app.init_bar() | |
101 | self.assertEquals(app.bar.enabled, False) |
|
101 | self.assertEquals(app.bar.enabled, False) | |
102 | app.parse_command_line(["--enable"]) |
|
102 | app.parse_command_line(["--enable"]) | |
103 | app.init_bar() |
|
103 | app.init_bar() | |
104 | self.assertEquals(app.bar.enabled, True) |
|
104 | self.assertEquals(app.bar.enabled, True) | |
105 |
|
105 |
General Comments 0
You need to be logged in to leave comments.
Login now