Permalink
Browse files

add features (moved from rspec-core)

  • Loading branch information...
1 parent 2168a58 commit 3e2e9bd99181b1e4d8abec34419641e08d06c322 @dchelimsky dchelimsky committed Feb 20, 2010
View
@@ -14,6 +14,8 @@ begin
gem.email = "dchelimsky@gmail.com;chad.humphries@gmail.com"
gem.homepage = "http://github.com/rspec/expectations"
gem.authors = ["David Chelimsky", "Chad Humphries"]
+ gem.add_development_dependency('cucumber', ">= 0.6.2")
+ gem.add_development_dependency('aruba', ">= 0.1.1")
gem.add_development_dependency('rspec-core', ">= #{Rspec::Expectations::Version::STRING}")
gem.add_development_dependency('rspec-mocks', ">= #{Rspec::Expectations::Version::STRING}")
end
@@ -31,7 +33,14 @@ rescue LoadError
puts "Rspec core or one of its dependencies is not installed. Install it with: gem install rspec-meta"
end
-task :default => [:check_dependencies, :spec]
+require 'cucumber/rake/task'
+
+Cucumber::Rake::Task.new do |t|
+ t.cucumber_opts = %w{--format progress}
+end
+
+
+task :default => [:check_dependencies, :spec, :cucumber]
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
@@ -0,0 +1,56 @@
+Feature: customized message
+
+ In order to get the feedback I want
+ As an RSpec user
+ I want to customize the failure message per example
+
+ Scenario: one additional method
+ Given a file named "node_spec.rb" with:
+ """
+ require "rspec/expectations"
+
+ class Node
+ def initialize(state=:waiting)
+ @state = state
+ end
+ def state
+ @state
+ end
+ def waiting?
+ @state == :waiting
+ end
+ def started?
+ @state == :started
+ end
+ def start
+ @state = :started
+ end
+ end
+
+ describe "a new Node" do
+ it "should be waiting" do
+ node = Node.new(:started) #start w/ started to trigger failure
+ node.should be_waiting, "node.state: #{node.state} (first example)"
+ end
+
+ it "should not be started" do
+ node = Node.new(:started) #start w/ started to trigger failure
+ node.should_not be_started, "node.state: #{node.state} (second example)"
+ end
+ end
+
+ describe "node.start" do
+ it "should change the state" do
+ node = Node.new(:started) #start w/ started to trigger failure
+ lambda {node.start}.should change{node.state}, "expected a change"
+ end
+ end
+
+ """
+ When I run "rspec node_spec.rb --format n"
+ Then I should see "3 examples, 3 failures"
+ And I should not see "to return true, got false"
+ And I should not see "to return false, got true"
+ And I should see "node.state: started (first example)"
+ And I should see "node.state: started (second example)"
+ And I should see "expected a change"
@@ -0,0 +1,193 @@
+Feature: define matcher
+
+ In order to express my domain clearly in my code examples
+ As an RSpec user
+ I want a shortcut to define custom matchers
+
+ Scenario: define a matcher with default messages
+ Given a file named "matcher_with_default_message_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :be_a_multiple_of do |expected|
+ match do |actual|
+ actual % expected == 0
+ end
+ end
+
+ describe 9 do
+ it {should be_a_multiple_of(3)}
+ end
+
+ describe 9 do
+ it {should_not be_a_multiple_of(4)}
+ end
+
+ # fail intentionally to generate expected output
+ describe 9 do
+ it {should be_a_multiple_of(4)}
+ end
+
+ # fail intentionally to generate expected output
+ describe 9 do
+ it {should_not be_a_multiple_of(3)}
+ end
+
+ """
+ When I run "rspec matcher_with_default_message_spec.rb --format specdoc"
+ Then the exit status should not be 0
+
+ And I should see "should be a multiple of 3"
+ And I should see "should not be a multiple of 4"
+ And I should see "Failure/Error: it {should be_a_multiple_of(4)}"
+ And I should see "Failure/Error: it {should_not be_a_multiple_of(3)}"
+
+ And I should see "4 examples, 2 failures"
+ And I should see "expected 9 to be a multiple of 4"
+ And I should see "expected 9 not to be a multiple of 3"
+
+ Scenario: overriding the failure_message_for_should
+ Given a file named "matcher_with_failure_message_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :be_a_multiple_of do |expected|
+ match do |actual|
+ actual % expected == 0
+ end
+ failure_message_for_should do |actual|
+ "expected that #{actual} would be a multiple of #{expected}"
+ end
+ end
+
+ # fail intentionally to generate expected output
+ describe 9 do
+ it {should be_a_multiple_of(4)}
+ end
+ """
+ When I run "rspec matcher_with_failure_message_spec.rb"
+ Then the exit status should not be 0
+ And the stdout should contain "1 example, 1 failure"
+ And the stdout should contain "expected that 9 would be a multiple of 4"
+
+ Scenario: overriding the failure_message_for_should_not
+ Given a file named "matcher_with_failure_for_message_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :be_a_multiple_of do |expected|
+ match do |actual|
+ actual % expected == 0
+ end
+ failure_message_for_should_not do |actual|
+ "expected that #{actual} would not be a multiple of #{expected}"
+ end
+ end
+
+ # fail intentionally to generate expected output
+ describe 9 do
+ it {should_not be_a_multiple_of(3)}
+ end
+ """
+ When I run "rspec matcher_with_failure_for_message_spec.rb"
+ Then the exit status should not be 0
+ And the stdout should contain "1 example, 1 failure"
+ And the stdout should contain "expected that 9 would not be a multiple of 3"
+
+ Scenario: overriding the description
+ Given a file named "matcher_overriding_description_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :be_a_multiple_of do |expected|
+ match do |actual|
+ actual % expected == 0
+ end
+ description do
+ "be multiple of #{expected}"
+ end
+ end
+
+ describe 9 do
+ it {should be_a_multiple_of(3)}
+ end
+
+ describe 9 do
+ it {should_not be_a_multiple_of(4)}
+ end
+ """
+ When I run "rspec matcher_overriding_description_spec.rb --format specdoc"
+ Then the exit status should be 0
+ And the stdout should contain "2 examples, 0 failures"
+ And the stdout should contain "should be multiple of 3"
+ And the stdout should contain "should not be multiple of 4"
+
+ Scenario: with no args
+ Given a file named "matcher_with_no_args_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :have_7_fingers do
+ match do |thing|
+ thing.fingers.length == 7
+ end
+ end
+
+ class Thing
+ def fingers; (1..7).collect {"finger"}; end
+ end
+
+ describe Thing do
+ it {should have_7_fingers}
+ end
+ """
+ When I run "rspec matcher_with_no_args_spec.rb --format specdoc"
+ Then the exit status should be 0
+ And the stdout should contain "1 example, 0 failures"
+ And the stdout should contain "should have 7 fingers"
+
+ Scenario: with multiple args
+ Given a file named "matcher_with_multiple_args_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :be_the_sum_of do |a,b,c,d|
+ match do |sum|
+ a + b + c + d == sum
+ end
+ end
+
+ describe 10 do
+ it {should be_the_sum_of(1,2,3,4)}
+ end
+ """
+ When I run "rspec matcher_with_multiple_args_spec.rb --format specdoc"
+ Then the exit status should be 0
+ And the stdout should contain "1 example, 0 failures"
+ And the stdout should contain "should be the sum of 1, 2, 3, and 4"
+
+ Scenario: with helper methods
+ Given a file named "matcher_with_internal_helper_spec.rb" with:
+ """
+ require 'rspec/expectations'
+
+ Rspec::Matchers.define :have_same_elements_as do |sample|
+ match do |actual|
+ similar?(sample, actual)
+ end
+
+ def similar?(a, b)
+ a.sort == b.sort
+ end
+ end
+
+ describe "these two arrays" do
+ specify "should be similar" do
+ [1,2,3].should have_same_elements_as([2,3,1])
+ end
+ end
+ """
+ When I run "rspec matcher_with_internal_helper_spec.rb"
+ Then the exit status should be 0
+ And the stdout should contain "1 example, 0 failures"
+
@@ -0,0 +1,38 @@
+Feature: define matcher outside rspec
+
+ In order to express my domain clearly in my code examples
+ As a non-rspec user
+ I want a shortcut to define custom matchers
+
+ Scenario: define a matcher with default messages
+ Given a file named "test_multiples.rb" with:
+ """
+ require "rspec/expectations"
+ require "test/unit"
+
+ Rspec::Matchers.define :be_a_multiple_of do |expected|
+ match do |actual|
+ actual % expected == 0
+ end
+ end
+
+ class Test::Unit::TestCase
+ include Rspec::Matchers
+ end
+
+ class TestMultiples < Test::Unit::TestCase
+
+ def test_9_should_be_a_multiple_of_3
+ 9.should be_a_multiple_of(3)
+ end
+
+ def test_9_should_be_a_multiple_of_4
+ 9.should be_a_multiple_of(4)
+ end
+
+ end
+ """
+ When I run "ruby test_multiples.rb"
+ Then the exit status should not be 0
+ And I should see "expected 9 to be a multiple of 4"
+ And I should see "2 tests, 0 assertions, 0 failures, 1 errors"
View
@@ -0,0 +1,5 @@
+$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
+require 'rspec/expectations'
+require 'aruba'
+
+

0 comments on commit 3e2e9bd

Please sign in to comment.