Mercurial > hg > MakeItSo
annotate examples/doctest.txt @ 70:cb00c011ff45
why do we care if they have templates or not?
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 07 Jan 2011 16:31:28 -0800 |
parents | a0f7bfa98755 |
children | 3571417ef92e |
rev | line source |
---|---|
35
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 MakeItSo! |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 ========= |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 Boilerplate: |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 >>> import makeitso |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 >>> import os |
37
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
8 >>> import shutil |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
9 >>> import tempfile |
35
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 >>> from StringIO import StringIO |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 Basic functionality: |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
37
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
14 >>> example = os.path.join(here, 'example.txt') |
35
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 >>> template = makeitso.PolyTemplate([example], interactive=False) |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 >>> template.missing() |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 set(['name']) |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 >>> template.substitute(name='foo') |
36
0cba953a03ca
fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
19 Hello foo |
35
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 Substitute to a buffer: |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 >>> buffer = StringIO() |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False) |
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 >>> template.substitute(name='bar') |
36
0cba953a03ca
fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
26 >>> buffer.getvalue().strip() |
0cba953a03ca
fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
27 'Hello bar' |
37
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
28 |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
29 Substitute to a file: |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
30 |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
31 >>> buffer = tempfile.mktemp() |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
32 >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False) |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
33 >>> template.substitute(name='fleem') |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
34 >>> file(buffer).read().strip() |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
35 'Hello fleem' |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
36 >>> os.remove(buffer) |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
37 |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
38 Directory case:: |
37
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
39 |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
40 >>> exampledir = os.path.join(here, 'directory-example') |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
41 >>> tempdir = tempfile.mkdtemp() |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
42 >>> template = makeitso.PolyTemplate([exampledir], output=tempdir, interactive=False) |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
43 >>> sorted(template.missing()) |
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
44 ['bar', 'foo', 'subdir'] |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
45 >>> template.substitute(foo='It', bar='life', subdir='mysubdir') |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
46 >>> sorted(os.listdir(tempdir)) |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
47 ['foo.txt', 'mysubdir'] |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
48 >>> file(os.path.join(tempdir, 'foo.txt')).read().strip() |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
49 'It is a wonderful life' |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
50 >>> os.listdir(os.path.join(tempdir, 'mysubdir')) |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
51 ['life.txt'] |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
52 >>> file(os.path.join(tempdir, 'mysubdir', 'life.txt')).read().strip() |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
53 'It' |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
54 >>> shutil.rmtree(tempdir) |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
55 |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
56 Mixed case:: |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
57 |
51
9a0014a760d1
missed an interactive=False
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
58 >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False) |
40
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
59 >>> variables = sorted(template.missing()) |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
60 >>> variables |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
61 ['bar', 'foo', 'name', 'subdir'] |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
62 |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
63 You need to provide output for mixing files and directory templates:: |
40
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
64 |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
65 >>> variables = dict([(i, i.title()) for i in variables]) |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
66 >>> try: |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
67 ... template.substitute(**variables) |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
68 ... except AssertionError, e: |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
69 ... pass |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
70 >>> e |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
71 AssertionError('Must specify output ... |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
72 |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
73 Provide an output:: |
40
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
74 |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
75 >>> template = makeitso.PolyTemplate([example, exampledir], output=tempdir, interactive=False) |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
76 >>> template.substitute(**variables) |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
77 >>> sorted(os.listdir(tempdir)) |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
78 ['Subdir', 'example.txt', 'foo.txt'] |
6b4c8f23192f
test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
79 >>> shutil.rmtree(tempdir) |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
80 |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
81 Test API templates:: |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
82 |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
83 >>> from makeitso.template import MakeItSoTemplate, Variable |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
84 >>> class MyTemplate(MakeItSoTemplate): |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
85 ... name = 'foo' |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
86 ... templates = ['example.txt'] |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
51
diff
changeset
|
87 ... vars = [Variable(name='name', default='bar')] |
67
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
88 >>> buffer = tempfile.mktemp() |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
89 >>> apitemplate = MyTemplate(output=buffer) |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
90 >>> apitemplate.substitute() |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
91 >>> file(buffer).read().strip() |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
92 'Hello bar' |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
93 >>> os.remove(buffer) |