Skip to content

Commit

Permalink
Handle strings in pretty_hash to prevent exceptions (errbit#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeterburs authored Aug 17, 2023
1 parent cc0249a commit 7bd1803
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/helpers/hash_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module HashHelper
def pretty_hash(hash, nesting = 0)
return '{}' if hash.empty?
return '{}' if hash.empty? || hash.is_a?(String)

tab_size = 2
nesting += 1
Expand Down
30 changes: 30 additions & 0 deletions spec/helpers/hash_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe HashHelper do
describe '.pretty_hash' do
let(:expected_pretty_hash) do
<<~HASH.chomp
{
"action" => "some_action",
"controller" => "some_controller"
}
HASH
end

it 'is expected to prettify hashes' do
expect(pretty_hash('controller' => 'some_controller', 'action' => 'some_action')).to eq expected_pretty_hash
end

it 'is expected to handle empty hashes' do
expect(pretty_hash({})).to eq '{}'
end

context 'with string, instead of hash' do
it 'is expected not to raise exception' do
expect { pretty_hash('This is a string.') }.not_to raise_exception
end

it 'is expected to handle strings' do
expect(pretty_hash('{ a_string: "But it looks like a hash"}')).to eq '{}'
end
end
end
end

0 comments on commit 7bd1803

Please sign in to comment.