Commit de0561d0 authored by Mark Hymers's avatar Mark Hymers

Test options

Signed-off-by: Mark Hymers's avatarMark Hymers <mhy@debian.org>
parent e5169e0e
......@@ -26,12 +26,12 @@ class AnamOptions(object):
self.verbose = 0
self.progress = 0
def write(self, s):
def write(self, s, target=stdout):
if self.verbose > 0:
stdout.write(s)
target.write(s)
def write_progress(self, s):
def write_progress(self, s, target=stdout):
if self.progress > 0:
stdout.write(s)
target.write(s)
__all__.append('AnamOptions')
#!/usr/bin/python
"""Tests for options module"""
# vim: set expandtab ts=4 sw=4:
from StringIO import StringIO
from ..test import AnamTestBase
class OptionsTest(AnamTestBase):
def test_init_options(self):
"""Check that we can initialise a the global AnamOptions object"""
from ..options import AnamOptions
a = AnamOptions()
assert(isinstance(a, AnamOptions))
def test_empty_store_equality(self):
"""Check that we can set and get the verbose and progress options"""
from ..options import AnamOptions
a = AnamOptions()
# Check we default to 0
assert(a.verbose == 0)
assert(a.progress == 0)
# Increment both
a.verbose += 1
a.progress += 2
# We should see this on another "instance" of the object
b = AnamOptions()
assert(b.verbose == 1)
assert(b.progress == 2)
# Put them back
b.verbose = 0
b.progress = 0
def test_write_progress(self):
"""Check that we write appropriately with progress set"""
from sys import stdout
from ..options import AnamOptions
a = AnamOptions()
a.progress = 0
s = StringIO()
a.write_progress("Should not be written", target=s)
s.seek(0)
assert(len(s.read()) == 0)
s.close()
# And now when we should get something
a.progress = 1
s = StringIO()
a.write_progress("Should be written", target=s)
s.seek(0)
t = s.read()
print(len(t), t)
print(len("Should be written"))
assert(t == "Should be written")
s.close()
def test_write_verbose(self):
"""Check that we write appropriately with verbose set"""
from sys import stdout
from ..options import AnamOptions
a = AnamOptions()
a.verbose = 0
s = StringIO()
a.write("Should not be written", target=s)
s.seek(0)
assert(len(s.read()) == 0)
s.close()
# And now when we should get something
a.verbose = 1
s = StringIO()
a.write("Should be written", target=s)
s.seek(0)
t = s.read()
print(len(t), t)
print(len("Should be written"))
assert(t == "Should be written")
s.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment