Skip to content

Commit

Permalink
Merge #695 and #696 from branch v1.3.x-bugfix. Update History.md
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Aug 25, 2014
2 parents c7c8c83 + 50668e4 commit 1006758
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 7 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ all.
* Better snippet comment ([579](https://github.com/cucumber/cucumber/pull/579) Jeff Nyman)
* Random scenario ordering with `--order random`
* Embed plain text ([712](https://github.com/cucumber/cucumber/pull/712) @bingwei
* Support the cucumber-reporting tools also when using Scenario Outlines ([700](https://github.com/cucumber/cucumber/pull/700) @brasmusson

### Features removed

Expand Down
61 changes: 61 additions & 0 deletions features/docs/formatters/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Feature: JSON output formatter
Given /^I print from step definition/ do
puts "from step definition"
end
Given /^I embed data directly/ do
data = "YWJj"
embed data, "mime-type;base64"
end
"""
And a file named "features/embed.feature" with:
"""
Expand Down Expand Up @@ -70,6 +75,14 @@ Feature: JSON output formatter
And I print from step definition
"""
And a file named "features/embed_data_directly.feature" with:
"""
Feature: An embed data directly feature
Scenario:
Given I embed data directly
"""

# Need to investigate why this won't pass in-process. error_message doesn't get det?
@spawn
Expand Down Expand Up @@ -586,3 +599,51 @@ Feature: JSON output formatter
]
"""

@spawn
Scenario: embedding data directly
When I run `cucumber -b --format json features/embed_data_directly.feature`
Then it should pass with JSON:
"""
[
{
"uri": "features/embed_data_directly.feature",
"id": "an-embed-data-directly-feature",
"keyword": "Feature",
"name": "An embed data directly feature",
"line": 1,
"description": "",
"elements": [
{
"id": "an-embed-data-directly-feature;",
"keyword": "Scenario",
"name": "",
"line": 3,
"description": "",
"type": "scenario",
"steps": [
{
"keyword": "Given ",
"name": "I embed data directly",
"line": 4,
"embeddings": [
{
"mime_type": "mime-type",
"data": "YWJj"
}
],
"match": {
"location": "features/step_definitions/json_steps.rb:10"
},
"result": {
"status": "passed",
"duration": 1
}
}
]
}
]
}
]
"""
17 changes: 11 additions & 6 deletions lib/cucumber/formatter/gherkin_formatter_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,20 @@ def after_features(features)
end

def embed(file, mime_type, label)
# Only embed if file exists
if File.exists?(file)
if File.file?(file)
data = File.open(file, 'rb') { |f| f.read }

if defined?(JRUBY_VERSION)
data = data.to_java_bytes
else
if mime_type =~ /;base64$/
mime_type = mime_type[0..-8]
data = Base64.decode64(file)
else
data = file
end
@gf.embedding(mime_type, data)
end
if defined?(JRUBY_VERSION)
data = data.to_java_bytes
end
@gf.embedding(mime_type, data)
end

def puts(message)
Expand Down
4 changes: 4 additions & 0 deletions lib/cucumber/formatter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def initialize(runtime, path_or_io, options)
def embed(src, mime_type, label)
case(mime_type)
when /^image\/(png|gif|jpg|jpeg)/
unless File.file?(src) or src =~ /^data:image\/(png|gif|jpg|jpeg);base64,/
type = mime_type =~ /;base[0-9]+$/ ? mime_type : mime_type + ";base64"
src = "data:" + type + "," + src
end
embed_image(src, label)
when /^text\/plain/
embed_text(src, label)
Expand Down
33 changes: 32 additions & 1 deletion spec/cucumber/formatter/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ module Formatter

describe "with a step that embeds a snapshot" do
define_steps do
Given(/snap/) { embed('snapshot.jpeg', 'image/jpeg') }
Given(/snap/) {
File.should_receive(:file?).with('snapshot.jpeg').and_return(true)
embed('snapshot.jpeg', 'image/jpeg')
}
end

define_feature(<<-FEATURE)
Expand All @@ -271,6 +274,34 @@ module Formatter
it { expect(@doc.at('a#text_0')['href'].to_s).to eq "log.txt" }
end

describe "with a step that embeds a snapshot content manually" do
define_steps do
Given(/snap/) { embed('data:image/png;base64,YWJj', 'image/png') }
end

define_feature(<<-FEATURE)
Feature:
Scenario:
Given snap
FEATURE

it { expect(@doc.css('.embed img').first.attributes['src'].to_s).to eq "data:image/png;base64,YWJj" }
end

describe "with a step that embeds a snapshot content" do
define_steps do
Given(/snap/) { embed('YWJj', 'image/png;base64') }
end

define_feature(<<-FEATURE)
Feature:
Scenario:
Given snap
FEATURE

it { expect(@doc.css('.embed img').first.attributes['src'].to_s).to eq "data:image/png;base64,YWJj" }
end

describe "with an undefined Given step then an undefined And step" do
define_feature(<<-FEATURE)
Feature:
Expand Down

1 comment on commit 1006758

@mattwynne
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for remembering to update History.md!

Please sign in to comment.