Browse Source

Singleton expansion has... expanded!

Closes #13, hopefully
dev
Andy Meneely 12 years ago
parent
commit
3e08a0d604
  1. 2
      lib/squib/api/background.rb
  2. 5
      lib/squib/api/image.rb
  3. 2
      lib/squib/api/settings.rb
  4. 24
      lib/squib/api/shapes.rb
  5. 9
      lib/squib/api/text.rb
  6. 98
      lib/squib/constants.rb
  7. 51
      lib/squib/graphics/text.rb
  8. 90
      lib/squib/input_helpers.rb
  9. 1
      samples/colors.rb
  10. 1
      samples/custom_config.rb
  11. 16
      spec/api/api_text_spec.rb
  12. 39
      spec/input_helpers_spec.rb

2
lib/squib/api/background.rb

@ -10,7 +10,7 @@ module Squib
# @api public # @api public
def background(opts = {}) def background(opts = {})
opts = needs(opts,[:range, :color]) opts = needs(opts,[:range, :color])
opts[:range].each { |i| @cards[i].background(opts[:color]) } opts[:range].each { |i| @cards[i].background(opts[:color][i]) }
end end
end end

5
lib/squib/api/image.rb

@ -20,7 +20,7 @@ module Squib
opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout]) opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout])
@progress_bar.start("Loading PNGs #{location(opts)}", opts[:range].size) do |bar| @progress_bar.start("Loading PNGs #{location(opts)}", opts[:range].size) do |bar|
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha]) @cards[i].png(opts[:file][i], opts[:x][i], opts[:y][i], opts[:alpha][i])
bar.increment bar.increment
end end
end end
@ -46,7 +46,8 @@ module Squib
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height, :layout]) p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height, :layout])
@progress_bar.start("Loading SVGs #{location(opts)}", p[:range].size) do |bar| @progress_bar.start("Loading SVGs #{location(opts)}", p[:range].size) do |bar|
p[:range].each do |i| p[:range].each do |i|
@cards[i].svg(p[:file][i], p[:id], p[:x], p[:y], p[:width], p[:height]) @cards[i].svg(p[:file][i], p[:id][i], p[:x][i], p[:y][i],
p[:width][i], p[:height][i])
bar.increment bar.increment
end end
end end

2
lib/squib/api/settings.rb

@ -30,7 +30,7 @@ module Squib
# @api public # @api public
def set(opts = {}) def set(opts = {})
opts = needs(opts, [:font]) opts = needs(opts, [:font])
@font = opts[:font] @font = opts[:font][0] #was expanded - just need the first
end end
end end

24
lib/squib/api/shapes.rb

@ -21,12 +21,13 @@ module Squib
# @return [nil] intended to be void # @return [nil] intended to be void
# @api public # @api public
def rect(opts = {}) def rect(opts = {})
opts = needs(opts, [:range, :x, :y, :width, :height, :radius, opts = needs(opts, [:range, :x, :y, :width, :height, :rect_radius, :x_radius, :y_radius,
:fill_color, :stroke_color, :stroke_width, :layout]) :fill_color, :stroke_color, :stroke_width, :layout])
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].rect(opts[:x], opts[:y], opts[:width], opts[:height], @cards[i].rect(opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
opts[:x_radius], opts[:y_radius], opts[:x_radius][i], opts[:y_radius][i],
opts[:fill_color], opts[:stroke_color], opts[:stroke_width]) opts[:fill_color][i], opts[:stroke_color][i],
opts[:stroke_width][i])
end end
end end
@ -49,8 +50,8 @@ module Squib
opts = needs(opts, [:range, :x, :y, :circle_radius, :layout, opts = needs(opts, [:range, :x, :y, :circle_radius, :layout,
:fill_color, :stroke_color, :stroke_width]) :fill_color, :stroke_color, :stroke_width])
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].circle(opts[:x], opts[:y], opts[:radius], @cards[i].circle(opts[:x][i], opts[:y][i], opts[:radius][i],
opts[:fill_color], opts[:stroke_color], opts[:stroke_width]) opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i])
end end
end end
@ -75,8 +76,11 @@ module Squib
opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :x3, :y3, :layout, opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :x3, :y3, :layout,
:fill_color, :stroke_color, :stroke_width]) :fill_color, :stroke_color, :stroke_width])
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].triangle(opts[:x1], opts[:y1], opts[:x2], opts[:y2],opts[:x3], opts[:y3], @cards[i].triangle(opts[:x1][i], opts[:y1][i],
opts[:fill_color], opts[:stroke_color], opts[:stroke_width]) opts[:x2][i], opts[:y2][i],
opts[:x3][i], opts[:y3][i],
opts[:fill_color][i], opts[:stroke_color][i],
opts[:stroke_width][i])
end end
end end
@ -98,8 +102,8 @@ module Squib
opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :layout, opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :layout,
:stroke_color, :stroke_width]) :stroke_color, :stroke_width])
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].line(opts[:x1], opts[:y1], opts[:x2], opts[:y2], @cards[i].line(opts[:x1][i], opts[:y1][i], opts[:x2][i], opts[:y2][i],
opts[:stroke_color], opts[:stroke_width]) opts[:stroke_color][i], opts[:stroke_width][i])
end end
end end

