1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA verbose and debug output"""
10
11 import unittest, re
12 from StringIO import StringIO
13
14 from mvpa.base.verbosity import OnceLogger
15
16 from mvpa.base import verbose, error
17
18 if __debug__:
19 from mvpa.base import debug
20 debug.register('1', 'id 1')
21 debug.register('2', 'id 2')
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
40
60
68
69
71 """Test if it doesn't output at higher levels"""
72 verbose(5, self.msg)
73 self.failUnlessEqual(self.sout.getvalue(), "")
74
75
77 """Test if outputs at lower levels and indents
78 by default with spaces
79 """
80 verbose(2, self.msg)
81 self.failUnlessEqual(self.sout.getvalue(),
82 " %s\n" % self.msg)
83
85 """Test indent symbol
86 """
87 verbose.indent = "."
88 verbose(2, self.msg)
89 self.failUnlessEqual(self.sout.getvalue(), "..%s\n" % self.msg)
90 verbose.indent = " "
91
93 """Test if chokes on negative level"""
94 self.failUnlessRaises( ValueError,
95 verbose._setLevel, -10 )
96
98 """Test if it works fine with no newline (LF) symbol"""
99 verbose(2, self.msg, lf=False)
100 verbose(2, " continue ", lf=False)
101 verbose(2, "end")
102 verbose(0, "new %s" % self.msg)
103 self.failUnlessEqual(self.sout.getvalue(),
104 " %s continue end\nnew %s\n" % \
105 (self.msg, self.msg))
106
108 """Test if works fine with carriage return (cr) symbol"""
109 verbose(2, self.msg, cr=True)
110 verbose(2, "rewrite", cr=True)
111 verbose(1, "rewrite 2", cr=True)
112 verbose(1, " add", cr=False, lf=False)
113 verbose(1, " finish")
114 target = '\r %s\r \rrewrite' % self.msg + \
115 '\r \rrewrite 2 add finish\n'
116 self.failUnlessEqual(self.sout.getvalue(), target)
117
119 """Test once logger"""
120 self.once("X", self.msg)
121 self.once("X", self.msg)
122 self.failUnlessEqual(self.sout.getvalue(), self.msg+"\n")
123
124 self.once("Y", "XXX", 2)
125 self.once("Y", "XXX", 2)
126 self.once("Y", "XXX", 2)
127 self.failUnlessEqual(self.sout.getvalue(), self.msg+"\nXXX\nXXX\n")
128
129
131 """Test error message"""
132 error(self.msg, critical=False)
133 self.failUnless(self.sout.getvalue().startswith("ERROR"))
134
135
136 if __debug__:
138 verbose.handlers = []
139 debug.active = ['1', '2', 'SLC']
140
141 debug('SLC', self.msg, lf=False)
142 self.failUnlessRaises(ValueError, debug, 3, 'bugga')
143
144 svalue = self.sout.getvalue()
145 regexp = "\[SLC\] DBG(?:{.*})?: %s" % self.msg
146 rematch = re.match(regexp, svalue)
147 self.failUnless(rematch, msg="Cannot match %s with regexp %s" %
148 (svalue, regexp))
149
150
166
167
168
169
172
173
174 if __name__ == '__main__':
175 import runner
176