Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added start_with and end_with support for Array

  • Loading branch information...
commit 9b9946c977ae1714145915bbd6eaa09353b2189b 1 parent 77a9ecc
@jeremywadsack jeremywadsack authored
View
32 features/built_in_matchers/end_with.feature
@@ -8,8 +8,8 @@ Feature: end_with matcher
The test is case sensitive.
- Scenario: basic usage
- Given a file named "end_with_matcher_spec.rb" with:
+ Scenario: string usage
+ Given a file named "string_end_with_matcher_spec.rb" with:
"""
describe "A test string" do
it { should end_with "string" }
@@ -20,8 +20,28 @@ Feature: end_with matcher
it { should end_with "Something" }
end
"""
- When I run `rspec end_with_matcher_spec.rb`
+ 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 |
+ | 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
29 features/built_in_matchers/start_with.feature
@@ -8,8 +8,8 @@ Feature: start_with matcher
The test is case sensitive.
- Scenario: basic usage
- Given a file named "start_with_matcher_spec.rb" with:
+ 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" }
@@ -20,10 +20,29 @@ Feature: start_with matcher
it { should start_with "Something" }
end
"""
- When I run `rspec start_with_matcher_spec.rb`
+ 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 |
+ | 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
32 lib/rspec/matchers/built_in/start_with_end_with.rb
@@ -4,20 +4,28 @@ module BuiltIn
class StartWith
include BaseMatcher
def initialize(expected)
- @expected = expected.to_s
+ @expected = expected
end
def matches?(actual)
- @actual = actual.to_s
- @actual[0, @expected.length] == @expected
+ @actual = actual
+ if @actual.respond_to?(:[])
+ if @expected.respond_to?(:length)
+ @actual[0, @expected.length] == @expected
+ else
+ @actual[0] == @expected
+ end
+ else
+ # TODO: Spec this case
+ end
end
def failure_message_for_should
- "expected '#{@actual}' to start with '#{@expected}'"
+ "expected #{@actual.inspect} to start with #{@expected.inspect}"
end
def failure_message_for_should_not
- "expected '#{@actual}' not to start with '#{@expected}'"
+ "expected #{@actual.inspect} not to start with #{@expected.inspect}"
end
end
@@ -29,15 +37,23 @@ def initialize(expected)
def matches?(actual)
@actual = actual
- @actual[-@expected.length, @expected.length] == @expected
+ if @actual.respond_to?(:[])
+ if @expected.respond_to?(:length)
+ @actual[-@expected.length, @expected.length] == @expected
+ else
+ @actual[-1] == @expected
+ end
+ else
+ # TODO: Spec this case
+ end
end
def failure_message_for_should
- "expected '#{@actual}' to end with '#{@expected}'"
+ "expected #{@actual.inspect} to end with #{@expected.inspect}"
end
def failure_message_for_should_not
- "expected '#{@actual}' not to end with '#{@expected}'"
+ "expected #{@actual.inspect} not to end with #{@expected.inspect}"
end
end
end
View
125 spec/rspec/matchers/start_with_end_with_spec.rb
@@ -2,9 +2,7 @@
describe "should start_with" do
- context "strings" do
- subject { "A test string" }
-
+ context "A test string" do
it "passes if it matches the start of the string" do
subject.should start_with "A test"
end
@@ -12,33 +10,36 @@
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'")
+ }.should fail_with("expected \"A test string\" to start with \"Something\"")
end
end
-end
-describe "should end_with" do
+ 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
- context "strings" do
- subject { "A test string" }
+ it "passes if the first elements of the array match" do
+ subject.should start_with [0, 1]
+ end
- it "passes if it matches the end of the string" do
- subject.should end_with "string"
+ 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 does not match the end of the string" do
+ it "fails if it the first elements of the array do not match" do
lambda {
- subject.should end_with "Something"
- }.should fail_with("expected 'A test string' to end with 'Something'")
+ subject.should start_with [1, 2]
+ }.should fail_with("expected [0, 1, 2, 3, 4] to start with [1, 2]")
end
end
end
describe "should_not start_with" do
- context "strings" do
- subject { "A test string" }
-
+ 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
@@ -46,16 +47,73 @@
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'")
+ }.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_not end_with" do
+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
- context "strings" do
- subject { "A test string" }
+ 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
+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
@@ -63,8 +121,31 @@
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'")
+ }.should fail_with("expected \"A test string\" not to end with \"string\"")
end
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.