Permalink
Browse files

Added start_with and end_with matchers for String

  • Loading branch information...
1 parent 40cbc30 commit 77a9ecc06de1130ca475a9ad789cbbf781171619 @jeremywadsack jeremywadsack committed Apr 8, 2012
@@ -0,0 +1,27 @@
+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: basic usage
+ Given a file named "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 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 |
@@ -0,0 +1,29 @@
+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: basic usage
+ Given a file named "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 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 |
+
+
View
@@ -534,6 +534,14 @@ def satisfy(&block)
BuiltIn::Satisfy.new(&block)
end
+ def start_with(expected)
+ BuiltIn::StartWith.new(expected)
+ end
+
+ def end_with(expected)
+ BuiltIn::EndWith.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.
@@ -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
@@ -0,0 +1,45 @@
+module RSpec
+ module Matchers
+ module BuiltIn
+ class StartWith
+ include BaseMatcher
+ def initialize(expected)
+ @expected = expected.to_s
+ end
+
+ def matches?(actual)
+ @actual = actual.to_s
+ @actual[0, @expected.length] == @expected
+ end
+
+ def failure_message_for_should
+ "expected '#{@actual}' to start with '#{@expected}'"
+ end
+
+ def failure_message_for_should_not
+ "expected '#{@actual}' not to start with '#{@expected}'"
+ end
+ end
+
+ class EndWith
+ include BaseMatcher
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def matches?(actual)
+ @actual = actual
+ @actual[-@expected.length, @expected.length] == @expected
+ end
+
+ def failure_message_for_should
+ "expected '#{@actual}' to end with '#{@expected}'"
+ end
+
+ def failure_message_for_should_not
+ "expected '#{@actual}' not to end with '#{@expected}'"
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,70 @@
+require "spec_helper"
+
+describe "should start_with" do
+
+ context "strings" do
+ subject { "A test string" }
+
+ 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
+end
+
+describe "should end_with" do
+
+ context "strings" do
+ subject { "A test string" }
+
+ 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
+end
+
+describe "should_not start_with" do
+
+ context "strings" do
+ subject { "A test string" }
+
+ 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
+end
+
+describe "should_not end_with" do
+
+ context "strings" do
+ subject { "A test string" }
+
+ 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
+end

0 comments on commit 77a9ecc

Please sign in to comment.