diff --git a/README.md b/README.md index da7b2e0..1fc5ef4 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ In no particular order, features that have been implemented and are planned: - [X] Pending tests - `pending` - [ ] Shared examples - `behaves_like`, `include_examples` - [ ] Matchers - - [ ] Equality matchers - `eq`, `be`, `be_a`, `match` + - [X] Equality matchers - `eq`, `be`, `be_a`, `match` - [ ] Truthy matchers - `be_true`, `be_false`, `be_truthy`, `be_falsey` - [ ] Comparison matchers - `<`, `<=`, `>`, `>=`, `be_within` - [ ] Question matchers - `be_nil`, `be_xxx` diff --git a/spec/helpers/spy_sut.cr b/spec/helpers/spy_sut.cr index 3c45ccb..b57b69c 100644 --- a/spec/helpers/spy_sut.cr +++ b/spec/helpers/spy_sut.cr @@ -7,6 +7,9 @@ class SpySUT # Number of times the `#===` method was called. getter case_eq_call_count = 0 + # Number of times the `#=~` method was called. + getter match_call_count = 0 + # Returns true and increments `#eq_call_count`. def ==(other : T) forall T @eq_call_count += 1 @@ -18,4 +21,10 @@ class SpySUT @case_eq_call_count += 1 true end + + # Returns true and increments `#match_eq_call_count`. + def =~(other : T) forall T + @match_call_count += 1 + true + end end diff --git a/spec/matchers/regex_matcher_spec.cr b/spec/matchers/regex_matcher_spec.cr new file mode 100644 index 0000000..8ecd2a5 --- /dev/null +++ b/spec/matchers/regex_matcher_spec.cr @@ -0,0 +1,109 @@ +require "../spec_helper" + +describe Spectator::Matchers::RegexMatcher do + describe "#match?" do + it "compares using #=~" do + spy = SpySUT.new + partial = Spectator::Expectations::ValueExpectationPartial.new(spy) + matcher = Spectator::Matchers::RegexMatcher.new(/foobar/) + matcher.match?(partial).should be_true + spy.match_call_count.should be > 0 + end + + context "with a matching pattern" do + it "is true" do + value = "foobar" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.match?(partial).should be_true + end + end + + context "with a non-matching pattern" do + it "is false" do + value = "foo" + pattern = /bar/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.match?(partial).should be_false + end + end + end + + describe "#message" do + it "mentions =~" do + value = "foobar" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.message(partial).should contain("=~") + end + + it "contains the actual label" do + value = "foobar" + label = "different" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(label, value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.message(partial).should contain(label) + end + + it "contains the expected label" do + value = "foobar" + label = "different" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(label, pattern) + matcher.message(partial).should contain(label) + end + + context "when expected label is omitted" do + it "contains stringified form of expected value" do + value = "foobar" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.message(partial).should contain(pattern.to_s) + end + end + end + + describe "#negated_message" do + it "mentions =~" do + value = "foobar" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.negated_message(partial).should contain("=~") + end + + it "contains the actual label" do + value = "foobar" + label = "different" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(label, value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.negated_message(partial).should contain(label) + end + + it "contains the expected label" do + value = "foobar" + label = "different" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(label, pattern) + matcher.negated_message(partial).should contain(label) + end + + context "when expected label is omitted" do + it "contains stringified form of expected value" do + value = "foobar" + pattern = /foo/ + partial = Spectator::Expectations::ValueExpectationPartial.new(value) + matcher = Spectator::Matchers::RegexMatcher.new(pattern) + matcher.negated_message(partial).should contain(pattern.to_s) + end + end + end +end