From 46383502259e60f77d03f6f818c4f1931a5930d3 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 9 Dec 2024 15:53:33 +1300 Subject: [PATCH] Allow non-exception objects to be passed to `exception:` keyword argument. Fixes #66. --- lib/console/output/failure.rb | 5 +++- test/console/output/failure.rb | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/console/output/failure.rb diff --git a/lib/console/output/failure.rb b/lib/console/output/failure.rb index 48e9b3c..f9f39a9 100644 --- a/lib/console/output/failure.rb +++ b/lib/console/output/failure.rb @@ -21,8 +21,11 @@ def call(subject = nil, *arguments, exception: nil, **options, &block) if last.is_a?(Exception) options[:event] = Event::Failure.for(last) end - else + elsif exception.is_a?(Exception) options[:event] = Event::Failure.for(exception) + else + # We don't know what this is, so we just pass it through: + options[:exception] = exception end super(subject, *arguments, **options) diff --git a/test/console/output/failure.rb b/test/console/output/failure.rb new file mode 100644 index 0000000..e250604 --- /dev/null +++ b/test/console/output/failure.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2021-2024, by Samuel Williams. + +require "console/logger" +require "console/capture" + +require "console/output/failure" + +describe Console::Output::Failure do + let(:output) {Console::Capture.new} + let(:logger) {subject.new(output)} + let(:error) {StandardError.new("Something went wrong")} + + it "logs exception messages" do + logger.call(self, error) + + expect(output.last).to have_keys( + event: have_keys( + message: be == "Something went wrong" + ) + ) + end + + it "logs exception keyword argument" do + logger.call(self, exception: error) + + expect(output.last).to have_keys( + event: have_keys( + message: be == "Something went wrong" + ) + ) + end + + it "logs non-exception exception keyword argument" do + logger.call(self, exception: "Something went wrong") + + expect(output.last).to have_keys( + exception: be == "Something went wrong" + ) + end +end