dummy-user

Simulates user input for testing interactive command-line programs.

  1. dummy-user
    1. Author
    2. Repository
    3. API
      1. make-dummy-user
      2. with-dummy-user
    4. Examples
    5. Requirements
    6. License
    7. Version history
      1. version 0.1

Author

Matt Gushee

Repository

https://github.com/mgushee/dummy-user

API

make-dummy-user

[procedure] (make-dummy-user STRINGS)

Given a list of strings representing a sequence of user input, this procedure returns a DUMMY-USER closure, which responds to the following messages:

(USER 'input)
(USER 'output)

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.

(USER 'reset)

Resets the dummy user to its initial state, allowing repeated tests with the same data.

(USER 'dump)

Returns a list of all data received from and sent to the test program, in the form '((program: STRING) (user: STRING) ...).

with-dummy-user

[procedure] (with-dummy-user USER THUNK [RETURN-RESULT])

Tests a program with a dummy user; to be precise, it binds stdin and stdout to the ports 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).

Examples

  (use dummy-user)

  (define my-results #f)

  (define (test-prog)
    (let ((collected-data (make-queue)))
      (display "Name: ")
      (queue-add! collected-data (cons 'name (read-line)))
      (display "Age: ")
      (queue-add! collected-data (cons 'age (read-line)))
      (display "Phone: ")
      (queue-add! collected-data (cons 'phone (read-line)))
      (print "OK, thanks!")
      collected-data))

  (define my-dummy (make-dummy-user '("Jenny" "24" "867-5309")))

  (pp (with-dummy-user my-dummy test-prog))

  ;;; Should print: ((program: "Name: ") (user: "Jenny") (program: "Age: ") (user: "24")
  ;;;                (program: "Phone: ") (user: "867-5309") (program: "OK, Thanks!") (program: "\n"))

  (my-dummy 'reset)

  (pp (with-dummy-user
        my-dummy
        (lambda () (set! my-results (test-prog)))
        (lambda () (queue->list my-results))))
  
  ;;; Should print: ((name . "Jenny") (age . "24") (phone . "867-5309"))

Requirements

None.

License

Copyright (c) 2012, Matthew C. Gushee All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   Neither the name of the author nor the names of its contributors may be
   used to endorse or promote products derived from this software without
   specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version history

version 0.1

Initial release