1 | == dummy-user |
---|
2 | |
---|
3 | Simulates user input for testing interactive command-line programs. |
---|
4 | |
---|
5 | [[toc:]] |
---|
6 | |
---|
7 | === Author |
---|
8 | Matt Gushee |
---|
9 | |
---|
10 | === API |
---|
11 | |
---|
12 | ==== make-dummy-user |
---|
13 | <procedure>(make-dummy-user STRINGS)</procedure> |
---|
14 | |
---|
15 | Given a list of strings representing a sequence of user input, this procedure returns a {{DUMMY-USER}} closure, which responds to the following messages: |
---|
16 | |
---|
17 | (USER 'input) |
---|
18 | (USER 'output) |
---|
19 | |
---|
20 | These calls return, respectively, an input and an output port. Generally you do not need to use them directly; they are used by {{with-dummy-user}} to rebind {{stdin}} and {{stdout}}. |
---|
21 | |
---|
22 | (USER 'reset) |
---|
23 | |
---|
24 | Resets the dummy user to its initial state, allowing repeated tests with the same data. |
---|
25 | |
---|
26 | (USER 'dump) |
---|
27 | |
---|
28 | Returns a list of all data received from and sent to the test program, in the form '((program: STRING) (user: STRING) ...). |
---|
29 | |
---|
30 | ==== with-dummy-user |
---|
31 | |
---|
32 | <procedure>(with-dummy-user USER THUNK [RETURN-RESULT])</procedure> |
---|
33 | |
---|
34 | Tests a program with a dummy user. The {{USER}} argument should be a closure such as that produced by {{make-dummy-user}}. {{THUNK}} is a procedure of no arguments that runs the program you wish to test, with {{stdin}} and {{stdout}} bound to the ports provided by {{USER}}. By default, this procedure calls {{(USER 'dump)}} and returns the result of that call. However, you may provide your own {{RETURN-RESULT}} procedure (with 0 arguments) to cause other data to be returned (e.g. the inputs collected by your program). |
---|
35 | |
---|
36 | === Examples |
---|
37 | |
---|
38 | <enscript highlight="scheme"> |
---|
39 | (use dummy-user) |
---|
40 | |
---|
41 | (define my-results #f) |
---|
42 | |
---|
43 | (define (test-prog) |
---|
44 | (let ((collected-data (make-queue))) |
---|
45 | (display "Name: ") |
---|
46 | (queue-add! collected-data (cons 'name (read-line))) |
---|
47 | (display "Age: ") |
---|
48 | (queue-add! collected-data (cons 'age (read-line))) |
---|
49 | (display "Phone: ") |
---|
50 | (queue-add! collected-data (cons 'phone (read-line))) |
---|
51 | (print "OK, thanks!") |
---|
52 | collected-data)) |
---|
53 | |
---|
54 | (define my-dummy (make-dummy-user '("Jenny" "24" "867-5309"))) |
---|
55 | |
---|
56 | (pp (with-dummy-user my-dummy test-prog)) |
---|
57 | |
---|
58 | ;;; Should print: ((program: "Name: ") (user: "Jenny") (program: "Age: ") (user: "24") |
---|
59 | ;;; (program: "Phone: ") (user: "867-5309") (program: "OK, Thanks!") (program: "\n")) |
---|
60 | |
---|
61 | (my-dummy 'reset) |
---|
62 | |
---|
63 | (pp (with-dummy-user |
---|
64 | my-dummy |
---|
65 | (lambda () (set! my-results (test-prog))) |
---|
66 | (lambda () (queue->list my-results)))) |
---|
67 | |
---|
68 | ;;; Should print: ((name . "Jenny") (age . "24") (phone . "867-5309")) |
---|
69 | </enscript> |
---|
70 | |
---|
71 | === Requirements |
---|
72 | None. |
---|
73 | |
---|
74 | |
---|
75 | === License |
---|
76 | |
---|
77 | Copyright (c) 2012, Matthew C. Gushee |
---|
78 | All rights reserved. |
---|
79 | |
---|
80 | Redistribution and use in source and binary forms, with or without |
---|
81 | modification, are permitted provided that the following conditions are met: |
---|
82 | |
---|
83 | |
---|
84 | Redistributions of source code must retain the above copyright notice, |
---|
85 | this list of conditions and the following disclaimer. |
---|
86 | |
---|
87 | Redistributions in binary form must reproduce the above copyright |
---|
88 | notice, this list of conditions and the following disclaimer in the |
---|
89 | documentation and/or other materials provided with the distribution. |
---|
90 | |
---|
91 | Neither the name of the author nor the names of its contributors may be |
---|
92 | used to endorse or promote products derived from this software without |
---|
93 | specific prior written permission. |
---|
94 | |
---|
95 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
96 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
97 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
98 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
99 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
100 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
101 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
102 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
103 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
104 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
105 | POSSIBILITY OF SUCH DAMAGE. |
---|
106 | |
---|
107 | |
---|
108 | === Version history |
---|
109 | |
---|
110 | ==== version 0.1 |
---|
111 | Initial release |
---|