Skip to content
Browse files

Initial

  • Loading branch information...
0 parents commit 3ad5539120b7dbd1fa38493c8d1163f26d6f9781 @ayanko ayanko committed Dec 28, 2011
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
3 Gemfile
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
35 README.md
@@ -0,0 +1,35 @@
+## RSpec example steps
+
+Given/When/Then steps for RSpec examples
+
+### Example
+
+ require 'rspec/example_steps'
+ require 'spec_helper'
+
+ context "Searching" do
+ Steps "Result found" do
+ Given "I am on search page" do
+ page.visit("/search")
+ page.should have_content("Search")
+ end
+
+ When "I search something" do
+ page.fill_in('Search', :with => 'John')
+ page.click_button "Go"
+ end
+
+ Then "I should see result" do
+ page.should have_content("Result")
+ end
+ end
+ end
+
+### Documentation formatting output:
+
+ Searching
+ User succesfully replaces device
+ Given I am on search page
+ When I search something
+ Then I should see result
+
1 Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
11 lib/rspec/example_steps.rb
@@ -0,0 +1,11 @@
+require 'rspec/example_steps/base_formatter'
+require 'rspec/example_steps/documentation_formatter'
+require "rspec/example_steps/example_group"
+require 'rspec/example_steps/reporter'
+
+RSpec::Core::Formatters::BaseFormatter.send :include, RSpec::ExampleSteps::BaseFormatter
+RSpec::Core::Formatters::DocumentationFormatter.send :include, RSpec::ExampleSteps::DocumentationFormatter
+RSpec::Core::ExampleGroup.send :include, RSpec::ExampleSteps::ExampleGroup
+RSpec::Core::Reporter.send :include, RSpec::ExampleSteps::Reporter
+
+RSpec::Core::ExampleGroup.define_example_method :Steps, :with_steps => true
17 lib/rspec/example_steps/base_formatter.rb
@@ -0,0 +1,17 @@
+module RSpec
+ module ExampleSteps
+ module BaseFormatter
+ def example_step_started(example, type, message)
+ end
+
+ def example_step_passed(example, type, message)
+ end
+
+ def example_step_pending(example, type, message)
+ end
+
+ def example_step_failed(example, type, message)
+ end
+ end
+ end
+end
37 lib/rspec/example_steps/documentation_formatter.rb
@@ -0,0 +1,37 @@
+module RSpec
+ module ExampleSteps
+ module DocumentationFormatter
+ def self.included(base)
+ base.class_eval do
+ include InstanceMethods
+
+ alias :example_started_without_steps :example_started
+ alias :example_started :example_started_with_steps
+
+ alias :example_passed_without_steps :example_passed
+ alias :example_passed :example_passed_with_steps
+ end
+ end
+
+ module InstanceMethods
+ def example_started_with_steps(example)
+ example_started_without_steps(example)
+
+ if example.options[:with_steps]
+ full_message = "#{current_indentation}#{example.description}"
+ output.puts white(full_message)
+ end
+ end
+
+ def example_passed_with_steps(example)
+ example_passed_without_steps(example) unless example.options[:with_steps]
+ end
+
+ def example_step_passed(example_group, type, message)
+ full_message = "#{current_indentation} #{type.to_s.capitalize} #{message}"
+ output.puts green(full_message)
+ end
+ end
+ end
+ end
+end
23 lib/rspec/example_steps/example_group.rb
@@ -0,0 +1,23 @@
+module RSpec
+ module ExampleSteps
+ module ExampleGroup
+ def Given(message)
+ ::RSpec.configuration.reporter.example_step_started(self, :given, message)
+ yield
+ ::RSpec.configuration.reporter.example_step_passed(self, :given, message)
+ end
+
+ def When(message)
+ ::RSpec.configuration.reporter.example_step_started(self, :when, message)
+ yield
+ ::RSpec.configuration.reporter.example_step_passed(self, :when, message)
+ end
+
+ def Then(message)
+ ::RSpec.configuration.reporter.example_step_started(self, :then, message)
+ yield
+ ::RSpec.configuration.reporter.example_step_passed(self, :then, message)
+ end
+ end
+ end
+end
21 lib/rspec/example_steps/reporter.rb
@@ -0,0 +1,21 @@
+module RSpec
+ module ExampleSteps
+ module Reporter
+ def example_step_started(example, type, message)
+ notify :example_step_started, example, type, message
+ end
+
+ def example_step_passed(example, type, message)
+ notify :example_step_passed, example, type, message
+ end
+
+ def example_step_pending(example, type, message)
+ notify :example_step_pending, example, type, message
+ end
+
+ def example_step_failed(example, type, message)
+ notify :example_step_failed, example, type, message
+ end
+ end
+ end
+end
20 rspec-example_steps.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = "rspec-example_steps"
+ s.version = "0.0.1"
+ s.authors = ["Andriy Yanko"]
+ s.email = ["andriy.yanko@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{Given/When/Then steps for RSpec examples}
+
+ s.rubyforge_project = "rspec_example_steps"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_runtime_dependency "rspec-core", ">2.0.0"
+end

0 comments on commit 3ad5539

Please sign in to comment.
Something went wrong with that request. Please try again.