mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
DRY up code by adding expectation helper methods
This commit is contained in:
parent
440a2c6643
commit
1064444f15
3 changed files with 51 additions and 44 deletions
|
@ -5,12 +5,7 @@ describe Spectator::Expectations::ExpectationReporter do
|
|||
context "with raise flag set" do
|
||||
context "given a successful result" do
|
||||
it "stores the result" do
|
||||
value = 42
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result = expectation.eval
|
||||
result.successful?.should be_true # Sanity check.
|
||||
result = new_successful_result
|
||||
reporter = Spectator::Expectations::ExpectationReporter.new(true)
|
||||
reporter.report(result)
|
||||
reporter.results.should contain(result)
|
||||
|
@ -19,25 +14,13 @@ describe Spectator::Expectations::ExpectationReporter do
|
|||
|
||||
context "given a failed result" do
|
||||
it "raises and error" do
|
||||
value1 = 42
|
||||
value2 = 777
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value1)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value2)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result = expectation.eval
|
||||
result.successful?.should be_false # Sanity check.
|
||||
result = new_failure_result
|
||||
reporter = Spectator::Expectations::ExpectationReporter.new(true)
|
||||
expect_raises(Spectator::ExpectationFailed) { reporter.report(result) }
|
||||
end
|
||||
|
||||
it "stores the result" do
|
||||
value1 = 42
|
||||
value2 = 777
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value1)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value2)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result = expectation.eval
|
||||
result.successful?.should be_false # Sanity check.
|
||||
result = new_failure_result
|
||||
reporter = Spectator::Expectations::ExpectationReporter.new(true)
|
||||
begin
|
||||
reporter.report(result)
|
||||
|
@ -52,12 +35,7 @@ describe Spectator::Expectations::ExpectationReporter do
|
|||
context "with raise flag not set" do
|
||||
context "given a successful result" do
|
||||
it "stores the result" do
|
||||
value = 42
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result = expectation.eval
|
||||
result.successful?.should be_true # Sanity check.
|
||||
result = new_successful_result
|
||||
reporter = Spectator::Expectations::ExpectationReporter.new(false)
|
||||
reporter.report(result)
|
||||
reporter.results.should contain(result)
|
||||
|
@ -66,13 +44,7 @@ describe Spectator::Expectations::ExpectationReporter do
|
|||
|
||||
context "given a failed result" do
|
||||
it "stores the result" do
|
||||
value1 = 42
|
||||
value2 = 777
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value1)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value2)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result = expectation.eval
|
||||
result.successful?.should be_false # Sanity check.
|
||||
result = new_failure_result
|
||||
reporter = Spectator::Expectations::ExpectationReporter.new(false)
|
||||
reporter.report(result)
|
||||
reporter.results.should contain(result)
|
||||
|
@ -91,17 +63,8 @@ describe Spectator::Expectations::ExpectationReporter do
|
|||
|
||||
context "with multiple expectations" do
|
||||
it "contains all expectations" do
|
||||
value1 = 42
|
||||
value2 = 777
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value1)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value1)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result1 = expectation.eval
|
||||
partial = Spectator::Expectations::ValueExpectationPartial.new(value1)
|
||||
matcher = Spectator::Matchers::EqualityMatcher.new(value2)
|
||||
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
result2 = expectation.eval
|
||||
|
||||
result1 = new_successful_result
|
||||
result2 = new_failure_result
|
||||
reporter = Spectator::Expectations::ExpectationReporter.new(false)
|
||||
begin
|
||||
reporter.report(result1)
|
||||
|
|
43
spec/expectations_helper.cr
Normal file
43
spec/expectations_helper.cr
Normal file
|
@ -0,0 +1,43 @@
|
|||
# Utility methods for creating expectations, partials, and matchers.
|
||||
|
||||
def new_partial(label : String, actual : T) forall T
|
||||
Spectator::Expectations::ValueExpectationPartial.new(label, actual)
|
||||
end
|
||||
|
||||
def new_partial(actual : T = 123) forall T
|
||||
new_partial(actual.to_s, actual)
|
||||
end
|
||||
|
||||
def new_matcher(label : String, expected : T) forall T
|
||||
Spectator::Matchers::EqualityMatcher.new(label, expected)
|
||||
end
|
||||
|
||||
def new_matcher(expected : T = 123) forall T
|
||||
new_matcher(expected.to_s, expected)
|
||||
end
|
||||
|
||||
def new_expectation(expected : ExpectedType = 123, actual : ActualType = 123) forall ExpectedType, ActualType
|
||||
partial = new_partial("foo", actual)
|
||||
matcher = new_matcher("bar", expected)
|
||||
Spectator::Expectations::ValueExpectation.new(partial, matcher)
|
||||
end
|
||||
|
||||
def new_met_expectation(value : T = 123) forall T
|
||||
new_expectation(value, value)
|
||||
end
|
||||
|
||||
def new_unmet_expectation(expected : ExpectedType = 123, actual : ActualType = 456) forall ExpectedType, ActualType
|
||||
new_expectation(expected, actual)
|
||||
end
|
||||
|
||||
def new_successful_result
|
||||
new_met_expectation.eval.tap do |result|
|
||||
result.successful?.should be_true # Sanity check.
|
||||
end
|
||||
end
|
||||
|
||||
def new_failure_result
|
||||
new_unmet_expectation.eval.tap do |result|
|
||||
result.successful?.should be_false # Sanity check.
|
||||
end
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
require "spec"
|
||||
require "../src/spectator"
|
||||
require "./expectations_helper"
|
||||
|
||||
# Prevent Spectator from trying to run tests.
|
||||
Spectator.autorun = false
|
||||
|
|
Loading…
Reference in a new issue