Permalink
Browse files

Allow setting defaults on BoolFields (defaulted to False before)

  • Loading branch information...
schmichael committed Sep 29, 2011
1 parent 4468d48 commit f4c223ede9c09d1933fcd5d475594ca0e13b87e1
Showing with 12 additions and 8 deletions.
  1. +4 −0 mmstats.py
  2. +8 −8 tests/test_types.py
View
@@ -272,6 +272,10 @@ class BoolField(ReadWriteField):
buffer_type = ctypes.c_byte
type_signature = '?'
def __init__(self, initial=False, **kwargs):
self.initial = initial
super(BoolField, self).__init__(**kwargs)
def __get__(self, inst, owner):
if inst is None:
return self
View
@@ -57,23 +57,23 @@ class ShortFields(mmstats.MmStats):
def test_bools(self):
class BoolFields(mmstats.MmStats):
a = mmstats.BoolField()
b = mmstats.BoolField()
b = mmstats.BoolField(initial=True)
s = BoolFields(filename='mmstats-test-bools')
self.assertTrue('a\x01\x00?\xff\x00' in s._mmap[:], repr(s._mmap[:30]))
self.assertTrue('b\x01\x00?\xff\x00' in s._mmap[:], repr(s._mmap[:30]))
self.assertTrue('b\x01\x00?\xff\x01' in s._mmap[:], repr(s._mmap[:30]))
self.assertTrue(s.a is False, s.a)
self.assertTrue(s.b is False, s.b)
self.assertTrue(s.b is True, s.b)
s.a = 'Anything truthy at all'
self.assertTrue(s.a is True, s.a)
self.assertTrue(s.b is False, s.b)
self.assertTrue(s.b is True, s.b)
s.a = [] # Anything falsey
self.assertTrue(s.a is False, s.a)
self.assertTrue(s.b is False, s.b)
s.b = 1
s.a = s.b
self.assertTrue(s.a is True, s.a)
self.assertTrue(s.b is True, s.b)
s.b = False
s.a = s.b
self.assertTrue(s.a is False, s.a)
self.assertTrue(s.b is False, s.b)
def test_strings(self):
class StringStats(mmstats.BaseMmStats):

0 comments on commit f4c223e

Please sign in to comment.