9
lib/squib/api/text.rb

@ -35,10 +35,13 @@ module Squib
# @api public # @api public
def text(opts = {}) def text(opts = {})
opts = needs(opts, [:range, :str, :font, :x, :y, :width, :height, :color, :wrap, opts = needs(opts, [:range, :str, :font, :x, :y, :width, :height, :color, :wrap,
:align, :justify, :valign, :ellipsize, :hint, :layout]) :align, :justify, :valign, :markup, :ellipsize, :hint, :layout])
opts[:str] = [opts[:str]] * @cards.size unless opts[:str].respond_to? :each
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].text(opts[:str][i], opts[:font], opts[:x], opts[:y], opts[:color], opts) @cards[i].text(opts[:str][i], opts[:font][i], opts[:color][i],
opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
opts[:markup][i], opts[:justify][i], opts[:wrap][i],
opts[:ellipsize][i], opts[:spacing][i], opts[:align][i],
opts[:valign][i], opts[:hint][i])
end end
end end

98
lib/squib/constants.rb

@ -3,51 +3,95 @@ module Squib
# #
# @api public # @api public
SYSTEM_DEFAULTS = { SYSTEM_DEFAULTS = {
:range => :all, :align => :left,
:alpha => 1.0,
:color => :black, :color => :black,
:str => '', :default_font => 'Arial 36',
:dir => "_output",
:ellipsize => :end,
:fill_color => '#0000', :fill_color => '#0000',
:stroke_color => :black,
:stroke_width => 2.0,
:font => :use_set, :font => :use_set,
:default_font => 'Arial 36', :format => :png,
:gap => 0,
:height => :native,
:justify => false,
:margin => 75,
:markup => false,
:prefix => "card_",
:progress_bar => false,
:range => :all,
:sheet => 0, :sheet => 0,
:spacing => 0,
:str => '',
:stroke_color => :black,
:stroke_width => 2.0,
:trim => 0,
:valign => :top,
:width => :native,
:wrap => true,
:x => 0, :x => 0,
:y => 0,
:x1 => 100, :x1 => 100,
:y1 => 100,
:x2 => 150, :x2 => 150,
:y2 => 150,
:x3 => 100, :x3 => 100,
:y3 => 150,
:x_radius => 0, :x_radius => 0,
:y => 0,
:y1 => 100,
:y2 => 150,
:y3 => 150,
:y_radius => 0, :y_radius => 0,
:align => :left,
:valign => :top,
:justify => false,
:ellipsize => :end,
:wrap => true,
:width => :native,
:height => :native,
:spacing => 0,
:alpha => 1.0,
:format => :png,
:dir => "_output",
:prefix => "card_",
:margin => 75,
:gap => 0,
:trim => 0,
:progress_bar => false
} }
# Squib's configuration defaults # Squib's configuration defaults
# #
# @api public # @api public
CONFIG_DEFAULTS = { CONFIG_DEFAULTS = {
'custom_colors' => {},
'dpi' => 300, 'dpi' => 300,
'progress_bar' => false,
'hint' => nil, 'hint' => nil,
'custom_colors' => {} 'progress_bar' => false,
} }
# These are parameters that are intended to be "expanded" across
# range if they are singletons.
#
# For example, using a different font for each card, using one `text`
#
# key: the internal name of the param (e.g. :files)
# value: the user-facing API key (e.g. file: 'abc.png')
#
# @api private
EXPANDING_PARAMS = {
:align => :align,
:alpha => :alpha,
:circle_radius => :radius,
:color => :color,
:ellipsize => :ellipsize,
:files => :file,
:fill_color => :fill_color,
:font => :font,
:height => :height,
:hint => :hint,
:justify => :justify,
:layout => :layout,
:markup => :markup,
:rect_radius => :radius,
:spacing => :spacing,
:str => :str,
:stroke_color => :stroke_color,
:stroke_width => :stroke_width,
:svgid => :id,
:valign => :valign,
:width => :width,
:wrap => :wrap,
:x => :x,
:x1 => :x1,
:x2 => :x2,
:x3 => :x3,
:x_radius => :x_radius,
:y => :y,
:y1 => :y1,
:y2 => :y2,
:y2 => :y3,
:y_radius => :y_radius,
}
end end

