Package mvpa :: Package tests :: Module test_verbosity
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_verbosity

  1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  # vi: set ft=python sts=4 ts=4 sw=4 et: 
  3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
  4  # 
  5  #   See COPYING file distributed along with the PyMVPA package for the 
  6  #   copyright and license terms. 
  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')           # needed for testing 
 21      debug.register('2', 'id 2') 
 22   
 23   
 24  ## XXX There must be smth analogous in python... don't know it yet 
 25  # And it is StringIO 
 26  #class StringStream(object): 
 27  #    def __init__(self): 
 28  #        self.__str = "" 
 29  # 
 30  #    def __repr__(self): 
 31  #        return self.__str 
 32  # 
 33  #    def write(self, s): 
 34  #        self.__str += s 
 35  # 
 36  #    def clean(self): 
 37  #        self.__str = "" 
 38  # 
39 -class VerboseOutputTest(unittest.TestCase):
40
41 - def setUp(self):
42 self.msg = "Test level 2" 43 # output stream 44 self.sout = StringIO() 45 46 self.once = OnceLogger(handlers=[self.sout]) 47 48 # set verbose to 4th level 49 self.__oldverbosehandlers = verbose.handlers 50 verbose.handlers = [] # so debug doesn't spoil it 51 verbose.level = 4 52 if __debug__: 53 self.__olddebughandlers = debug.handlers 54 self.__olddebugactive = debug.active 55 debug.active = ['1', '2', 'SLC'] 56 debug.handlers = [self.sout] 57 debug.offsetbydepth = False 58 59 verbose.handlers = [self.sout]
60
61 - def tearDown(self):
62 if __debug__: 63 debug.active = self.__olddebugactive 64 debug.handlers = self.__olddebughandlers 65 debug.offsetbydepth = True 66 verbose.handlers = self.__oldverbosehandlers 67 self.sout.close()
68 69
70 - def testVerboseAbove(self):
71 """Test if it doesn't output at higher levels""" 72 verbose(5, self.msg) 73 self.failUnlessEqual(self.sout.getvalue(), "")
74 75
76 - def testVerboseBelow(self):
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
84 - def testVerboseIndent(self):
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 = " " # restore
91
92 - def testVerboseNegative(self):
93 """Test if chokes on negative level""" 94 self.failUnlessRaises( ValueError, 95 verbose._setLevel, -10 )
96
97 - def testNoLF(self):
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
107 - def testCR(self):
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
118 - def testOnceLogger(self):
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
130 - def testError(self):
131 """Test error message""" 132 error(self.msg, critical=False) # should not exit 133 self.failUnless(self.sout.getvalue().startswith("ERROR"))
134 135 136 if __debug__:
137 - def testDebug(self):
138 verbose.handlers = [] # so debug doesn't spoil it 139 debug.active = ['1', '2', 'SLC'] 140 # do not offset for this test 141 debug('SLC', self.msg, lf=False) 142 self.failUnlessRaises(ValueError, debug, 3, 'bugga') 143 #Should complain about unknown debug id 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
151 - def testDebugRgexp(self):
152 verbose.handlers = [] # so debug doesn't spoil it 153 debug.active = ['.*'] 154 # we should have enabled all of them 155 self.failUnlessEqual(set(debug.active), 156 set(debug.registered.keys())) 157 debug.active = ['S.*', 'CLF'] 158 self.failUnlessEqual(set(debug.active), 159 set(filter(lambda x:x.startswith('S'), 160 debug.registered.keys())+['CLF'])) 161 debug.active = ['SG', 'CLF'] 162 self.failUnlessEqual(set(debug.active), set(['SG', 'CLF']), 163 msg="debug should do full line matching") 164 165 debug.offsetbydepth = True
166 167 168 # TODO: More tests needed for debug output testing 169
170 -def suite():
171 return unittest.makeSuite(VerboseOutputTest)
172 173 174 if __name__ == '__main__': 175 import runner 176