"Hal E. Fulton" <hal9000 / hypermetrics.com> wrote in message
> I'm talking to a device via a serial port. The
> protocol is described in terms of hex, so for a
> first pass I was using a sort of mock object
> serial port stub:

[snip]

How about using the Test::Unit::MockObject framework like this:

require 'test/unit/mock'

class SerialPort < IO

  attr_accessor :read_timeout, :sync

  def initialize(*args)
  end

  def putc(ch)
    STDOUT.printf "PC:    0x%02x\n", ch
  end

  def getc(s)
    STDOUT.print "CM11A: "
    s = STDIN.gets
    n = Integer(s)
    exit if n==999
    n
  end

end

mockSerial = Test::Unit::MockObject( SerialPort ).new
mockSerial.setReturnValues( :putc => Proc::new{|ch| STDOUT.print "PC:
0x%02x\n" % ch},
                            :getc => Proc::new{|s| STDOUT.print "CM11A: ";
                                                   (s == "999") ? exit :
Integer(s) }
                          )

mockSerial.setCallOrder( :getc, :putc, :putc, :putc,
                         :getc, :putc , :putc, :putc, :putc,
                         :getc)
mockSerial.strictCallOrder = true
mockSerial.activate

mockSerial.getc("0x5a")
mockSerial.putc(0xc3)
mockSerial.putc(0x2e)
mockSerial.putc(0x66)

mockSerial.getc("0x57")
mockSerial.putc(0x2e)
mockSerial.putc(0x66)
mockSerial.putc(0x94)
mockSerial.putc(0x00)

mockSerial.getc("0x2d")
#mockSerial.getc("999")

mockSerial.verify
puts "Call trace:\n\t" + mockSerial.callTrace.join("\n\t")

#---------------------------------------------------------
Produces:

CM11A: PC:    0xc3
PC:    0x2e
PC:    0x66
CM11A: PC:    0x2e
PC:    0x66
PC:    0x94
PC:    0x00
CM11A: Call trace:
 getc( "0x5a" ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:37
 putc( 195 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:38
 putc( 46 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:39
 putc( 102 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:40
 getc( "0x57" ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:42
 putc( 46 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:43
 putc( 102 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:44
 putc( 148 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:45
 putc( 0 ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:46
 getc( "0x2d" ) at 0.00000 seconds from C:/atest/tst_mock_serial.rb:48
Loaded suite C:/atest/tst_mock_serial
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors