Browse Source

Allowing strings for some inputs on text

Also writing mock object tests
dev
Andy Meneely 11 years ago
parent
commit
f4d2d759c1
  1. 39
      lib/squib/graphics/text.rb
  2. 98
      spec/graphics/graphics_text_spec.rb
  3. 7
      spec/input_helpers_spec.rb
  4. 15
      spec/spec_helper.rb

39
lib/squib/graphics/text.rb

@ -40,15 +40,15 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def wrap(layout, wrap) def wrap(layout, wrap)
unless wrap.nil? layout.wrap = case wrap.to_s
h = { :word => Pango::Layout::WRAP_WORD, when 'word'
:char => Pango::Layout::WRAP_CHAR, Pango::Layout::WRAP_WORD
:word_char => Pango::Layout::WRAP_WORD_CHAR, when 'char'
true => Pango::Layout::WRAP_WORD_CHAR, Pango::Layout::WRAP_CHAR
false => nil, when 'word_char', true
:none => nil Pango::Layout::WRAP_WORD_CHAR
} else
layout.wrap = h[wrap] nil
end end
layout layout
end end
@ -56,12 +56,13 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def align(layout, align) def align(layout, align)
unless align.nil? case align.to_s
h = { :left => Pango::ALIGN_LEFT, when 'left'
:right => Pango::ALIGN_RIGHT, layout.alignment = Pango::ALIGN_LEFT
:center => Pango::ALIGN_CENTER when 'right'
} layout.alignment = Pango::ALIGN_RIGHT
layout.alignment = h[align] when 'center'
layout.alignment = Pango::ALIGN_CENTER
end end
layout layout
end end
@ -71,10 +72,10 @@ module Squib
def valign(cc, layout, x, y, valign) def valign(cc, layout, x, y, valign)
if layout.height > 0 if layout.height > 0
ink_extents = layout.extents[1] ink_extents = layout.extents[1]
case valign case valign.to_s
when :middle when 'middle'
cc.move_to(x, y + (layout.height - ink_extents.height) / (2 * Pango::SCALE)) cc.move_to(x, y + (layout.height - ink_extents.height) / (2 * Pango::SCALE))
when :bottom when 'bottom'
cc.move_to(x, y + (layout.height - ink_extents.height) / Pango::SCALE) cc.move_to(x, y + (layout.height - ink_extents.height) / Pango::SCALE)
end end
end end
@ -113,7 +114,7 @@ module Squib
layout.justify = justify unless justify.nil? layout.justify = justify unless justify.nil?
layout.spacing = spacing * Pango::SCALE unless spacing.nil? layout.spacing = spacing * Pango::SCALE unless spacing.nil?
cc.update_pango_layout(layout) cc.update_pango_layout(layout)
valign(cc, layout, x,y, valign) valign(cc, layout, x, y, valign)
cc.update_pango_layout(layout) ; cc.show_pango_layout(layout) cc.update_pango_layout(layout) ; cc.show_pango_layout(layout)
draw_text_hint(cc,x,y,layout,hint,angle) draw_text_hint(cc,x,y,layout,hint,angle)
end end

98
spec/graphics/graphics_text_spec.rb

