Sha256: 600151b3d31713a244aadd26989fcbd427939339c8c5ce78e7d2dc9d50342b17

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require 'my_scripts'
require 'spec'
require 'spec/autorun'

Spec::Runner.configure do |config|
end

module MyScriptsTest

  def create_cli(opts={})
    @stdout = opts[:stdout] || mock('stdout').as_null_object
    @stdin = opts[:stdin] || mock('stdin')
    if opts[:input]
      @stdin.stub(:gets).and_return(*opts[:input])
    else
      @stdin.stub(:gets).and_return('')
    end
    @kernel = Kernel
    if opts[:system]
      @kernel = mock('Kernel')
      @kernel.stub(:system).and_return(*opts[:system])
    end
    @cli = MyScripts::CLI.new(@stdin, @stdout, @kernel)
  end

  def cli(command_line)
    raise "Command line should be non-empty String" unless command_line.respond_to?(:split) && command_line != ''
    argv = command_line.split(' ')
    @cli.run argv.shift.to_sym, argv
  end

  # Sets expectation for Kernel to receive system call with specific messages/patterns
  def system_should_receive(*messages)
    entity_should_receive(@kernel, :system, *messages)
  end

  # Sets expectation for stdout to receive puts with specific messages/patterns
  def stdout_should_receive(*messages)
    entity_should_receive(@stdout, :puts, *messages)
  end

  # Sets expectation for entity to receive either:
  # :message(s) - strictly ordered sequence of exact messages
  # :pattern(s) - specific patterns (unordered)
  #
  def entity_should_receive(entity, method, *messages)
    # If symbol is coming after method, it must be message type
    type = messages.first.is_a?(Symbol) ? messages.shift : :message
    messages.each do |message|
      if type.to_s =~ /message/
        entity.should_receive(method).with(message).once.ordered
      elsif type.to_s =~ /(pattern|regex)/
        re = Regexp === message ? message : Regexp.new(Regexp.escape(message))
        entity.should_receive(method).with(re).at_least(:once)
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
my_scripts-0.1.5 spec/spec_helper.rb
my_scripts-0.1.3 spec/spec_helper.rb