view examples/doctest.txt @ 40:6b4c8f23192f

test and fix mixed output case
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 02 Jan 2011 00:48:58 -0800
parents a2cdce0108e1
children 9a0014a760d1
line wrap: on
line source

MakeItSo!
=========

Boilerplate:

    >>> import makeitso
    >>> import os
    >>> import shutil
    >>> import tempfile
    >>> from StringIO import StringIO

Basic functionality:

    >>> example = os.path.join(here, 'example.txt')
    >>> template = makeitso.PolyTemplate([example], interactive=False)
    >>> template.missing()
    set(['name'])
    >>> template.substitute(name='foo')
    Hello foo
    
Substitute to a buffer:

    >>> buffer = StringIO()
    >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False)
    >>> template.substitute(name='bar')
    >>> buffer.getvalue().strip()
    'Hello bar'

Substitute to a file:

    >>> buffer = tempfile.mktemp()
    >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False)
    >>> template.substitute(name='fleem')
    >>> file(buffer).read().strip()
    'Hello fleem'
    >>> os.remove(buffer)

Directory case:

    >>> exampledir = os.path.join(here, 'directory-example')
    >>> tempdir = tempfile.mkdtemp()
    >>> template = makeitso.PolyTemplate([exampledir], output=tempdir, interactive=False)
    >>> sorted(template.missing())
    ['bar', 'foo', 'subdir']
    >>> template.substitute(foo='It', bar='life', subdir='mysubdir')
    >>> sorted(os.listdir(tempdir))
    ['foo.txt', 'mysubdir']
    >>> file(os.path.join(tempdir, 'foo.txt')).read().strip()
    'It is a wonderful life'
    >>> os.listdir(os.path.join(tempdir, 'mysubdir'))
    ['life.txt']
    >>> file(os.path.join(tempdir, 'mysubdir', 'life.txt')).read().strip()
    'It'
    >>> shutil.rmtree(tempdir)

Mixed case:

    >>> template = makeitso.PolyTemplate([example, exampledir])
    >>> variables = sorted(template.missing())
    >>> variables
    ['bar', 'foo', 'name', 'subdir']

You need to provide output for mixing files and directory templates:

    >>> variables = dict([(i, i.title()) for i in variables])
    >>> try:
    ...    template.substitute(**variables)
    ... except AssertionError, e:
    ...    pass
    >>> e
    AssertionError('Must specify output ...

Provide an output:

    >>> template = makeitso.PolyTemplate([example, exampledir], output=tempdir, interactive=False)
    >>> template.substitute(**variables)
    >>> sorted(os.listdir(tempdir))
    ['Subdir', 'example.txt', 'foo.txt']
    >>> shutil.rmtree(tempdir)