From 2c542e0ebdf1c3ffd88f9a1ad2f9384e04cd5c23 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 5 Nov 2024 15:24:01 +1300 Subject: [PATCH] Add support for `Kernel#warn`. --- gems.rb | 2 ++ lib/console.rb | 1 + lib/console/warn.rb | 18 ++++++++++++++++++ test/console/warn.rb | 24 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 lib/console/warn.rb create mode 100644 test/console/warn.rb diff --git a/gems.rb b/gems.rb index c1f2dea..059ba7a 100644 --- a/gems.rb +++ b/gems.rb @@ -22,4 +22,6 @@ gem "bake-test" gem "bake-test-external" + + gem "sus-fixtures-console" end diff --git a/lib/console.rb b/lib/console.rb index 7397722..c4a18a5 100644 --- a/lib/console.rb +++ b/lib/console.rb @@ -8,6 +8,7 @@ require_relative 'console/version' require_relative 'console/logger' +require_relative 'console/warn' module Console class << self diff --git a/lib/console/warn.rb b/lib/console/warn.rb new file mode 100644 index 0000000..0c25784 --- /dev/null +++ b/lib/console/warn.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2019-2024, by Samuel Williams. + +module Console + module Warn + def warn(*arguments, uplevel: nil, **options) + if uplevel + options[:backtrace] = caller(uplevel, 1) + end + + Console.warn(*arguments, **options) + end + end + + ::Kernel.prepend(Warn) +end diff --git a/test/console/warn.rb b/test/console/warn.rb new file mode 100644 index 0000000..c4a9f63 --- /dev/null +++ b/test/console/warn.rb @@ -0,0 +1,24 @@ +require 'sus/fixtures/console' + +describe "Kernel#warn" do + include_context Sus::Fixtures::Console::CapturedLogger + + it "redirects to Console.warn" do + warn "It did not work as expected!" + + expect(console_capture.last).to have_keys( + severity: be == :warn, + subject: be == "It did not work as expected!" + ) + end + + it "supports uplevel" do + warn "It did not work as expected!", uplevel: 1 + + expect(console_capture.last).to have_keys( + severity: be == :warn, + subject: be == "It did not work as expected!", + backtrace: be_a(Array) + ) + end +end \ No newline at end of file