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

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

Anonymous wiki edit for IP [216.241.35.41]:

File size: 3.9 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    (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
72None.
73
74
75=== License
76
77Copyright (c) 2012, Matthew C. Gushee
78All rights reserved.
79
80Redistribution and use in source and binary forms, with or without
81modification, 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
95THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
96AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
99LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
100CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
101SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
102INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
103CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
105POSSIBILITY OF SUCH DAMAGE.
106
107
108=== Version history
109
110==== version 0.1
111Initial release
Note: See TracBrowser for help on using the repository browser.