view martini/tests/test_config.txt @ 2:d172c078f4a2

add -O option for specifying output file
author k0s <k0scist@gmail.com>
date Tue, 08 Dec 2009 16:23:50 -0500
parents 3c3522ce6e3a
children 09bed87f7fa4
line wrap: on
line source

Test martini.config
=====================

The obligatory imports:

    >>> import sys
    >>> from pprint import pprint
    >>> from martini.config import ConfigMunger
    >>> from ConfigParser import InterpolationMissingOptionError
    >>> from StringIO import StringIO

Make the munger and some .ini files:

     >>> munger = ConfigMunger()
     >>> foo = StringIO("[foo]\nbar = fleem")
     
Test basic reading + writing functionality:

     >>> munger.read(foo)
     >>> munger.write(sys.stdout)
     [foo]
     bar = fleem

Test overriding:
     
     >>> munger.read("[foo]\nblah = florg\nbar=thorg")
     >>> munger.write(sys.stdout)
     [foo]
     bar = thorg
     blah = florg

Test variable interpolation:

     >>> munger.read("[variable]\nvar=%(value)s")
     >>> buffer = StringIO()
     >>> try:
     ...	munger.write(buffer)
     ... except InterpolationMissingOptionError, e:
     ... 	print e.reference
     value
     >>> munger.write(sys.stdout, vars=dict(value='interpolated'))
     [foo]
     bar = thorg
     blah = florg
     <BLANKLINE>
     [variable]
     var = interpolated

Testing dict-like functionality:

	>>> munger = ConfigMunger()
	>>> munger.read("[foo]\nblah = florg\nbar=thorg")
	>>> munger['foo']['bar']
	'thorg'
	>>> pprint(munger.dict())
	{'foo': {'bar': 'thorg', 'blah': 'florg'}}
	>>> pprint(munger['foo'])
	{'bar': 'thorg', 'blah': 'florg'}

You can read from a dictionary too:
    
    >>> munger = ConfigMunger()
    >>> munger.read({'foo': {'bar': 'baz'}})
    >>> munger['foo']['bar']
    'baz'
    >>> munger.write(sys.stdout)
    [foo]
    bar = baz
	
Test the ability to parse sectionless .ini files:

    >>> munger = ConfigMunger()
    >>> munger.read("foo = bar\nbaz=fleem")
    >>> munger.sections()
    ['DEFAULTS']
    >>> munger['DEFAULTS']
    {'foo': 'bar', 'baz': 'fleem'}
    >>> munger.write(sys.stdout)
    [DEFAULTS]
    baz = fleem
    foo = bar
    >>> munger = ConfigMunger()
    >>> munger.read("foo = bar\n\n[foo]\nbaz=fleem")
    >>> sorted(munger.sections())
    ['DEFAULTS', 'foo']
    >>> munger['DEFAULTS']['foo']
    'bar'
    >>> munger['foo']['baz']
    'fleem'

Move a secton around:
     
     >>> munger.rename_section('foo', 'oof')
     >>> sorted(munger.sections())
     ['DEFAULTS', 'oof']
     >>> munger['oof']
     {'baz': 'fleem'}

Test the ability to parse multi-line .ini files [TODO]:

     >>> munger = ConfigMunger()
     >>> # munger.read("[Jeff Hammel]\naddress = 639 W. 173 St.\nApt. 11D\nNew York, NY  10032")