Browse Source

refining and getting tests for placeholder

dev
Andy Meneely 4 years ago
parent
commit
9e49b4053a
  1. 9
      lib/squib/args/arg_loader.rb
  2. 4
      lib/squib/args/input_file.rb
  3. 25
      samples/images/_placeholders.rb
  4. 18
      spec/args/input_file_spec.rb

9
lib/squib/args/arg_loader.rb

@ -126,4 +126,13 @@ module Squib::Args::ArgLoader
self
end
# Return the deck's configuration
# This keeps the @deck local to this mixin instead of forcing args classes
# to "know" that @deck works.
#
# It also makes unit testing easier. Sue me.
def deck_conf
@deck.conf
end
end

4
lib/squib/args/input_file.rb

@ -30,9 +30,9 @@ module Squib::Args
def validate_file(arg, i)
return nil if arg.nil?
return File.expand_path(arg) if File.exists?(arg)
return File.expand_path(placeholder[i]) if File.exists?(placeholder[i])
return File.expand_path(placeholder[i]) if File.exists?(placeholder[i].to_s)
case @deck.conf.img_missing.to_sym
case deck_conf.img_missing.to_sym
when :error
raise "File #{File.expand_path(arg)} does not exist!"
when :warn

25
samples/images/_placeholders.rb

@ -16,13 +16,28 @@ Squib::Deck.new(width: 100, height: 100, cards: 3) do
end
# Placeholders can be per-image too.
# What if a placeholder is specified but doesn't exist?
# It'll always warn.
Squib::Deck.new(width: 100, height: 100, cards: 3) do
# What if a placeholder is specified but doesn't exist? It'll always warn.
Squib.configure img_missing: :warn # default
Squib::Deck.new(width: 100, height: 100, cards: 4) do
background color: :white
files = %w(angler-fish.png does-not-exist.png does-not-exist.png)
placeholders = %w(grit.png does-not-exist.png grit.png)
files = %w(angler-fish.png does-not-exist.png does-not-exist.png does-not-exist.png)
placeholders = %w(grit.png does-not-exist.png grit.png )
png file: files, placeholder: placeholders
save_sheet columns: 1, prefix: 'multi_placeholder_sheet_'
end
# Do errors work?
# If you REALLY want the old, pre-Squib v0.18 functionality
# ...you can still have it
# This is really more of a regression test than example.
Squib.configure img_missing: :error
Squib::Deck.new(width: 100, height: 100, cards: 1) do
begin
png file: 'does-not-exist.png' # no placeholder... should error!
raise Exception.new 'Runtime Error should have been thrown!'
rescue RuntimeError => e
# a runtime error should have happened here. So nothing happens. Good.
Squib.logger.error 'Yay! An error we expected was thrown.'
end
end

18
spec/args/input_file_spec.rb

@ -9,12 +9,24 @@ describe Squib::Args::InputFile do
it 'allows a file if it exists' do
args = { file: __FILE__ } # I code therefore I am.
ifile.load!(args, expand_by: 1)
expect(ifile).to have_attributes(file: [File.expand_path(__FILE__)])
expect(ifile.file).to eq([File.expand_path(__FILE__)])
end
it 'raises on non-existent file' do
it 'warns on non-existent file by default' do
args = { file: 'foo.rb' }
expect { ifile.load!(args, expand_by: 1) }.to raise_error("File #{File.expand_path('foo.rb')} does not exist!")
conf = double("conf", conf: Squib::Conf.new)
expect(conf).to receive(:img_missing).and_return(:warn)
expect(ifile).to receive(:deck_conf).and_return(conf)
expect(Squib.logger).to receive(:warn).once
expect(ifile.load!(args, expand_by: 1)).to have_attributes(file: [nil])
end
it 'uses placeholder when file does not exist but placeholder is non-nil and does exist' do
args = { file: 'foo.rb', placeholder: __FILE__ }
expect(Squib.logger).not_to receive(:warn)
expect(ifile.load!(args, expand_by: 1).file).to eq([File.expand_path(__FILE__)])
end
end

Loading…
Cancel
Save