##// END OF EJS Templates
Fernando Perez -
Show More
@@ -1,163 +1,164 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Tests for IPython.config.loader
4 Tests for IPython.config.loader
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez (design help)
9 * Fernando Perez (design help)
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import os
23 import os
24 from tempfile import mkstemp
24 from tempfile import mkstemp
25 from unittest import TestCase
25 from unittest import TestCase
26
26
27 from IPython.config.loader import (
27 from IPython.config.loader import (
28 Config,
28 Config,
29 PyFileConfigLoader,
29 PyFileConfigLoader,
30 ArgParseConfigLoader,
30 ArgParseConfigLoader,
31 ConfigError
31 ConfigError
32 )
32 )
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Actual tests
35 # Actual tests
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38
38
39 pyfile = """
39 pyfile = """
40 a = 10
40 c = get_config()
41 b = 20
41 c.a = 10
42 Foo.Bar.value = 10
42 c.b = 20
43 Foo.Bam.value = range(10)
43 c.Foo.Bar.value = 10
44 D.C.value = 'hi there'
44 c.Foo.Bam.value = range(10)
45 c.D.C.value = 'hi there'
45 """
46 """
46
47
47 class TestPyFileCL(TestCase):
48 class TestPyFileCL(TestCase):
48
49
49 def test_basic(self):
50 def test_basic(self):
50 fd, fname = mkstemp('.py')
51 fd, fname = mkstemp('.py')
51 f = os.fdopen(fd, 'w')
52 f = os.fdopen(fd, 'w')
52 f.write(pyfile)
53 f.write(pyfile)
53 f.close()
54 f.close()
54 # Unlink the file
55 # Unlink the file
55 cl = PyFileConfigLoader(fname)
56 cl = PyFileConfigLoader(fname)
56 config = cl.load_config()
57 config = cl.load_config()
57 self.assertEquals(config.a, 10)
58 self.assertEquals(config.a, 10)
58 self.assertEquals(config.b, 20)
59 self.assertEquals(config.b, 20)
59 self.assertEquals(config.Foo.Bar.value, 10)
60 self.assertEquals(config.Foo.Bar.value, 10)
60 self.assertEquals(config.Foo.Bam.value, range(10))
61 self.assertEquals(config.Foo.Bam.value, range(10))
61 self.assertEquals(config.D.C.value, 'hi there')
62 self.assertEquals(config.D.C.value, 'hi there')
62
63
63
64
64 class TestArgParseCL(TestCase):
65 class TestArgParseCL(TestCase):
65
66
66 def test_basic(self):
67 def test_basic(self):
67
68
68 class MyLoader(ArgParseConfigLoader):
69 class MyLoader(ArgParseConfigLoader):
69 arguments = (
70 arguments = (
70 (('-f','--foo'), dict(dest='Global.foo', type=str)),
71 (('-f','--foo'), dict(dest='Global.foo', type=str)),
71 (('-b',), dict(dest='MyClass.bar', type=int)),
72 (('-b',), dict(dest='MyClass.bar', type=int)),
72 (('-n',), dict(dest='n', action='store_true')),
73 (('-n',), dict(dest='n', action='store_true')),
73 (('Global.bam',), dict(type=str))
74 (('Global.bam',), dict(type=str))
74 )
75 )
75
76
76 cl = MyLoader()
77 cl = MyLoader()
77 config = cl.load_config('-f hi -b 10 -n wow'.split())
78 config = cl.load_config('-f hi -b 10 -n wow'.split())
78 self.assertEquals(config.Global.foo, 'hi')
79 self.assertEquals(config.Global.foo, 'hi')
79 self.assertEquals(config.MyClass.bar, 10)
80 self.assertEquals(config.MyClass.bar, 10)
80 self.assertEquals(config.n, True)
81 self.assertEquals(config.n, True)
81 self.assertEquals(config.Global.bam, 'wow')
82 self.assertEquals(config.Global.bam, 'wow')
82
83
83 def test_add_arguments(self):
84 def test_add_arguments(self):
84
85
85 class MyLoader(ArgParseConfigLoader):
86 class MyLoader(ArgParseConfigLoader):
86 def _add_arguments(self):
87 def _add_arguments(self):
87 subparsers = self.parser.add_subparsers(dest='subparser_name')
88 subparsers = self.parser.add_subparsers(dest='subparser_name')
88 subparser1 = subparsers.add_parser('1')
89 subparser1 = subparsers.add_parser('1')
89 subparser1.add_argument('-x',dest='Global.x')
90 subparser1.add_argument('-x',dest='Global.x')
90 subparser2 = subparsers.add_parser('2')
91 subparser2 = subparsers.add_parser('2')
91 subparser2.add_argument('y')
92 subparser2.add_argument('y')
92
93
93 cl = MyLoader()
94 cl = MyLoader()
94 config = cl.load_config('2 frobble'.split())
95 config = cl.load_config('2 frobble'.split())
95 self.assertEquals(config.subparser_name, '2')
96 self.assertEquals(config.subparser_name, '2')
96 self.assertEquals(config.y, 'frobble')
97 self.assertEquals(config.y, 'frobble')
97 config = cl.load_config('1 -x frobble'.split())
98 config = cl.load_config('1 -x frobble'.split())
98 self.assertEquals(config.subparser_name, '1')
99 self.assertEquals(config.subparser_name, '1')
99 self.assertEquals(config.Global.x, 'frobble')
100 self.assertEquals(config.Global.x, 'frobble')
100
101
101 class TestConfig(TestCase):
102 class TestConfig(TestCase):
102
103
103 def test_setget(self):
104 def test_setget(self):
104 c = Config()
105 c = Config()
105 c.a = 10
106 c.a = 10
106 self.assertEquals(c.a, 10)
107 self.assertEquals(c.a, 10)
107 self.assertEquals(c.has_key('b'), False)
108 self.assertEquals(c.has_key('b'), False)
108
109
109 def test_auto_section(self):
110 def test_auto_section(self):
110 c = Config()
111 c = Config()
111 self.assertEquals(c.has_key('A'), True)
112 self.assertEquals(c.has_key('A'), True)
112 self.assertEquals(c._has_section('A'), False)
113 self.assertEquals(c._has_section('A'), False)
113 A = c.A
114 A = c.A
114 A.foo = 'hi there'
115 A.foo = 'hi there'
115 self.assertEquals(c._has_section('A'), True)
116 self.assertEquals(c._has_section('A'), True)
116 self.assertEquals(c.A.foo, 'hi there')
117 self.assertEquals(c.A.foo, 'hi there')
117 del c.A
118 del c.A
118 self.assertEquals(len(c.A.keys()),0)
119 self.assertEquals(len(c.A.keys()),0)
119
120
120 def test_merge_doesnt_exist(self):
121 def test_merge_doesnt_exist(self):
121 c1 = Config()
122 c1 = Config()
122 c2 = Config()
123 c2 = Config()
123 c2.bar = 10
124 c2.bar = 10
124 c2.Foo.bar = 10
125 c2.Foo.bar = 10
125 c1._merge(c2)
126 c1._merge(c2)
126 self.assertEquals(c1.Foo.bar, 10)
127 self.assertEquals(c1.Foo.bar, 10)
127 self.assertEquals(c1.bar, 10)
128 self.assertEquals(c1.bar, 10)
128 c2.Bar.bar = 10
129 c2.Bar.bar = 10
129 c1._merge(c2)
130 c1._merge(c2)
130 self.assertEquals(c1.Bar.bar, 10)
131 self.assertEquals(c1.Bar.bar, 10)
131
132
132 def test_merge_exists(self):
133 def test_merge_exists(self):
133 c1 = Config()
134 c1 = Config()
134 c2 = Config()
135 c2 = Config()
135 c1.Foo.bar = 10
136 c1.Foo.bar = 10
136 c1.Foo.bam = 30
137 c1.Foo.bam = 30
137 c2.Foo.bar = 20
138 c2.Foo.bar = 20
138 c2.Foo.wow = 40
139 c2.Foo.wow = 40
139 c1._merge(c2)
140 c1._merge(c2)
140 self.assertEquals(c1.Foo.bam, 30)
141 self.assertEquals(c1.Foo.bam, 30)
141 self.assertEquals(c1.Foo.bar, 20)
142 self.assertEquals(c1.Foo.bar, 20)
142 self.assertEquals(c1.Foo.wow, 40)
143 self.assertEquals(c1.Foo.wow, 40)
143 c2.Foo.Bam.bam = 10
144 c2.Foo.Bam.bam = 10
144 c1._merge(c2)
145 c1._merge(c2)
145 self.assertEquals(c1.Foo.Bam.bam, 10)
146 self.assertEquals(c1.Foo.Bam.bam, 10)
146
147
147 def test_deepcopy(self):
148 def test_deepcopy(self):
148 c1 = Config()
149 c1 = Config()
149 c1.Foo.bar = 10
150 c1.Foo.bar = 10
150 c1.Foo.bam = 30
151 c1.Foo.bam = 30
151 c1.a = 'asdf'
152 c1.a = 'asdf'
152 c1.b = range(10)
153 c1.b = range(10)
153 import copy
154 import copy
154 c2 = copy.deepcopy(c1)
155 c2 = copy.deepcopy(c1)
155 self.assertEquals(c1, c2)
156 self.assertEquals(c1, c2)
156 self.assert_(c1 is not c2)
157 self.assert_(c1 is not c2)
157 self.assert_(c1.Foo is not c2.Foo)
158 self.assert_(c1.Foo is not c2.Foo)
158
159
159 def test_builtin(self):
160 def test_builtin(self):
160 c1 = Config()
161 c1 = Config()
161 exec 'foo = True' in c1
162 exec 'foo = True' in c1
162 self.assertEquals(c1.foo, True)
163 self.assertEquals(c1.foo, True)
163 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
164 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
General Comments 0
You need to be logged in to leave comments. Login now