@ -0,0 +1,98 @@
require 'spec_helper'
require 'squib'
describe Squib::Card, '#text' do
context 'typical inputs' do
before(:each) do
@deck = double(Squib::Deck)
@context = double(Cairo::Context)
@layout = double(Pango::Layout)
allow(Cairo::Context).to receive(:new).and_return(@context)
end
it 'make all the expected calls on a smoke test' do
mock_squib_logger(@old_logger) do
expect(Squib.logger).to receive(:debug).once
expect(@context).to receive(:save).once
expect(@context).to receive(:set_source_color).once
expect(@context).to receive(:move_to).with(10, 15).once
expect(@context).to receive(:rotate).with(0.0).once
expect(@context).to receive(:create_pango_layout).once.and_return(@layout)
expect(@layout).to receive(:font_description=).with(Pango::FontDescription.new('Sans 12')).once
expect(@layout).to receive(:text=).with('foo').once
expect(@layout).to receive(:width=).with(20 * Pango::SCALE).once
expect(@layout).to receive(:height=).with(25 * Pango::SCALE).once
expect(@layout).to receive(:wrap=).with(nil).once
expect(@layout).to receive(:ellipsize=).with(Pango::Layout::ELLIPSIZE_NONE).once
expect(@layout).to receive(:alignment=).with(Pango::Layout::ALIGN_LEFT).once
expect(@layout).to receive(:justify=).with(false).once
expect(@layout).to receive(:spacing=).with(1.0 * Pango::SCALE).once
expect(@context).to receive(:update_pango_layout).once
expect(@layout).to receive(:height).once.and_return(25)
expect(@layout).to receive(:extents).once.and_return([0,0])
expect(@context).to receive(:update_pango_layout).once
expect(@context).to receive(:show_pango_layout).once
expect(@context).to receive(:restore).once
card = Squib::Card.new(@deck, 100, 150)
# text(str, font, font_size, color,
# x, y, width, height,
# markup, justify, wrap, ellipsize,
# spacing, align, valign, hint, angle)
card.text('foo', 'Sans 12', nil, '#abc',
10, 15, 20, 25,
nil, false, false, false,
1.0, :left, :top, nil, 0.0)
end
end
end
context 'convenience lookups' do
before(:each) do
@deck = double(Squib::Deck)
@context = double(Cairo::Context).as_null_object
@layout = double(Pango::Layout).as_null_object
@extents = double("extents")
allow(Cairo::Context).to receive(:new).and_return(@context)
expect(@context).to receive(:create_pango_layout).once.and_return(@layout)
end
it 'aligns right with strings' do
card = Squib::Card.new(@deck, 100, 150)
expect(@layout).to receive(:alignment=).with(Pango::Layout::ALIGN_RIGHT).once
card.text('foo', 'Sans 12', nil, '#abc',
10, 15, 20, 50,
nil, false, false, false,
1.0, 'right', :top, nil, 0.0)
end
it 'aligns center with strings' do
card = Squib::Card.new(@deck, 100, 150)
expect(@layout).to receive(:alignment=).with(Pango::Layout::ALIGN_CENTER).once
card.text('foo', 'Sans 12', nil, '#abc',
10, 15, 20, 50,
nil, false, false, false,
1.0, 'center', :top, nil, 0.0)
end
it 'sets wrap to char with string char' do
card = Squib::Card.new(@deck, 100, 150)
expect(@layout).to receive(:wrap=).with(Pango::Layout::WRAP_CHAR).once
card.text('foo', 'Sans 12', nil, '#abc',
10, 15, 20, 50,
nil, false, 'char', false,
1.0, :left, :top, nil, 0.0)
end
it 'sets wrap to word_char with symbol word_char' do
card = Squib::Card.new(@deck, 100, 150)
expect(@layout).to receive(:wrap=).with(Pango::Layout::WRAP_WORD_CHAR).once
card.text('foo', 'Sans 12', nil, '#abc',
10, 15, 20, 50,
nil, false, :word_char, false,
1.0, :left, :top, nil, 0.0)
end
end
end

7
spec/input_helpers_spec.rb

@ -7,13 +7,6 @@ class DummyDeck
attr_accessor :layout, :cards, :custom_colors attr_accessor :layout, :cards, :custom_colors
end end
module Squib
def logger=(l)
@logger = l
end
module_function 'logger='
end
describe Squib::InputHelpers do describe Squib::InputHelpers do
before(:each) do before(:each) do

15
spec/spec_helper.rb

@ -1,5 +1,6 @@
require 'simplecov' require 'simplecov'
require 'coveralls' require 'coveralls'
require 'squib'
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter, SimpleCov::Formatter::HTMLFormatter,
@ -7,10 +8,24 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
] ]
SimpleCov.start SimpleCov.start
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
def test_file(str) def test_file(str)
"#{File.expand_path(File.dirname(__FILE__))}/data/#{str}" "#{File.expand_path(File.dirname(__FILE__))}/data/#{str}"
end end
# Refine Squib to allow setting the logger
module Squib
def logger=(l)
@logger = l
end
module_function 'logger='
end
def mock_squib_logger(old_logger) def mock_squib_logger(old_logger)
old_logger = Squib.logger old_logger = Squib.logger
Squib.logger = instance_double(Logger) Squib.logger = instance_double(Logger)

Loading…
Cancel
Save