Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable logging non-string - fix #1410 #1412

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Please visit [cucumber/CONTRIBUTING.md](https://github.com/cucumber/cucumber/blo

### Changed

* N/A
* `log` method can now be called with non-string objects and will run `.to_s` on them. [#1410](https://github.com/cucumber/cucumber-ruby/issues/1410)

### Removed

Expand Down
3 changes: 0 additions & 3 deletions lib/cucumber/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@ def initialize(messages)
super(messages.join("\n"))
end
end

class LogTypeInvalid < StandardError
end
end
5 changes: 2 additions & 3 deletions lib/cucumber/glue/proto_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ def embed(file, mime_type, _label = 'Screenshot')
attach(file, mime_type)
end

def log(message)
raise Cucumber::LogTypeInvalid unless message.is_a?(String)
attach(message.dup, 'text/x.cucumber.log+plain')
def log(*messages)
messages.each { |message| attach(message.to_s.dup, 'text/x.cucumber.log+plain') }
end

def attach(file, media_type)
Expand Down
43 changes: 43 additions & 0 deletions spec/cucumber/glue/proto_world_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,49 @@ module Glue
end
end

describe 'when logging an object' do
define_feature <<-FEATURE
Feature: Banana party

Scenario: Monkey eats banana
When an object is logged
FEATURE

define_steps do
When('an object is logged') do
log(a: 1, b: 2, c: 3)
end
end

it 'attached the styring version on the object' do
expect(@out.string).to include '{:a=>1, :b=>2, :c=>3}'
end
end

describe 'when logging multiple items on one call' do
define_feature <<-FEATURE
Feature: Banana party

Scenario: Monkey eats banana
When monkey eats banana
FEATURE

define_steps do
When('{word} {word} {word}') do |subject, verb, complement|
log "subject: #{subject}", "verb: #{verb}", "complement: #{complement}", subject: subject, verb: verb, complement: complement
end
end

it 'logs each parameter independently' do
expect(@out.string).to include [
' subject: monkey',
' verb: eats',
' complement: banana',
' {:subject=>"monkey", :verb=>"eats", :complement=>"banana"}'
].join("\n")
end
end

describe 'when modifying the printed variable after the call to log' do
define_feature <<-FEATURE
Feature: Banana party
Expand Down
5 changes: 3 additions & 2 deletions spec/cucumber/glue/step_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ def step_match(text)
run_step 'Loud'
end

it 'raises an exception if the message is not a String' do
it 'calls `to_s` if the message is not a String' do
expect(user_interface).to receive(:attach).with('["Not", 1, "string"]', 'text/x.cucumber.log+plain')
dsl.Given(/Loud/) do
log ['Not', 1, 'string']
end
expect { run_step 'Loud' }.to raise_exception(Cucumber::LogTypeInvalid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Cucumber::LogTypeInvalid still used? Should it be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

run_step 'Loud'
end
end

Expand Down