Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #135 from jeremywadsack/start_with_end_with

Add matchers start_with end_with
  • Loading branch information...
commit 05d9853b069833712ef1ac7abc938e0e36ac5811 2 parents 40cbc30 + 5f367d9
@dchelimsky dchelimsky authored
View
47 features/built_in_matchers/end_with.feature
@@ -0,0 +1,47 @@
+Feature: end_with matcher
+
+ The end_with matcher is mostly sugar to make your string tests
+ read better
+
+ "A test string".should end_with "string"
+ "A test string".should_not end_with "Something"
+
+ The test is case sensitive.
+
+ Scenario: string usage
+ Given a file named "string_end_with_matcher_spec.rb" with:
+ """
+ describe "A test string" do
+ it { should end_with "string" }
+ it { should_not end_with "Something" }
+
+ # deliberate failures
+ it { should_not end_with "string" }
+ it { should end_with "Something" }
+ end
+ """
+ When I run `rspec string_end_with_matcher_spec.rb`
+ Then the output should contain all of these:
+ | 4 examples, 2 failures |
+ | expected "A test string" not to end with "string" |
+ | expected "A test string" to end with "Something" |
+
+ Scenario: array usage
+ Given a file named "array_end_with_matcher_spec.rb" with:
+ """
+ describe [0, 1, 2, 3, 4] do
+ it { should end_with 4 }
+ it { should end_with [3, 4] }
+ it { should_not end_with "Something" }
+ it { should_not end_with [0, 1, 2, 3, 4, 5] }
+
+ # deliberate failures
+ it { should_not end_with 4 }
+ it { should end_with "Something" }
+ end
+ """
+ When I run `rspec array_end_with_matcher_spec.rb`
+ Then the output should contain all of these:
+ | 6 examples, 2 failures |
+ | expected [0, 1, 2, 3, 4] not to end with 4 |
+ | expected [0, 1, 2, 3, 4] to end with "Something" |
View
48 features/built_in_matchers/start_with.feature
@@ -0,0 +1,48 @@
+Feature: start_with matcher
+
+ The start_with matcher is mostly sugar to make your string tests
+ read better
+
+ "A test string".should start_with "A test"
+ "A test string".should_not start_with "Something"
+
+ The test is case sensitive.
+
+ Scenario: string usage
+ Given a file named "string_start_with_matcher_spec.rb" with:
+ """
+ describe "A test string" do
+ it { should start_with "A test" }
+ it { should_not start_with "Something" }
+
+ # deliberate failures
+ it { should_not start_with "A test" }
+ it { should start_with "Something" }
+ end
+ """
+ When I run `rspec string_start_with_matcher_spec.rb`
+ Then the output should contain all of these:
+ | 4 examples, 2 failures |
+ | expected "A test string" not to start with "A test" |
+ | expected "A test string" to start with "Something" |
+
+ Scenario: array usage
+ Given a file named "array_start_with_matcher_spec.rb" with:
+ """
+ describe [0, 1, 2, 3, 4] do
+ it { should start_with 0 }
+ it { should start_with [0, 1] }
+ it { should_not start_with "Something" }
+ it { should_not start_with [0, 1, 2, 3, 4, 5] }
+
+ # deliberate failures
+ it { should_not start_with 0 }
+ it { should start_with "Something" }
+ end
+ """
+ When I run `rspec array_start_with_matcher_spec.rb`
+ Then the output should contain all of these:
+ | 6 examples, 2 failures |
+ | expected [0, 1, 2, 3, 4] not to start with 0 |
+ | expected [0, 1, 2, 3, 4] to start with "Something" |
+
View
28 lib/rspec/matchers.rb
@@ -355,6 +355,20 @@ def cover(*values)
BuiltIn::Cover.new(*values)
end if (1..2).respond_to?(:cover?)
+ # Matches if the target ends with the expected value. In the case
+ # of strings tries to match the last expected.length characters of
+ # target. In the case of an array tries to match the last expected.length
+ # elements of target.
+ #
+ # @example
+ #
+ # "A test string".should end_with 'string'
+ # [0, 1, 2, 3, 4].should end_with 4
+ # [0, 2, 3, 4, 4].should end_with [3, 4]
+ def end_with(expected)
+ BuiltIn::EndWith.new(expected)
+ end
+
# Passes if <tt>actual == expected</tt>.
#
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
@@ -534,6 +548,20 @@ def satisfy(&block)
BuiltIn::Satisfy.new(&block)
end
+ # Matches if the target starts with the expected value. In the case
+ # of strings tries to match the first expected.length characters of
+ # target. In the case of an array tries to match the first expected.length
+ # elements of target.
+ #
+ # @example
+ #
+ # "A test string".should start_with 'A test'
+ # [0, 1, 2, 3, 4].should start_with 0
+ # [0, 2, 3, 4, 4].should start_with [0, 1]
+ def start_with(expected)
+ BuiltIn::StartWith.new(expected)
+ end
+
# Given no argument, matches if a proc throws any Symbol.
#
# Given a Symbol, matches if the given proc throws the specified Symbol.
View
2  lib/rspec/matchers/built_in.rb
@@ -24,6 +24,8 @@ module BuiltIn
autoload :MatchArray, 'rspec/matchers/built_in/match_array'
autoload :RaiseError, 'rspec/matchers/built_in/raise_error'
autoload :RespondTo, 'rspec/matchers/built_in/respond_to'
+ autoload :StartWith, 'rspec/matchers/built_in/start_with_end_with'
+ autoload :EndWith, 'rspec/matchers/built_in/start_with_end_with'
autoload :Satisfy, 'rspec/matchers/built_in/satisfy'
autoload :ThrowSymbol, 'rspec/matchers/built_in/throw_symbol'
end
View
61 lib/rspec/matchers/built_in/start_with_end_with.rb
@@ -0,0 +1,61 @@
+module RSpec
+ module Matchers
+ module BuiltIn
+ class StartWith
+ include BaseMatcher
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def matches?(actual)
+ @actual = actual
+ if @actual.respond_to?(:[])
+ if @expected.respond_to?(:length)
+ @actual[0, @expected.length] == @expected
+ else
+ @actual[0] == @expected
+ end
+ else
+ raise ArgumentError.new("#{@expected.inspect} does not respond to :[]")
+ end
+ end
+
+ def failure_message_for_should
+ "expected #{@actual.inspect} to start with #{@expected.inspect}"
+ end
+
+ def failure_message_for_should_not
+ "expected #{@actual.inspect} not to start with #{@expected.inspect}"
+ end
+ end
+
+ class EndWith
+ include BaseMatcher
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def matches?(actual)
+ @actual = actual
+ if @actual.respond_to?(:[])
+ if @expected.respond_to?(:length)
+ @actual[-@expected.length, @expected.length] == @expected
+ else
+ @actual[-1] == @expected
+ end
+ else
+ raise ArgumentError.new("#{@expected.inspect} does not respond to :[]")
+ end
+ end
+
+ def failure_message_for_should
+ "expected #{@actual.inspect} to end with #{@expected.inspect}"
+ end
+
+ def failure_message_for_should_not
+ "expected #{@actual.inspect} not to end with #{@expected.inspect}"
+ end
+ end
+ end
+ end
+end
View
163 spec/rspec/matchers/start_with_end_with_spec.rb
@@ -0,0 +1,163 @@
+require "spec_helper"
+
+describe "should start_with" do
+
+ context "A test string" do
+ it "passes if it matches the start of the string" do
+ subject.should start_with "A test"
+ end
+
+ it "fails if it does not match the start of the string" do
+ lambda {
+ subject.should start_with "Something"
+ }.should fail_with("expected \"A test string\" to start with \"Something\"")
+ end
+ end
+
+ context [0, 1, 2, 3, 4] do
+ it "passes if it is the first element of the array" do
+ subject.should start_with 0
+ end
+
+ it "passes if the first elements of the array match" do
+ subject.should start_with [0, 1]
+ end
+
+ it "fails if it does not match the first element of the array" do
+ lambda {
+ subject.should start_with "Something"
+ }.should fail_with("expected [0, 1, 2, 3, 4] to start with \"Something\"")
+ end
+
+ it "fails if it the first elements of the array do not match" do
+ lambda {
+ subject.should start_with [1, 2]
+ }.should fail_with("expected [0, 1, 2, 3, 4] to start with [1, 2]")
+ end
+ end
+
+ context Object.new do
+ it "should raise an error if expected value can't be indexed'" do
+ expect { subject.should start_with 0 }.to raise_error(ArgumentError, /does not respond to :\[\]/)
+ end
+ end
+end
+
+describe "should_not start_with" do
+
+ context "A test string" do
+ it "passes if it does not match the start of the string" do
+ subject.should_not start_with "Something"
+ end
+
+ it "fails if it does match the start of the string" do
+ lambda {
+ subject.should_not start_with "A test"
+ }.should fail_with("expected \"A test string\" not to start with \"A test\"")
+ end
+ end
+
+ context [0, 1, 2, 3, 4] do
+ it "passes if it is not the first element of the array" do
+ subject.should_not start_with "Something"
+ end
+
+ it "passes if the first elements of the array do not match" do
+ subject.should_not start_with [1, 2]
+ end
+
+ it "fails if it matches the first element of the array" do
+ lambda {
+ subject.should_not start_with 0
+ }.should fail_with("expected [0, 1, 2, 3, 4] not to start with 0")
+ end
+
+ it "fails if it the first elements of the array match" do
+ lambda {
+ subject.should_not start_with [0, 1]
+ }.should fail_with("expected [0, 1, 2, 3, 4] not to start with [0, 1]")
+ end
+ end
+end
+
+describe "should end_with" do
+
+ context "A test string" do
+ it "passes if it matches the end of the string" do
+ subject.should end_with "string"
+ end
+
+ it "fails if it does not match the end of the string" do
+ lambda {
+ subject.should end_with "Something"
+ }.should fail_with("expected \"A test string\" to end with \"Something\"")
+ end
+ end
+
+ context [0, 1, 2, 3, 4] do
+ it "passes if it is the last element of the array" do
+ subject.should end_with 4
+ end
+
+ it "passes if the last elements of the array match" do
+ subject.should end_with [3, 4]
+ end
+
+ it "fails if it does not match the last element of the array" do
+ lambda {
+ subject.should end_with "Something"
+ }.should fail_with("expected [0, 1, 2, 3, 4] to end with \"Something\"")
+ end
+
+ it "fails if it the last elements of the array do not match" do
+ lambda {
+ subject.should end_with [1, 2]
+ }.should fail_with("expected [0, 1, 2, 3, 4] to end with [1, 2]")
+ end
+ end
+
+ context Object.new do
+ it "should raise an error if expected value can't be indexed'" do
+ expect { subject.should start_with 0 }.to raise_error(ArgumentError, /does not respond to :\[\]/)
+ end
+ end
+end
+
+describe "should_not end_with" do
+
+ context "A test string" do
+ it "passes if it does not match the end of the string" do
+ subject.should_not end_with "Something"
+ end
+
+ it "fails if it matches the end of the string" do
+ lambda {
+ subject.should_not end_with "string"
+ }.should fail_with("expected \"A test string\" not to end with \"string\"")
+
+ end
+ end
+
+ context [0, 1, 2, 3, 4] do
+ it "passes if it is not the last element of the array" do
+ subject.should_not end_with "Something"
+ end
+
+ it "passes if the last elements of the array do not match" do
+ subject.should_not end_with [0, 1]
+ end
+
+ it "fails if it matches the last element of the array" do
+ lambda {
+ subject.should_not end_with 4
+ }.should fail_with("expected [0, 1, 2, 3, 4] not to end with 4")
+ end
+
+ it "fails if it the last elements of the array match" do
+ lambda {
+ subject.should_not end_with [3, 4]
+ }.should fail_with("expected [0, 1, 2, 3, 4] not to end with [3, 4]")
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.