logger.py 2.1 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
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
62
63
64
65
66
# Copyright (c) 2010- The University of Notre Dame.
# This software is distributed under the GNU General Public License.
# See the file COPYING for details.

""" Weaver logger test units """

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/../..'))

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from unittest import TestCase, TestLoader, TestSuite, TextTestRunner

from weaver.logger import Logger

# Logger test case -------------------------------------------------------------

class LoggerTestCase(TestCase):

    logger = Logger(StringIO(), '[{flag}] {system:<10} {message}')

    def test_00_enable(self):
        self.logger.enable('logger')
        self.logger.enable(['a', 'b', 'c'])
    
    def test_01_disable(self):
        self.logger.disable('a')
        self.logger.disable(['a', 'b', 'c'])

    def test_02_debug(self):
        self.logger.stream.close()
        self.logger.stream = StringIO()
        self.logger.debug('asdf', 'hello, world')
        self.assertEqual(self.logger.stream.getvalue(), '')
    
    def test_03_debug(self):
        self.logger.debug('logger', 'debug')
        self.assertEqual(self.logger.stream.getvalue(), '[D] LOGGER     debug\n')
    
    def test_04_fatal(self):
        self.logger.stream.close()
        self.logger.stream = StringIO()
        self.logger.exit_on_fatal = False

        self.assertRaises(Exception, self.logger.fatal, 'logger', 'fatal')
        self.assertEqual(self.logger.stream.getvalue(), '[F] LOGGER     fatal\n')
    
    def test_05_warn(self):
        self.logger.stream.close()
        self.logger.stream = StringIO()

        self.logger.warn('logger', 'warn')
        self.assertEqual(self.logger.stream.getvalue(), '[W] LOGGER     warn\n')

# Main execution ---------------------------------------------------------------

if __name__ == '__main__':
    test_runner = TextTestRunner(verbosity = 2)
    test_suite  = TestLoader().loadTestsFromTestCase(LoggerTestCase)
    test_runner.run(test_suite)

Jerome Mariette's avatar
Jerome Mariette committed
67
# vim: set sts=4 sw=4 ts=8 expandtab ft=python: --------------------------------