Sha256: 53bad5ab6d4ba659341ff6984eab98bc9db1058e57a253df143a4699205e3961

Contents?: true

Size: 794 Bytes

Versions: 1

Compression:

Stored size: 794 Bytes

Contents

# frozen_string_literal: true
module TL1
  # A simple IO-like object that accepts a hash of string inputs and
  # corresponding outputs, useful for testing.
  class TestIO
    def initialize(commands, first_output: '')
      commands.each_pair do |input, output|
        next if output =~ COMPLD
        raise ArgumentError, "incomplete output for #{input}"
      end

      @commands = commands
      @next_output = first_output
    end

    def expect(pattern, _timeout = nil)
      unless pattern =~ @next_output
        raise TimeoutError, 'pattern does not match the next output'
      end

      @next_output
    end

    def write(message)
      @next_output = @commands.fetch(message)
      true
    end

    class TimeoutError < RuntimeError; end
  end # class TestIO
end # module TL1

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tl1-0.1.0 lib/tl1/test_io.rb