Skip to content
Permalink
Browse files

more container tests

  • Loading branch information
wsanchez committed Feb 6, 2014
1 parent 0abf7c1 commit 3666a965c82fbf37aedc3ea140559c14cab82ec0
Showing with 69 additions and 2 deletions.
  1. +69 −2 twext/who/test/test_util.py
@@ -22,7 +22,7 @@

from twisted.trial import unittest
from twisted.python.constants import (
Names, NamedConstant, Flags, FlagConstant,
Names, NamedConstant, Values, ValueConstant, Flags, FlagConstant
)

from ..idirectory import DirectoryServiceError
@@ -59,6 +59,27 @@ class Instruments(Names):



class Statuses(Values):
OK = ValueConstant(200)
NOT_OK = ValueConstant(500)


@staticmethod
def isError(status):
return status.value < 500



class MoreStatuses(Values):
MOOLAH = ValueConstant(402)


@staticmethod
def isError(status):
return status.value < 400



class Switches(Flags):
r = FlagConstant()
g = FlagConstant()
@@ -98,6 +119,21 @@ def test_constants_from_containers(self):
)


def test_constants_from_constantsContainers(self):
"""
Initialize a container from other L{ConstantsContainer}s.
"""
self.assertEquals(
set(
ConstantsContainer((
ConstantsContainer((Tools,)),
ConstantsContainer((MoreTools,)),
)).iterconstants()
),
set(chain(Tools.iterconstants(), MoreTools.iterconstants())),
)


def test_constants_from_iterables(self):
"""
Initialize a container from iterables of constants.
@@ -114,7 +150,7 @@ def test_constants_from_iterables(self):

def test_conflictingClasses(self):
"""
A container can't contain two constants with the same name.
A container can't contain two constants with different types.
"""
self.assertRaises(TypeError, ConstantsContainer, (Tools, Switches))

@@ -126,6 +162,13 @@ def test_conflictingNames(self):
self.assertRaises(ValueError, ConstantsContainer, (Tools, Instruments))


def test_notConstantClass(self):
"""
A container can't contain random classes.
"""
self.assertRaises(TypeError, ConstantsContainer, (self.__class__,))


def test_attrs(self):
"""
Constants are assessible via attributes.
@@ -159,12 +202,23 @@ def test_staticmethod(self):
Static methods from source containers are accessible via attributes.
"""
container = ConstantsContainer((Tools, MoreTools))

self.assertTrue(container.isPounder(container.hammer))
self.assertTrue(container.isPounder(container.mallet))
self.assertFalse(container.isPounder(container.screwdriver))
self.assertFalse(container.isPounder(container.saw))


def test_conflictingMethods(self):
"""
A container can't contain two static methods with the same name.
"""
self.assertRaises(
ValueError, ConstantsContainer, (Statuses, MoreStatuses)
)



def test_lookupByName(self):
"""
Constants are assessible via L{ConstantsContainer.lookupByName}.
@@ -195,6 +249,19 @@ def test_lookupByName(self):
)


def test_lookupByValue(self):
"""
Containers with L{ValueConstant}s are assessible via
L{ConstantsContainer.lookupByValue}.
"""
container = ConstantsContainer((Statuses,))

self.assertEquals(container.lookupByValue(200), Statuses.OK)
self.assertEquals(container.lookupByValue(500), Statuses.NOT_OK)

self.assertRaises(ValueError, container.lookupByValue, 999)



class UtilTest(unittest.TestCase):
"""

0 comments on commit 3666a96

Please sign in to comment.
You can’t perform that action at this time.