Browse Source

Add nil check for colors, just in case

Probably not possible, but provides a more descriptive error than before anyway.

Fixes #84.
dev
Andy Meneely 11 years ago
parent
commit
70a5f48bda
  1. 3
      lib/squib/graphics/cairo_context_wrapper.rb
  2. 35
      spec/graphics/cairo_context_wrapper_spec.rb

3
lib/squib/graphics/cairo_context_wrapper.rb

@ -30,6 +30,7 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def set_source_squibcolor(arg) def set_source_squibcolor(arg)
raise 'nil is not a valid color' if arg.nil?
if match = arg.match(LINEAR_GRADIENT) if match = arg.match(LINEAR_GRADIENT)
x1, y1, x2, y2 = match.captures x1, y1, x2, y2 = match.captures
linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f) linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f)
@ -82,4 +83,4 @@ module Squib
end end
end end
end end

35
spec/graphics/cairo_context_wrapper_spec.rb

@ -6,22 +6,31 @@ describe Squib::Graphics::CairoContextWrapper do
let(:cairo) { double(Cairo::Context) } let(:cairo) { double(Cairo::Context) }
subject { Squib::Graphics::CairoContextWrapper.new(cairo) } subject { Squib::Graphics::CairoContextWrapper.new(cairo) }
it 'passes on colors as normal' do context '#set_source_squibcolor' do
expect(cairo).to receive(:set_source_color).with('blue')
subject.set_source_squibcolor('blue')
end
it 'passes on color symbols as normal' do it 'passes on colors as normal' do
expect(cairo).to receive(:set_source_color).with(:blue) expect(cairo).to receive(:set_source_color).with('blue')
subject.set_source_squibcolor(:blue) subject.set_source_squibcolor('blue')
end end
it 'passes on color symbols as normal' do
expect(cairo).to receive(:set_source_color).with(:blue)
subject.set_source_squibcolor(:blue)
end
it 'passes on color hashes' do
expect(cairo).to receive(:set_source_color)
.with('#aabbccdd')
subject.set_source_squibcolor('#aabbccdd')
end
it 'raises on nil' do
expect { subject.set_source_squibcolor(nil) }.to raise_error('nil is not a valid color')
end
it 'passes on color hashes' do
expect(cairo).to receive(:set_source_color)
.with('#aabbccdd')
subject.set_source_squibcolor('#aabbccdd')
end end
context 'regex variations for linear gradients' do context 'regex variations for linear gradients' do
before(:each) do before(:each) do
dbl = double(Cairo::LinearPattern) dbl = double(Cairo::LinearPattern)
@ -72,4 +81,4 @@ describe Squib::Graphics::CairoContextWrapper do
end end
end end
end end

Loading…
Cancel
Save