Skip to content

Commit

Permalink
Merge pull request #1412 from cucumber/1410-enable-logging-objects
Browse files Browse the repository at this point in the history
Enable logging non-string - fix #1410
  • Loading branch information
vincent-psarga authored Apr 28, 2020
2 parents 516738c + 0db705c commit 3c96f89
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
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)
run_step 'Loud'
end
end

Expand Down

0 comments on commit 3c96f89

Please sign in to comment.