Skip to content

Commit

Permalink
mattheworiordan#115, mattheworiordan#93: Replaced colored gem depency…
Browse files Browse the repository at this point in the history
… with custom helper methods
  • Loading branch information
fbernier committed Feb 17, 2015
1 parent be7acb8 commit 618c715
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
1 change: 0 additions & 1 deletion capybara-screenshot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Gem::Specification.new do |s|
s.add_dependency 'capybara', ['>= 1.0', '< 3']
end
s.add_dependency 'launchy'
s.add_dependency 'colored'

s.add_development_dependency 'rspec'
s.add_development_dependency 'timecop'
Expand Down
28 changes: 28 additions & 0 deletions lib/capybara-screenshot/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class CapybaraScreenshot
module Helpers
extend self

COLORS =
{
"black" => 0,
"red" => 1,
"green" => 2,
"yellow" => 3,
"blue" => 4,
"purple" => 5,
"magenta" => 5,
"cyan" => 6,
"white" => 7
}

COLORS.each_pair do |color, value|
define_method color do |text|
"\033[0;#{30+value}m#{text}\033[0m"
end

define_method "bright_#{color}" do |text|
"\033[1;#{30+value}m#{text}\033[0m"
end
end
end
end
5 changes: 3 additions & 2 deletions lib/capybara-screenshot/rspec/text_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'capybara-screenshot/rspec/base_reporter'
require 'capybara-screenshot/helpers'

module Capybara
module Screenshot
Expand All @@ -25,8 +26,8 @@ def example_failed_with_screenshot(notification)
private
def output_screenshot_info(example)
return unless (screenshot = example.metadata[:screenshot])
output.puts(long_padding + "HTML screenshot: #{screenshot[:html]}".yellow) if screenshot[:html]
output.puts(long_padding + "Image screenshot: #{screenshot[:image]}".yellow) if screenshot[:image]
output.puts(long_padding + CapybaraScreenshot::Helpers.yellow("HTML screenshot: #{screenshot[:html]}")) if screenshot[:html]
output.puts(long_padding + CapybaraScreenshot::Helpers.yellow("Image screenshot: #{screenshot[:image]}")) if screenshot[:image]
end

def long_padding
Expand Down
11 changes: 2 additions & 9 deletions lib/capybara-screenshot/saver.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
if RUBY_PLATFORM =~ /win32/
begin
require 'Win32/Console/ANSI'
rescue LoadError => e
fail "win32console gem is required to output colorized messages, please add this to your Gemfile or install"
end
end
require 'colored'
require 'capybara-screenshot/helpers'

module Capybara
module Screenshot
Expand Down Expand Up @@ -89,7 +82,7 @@ def output_screenshot_path

private
def output(message)
puts " #{message.yellow}"
puts " #{CapybaraScreenshot::Helpers.yellow(message)}"
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/unit/rspec_reporters/text_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'capybara-screenshot/helpers'

describe Capybara::Screenshot::RSpec::TextReporter do
before do
Expand Down Expand Up @@ -67,7 +68,7 @@ def example_failed_method_argument_double(metadata = {})

it 'appends the html file path to the original output' do
@reporter.send(example_failed_method, example)
expect(@reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html".yellow}\n")
expect(@reporter.output.string).to eql("original failure info\n #{CapybaraScreenshot::Helpers.yellow("HTML screenshot: path/to/html")}\n")
end
end

Expand All @@ -76,7 +77,7 @@ def example_failed_method_argument_double(metadata = {})

it 'appends the image path to the original output' do
@reporter.send(example_failed_method, example)
expect(@reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html".yellow}\n #{"Image screenshot: path/to/image".yellow}\n")
expect(@reporter.output.string).to eql("original failure info\n #{CapybaraScreenshot::Helpers.yellow("HTML screenshot: path/to/html")}\n #{CapybaraScreenshot::Helpers.yellow("Image screenshot: path/to/image")}\n")
end
end

Expand All @@ -92,6 +93,6 @@ def red(str)
old_reporter.singleton_class.send :include, described_class
example = example_failed_method_argument_double(screenshot: { html: "path/to/html" })
old_reporter.send(example_failed_method, example)
expect(old_reporter.output.string).to eql("original failure info\n #{"HTML screenshot: path/to/html".yellow}\n")
expect(old_reporter.output.string).to eql("original failure info\n #{CapybaraScreenshot::Helpers.yellow("HTML screenshot: path/to/html")}\n")
end
end

0 comments on commit 618c715

Please sign in to comment.