diff --git a/lib/kramdown-plantuml/options.rb b/lib/kramdown-plantuml/options.rb index 724fb304..bd9ae51c 100644 --- a/lib/kramdown-plantuml/options.rb +++ b/lib/kramdown-plantuml/options.rb @@ -10,7 +10,7 @@ class Options def initialize(options_hash = {}) @logger = LogWrapper.init - @options = massage(options_hash) + @options = massage(options_hash) || {} @raise_errors = extract_raise_errors(@options) extract_theme_options(@options) end @@ -44,9 +44,9 @@ def extract_plantuml_options(options_hash) end def extract_theme_options(options) - return if options.empty? || !options.key?(:theme) + return if options.nil? || options.empty? || !options.key?(:theme) - theme = options[:theme] || {} + theme = options[:theme] unless theme.is_a?(Hash) @logger.warn ":theme is not a Hash: #{theme}" @@ -58,12 +58,10 @@ def extract_theme_options(options) end def extract_raise_errors(options) - if options.key?(:raise_errors) - raise_errors = options[:raise_errors] - return boolean(raise_errors, true) - end + return true if options.nil? || options.empty? || !options.key?(:raise_errors) - true + raise_errors = options[:raise_errors] + boolean(raise_errors, true) end def massage(options_hash) diff --git a/spec/options_spec.rb b/spec/options_spec.rb index 30230b83..cc2d5791 100644 --- a/spec/options_spec.rb +++ b/spec/options_spec.rb @@ -24,22 +24,50 @@ its(:to_h) { is_expected.to eq({ }) } end + context 'nil :plantuml' do + let(:hash) { { plantuml: nil } } + its(:theme_name) { is_expected.to be_nil } + its(:theme_directory) { is_expected.to be_nil } + its(:raise_errors?) { is_expected.to be true } + its(:to_h) { is_expected.to eq({ }) } + end + context 'empty :plantuml' do let(:hash) { { plantuml: { } } } its(:to_h) { is_expected.to eq({ }) } end + context 'nil :theme' do + let(:hash) { { plantuml: { theme: nil } } } + its(:to_h) { is_expected.to eq({ theme: nil }) } + end + context 'empty :theme' do let(:hash) { { plantuml: { theme: { } } } } its(:to_h) { is_expected.to eq({ theme: { } }) } end + context 'with nil :name' do + let(:hash) { { plantuml: { theme: { name: nil } } } } + its(:to_h) { is_expected.to eq({ theme: { name: nil } }) } + end + context 'with :theme :name' do let(:hash) { { plantuml: { theme: { name: 'custom' } } } } its(:to_h) { is_expected.to eq({ theme: { name: 'custom' } }) } end - context 'invalid :raise_errors' do + context 'with nil :directory' do + let(:hash) { { plantuml: { theme: { name: 'custom', directory: nil } } } } + its(:to_h) { is_expected.to eq({ theme: { name: 'custom', directory: nil } }) } + end + + context 'with nil :raise_errors' do + let(:hash) { { plantuml: { theme: { }, raise_errors: nil } } } + its(:raise_errors?) { is_expected.to be true } + end + + context 'with invalid :raise_errors' do let(:hash) { { plantuml: { theme: { }, raise_errors: 'xyz' } } } its(:raise_errors?) { is_expected.to be true } end