Skip to content

Commit

Permalink
Add expect_console and have_logged predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 17, 2024
1 parent 424f9a4 commit f093d9c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/sus/fixtures/console/captured_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ module Sus
module Fixtures
module Console
CapturedLogger = Sus::Shared("captured logger") do
let(:capture) {::Console::Capture.new}
let(:logger) {::Console::Logger.new(capture, level: ::Console::Logger::DEBUG)}
let(:console_capture) {::Console::Capture.new}
let(:console_logger) {::Console::Logger.new(console_capture, level: ::Console::Logger::DEBUG)}

def around
::Console.logger = logger
::Console.logger = console_logger
super
ensure
::Console.logger = nil
end

def expect_console
expect(console_capture)
end

def have_logged(**fields)
have_value(have_keys(**fields))
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/sus/fixtures/console/null_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module Sus
module Fixtures
module Console
NullLogger = Sus::Shared("null logger") do
let(:logger) {::Console::Logger.new(::Console::Output::Null.new)}
let(:console_logger) {::Console::Logger.new(::Console::Output::Null.new)}

def around
::Console.logger = logger
::Console.logger = console_logger
super
ensure
::Console.logger = nil
Expand Down
32 changes: 31 additions & 1 deletion test/sus/fixtures/console/captured_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,39 @@
it "should capture output" do
Console.debug("Hello, World!")

expect(capture.last).to have_keys(
expect(console_capture.last).to have_keys(
severity: be == :debug,
subject: be == "Hello, World!"
)
end

with "#expect_console" do
it "can use custom expectation" do
Console.debug("Hello, World!")

expect_console.to have_logged(
severity: be == :debug,
subject: be == "Hello, World!"
)
end
end

with '#have_logged' do
it "can expect specific fields to not be logged" do
Console.debug("Hello, World!")

expect(console_capture).not.to have_logged(
severity: be == :info
)
end

it "can expect specific fields" do
Console.debug("Hello, World!")

expect(console_capture).to have_logged(
severity: be == :debug,
subject: be == "Hello, World!"
)
end
end
end

0 comments on commit f093d9c

Please sign in to comment.