Skip to content

Instantly share code, notes, and snippets.

@rbxbx
Forked from trek/baisc.feature
Created March 9, 2011 15:53
Show Gist options
  • Save rbxbx/862439 to your computer and use it in GitHub Desktop.
Save rbxbx/862439 to your computer and use it in GitHub Desktop.
Feature: Get
In order to interact with the command line
As an end user
I want to see and enter things
Scenario:
Given I will see "Private?"
Then I will enter "Yes, sir?"
Then I will see "Jump"
Then I will enter "Yes, sir"
And I will see "Well done."
And I run the script
Then I should have jumped
module Colonel
class << self
def inspect
<<-END
#{@actual_interactions.inspect}
#{@expected_interactions.inspect}
END
end
def interact(how, message)
@expected_interactions << [ how, message ]
@expected_interactions_by_type[how] << message
end
def activate!
actual_interactions = []
expected_interactions = []
expected_interactions_by_type = {
:see => [],
:enter => []
}
interaction_index = 0
gets_interaction_index = 0
Kernel.module_eval <<-END
alias :_puts :puts
alias :_gets :gets
END
Kernel.send(:define_method, :gets) do
actual_interactions << [:enter, expected_interactions_by_type[:enter][gets_interaction_index]]
return actual_interactions.last
end
Kernel.send(:define_method, :puts) do |message|
actual_interactions << [:see, message]
if actual_interactions.last != expected_interactions[actual_interactions.size-1]
raise <<-END
Expected to see #{expected_interactions[actual_interactions.size-1]}
But saw #{actual_interactions.last} instead.
#{actual_interactions.size-1}
#{actual_interactions.last}
#{expected_interactions[actual_interactions.size-1]}
END
end
message
end
Kernel.send(:define_method, :print) do |message|
actual_interactions << [:see, message+"\n"]
message + "\n"
end
@expected_interactions_by_type = expected_interactions_by_type
@expected_interactions = expected_interactions
@actual_interactions = actual_interactions
end
def deactivate!
Kernel.module_eval <<-END
alias :puts :_puts
alias :gets :_gets
END
if @expected_interactions.size != @actual_interactions.size
raise <<-END
There were some unexpected interactions:
#{inspect}
END
end
end
end
end
require 'rspec-expectations'
Before() do
Colonel.activate!
end
After() do
Colonel.deactivate!
end
Then /^I should have jumped$/ do
# ok, it worked. You'd put additional steps here normally.
end
When /^I run the script$/ do
puts "Private?"
gets
puts "Jump"
end
Given /^I will see "([^"]*)"$/ do |words|
Colonel.interact :see, words
end
Then /^I will enter "([^"]*)"$/ do |words|
Colonel.interact :enter, words
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment