view martini/tests/test_config.txt @ 16:8ae3a7fd466a

py3
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Feb 2017 14:56:46 -0800
parents 81aed4352851
children
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.move_section('foo', 'oof')
     >>> sorted(munger.sections())
     ['DEFAULTS', 'oof']
     >>> munger['oof']
     {'baz': 'fleem'}

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

     >>> munger = ConfigMunger()
     >>> munger.read("[Jeff Hammel]\naddress = 639 W. 173 St.\n Apt. 11D\n New York, NY  10032")
     >>> munger.get('Jeff Hammel', 'address')
     '639 W. 173 St.\nApt. 11D\nNew York, NY  10032'

Test the ability to keep string case:

     >>> munger = ConfigMunger()
     >>> munger.read("[Foo]\nBar=fleem\nfOo=blah\nbAr=flu")
     >>> munger.write()
     [Foo]
     Bar = fleem
     bAr = flu
     fOo = blah
     
Test the ability to keep section order:
     >>> munger = ConfigMunger()
     >>> munger.read('[foo]\n[bar]\n[fleem]\n[baz]\n[best]')
     >>> munger.sections()
     ['foo', 'bar', 'fleem', 'baz', 'best']