From 0f201ade0f1db95f8ea0f4eadce7e3245aedbc10 Mon Sep 17 00:00:00 2001 From: bryce Date: Mon, 22 Jun 2015 15:39:42 -0700 Subject: [PATCH] tests: Make tests easier to run at a package level Add a run_tests script. Move tests subdir to top level. Force terminatorlib into path for tests. As of this commit, all tests pass: ~/src/Terminator/terminator$ ./run_tests tests/testborg.py tests/test_doctests.py tests/testsignalman.py 0/14 tests failed --- run_tests | 14 ++++++++++ tests/test_doctests.py | 22 +++++++++++++++ tests/testborg.py | 59 +++++++++++++++++++++++++++++++++++++++ tests/testsignalman.py | 63 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100755 run_tests create mode 100644 tests/test_doctests.py create mode 100755 tests/testborg.py create mode 100755 tests/testsignalman.py diff --git a/run_tests b/run_tests new file mode 100755 index 00000000..3bf51e4a --- /dev/null +++ b/run_tests @@ -0,0 +1,14 @@ +#!/bin/bash + +for t in tests/test*; do + echo $t + file_type=$(file -b $t) + case ${file_type} in + *[Pp]ython*) python ${t} ;; + *Bourne*) bash ${t} ;; + *bash*) bash ${t} ;; + *perl*) perl ${t} ;; + *) echo "Unknown" ;; + esac + echo +done diff --git a/tests/test_doctests.py b/tests/test_doctests.py new file mode 100644 index 00000000..6ca0d210 --- /dev/null +++ b/tests/test_doctests.py @@ -0,0 +1,22 @@ +"""Load up the tests.""" + +import os +import sys, os.path +sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))) + +from unittest import TestSuite +from doctest import DocTestSuite, ELLIPSIS + +def test_suite(): + suite = TestSuite() + for name in ( + 'config', + 'plugin', + 'cwd', + 'factory', + 'util', + 'tests.testborg', + 'tests.testsignalman', + ): + suite.addTest(DocTestSuite('terminatorlib.' + name)) + return suite diff --git a/tests/testborg.py b/tests/testborg.py new file mode 100755 index 00000000..38794019 --- /dev/null +++ b/tests/testborg.py @@ -0,0 +1,59 @@ +#!/usr/bin/python +# Terminator by Chris Jones +# GPL v2 only +"""testborg.py - We are the borg. Resistance is futile. + doctests for borg.py + +>>> obj1 = TestBorg() +>>> obj2 = TestBorg() +>>> obj1.attribute +0 +>>> obj2.attribute +0 +>>> obj1.attribute = 12345 +>>> obj1.attribute +12345 +>>> obj2.attribute +12345 +>>> obj2.attribute = 54321 +>>> obj1.attribute +54321 +>>> obj3 = TestBorg2() +>>> obj3.attribute +1 +>>> obj4 = TestBorg2() +>>> obj3.attribute = 98765 +>>> obj4.attribute +98765 +>>> + +""" + +import os +import sys, os.path +sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))) + +from terminatorlib.borg import Borg + +class TestBorg(Borg): + attribute = None + + def __init__(self): + Borg.__init__(self, self.__class__.__name__) + self.prepare_attributes() + + def prepare_attributes(self): + if not self.attribute: + self.attribute = 0 + +class TestBorg2(Borg): + attribute = None + + def __init__(self): + Borg.__init__(self, self.__class__.__name__) + self.prepare_attributes() + + def prepare_attributes(self): + if not self.attribute: + self.attribute = 1 + diff --git a/tests/testsignalman.py b/tests/testsignalman.py new file mode 100755 index 00000000..410c5f83 --- /dev/null +++ b/tests/testsignalman.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# Terminator by Chris Jones +# GPL v2 only +"""testsignalman.py - Test the signalman class + +>>> widget = TestWidget() +>>> signalman = Signalman() +>>> signalman.new(widget, 'test1', handler) +1 +>>> signalman.cnxids[widget].keys() +['test1'] +>>> widget.signals.values() +['test1'] +>>> signalman.remove_widget(widget) +>>> signalman.cnxids.has_key(widget) +False +>>> widget.signals.values() +[] +>>> signalman.new(widget, 'test2', handler) +2 +>>> signalman.new(widget, 'test3', handler) +3 +>>> signalman.remove_signal(widget, 'test2') +>>> signalman.cnxids[widget].keys() +['test3'] +>>> widget.signals.values() +['test3'] +>>> signalman.remove_widget(widget) +>>> + +""" + +import os +import sys, os.path +sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))) + +from terminatorlib.signalman import Signalman + +class TestWidget(): + signals = None + count = None + + def __init__(self): + self.signals = {} + self.count = 0 + + def connect(self, signal, handler, *args): + self.count = self.count + 1 + self.signals[self.count] = signal + return(self.count) + + def disconnect(self, signalid): + del(self.signals[signalid]) + +def handler(): + print "I am a test handler" + +if __name__ == '__main__': + import sys + import doctest + (failed, attempted) = doctest.testmod() + print "%d/%d tests failed" % (failed, attempted) + sys.exit(failed)