
###
### auto generated by ../doc/Kookbook.py
###

import sys, os, re
from glob import glob
import pytest

from subprocess import Popen, PIPE

def _popen(command):
    proc = Popen(command, shell=True, stdout=PIPE)
    output = proc.stdout.read()
    proc.stdout.close()
    proc.wait()
    return output


class TestExamples:

    DIR = os.path.dirname(os.path.abspath(__file__)) + '/data/examples'
    CWD = os.getcwd()

    def setup_method(self, method):
        self.__name__ = method.__name__[len('test_'):]
        sys.stdout.write(' (')
        os.chdir(self.DIR + '/' + self.__name__)
        for x in glob('views/*.cache'):
            os.unlink(x)

    def teardown_method(self):
        os.chdir(self.CWD)

    def _test(self):
        for fname in glob('*.result'):
            sys.stdout.write(' %s' % fname)
            with open(fname) as f:
                result = f.read()
            command, expected = re.split(r'\n', result, 1)
            command = re.sub(r'^\$ ', '', command)
            actual = _popen(command).decode('utf-8')
            assert actual == expected
        sys.stdout.write(' )')

    def test_form(self):
        self._test()

    def test_preprocessing(self):
        self._test()

    def test_table(self):
        self._test()
