summaryrefslogtreecommitdiff
path: root/doxygen/man/man3/cmd2_Cmd2TestCase.3
blob: 7df05d275a0d7952fcea5633faa94a3d7c12249b (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
.TH "cmd2::Cmd2TestCase" 3 "Fri Sep 9 2011" "Cmd2" \" -*- nroff -*-
.ad l
.nh
.SH NAME
cmd2::Cmd2TestCase \- 
.SH SYNOPSIS
.br
.PP
.SS "Public Member Functions"

.in +1c
.ti -1c
.RI "def \fBfetchTranscripts\fP"
.br
.ti -1c
.RI "def \fBrunTest\fP"
.br
.ti -1c
.RI "def \fBsetUp\fP"
.br
.ti -1c
.RI "def \fBtearDown\fP"
.br
.in -1c
.SS "Public Attributes"

.in +1c
.ti -1c
.RI "\fBcmdapp\fP"
.br
.ti -1c
.RI "\fBoutputTrap\fP"
.br
.ti -1c
.RI "\fBtranscripts\fP"
.br
.in -1c
.SS "Static Public Attributes"

.in +1c
.ti -1c
.RI "tuple \fBanyWhitespace\fP = re\&.compile(r'\\s', re\&.DOTALL | re\&.MULTILINE)"
.br
.ti -1c
.RI "\fBCmdApp\fP = None"
.br
.ti -1c
.RI "\fBexpectationParser\fP = \fBregexPattern\fP|\fBnotRegexPattern\fP"
.br
.ti -1c
.RI "tuple \fBnotRegexPattern\fP = pyparsing\&.Word(pyparsing\&.printables)"
.br
.ti -1c
.RI "tuple \fBregexPattern\fP = pyparsing\&.QuotedString(quoteChar=r'/', escChar='\\\\', multiline=True, unquoteResults=True)"
.br
.in -1c
.SS "Private Member Functions"

.in +1c
.ti -1c
.RI "def \fB_test_transcript\fP"
.br
.in -1c
.SH "Detailed Description"
.PP 
.PP
.nf
Subclass this, setting CmdApp, to make a unittest.TestCase class
   that will execute the commands in a transcript file and expect the results shown.
   See example.py.fi
.PP
 
.PP
Definition at line 1476 of file cmd2\&.py'\&.
.SH "Member Function Documentation"
.PP 
.SS "def cmd2::Cmd2TestCase::_test_transcript (self, fname, transcript)\fC [private]\fP"
.PP
Definition at line 1506 of file cmd2\&.py'\&.
.PP
References cmdapp\&.
.PP
Referenced by runTest()\&.
.PP
.nf
1506 
1507     def _test_transcript(self, fname, transcript):
1508         lineNum = 0
1509         finished = False
1510         line = transcript\&.next()
1511         lineNum += 1
1512         tests_run = 0
1513         while not finished:
1514             # Scroll forward to where actual commands begin
1515             while not line\&.startswith(self\&.cmdapp\&.prompt):
1516                 try:
1517                     line = transcript\&.next()
1518                 except StopIteration:
1519                     finished = True
1520                     break
1521                 lineNum += 1
1522             command = [line[len(self\&.cmdapp\&.prompt):]]
1523             line = transcript\&.next()
1524             # Read the entirety of a multi-line command
1525             while line\&.startswith(self\&.cmdapp\&.continuation_prompt):
1526                 command\&.append(line[len(self\&.cmdapp\&.continuation_prompt):])
1527                 try:
1528                     line = transcript\&.next()
1529                 except StopIteration:
1530                     raise (StopIteration, 
1531                            'Transcript broke off while reading command beginning at line %d with\n%s' 
1532                            % (command[0]))
1533                 lineNum += 1
1534             command = ''\&.join(command)               
1535             # Send the command into the application and capture the resulting output
1536             stop = self\&.cmdapp\&.onecmd_plus_hooks(command)
1537             #TODO: should act on ``stop``
1538             result = self\&.outputTrap\&.read()
1539             # Read the expected result from transcript
1540             if line\&.startswith(self\&.cmdapp\&.prompt):
1541                 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\
1542                     (fname, lineNum, command, result)     
1543                 self\&.assert_(not(result\&.strip()), message)
1544                 continue
1545             expected = []
1546             while not line\&.startswith(self\&.cmdapp\&.prompt):
1547                 expected\&.append(line)
1548                 try:
1549                     line = transcript\&.next()
1550                 except StopIteration:
1551                     finished = True                       
1552                     break
1553                 lineNum += 1
1554             expected = ''\&.join(expected)
1555             # Compare actual result to expected
1556             message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
1557                 (fname, lineNum, command, expected, result)      
1558             expected = self\&.expectationParser\&.transformString(expected)
1559             # checking whitespace is a pain - let's skip it
1560             expected = self\&.anyWhitespace\&.sub('', expected)
1561             result = self\&.anyWhitespace\&.sub('', result)
1562             self\&.assert_(re\&.match(expected, result, re\&.MULTILINE | re\&.DOTALL), message)

.fi
.SS "def cmd2::Cmd2TestCase::fetchTranscripts (self)"
.PP
Definition at line 1481 of file cmd2\&.py'\&.
.PP
Referenced by setUp()\&.
.PP
.nf
1481 
1482     def fetchTranscripts(self):
1483         self\&.transcripts = {}
1484         for fileset in self\&.CmdApp\&.testfiles:
1485             for fname in glob\&.glob(fileset):
1486                 tfile = open(fname)
1487                 self\&.transcripts[fname] = iter(tfile\&.readlines())
1488                 tfile\&.close()
1489         if not len(self\&.transcripts):
            raise (StandardError,), 'No test files found - nothing to test\&.'
.fi
.SS "def cmd2::Cmd2TestCase::runTest (self)"
.PP
Definition at line 1495 of file cmd2\&.py'\&.
.PP
References _test_transcript(), and CmdApp\&.
.PP
.nf
1495 
1496     def runTest(self): # was testall
1497         if self\&.CmdApp:
1498             its = sorted(self\&.transcripts\&.items())
1499             for (fname, transcript) in its:
                self\&._test_transcript(fname, transcript)
.fi
.SS "def cmd2::Cmd2TestCase::setUp (self)"
.PP
Definition at line 1490 of file cmd2\&.py'\&.
.PP
References CmdApp, cmdapp, fetchTranscripts(), and outputTrap\&.
.PP
.nf
1490 
1491     def setUp(self):
1492         if self\&.CmdApp:
1493             self\&.outputTrap = OutputTrap()
1494             self\&.cmdapp = self\&.CmdApp()
            self\&.fetchTranscripts()
.fi
.SS "def cmd2::Cmd2TestCase::tearDown (self)"
.PP
Definition at line 1563 of file cmd2\&.py'\&.
.PP
References CmdApp\&.
.PP
.nf
1563 
1564     def tearDown(self):
1565         if self\&.CmdApp:
1566             self\&.outputTrap\&.tearDown()

.fi
.SH "Member Data Documentation"
.PP 
.SS "tuple \fBcmd2::Cmd2TestCase::anyWhitespace\fP = re\&.compile(r'\\s', re\&.DOTALL | re\&.MULTILINE)\fC [static]\fP"
.PP
Definition at line 1505 of file cmd2\&.py'\&.
.SS "\fBcmd2::Cmd2TestCase::CmdApp\fP = None\fC [static]\fP"
.PP
Definition at line 1480 of file cmd2\&.py'\&.
.PP
Referenced by runTest(), setUp(), and tearDown()\&.
.SS "\fBcmd2::Cmd2TestCase::cmdapp\fP"
.PP
Definition at line 1490 of file cmd2\&.py'\&.
.PP
Referenced by _test_transcript(), and setUp()\&.
.SS "\fBcmd2::Cmd2TestCase::expectationParser\fP = \fBregexPattern\fP|\fBnotRegexPattern\fP\fC [static]\fP"
.PP
Definition at line 1504 of file cmd2\&.py'\&.
.SS "tuple \fBcmd2::Cmd2TestCase::notRegexPattern\fP = pyparsing\&.Word(pyparsing\&.printables)\fC [static]\fP"
.PP
Definition at line 1502 of file cmd2\&.py'\&.
.SS "\fBcmd2::Cmd2TestCase::outputTrap\fP"
.PP
Definition at line 1490 of file cmd2\&.py'\&.
.PP
Referenced by setUp()\&.
.SS "tuple \fBcmd2::Cmd2TestCase::regexPattern\fP = pyparsing\&.QuotedString(quoteChar=r'/', escChar='\\\\', multiline=True, unquoteResults=True)\fC [static]\fP"
.PP
Definition at line 1500 of file cmd2\&.py'\&.
.SS "\fBcmd2::Cmd2TestCase::transcripts\fP"
.PP
Definition at line 1481 of file cmd2\&.py'\&.

.SH "Author"
.PP 
Generated automatically by Doxygen for Cmd2 from the source code'\&.