refining and getting tests for placeholder
parent
b4d6b2aa0a
commit
9e49b4053a
|
|
@ -126,4 +126,13 @@ module Squib::Args::ArgLoader
|
||||||
self
|
self
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,9 @@ module Squib::Args
|
||||||
def validate_file(arg, i)
|
def validate_file(arg, i)
|
||||||
return nil if arg.nil?
|
return nil if arg.nil?
|
||||||
return File.expand_path(arg) if File.exists?(arg)
|
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
|
when :error
|
||||||
raise "File #{File.expand_path(arg)} does not exist!"
|
raise "File #{File.expand_path(arg)} does not exist!"
|
||||||
when :warn
|
when :warn
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,28 @@ Squib::Deck.new(width: 100, height: 100, cards: 3) do
|
||||||
end
|
end
|
||||||
|
|
||||||
# Placeholders can be per-image too.
|
# Placeholders can be per-image too.
|
||||||
# What if a placeholder is specified but doesn't exist?
|
# What if a placeholder is specified but doesn't exist? It'll always warn.
|
||||||
# It'll always warn.
|
Squib.configure img_missing: :warn # default
|
||||||
Squib::Deck.new(width: 100, height: 100, cards: 3) do
|
Squib::Deck.new(width: 100, height: 100, cards: 4) do
|
||||||
background color: :white
|
background color: :white
|
||||||
|
|
||||||
files = %w(angler-fish.png does-not-exist.png does-not-exist.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 )
|
placeholders = %w(grit.png does-not-exist.png grit.png )
|
||||||
png file: files, placeholder: placeholders
|
png file: files, placeholder: placeholders
|
||||||
save_sheet columns: 1, prefix: 'multi_placeholder_sheet_'
|
save_sheet columns: 1, prefix: 'multi_placeholder_sheet_'
|
||||||
end
|
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
|
||||||
|
|
@ -9,12 +9,24 @@ describe Squib::Args::InputFile do
|
||||||
it 'allows a file if it exists' do
|
it 'allows a file if it exists' do
|
||||||
args = { file: __FILE__ } # I code therefore I am.
|
args = { file: __FILE__ } # I code therefore I am.
|
||||||
ifile.load!(args, expand_by: 1)
|
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
|
end
|
||||||
|
|
||||||
it 'raises on non-existent file' do
|
it 'warns on non-existent file by default' do
|
||||||
args = { file: 'foo.rb' }
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue