source: project/wiki/eggref/4/dummy-user @ 27370

Last change on this file since 27370 was 27370, checked in by svnwiki, 9 years ago

Anonymous wiki edit for IP [216.241.35.41]:

File size: 3.8 KB
Line 
1== dummy-user
2
3Simulates user input for testing interactive command-line programs.
4
5[[toc:]]
6
7=== Author
8Matt Gushee
9
10=== API
11
12==== make-dummy-user
13<procedure>(make-dummy-user STRINGS)</procedure>
14
15Given 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
20These 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
24Resets the dummy user to its initial state, allowing repeated tests with the same data.
25
26 (USER 'dump)
27
28Returns 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
34Tests 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    (set! my-results (make-queue))
45    (display "Name: ")
46    (queue-add! my-results (cons 'name (read-line)))
47    (display "Age: ")
48    (queue-add! my-results (cons 'age (read-line)))
49    (display "Phone: ")
50    (queue-add! my-results (cons 'phone (read-line)))
51    (print "OK, thanks!"))
52
53  (define my-dummy (make-dummy-user '("Jenny" "24" "867-5309")))
54
55  (pp (with-dummy-user my-dummy test-prog))
56
57  ;;; Should print: ((program: "Name: ") (user: "Jenny") (program: "Age: ") (user: "24")
58  ;;;                (program: "Phone: ") (user: "867-5309") (program: "OK, Thanks!") (program: "\n"))
59
60  (my-dummy 'reset)
61
62  (pp (with-dummy-user my-dummy test-prog (lambda () (queue->list my-results))))
63 
64  ;;; Should print: ((name . "Jenny") (age . "24") (phone . "867-5309"))
65</enscript>
66
67=== Requirements
68None.
69
70
71=== License
72
73Copyright (c) 2012, Matthew C. Gushee
74All rights reserved.
75
76Redistribution and use in source and binary forms, with or without
77modification, are permitted provided that the following conditions are met:
78
79
80    Redistributions of source code must retain the above copyright notice,
81    this list of conditions and the following disclaimer.
82
83    Redistributions in binary form must reproduce the above copyright
84    notice, this list of conditions and the following disclaimer in the
85    documentation and/or other materials provided with the distribution.
86   
87    Neither the name of the author nor the names of its contributors may be
88    used to endorse or promote products derived from this software without
89    specific prior written permission.
90
91THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
92AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
95LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
96CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
97SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
98INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
99CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
100ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
101POSSIBILITY OF SUCH DAMAGE.
102
103
104=== Version history
105
106==== version 0.1
107Initial release
Note: See TracBrowser for help on using the repository browser.