summaryrefslogtreecommitdiff
path: root/templates/python.py
blob: d8905f1b9f75fde69c39f993c172d129252f8aea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#############################################################################
# Functions that implement steps. From {{ functions_filename }}.

{{ functions }}


#############################################################################
# Helper code generated by Subplot.

import base64
import logging
import os
import shutil
import tempfile

# Store context between steps.
class Context:

    def __init__(self):
        self._vars = {}

    def as_dict(self):
        return dict(self._vars)

    def get(self, key, default=None):
        return self._vars.get(key, default)

    def __getitem__(self, key):
        return self._vars[key]

    def __setitem__(self, key, value):
        self._vars[key] = value

# Test data files that were embedded in the source document. Base64
# encoding is used to allow arbitrary data.
_files = {}
{% for file in files %}
_files['''{{ file.filename }}'''] = '''{{ file.contents | base64 }}'''
{% endfor %}


# Retrieve an embedded test data file using filename.
def get_file(filename):
    return base64.b64decode(_files[filename])

# Check two values for equality and give error if they
def assert_eq(a, b):
    assert a == b, 'expected %r == %r' % (a, b)

# Remember where we started from. The step functions may need to refer
# to files there.
srcdir = os.getcwd()
print('srcdir', srcdir)

# Create a new temporary directory and chdir there. This allows step
# functions to create new files in the current working directory
# without having to be so careful.
_datadir = tempfile.mkdtemp()
print('datadir', _datadir)
os.chdir(_datadir)

#############################################################################
# Code to implement the scenarios.

{% for scenario in scenarios %}
######################################
print('''scenario: {{ scenario.title }}''')
_scendir = tempfile.mkdtemp(dir=_datadir)
os.chdir(_scendir)
ctx = Context()
{% for step in scenario.steps %}
print('''  step: {{ step.kind | lower }} {{ step.text }}''')
args = {}
{% for part in step.parts %}{% if part.CapturedText is defined -%}
args['''{{ part.CapturedText.name }}'''] = '''{{ part.CapturedText.text }}'''
{% endif -%}
{% endfor -%}
{{ step.function }}(ctx, **args)
{% endfor %}
{% endfor %}


#############################################################################
# Clean up temporary directory and report success.

shutil.rmtree(_datadir)
print('OK, all scenarios finished successfully')