implemented null object design pattern
This commit is contained in:
75
pypatterns/behavioral/null.py
Normal file
75
pypatterns/behavioral/null.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
class Null(object):
|
||||||
|
"""
|
||||||
|
A class for implementing Null objects.
|
||||||
|
|
||||||
|
This class ignores all parameters passed when constructing or
|
||||||
|
calling instances and traps all attribute and method requests.
|
||||||
|
Instances of it always (and reliably) do 'nothing'.
|
||||||
|
|
||||||
|
The code might benefit from implementing some further special
|
||||||
|
Python methods depending on the context in which its instances
|
||||||
|
are used. Especially when comparing and coercing Null objects
|
||||||
|
the respective methods' implementation will depend very much
|
||||||
|
on the environment and, hence, these special methods are not
|
||||||
|
provided here.
|
||||||
|
|
||||||
|
Dinu C. Gherman,
|
||||||
|
August 2001
|
||||||
|
http://code.activestate.com/recipes/68205-null-object-design-pattern/
|
||||||
|
"""
|
||||||
|
|
||||||
|
# object constructing
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Ignore parameters.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
# object calling
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Ignore method calls.
|
||||||
|
"""
|
||||||
|
return self
|
||||||
|
|
||||||
|
# attribute handling
|
||||||
|
|
||||||
|
def __getattr__(self, mname):
|
||||||
|
"""
|
||||||
|
Ignore attribute requests.
|
||||||
|
"""
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
"""
|
||||||
|
Ignore attribute setting.
|
||||||
|
"""
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __delattr__(self, name):
|
||||||
|
"""
|
||||||
|
Ignore deleting attributes.
|
||||||
|
"""
|
||||||
|
return self
|
||||||
|
|
||||||
|
# misc.
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
"""
|
||||||
|
Return a string representation.
|
||||||
|
"""
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""
|
||||||
|
Convert to a string and return it.
|
||||||
|
"""
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def __bool__(self):
|
||||||
|
"""
|
||||||
|
Boolean of Null object is always False.
|
||||||
|
"""
|
||||||
|
return False
|
||||||
80
tests/behavioral_tests/test_null.py
Normal file
80
tests/behavioral_tests/test_null.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
from pypatterns.behavioral.null import Null
|
||||||
|
|
||||||
|
|
||||||
|
class NullTestCase(TestCase):
|
||||||
|
"""
|
||||||
|
Unit testing class for the Null class.
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
"""
|
||||||
|
Initialize testing data.
|
||||||
|
"""
|
||||||
|
self.null = Null()
|
||||||
|
|
||||||
|
def test_constructing(self):
|
||||||
|
"""
|
||||||
|
Test constructing a Null object.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
n = Null()
|
||||||
|
n = Null('value')
|
||||||
|
n = Null('value', param='value')
|
||||||
|
except:
|
||||||
|
raise AssertionError()
|
||||||
|
|
||||||
|
def test_calling(self):
|
||||||
|
"""
|
||||||
|
Test calling a Null object.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
|
self.assertEquals(self.null, self.null())
|
||||||
|
self.assertEquals(self.null, self.null('value'))
|
||||||
|
self.assertEquals(self.null, self.null('value', param='value'))
|
||||||
|
|
||||||
|
def test_attribute_handling(self):
|
||||||
|
"""
|
||||||
|
Test attribute handling on a Null object.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
|
self.assertEquals(self.null, self.null.attr1)
|
||||||
|
self.assertEquals(self.null, self.null.attr2)
|
||||||
|
self.assertEquals(self.null, self.null.method1())
|
||||||
|
self.assertEquals(self.null, self.null.method1().method2())
|
||||||
|
self.assertEquals(self.null, self.null.method('value'))
|
||||||
|
self.assertEquals(self.null, self.null.method(param='value'))
|
||||||
|
self.assertEquals(self.null, self.null.method('value', param='value'))
|
||||||
|
self.assertEquals(self.null, self.null.attr1.method1())
|
||||||
|
self.assertEquals(self.null, self.null.method1().attr1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.null.attr1 = 'value'
|
||||||
|
self.null.attr1.attr2 = 'value'
|
||||||
|
del self.null.attr1
|
||||||
|
del self.null.attr1.attr2.attr3
|
||||||
|
except:
|
||||||
|
raise AssertionError()
|
||||||
|
|
||||||
|
def test_string_representation(self):
|
||||||
|
"""
|
||||||
|
Test the string representation of a Null object.
|
||||||
|
|
||||||
|
@raise AssertionError:
|
||||||
|
"""
|
||||||
|
self.assertEquals('', repr(self.null))
|
||||||
|
self.assertEquals('', str(self.null))
|
||||||
|
|
||||||
|
def test_truthiness(self):
|
||||||
|
"""
|
||||||
|
Test the truthiness of a Null object.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
|
self.assertFalse(bool(self.null))
|
||||||
|
self.assertFalse(bool(self.null.attr1))
|
||||||
|
self.assertFalse(bool(self.null.attr1.method1()))
|
||||||
|
self.assertFalse(bool(self.null.method2().attr2.method1().attr1))
|
||||||
Reference in New Issue
Block a user