Skip to content

Commit

Permalink
Merge pull request #76 from SwedbankPay/feature/dx-914_fix_nil_key
Browse files Browse the repository at this point in the history
DX-914: Fix nil key
  • Loading branch information
asbjornu authored Nov 4, 2021
2 parents 26cdcb6 + e9441c3 commit db286ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 6 additions & 8 deletions lib/kramdown-plantuml/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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)
Expand Down
30 changes: 29 additions & 1 deletion spec/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db286ae

Please sign in to comment.