# Copyright 2015 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # =*= License: GPL-3+ =*= import StringIO import unittest import icklib class LoggerTests(unittest.TestCase): def test_logs_without_indentation_initially(self): f = StringIO.StringIO() logger = icklib.Logger() logger.add_output_file(f, quiet=False) logger.set_indentation(1) logger.log(False, 'foo') self.assertEqual(f.getvalue(), 'foo\n') def test_logs_with_one_level_of_indentation(self): f = StringIO.StringIO() logger = icklib.Logger() logger.add_output_file(f, quiet=False) logger.set_indentation(1) with logger: logger.log(False, 'foo') self.assertEqual(f.getvalue(), ' foo\n') def test_logs_with_keyword_arguments(self): f = StringIO.StringIO() logger = icklib.Logger() logger.add_output_file(f, quiet=False) logger.set_indentation(1) with logger: logger.log(False, '{foo} {bar}', foo=1, bar=2) self.assertEqual(f.getvalue(), ' 1 2\n') def test_logs_to_two_files(self): f1 = StringIO.StringIO() f2 = StringIO.StringIO() logger = icklib.Logger() logger.add_output_file(f1, quiet=False) logger.add_output_file(f2, quiet=False) with logger: logger.log(False, '{foo} {bar}', foo=1, bar=2) self.assertEqual(f1.getvalue(), f2.getvalue()) def test_stops_logging_to_file(self): f = StringIO.StringIO() logger = icklib.Logger() logger.add_output_file(f, quiet=False) logger.log(False, 'first') logger.drop_output_file(f) logger.log(False, 'second') self.assertNotIn('second', f.getvalue()) def test_only_logs_if_not_quieted(self): f = StringIO.StringIO() logger = icklib.Logger() logger.add_output_file(f, quiet=True) logger.log(True, 'foo') self.assertNotIn('foo', f.getvalue())