
commit d30e229c325bc243697d1d015a7bda03082c9756 Author: Damian Johnson <atagar@torproject.org> Date: Sun Nov 13 15:02:47 2011 -0800 Unit tests for enum --- run_tests.py | 2 + test/unit/util/__init__.py | 6 ++++ test/unit/util/enum.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 0 deletions(-) diff --git a/run_tests.py b/run_tests.py index 1107a59..530466a 100755 --- a/run_tests.py +++ b/run_tests.py @@ -16,6 +16,7 @@ import test.unit.types.control_message import test.unit.types.control_line import test.unit.types.version import test.unit.connection.protocolinfo_response +import test.unit.util.enum import test.integ.message import test.integ.system @@ -31,6 +32,7 @@ UNIT_TESTS = (("stem.types.ControlMessage", test.unit.types.control_message.Test ("stem.types.ControlLine", test.unit.types.control_line.TestControlLine), ("stem.types.Version", test.unit.types.version.TestVerion), ("stem.connection.ProtocolInfoResponse", test.unit.connection.protocolinfo_response.TestProtocolInfoResponse), + ("stem.util.enum", test.unit.util.enum.TestEnum), ) INTEG_TESTS = (("stem.types.ControlMessage", test.integ.message.TestMessageFunctions), diff --git a/test/unit/util/__init__.py b/test/unit/util/__init__.py new file mode 100644 index 0000000..84f12bf --- /dev/null +++ b/test/unit/util/__init__.py @@ -0,0 +1,6 @@ +""" +Unit tests for stem.util.* contents. +""" + +__all__ = ["enum"] + diff --git a/test/unit/util/enum.py b/test/unit/util/enum.py new file mode 100644 index 0000000..abdff06 --- /dev/null +++ b/test/unit/util/enum.py @@ -0,0 +1,59 @@ +""" +Unit tests for the stem.util.enum class and functions. +""" + +import unittest +import stem.util.enum + +class TestEnum(unittest.TestCase): + """ + Tests the stem.util.enum contents. + """ + + def test_to_camel_case(self): + """ + Checks the stem.util.enum.to_camel_case function. + """ + + # test the pydoc example + self.assertEquals("I Like Pepperjack!", stem.util.enum.to_camel_case("I_LIKE_PEPPERJACK!")) + + # check a few edge cases + self.assertEquals("", stem.util.enum.to_camel_case("")) + self.assertEquals("Hello", stem.util.enum.to_camel_case("hello")) + self.assertEquals("Hello", stem.util.enum.to_camel_case("HELLO")) + self.assertEquals("Hello World", stem.util.enum.to_camel_case("hello__world")) + self.assertEquals("Hello\tworld", stem.util.enum.to_camel_case("hello\tWORLD")) + self.assertEquals("Hello\t\tWorld", stem.util.enum.to_camel_case("hello__world", "\t")) + + def test_enum_examples(self): + """ + Checks that the pydoc examples are accurate. + """ + + insects = stem.util.enum.Enum("ANT", "WASP", "LADYBUG", "FIREFLY") + self.assertEquals("Ant", insects.ANT) + self.assertEquals(("Ant", "Wasp", "Ladybug", "Firefly"), insects.values()) + + pets = stem.util.enum.Enum(("DOG", "Skippy"), "CAT", ("FISH", "Nemo")) + self.assertEquals("Skippy", pets.DOG) + self.assertEquals("Cat", pets.CAT) + + def test_enum_methods(self): + """ + Exercises enumeration methods. + """ + + insects = stem.util.enum.Enum("ANT", "WASP", "LADYBUG", "FIREFLY") + + # next method + self.assertEquals(insects.WASP, insects.next(insects.ANT)) + self.assertEquals(insects.ANT, insects.next(insects.FIREFLY)) + + # previous method + self.assertEquals(insects.FIREFLY, insects.previous(insects.ANT)) + self.assertEquals(insects.LADYBUG, insects.previous(insects.FIREFLY)) + + # __iter__ method + self.assertEquals(("ANT", "WASP", "LADYBUG", "FIREFLY"), tuple(insects)) +