File: test_log.py

package info (click to toggle)
openstructure 2.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 205,228 kB
  • sloc: cpp: 188,129; python: 35,361; ansic: 34,298; fortran: 3,275; sh: 286; xml: 146; makefile: 29
file content (61 lines) | stat: -rw-r--r-- 1,938 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import unittest
import ost

# helper log sink to campture messages
class _CapturingLogSink(ost.LogSink):
  def __init__(self):
    ost.LogSink.__init__(self)
  def LogMessage(self, message, severity):
    self.message = message
    self.severity = severity

# Altough the logging system might appear to be too simple to be worth writing a 
# specific test case for, it actually isn't. The python export is very fragile 
# and seemingly trivial changes can break the code in unexpected ways. So let's 
# check for some invariants
class TestLog(unittest.TestCase):

  def testGetLogSink(self):
    logsink = ost.GetCurrentLogSink()
    self.assertTrue(hasattr(logsink, 'LogMessage'))
    # Check if the return type of logsink is sane
    ost.PushLogSink(ost.GetCurrentLogSink())

  def testPushPopLogSink(self):
    class MyLogSink(ost.LogSink):
       def __init__(self):
         ost.LogSink.__init__(self)
    ls = MyLogSink()
    ost.PushLogSink(ls)
    self.assertEqual(ls, ost.GetCurrentLogSink())
    ost.PopLogSink()
    self.assertNotEqual(ls, ost.GetCurrentLogSink())

  def testLogMessage(self):
    ls = _CapturingLogSink()
    ost.PushVerbosityLevel(1)
    ost.PushLogSink(ls)
    ost.LogError('error message')
    self.assertEqual(ls.message, 'error message\n')
    self.assertEqual(ls.severity, 0)
    ost.LogWarning(1, 2, 3)
    self.assertEqual(ls.message, '1 2 3\n')
    self.assertEqual(ls.severity, 1)
    ost.PopLogSink()

  def testLogMultipleMessages(self):
    # observation: converting non-strings in logging can break following calls
    ls = _CapturingLogSink()
    ost.PushVerbosityLevel(1)
    ost.PushLogSink(ls)
    ost.LogWarning('foo')
    self.assertEqual(ls.message, 'foo\n')
    ost.LogWarning(1)
    self.assertEqual(ls.message, '1\n')
    ost.LogWarning('bar')
    self.assertEqual(ls.message, 'bar\n')
    ost.PopLogSink()

if __name__ == "__main__":
  from ost import testutils
  testutils.RunTests()