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

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

Anonymous wiki edit for IP [216.241.35.41]: Clarified the description of with-dummy-user

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