51
lib/squib/graphics/text.rb

@ -8,7 +8,7 @@ module Squib
def draw_text_hint(x,y,layout, color) def draw_text_hint(x,y,layout, color)
return if color.nil? && @deck.text_hint.nil? return if color.nil? && @deck.text_hint.nil?
color ||= @deck.text_hint color ||= @deck.text_hint
# when w,h < 0, it was never set. extents[0] are ink extents # when w,h < 0, it was never set. extents[1] are ink extents
w = layout.width / Pango::SCALE w = layout.width / Pango::SCALE
w = layout.extents[1].width / Pango::SCALE if w < 0 w = layout.extents[1].width / Pango::SCALE if w < 0
h = layout.height / Pango::SCALE h = layout.height / Pango::SCALE
@ -18,8 +18,8 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def ellipsize(layout, options) def ellipsize(layout, ellipsize)
unless options[:ellipsize].nil? unless ellipsize.nil?
h = { :none => Pango::Layout::ELLIPSIZE_NONE, h = { :none => Pango::Layout::ELLIPSIZE_NONE,
:start => Pango::Layout::ELLIPSIZE_START, :start => Pango::Layout::ELLIPSIZE_START,
:middle => Pango::Layout::ELLIPSIZE_MIDDLE, :middle => Pango::Layout::ELLIPSIZE_MIDDLE,
@ -27,15 +27,15 @@ module Squib
true => Pango::Layout::ELLIPSIZE_END, true => Pango::Layout::ELLIPSIZE_END,
false => Pango::Layout::ELLIPSIZE_NONE false => Pango::Layout::ELLIPSIZE_NONE
} }
layout.ellipsize = h[options[:ellipsize]] layout.ellipsize = h[ellipsize]
end end
layout layout
end end
# :nodoc: # :nodoc:
# @api private # @api private
def wrap(layout, options) def wrap(layout, wrap)
unless options[:wrap].nil? unless wrap.nil?
h = { :word => Pango::Layout::WRAP_WORD, h = { :word => Pango::Layout::WRAP_WORD,
:char => Pango::Layout::WRAP_CHAR, :char => Pango::Layout::WRAP_CHAR,
:word_char => Pango::Layout::WRAP_WORD_CHAR, :word_char => Pango::Layout::WRAP_WORD_CHAR,
@ -43,20 +43,20 @@ module Squib
false => nil, false => nil,
:none => nil :none => nil
} }
layout.wrap = h[options[:wrap]] layout.wrap = h[wrap]
end end
layout layout
end end
# :nodoc: # :nodoc:
# @api private # @api private
def align(layout, options) def align(layout, align)
unless options[:align].nil? unless align.nil?
h = { :left => Pango::ALIGN_LEFT, h = { :left => Pango::ALIGN_LEFT,
:right => Pango::ALIGN_RIGHT, :right => Pango::ALIGN_RIGHT,
:center => Pango::ALIGN_CENTER :center => Pango::ALIGN_CENTER
} }
layout.alignment = h[options[:align]] layout.alignment = h[align]
end end
layout layout
end end
@ -77,33 +77,36 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def setwh(layout, options) def setwh(layout, width, height)
layout.width = options[:width] * Pango::SCALE unless options[:width].nil? || options[:width] == :native layout.width = width * Pango::SCALE unless width.nil? || width == :native
layout.height = options[:height] * Pango::SCALE unless options[:height].nil? || options[:height] == :native layout.height = height * Pango::SCALE unless height.nil? || height == :native
layout layout
end end
# :nodoc: # :nodoc:
# @api private # @api private
def text(str, font, x, y, color, options) def text(str, font, color,
Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, and options: #{options}"} x, y, width, height,
markup, justify, wrap, ellipsize,
spacing, align, valign, hint)
Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, etc. (TODO FILL THIS IN WITH METAPROGRAMMING)"}
cc = cairo_context cc = cairo_context
cc.set_source_color(color) cc.set_source_color(color)
cc.move_to(x,y) cc.move_to(x,y)
layout = cc.create_pango_layout layout = cc.create_pango_layout
layout.font_description = Pango::FontDescription.new(font) layout.font_description = Pango::FontDescription.new(font)
layout.text = str.to_s layout.text = str.to_s
layout.markup = str.to_s if options[:markup] layout.markup = str.to_s if markup
layout = setwh(layout, options) layout = setwh(layout, width, height)
layout = wrap(layout, options) layout = wrap(layout, wrap)
layout = ellipsize(layout, options) layout = ellipsize(layout, ellipsize)
layout = align(layout, options) layout = align(layout, align)
layout.justify = options[:justify] unless options[:justify].nil? layout.justify = justify unless justify.nil?
layout.spacing = options[:spacing] * Pango::SCALE unless options[: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, options[: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(x,y,layout,options[:hint]) draw_text_hint(x,y,layout,hint)
end end
end end

90
lib/squib/input_helpers.rb

@ -1,4 +1,5 @@
require 'squib/constants' require 'squib/constants'
require 'pp'
module Squib module Squib
# :nodoc: # :nodoc:
@ -8,37 +9,59 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def needs(opts, params) def needs(opts, params)
Squib.logger.debug {"Pre input-helper opts: #{opts}"}
opts = layoutify(opts) if params.include? :layout opts = layoutify(opts) if params.include? :layout
opts = Squib::SYSTEM_DEFAULTS.merge(opts) opts = Squib::SYSTEM_DEFAULTS.merge(opts)
opts = expand_singletons(opts, params)
Squib.logger.debug {"Post expand opts: #{opts}"}
opts = rangeify(opts) if params.include? :range opts = rangeify(opts) if params.include? :range
opts = fileify(opts) if params.include? :file opts = fileify(opts) if params.include? :file
opts = fileify(opts, false, false) if params.include? :file_to_save opts = fileify(opts, false) if params.include? :file_to_save
opts = fileify(opts, true, false) if params.include? :files opts = colorify(opts, true) if params.include? :nillable_color
opts = dirify(opts, true) if params.include? :creatable_dir
opts = fileify(opts, false) if params.include? :files
opts = colorify(opts) if params.include? :color opts = colorify(opts) if params.include? :color
opts = colorify(opts, false, :fill_color) if params.include? :fill_color opts = colorify(opts, false, :fill_color) if params.include? :fill_color
opts = colorify(opts, false, :stroke_color) if params.include? :stroke_color opts = colorify(opts, false, :stroke_color) if params.include? :stroke_color
opts = colorify(opts, true) if params.include? :nillable_color
opts = dirify(opts) if params.include? :dir
opts = dirify(opts, true) if params.include? :creatable_dir
opts = fontify(opts) if params.include? :font opts = fontify(opts) if params.include? :font
opts = radiusify(opts) if params.include? :radius opts = radiusify(opts) if params.include? :rect_radius
opts = svgidify(opts) if params.include? :svgid opts = svgidify(opts) if params.include? :svgid
opts = formatify(opts) if params.include? :formats opts = formatify(opts) if params.include? :formats
opts opts
end end
module_function :needs module_function :needs
# :nodoc:
# @api private
def expand_singletons(opts, needed_params)
Squib::EXPANDING_PARAMS.each_pair do |param_name, api_param|
if needed_params.include? param_name
unless opts[api_param].respond_to?(:each)
opts[api_param] = [opts[api_param]] * @cards.size
end
end
end
opts
end
module_function :expand_singletons
# Layouts have to come before, so we repeat expand_singletons here
# :nodoc: # :nodoc:
# @api private # @api private
def layoutify(opts) def layoutify(opts)
unless opts[:layout].nil? unless opts[:layout].respond_to?(:each)
entry = @layout[opts[:layout].to_s] opts[:layout] = [opts[:layout]] * @cards.size
unless entry.nil? end
entry.each do |key, value| opts[:layout].each_with_index do |layout, i|
opts[key.to_sym] ||= entry[key] unless layout.nil?
entry = @layout[layout.to_s]
unless entry.nil?
entry.each do |key, value|
opts[key.to_sym] ||= entry[key]
end
else
Squib.logger.warn ("Layout entry '#{layout}' does not exist." )
end end
else
Squib.logger.warn "Layout entry '#{opts[:layout]}' does not exist."
end end
end end
opts opts
@ -70,10 +93,8 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def fileify(opts, expand_singletons=false, file_must_exist=true) def fileify(opts, file_must_exist=true)
opts[:file] = [opts[:file]] * @cards.size if expand_singletons && !(opts[:file].respond_to? :each) [opts[:file]].flatten.each do |file|
files = [opts[:file]].flatten
files.each do |file|
if file_must_exist and !File.exists?(file) if file_must_exist and !File.exists?(file)
raise "File #{File.expand_path(file)} does not exist!" raise "File #{File.expand_path(file)} does not exist!"
end end
@ -87,7 +108,7 @@ module Squib
def dirify(opts, allow_create=false) def dirify(opts, allow_create=false)
return opts if Dir.exists?(opts[:dir]) return opts if Dir.exists?(opts[:dir])
if allow_create if allow_create
Squib.logger.warn "Dir #{opts[:dir]} does not exist, creating it." Squib.logger.warn {"Dir #{opts[:dir]} does not exist, creating it."}
Dir.mkdir opts[:dir] Dir.mkdir opts[:dir]
return opts return opts
else else
@ -99,11 +120,16 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def colorify(opts, nillable=false, key=:color) def colorify(opts, nillable=false, key=:color)
return opts if nillable && opts[key].nil? opts[key].each_with_index do |color, i|
if @custom_colors.key? opts[key].to_s unless nillable && color.nil?
opts[key] = @custom_colors[opts[key].to_s] if @custom_colors.key? color.to_s
color = @custom_colors[color.to_s]
end
opts[key][i] = Cairo::Color.parse(color)
end
end end
opts[key] = Cairo::Color.parse(opts[key]) # pp "===Colorified opts==="
# pp opts
opts opts
end end
module_function :colorify module_function :colorify
@ -111,8 +137,10 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def fontify (opts) def fontify (opts)
opts[:font] = @font if opts[:font]==:use_set opts[:font].each_with_index do |font, i|
opts[:font] = Squib::SYSTEM_DEFAULTS[:default_font] if opts[:font] == :default opts[:font][i] = @font if font==:use_set
opts[:font][i] = Squib::SYSTEM_DEFAULTS[:default_font] if font == :default
end
opts opts
end end
module_function :fontify module_function :fontify
@ -120,9 +148,11 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def radiusify(opts) def radiusify(opts)
unless opts[:radius].nil? opts[:radius].each_with_index do |radius, i|
opts[:x_radius] = opts[:radius] unless radius.nil?
opts[:y_radius] = opts[:radius] opts[:x_radius][i] = radius
opts[:y_radius][i] = radius
end
end end
opts opts
end end
@ -131,8 +161,10 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def svgidify(opts) def svgidify(opts)
unless opts[:id].nil? opts[:id].each_with_index do |id, i|
opts[:id] = '#' << opts[:id] unless opts[:id].start_with? '#' unless id.nil?
opts[:id][i] = '#' << id unless id.start_with? '#'
end
end end
opts opts
end end

1
samples/colors.rb

@ -11,7 +11,6 @@ Squib::Deck.new(width: 825, height: 1125, cards: 1) do
text color: '#ffff00000000', str: '12-hex', x: 50, y: y+=50 text color: '#ffff00000000', str: '12-hex', x: 50, y: y+=50
text color: '#ffff000000009999', str: '12-hex (alpha)', x: 50, y: y+=50 text color: '#ffff000000009999', str: '12-hex (alpha)', x: 50, y: y+=50
text color: :burnt_orange, str: 'Symbols of constants too', x: 50, y: y+=50 text color: :burnt_orange, str: 'Symbols of constants too', x: 50, y: y+=50
text color: [1.0, 0.0, 0.0], str: 'Array of percentages', x: 50, y: y+=50
save_png prefix: "colors_" save_png prefix: "colors_"
end end

1
samples/custom_config.rb

@ -2,7 +2,6 @@
require 'squib' require 'squib'
Squib::Deck.new(config: 'custom-config.yml') do Squib::Deck.new(config: 'custom-config.yml') do
# Custom color defined in our config # Custom color defined in our config
background color: :foo background color: :foo

16
spec/api/api_text_spec.rb

@ -3,10 +3,12 @@ require 'squib'
describe Squib::Deck, '#text' do describe Squib::Deck, '#text' do
context "when working with fonts" do context "fonts" do
it"should use the default font when #text and #set_font don't specify" do it "should use the default font when #text and #set_font don't specify" do
card = instance_double(Squib::Card) card = instance_double(Squib::Card)
expect(card).to receive(:text).with('a', 'Arial 36', anything, anything, anything, anything).once expect(card).to receive(:text).with('a', 'Arial 36',
anything, anything, anything, anything,anything, anything, anything, anything,anything, anything, anything, anything, anything
).once
Squib::Deck.new do Squib::Deck.new do
@cards = [card] @cards = [card]
text str: 'a' text str: 'a'
@ -15,7 +17,9 @@ describe Squib::Deck, '#text' do
it "should use the #set_font when #text doesn't specify" do it "should use the #set_font when #text doesn't specify" do
card = instance_double(Squib::Card) card = instance_double(Squib::Card)
expect(card).to receive(:text).with('a', 'Times New Roman 16', anything, anything, anything, anything).once expect(card).to receive(:text).with('a', 'Times New Roman 16',
anything, anything, anything, anything,anything, anything, anything, anything,anything, anything, anything, anything, anything
).once
Squib::Deck.new do Squib::Deck.new do
@cards = [card] @cards = [card]
set font: 'Times New Roman 16' set font: 'Times New Roman 16'
@ -25,7 +29,9 @@ describe Squib::Deck, '#text' do
it "should use the specified font no matter what" do it "should use the specified font no matter what" do
card = instance_double(Squib::Card) card = instance_double(Squib::Card)
expect(card).to receive(:text).with('a', 'Arial 18', anything, anything, anything, anything).once expect(card).to receive(:text).with('a', 'Arial 18',
anything, anything, anything, anything,anything, anything, anything, anything,anything, anything, anything, anything, anything
).once
Squib::Deck.new do Squib::Deck.new do
@cards = [card] @cards = [card]
set font: 'Times New Roman 16' set font: 'Times New Roman 16'

39
spec/input_helpers_spec.rb

@ -27,27 +27,31 @@ describe Squib::InputHelpers do
it "should warn on the logger when the layout doesn't exist" do it "should warn on the logger when the layout doesn't exist" do
@old_logger = Squib.logger @old_logger = Squib.logger
Squib.logger = instance_double(Logger) Squib.logger = instance_double(Logger)
expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.") expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.").twice
expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: :foo}) expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: [:foo,:foo]})
Squib.logger = @old_logger Squib.logger = @old_logger
end end
it "should apply the layout in a normal situation" do it "should apply the layout in a normal situation" do
expect(@deck.send(:layoutify, {layout: :blah})).to eq({layout: :blah, x: 25}) expect(@deck.send(:layoutify, {layout: :blah})).to \
eq({layout: [:blah, :blah], x: 25})
end end
it "also look up based on strings" do it "also look up based on strings" do
expect(@deck.send(:layoutify, {layout: 'blah'})).to eq({layout: 'blah', x: 25}) expect(@deck.send(:layoutify, {layout: 'blah'})).to \
eq({layout: ['blah','blah'], x: 25})
end end
end end
context '#rangeify' do context '#rangeify' do
it "must be within the card size range" do it "must be within the card size range" do
expect{@deck.send(:rangeify, {range: 2..3})}.to raise_error(ArgumentError, '2..3 is outside of deck range of 0..1') expect{@deck.send(:rangeify, {range: 2..3})}.to \
raise_error(ArgumentError, '2..3 is outside of deck range of 0..1')
end end
it "cannot be nil" do it "cannot be nil" do
expect{@deck.send(:rangeify, {range: nil})}.to raise_error(RuntimeError, 'Range cannot be nil') expect{@deck.send(:rangeify, {range: nil})}.to \
raise_error(RuntimeError, 'Range cannot be nil')
end end
it "defaults to a range of all cards if :all" do it "defaults to a range of all cards if :all" do
@ -57,38 +61,39 @@ describe Squib::InputHelpers do
context "#fileify" do context "#fileify" do
it "should throw an error if the file doesn't exist" do it "should throw an error if the file doesn't exist" do
expect{@deck.send(:fileify, {file: 'nonexist.txt'}, false, true)}.to raise_error(RuntimeError,"File #{File.expand_path('nonexist.txt')} does not exist!") expect{@deck.send(:fileify, {file: 'nonexist.txt'}, true)}.to \
end raise_error(RuntimeError,"File #{File.expand_path('nonexist.txt')} does not exist!")
it "should expand singletons when asked" do
expect(@deck.send(:fileify, {file: 'foo.txt'}, true, false)).to eq({file: ['foo.txt', 'foo.txt']})
end end
end end
context "#dir" do context "#dir" do
it "should raise an error if the directory does not exist" do it "should raise an error if the directory does not exist" do
expect{@deck.send(:dirify, {dir: 'nonexist'}, false)}.to raise_error(RuntimeError,"'nonexist' does not exist!") expect{@deck.send(:dirify, {dir: 'nonexist'}, false)}.to \
raise_error(RuntimeError,"'nonexist' does not exist!")
end end
end end
context "#colorify" do context "#colorify" do
it "should parse if nillable" do it "should parse if nillable" do
color = @deck.send(:colorify, {color: '#fff'}, true)[:color] color = @deck.send(:colorify, {color: ['#fff']}, true)[:color]
expect(color.to_a).to eq([1.0, 1.0, 1.0, 1.0]) expect(color.to_a[0].to_a).to eq([1.0, 1.0, 1.0, 1.0])
end end
it "raises and error if the color doesn't exist" do it "raises and error if the color doesn't exist" do
expect{ @deck.send(:colorify, {color: :nonexist}, false) }.to raise_error(ArgumentError, "unknown color name: nonexist") expect{ @deck.send(:colorify, {color: [:nonexist]}, false) }.to \
raise_error(ArgumentError, "unknown color name: nonexist")
end end
it "pulls from config's custom colors" do it "pulls from config's custom colors" do
@deck.custom_colors['foo'] = "#abc" @deck.custom_colors['foo'] = "#abc"
expect(@deck.send(:colorify, {color: :foo}, false)[:color].to_s).to eq('#AABBCCFF') expect(@deck.send(:colorify, {color: [:foo]}, false)[:color][0].to_s).to \
eq('#AABBCCFF')
end end
it "pulls from config's custom colors even when a string" do it "pulls from config's custom colors even when a string" do
@deck.custom_colors['foo'] = "#abc" @deck.custom_colors['foo'] = "#abc"
expect(@deck.send(:colorify, {color: 'foo'}, false)[:color].to_s).to eq('#AABBCCFF') expect(@deck.send(:colorify, {color: ['foo']}, false)[:color][0].to_s).to \
eq('#AABBCCFF')
end end
end end

Loading…
Cancel
Save