lib/composable_operations/matcher/execution.rb in composable_operations-0.9.1 vs lib/composable_operations/matcher/execution.rb in composable_operations-0.9.2

- old
+ new

@@ -12,13 +12,23 @@ def when_initialized_with(*input) @input = input self end + def failure_message + raise NotImplementedError, "Expected #{self.class} to implement ##{__callee__}" + end + + def failure_message_when_negated + raise NotImplementedError, "Expected #{self.class} to implement ##{__callee__}" + end + alias negative_failure_message failure_message_when_negated + protected attr_reader :operation + attr_reader :message attr_reader :result attr_reader :input def operation=(operation) operation = operation.new(*input) if operation.kind_of?(Class) @@ -32,15 +42,24 @@ def succeeded? operation.succeeded? end + def halted? + operation.halted? + end + def result_as_expected? return true unless result operation.result == result end + def message_as_expected? + return true unless message + operation.message == message + end + def input_as_text humanize(*input) end def result_as_text @@ -76,11 +95,10 @@ end def failure_message_when_negated "the operation succeeded unexpectedly" end - alias negative_failure_message failure_message_when_negated private def failure_reasons reasons = [] @@ -122,24 +140,62 @@ end def failure_message_when_negated "the operation failed unexpectedly" end - alias negative_failure_message failure_message_when_negated protected - attr_reader :message - - def message_as_expected? - return true unless message - operation.message == message + def failure_reasons + reasons = [] + reasons << "it did not fail at all" unless failed? + reasons << "its message was not as expected" unless message_as_expected? + unless result_as_expected? + reasons << [ + "it did not return the expected result", + "Expected: #{result.inspect}", + "Got: #{operation.result.inspect}" + ].join("\n\t ") + end + reasons.map { |r| "\t- #{r}" }.join("\n") end + end + + class HaltWhilePerforming < Base + + def matches?(operation) + self.operation = operation + halted? && result_as_expected? && message_as_expected? + end + + def because(message) + @message = message + self + end + + def description + description = "halt while performing" + description += " because #{message}" if message + description += " when initialized with custom input (#{input_as_text})" if input + description += " and return the expected result (#{result_as_text})" if result + description + end + + def failure_message + "the operation did not halt while performing for the following reason(s):\n#{failure_reasons}" + end + + def failure_message_when_negated + "the operation was halted unexpectedly" + end + + protected + def failure_reasons reasons = [] - reasons << "it did not fail at all" unless failed? + reasons << "it did not halt at all" unless halted? reasons << "its message was not as expected" unless message_as_expected? unless result_as_expected? reasons << [ "it did not return the expected result", "Expected: #{result.inspect}", @@ -155,9 +211,13 @@ SucceedToPerform.new end def fail_to_perform FailToPerform.new + end + + def halt_while_performing + HaltWhilePerforming.new end end end end