From 9e49b4053afda3d49b57ba742e16b73c29c38070 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 12 Oct 2021 23:40:08 -0400 Subject: [PATCH] refining and getting tests for placeholder --- lib/squib/args/arg_loader.rb | 9 +++++++++ lib/squib/args/input_file.rb | 4 ++-- samples/images/_placeholders.rb | 25 ++++++++++++++++++++----- spec/args/input_file_spec.rb | 18 +++++++++++++++--- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/squib/args/arg_loader.rb b/lib/squib/args/arg_loader.rb index 0db7514..31ef3f8 100644 --- a/lib/squib/args/arg_loader.rb +++ b/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 diff --git a/lib/squib/args/input_file.rb b/lib/squib/args/input_file.rb index 1941a12..15f778f 100644 --- a/lib/squib/args/input_file.rb +++ b/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 diff --git a/samples/images/_placeholders.rb b/samples/images/_placeholders.rb index 6595481..99c90d7 100644 --- a/samples/images/_placeholders.rb +++ b/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 \ No newline at end of file diff --git a/spec/args/input_file_spec.rb b/spec/args/input_file_spec.rb index c6c17d1..23d64ea 100644 --- a/spec/args/input_file_spec.rb +++ b/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