From b76004e7a1b837921489e11b1050a3d2a45655df Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 27 May 2014 13:36:11 +0200 Subject: [PATCH] Add tests for LogFilter --- qutebrowser/test/utils/test_log.py | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 qutebrowser/test/utils/test_log.py diff --git a/qutebrowser/test/utils/test_log.py b/qutebrowser/test/utils/test_log.py new file mode 100644 index 000000000..e0cdb0d8f --- /dev/null +++ b/qutebrowser/test/utils/test_log.py @@ -0,0 +1,83 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser 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. +# +# qutebrowser 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 qutebrowser. If not, see . + +"""Tests for qutebrowser.utils.log.""" + +import logging +import unittest +from unittest import TestCase + +import qutebrowser.utils.log as log + + +class LogFilterTests(TestCase): + + """Tests for LogFilter. + + Attributes: + logger: The logger we use to create records. + """ + + def setUp(self): + self.logger = logging.getLogger("foo") + + def _make_record(self, name): + """Create a bogus logging record with the supplied logger name.""" + return self.logger.makeRecord(name, level=logging.DEBUG, fn=None, + lno=0, msg="", args=None, exc_info=None) + + def test_empty(self): + """Test if an empty filter lets all messages through.""" + logfilter = log.LogFilter(None) + record = self._make_record("eggs.bacon.spam") + self.assertTrue(logfilter.filter(record)) + record = self._make_record("eggs") + self.assertTrue(logfilter.filter(record)) + + def test_matching(self): + """Test if a filter lets an exactly matching logrecord through.""" + logfilter = log.LogFilter(["eggs", "bacon"]) + record = self._make_record("eggs") + self.assertTrue(logfilter.filter(record)) + record = self._make_record("bacon") + self.assertTrue(logfilter.filter(record)) + record = self._make_record("spam") + self.assertFalse(logfilter.filter(record)) + logfilter = log.LogFilter(["eggs.bacon"]) + record = self._make_record("eggs.bacon") + self.assertTrue(logfilter.filter(record)) + + def test_equal_start(self): + """Test if a filter blocks a logger which looks equal but isn't.""" + logfilter = log.LogFilter(["eggs"]) + record = self._make_record("eggsauce") + self.assertFalse(logfilter.filter(record)) + logfilter = log.LogFilter("eggs.bacon") + record = self._make_record("eggs.baconstrips") + self.assertFalse(logfilter.filter(record)) + + def test_child(self): + """Test if a filter lets through a logger which is a child.""" + logfilter = log.LogFilter(["eggs.bacon", "spam.ham"]) + record = self._make_record("eggs.bacon.spam") + self.assertTrue(logfilter.filter(record)) + record = self._make_record("spam.ham.salami") + self.assertTrue(logfilter.filter(record)) + + +if __name__ == '__main__': + unittest.main()