diff --git a/spec/expectations/block_expectation_partial_spec.cr b/spec/expectations/block_expectation_partial_spec.cr new file mode 100644 index 0000000..e30dca2 --- /dev/null +++ b/spec/expectations/block_expectation_partial_spec.cr @@ -0,0 +1,150 @@ +require "../spec_helper" + +describe Spectator::Expectations::BlockExpectationPartial do + describe "#actual" do + context "with a label" do + it "contains the value passed to the constructor" do + actual = ->{ 777 } + partial = Spectator::Expectations::BlockExpectationPartial.new(actual.to_s, actual) + partial.actual.should eq(actual.call) + end + end + + context "without a label" do + it "contains the value passed to the constructor" do + actual = ->{ 777 } + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + partial.actual.should eq(actual.call) + end + end + end + + describe "#label" do + context "when provided" do + it "contains the value passed to the constructor" do + actual = ->{ 777 } + label = "lucky" + partial = Spectator::Expectations::BlockExpectationPartial.new(label, actual) + partial.label.should eq(label) + end + end + + context "when omitted" do + it "contains a stringified version of #actual" do + actual = ->{ 777 } + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + partial.label.should eq(actual.to_s) + end + end + end + + describe "#to" do + it "reports an expectation" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 777 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + partial.to(matcher) + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.size.should eq(1) + end + + it "reports multiple expectations" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 777 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + 5.times { partial.to(matcher) } + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.size.should eq(5) + end + + context "with a met condition" do + it "reports a satisifed expectation" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 777 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + partial.to(matcher) + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.first.satisfied?.should be_true + end + end + + context "with an unmet condition" do + it "reports an unsatisfied expectation" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 42 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + partial.to(matcher) + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.first.satisfied?.should be_false + end + end + end + + {% for method in %i[to_not not_to] %} + describe "#" + {{method.id.stringify}} do + it "reports an expectation" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 777 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + partial.{{method.id}}(matcher) + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.size.should eq(1) + end + + it "reports multiple expectations" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 42 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + 5.times { partial.{{method.id}}(matcher) } + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.size.should eq(5) + end + + context "with a met condition" do + it "reports an unsatisifed expectation" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 777 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + partial.{{method.id}}(matcher) + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.first.satisfied?.should be_false + end + end + + context "with an unmet condition" do + it "reports an satisfied expectation" do + spy = SpyExample.create do + actual = ->{ 777 } + expected = 42 + partial = Spectator::Expectations::BlockExpectationPartial.new(actual) + matcher = Spectator::Matchers::EqualityMatcher.new(expected) + partial.{{method.id}}(matcher) + end + Spectator::Internals::Harness.run(spy) + spy.harness.expectations.first.satisfied?.should be_true + end + end + end + {% end %} +end diff --git a/spec/expectations/block_expectation_spec.cr b/spec/expectations/block_expectation_spec.cr new file mode 100644 index 0000000..e509d4c --- /dev/null +++ b/spec/expectations/block_expectation_spec.cr @@ -0,0 +1,159 @@ +require "../spec_helper" + +describe Spectator::Expectations::BlockExpectation do + describe "#satisifed?" do + context "with a successful match" do + it "is true" do + value = 42 + block = ->{ value } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::BlockExpectation.new(true, false, partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.satisfied?.should be_true + end + + context "when negated" do + it "is false" do + value = 42 + block = ->{ value } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::BlockExpectation.new(true, true, partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.satisfied?.should be_false + end + end + end + + context "with an unsuccessful match" do + it "is false" do + value1 = 42 + value2 = 777 + block = ->{ value1 } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::BlockExpectation.new(false, false, partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.satisfied?.should be_false + end + + context "when negated" do + it "is true" do + value1 = 42 + value2 = 777 + block = ->{ value1 } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::BlockExpectation.new(false, true, partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.satisfied?.should be_true + end + end + end + end + + describe "#actual_message" do + context "with a successful match" do + it "equals the matcher's #message" do + value = 42 + block = ->{ value } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::BlockExpectation.new(true, false, partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.actual_message.should eq(matcher.message(partial)) + end + + context "when negated" do + it "equals the matcher's #negated_message" do + value = 42 + block = ->{ value } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::BlockExpectation.new(true, true, partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.actual_message.should eq(matcher.negated_message(partial)) + end + end + end + + context "with an unsuccessful match" do + it "equals the matcher's #negated_message" do + value1 = 42 + value2 = 777 + block = ->{ value1 } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::BlockExpectation.new(false, false, partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.actual_message.should eq(matcher.negated_message(partial)) + end + + context "when negated" do + it "equals the matcher's #message" do + value1 = 42 + value2 = 777 + block = ->{ value1 } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::BlockExpectation.new(false, true, partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.actual_message.should eq(matcher.message(partial)) + end + end + end + end + + describe "#expected_message" do + context "with a successful match" do + it "equals the matcher's #message" do + value = 42 + block = ->{ value } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::BlockExpectation.new(true, false, partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.expected_message.should eq(matcher.message(partial)) + end + + context "when negated" do + it "equals the matcher's #negated_message" do + value = 42 + block = ->{ value } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::BlockExpectation.new(true, true, partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.expected_message.should eq(matcher.negated_message(partial)) + end + end + end + + context "with an unsuccessful match" do + it "equals the matcher's #message" do + value1 = 42 + value2 = 777 + block = ->{ value1 } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::BlockExpectation.new(false, false, partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.expected_message.should eq(matcher.message(partial)) + end + + context "when negated" do + it "equals the matcher's #negated_message" do + value1 = 42 + value2 = 777 + block = ->{ value1 } + partial = Spectator::Expectations::BlockExpectationPartial.new(block.to_s, block) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::BlockExpectation.new(false, true, partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.expected_message.should eq(matcher.negated_message(partial)) + end + end + end + end +end