From a3a8a1fa4ba5723c0af1d4bf7873a10edabeb6fc Mon Sep 17 00:00:00 2001 From: Joe Fredette Date: Thu, 30 Oct 2014 11:45:42 -0400 Subject: [PATCH] fix nil-reference issue in QuotedString module quote(nil) now does not through an error --- lib/webmachine/quoted_string.rb | 4 +- spec/webmachine/quoted_string_spec.rb | 59 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 spec/webmachine/quoted_string_spec.rb diff --git a/lib/webmachine/quoted_string.rb b/lib/webmachine/quoted_string.rb index 7c09a91c..aa967229 100644 --- a/lib/webmachine/quoted_string.rb +++ b/lib/webmachine/quoted_string.rb @@ -28,12 +28,12 @@ def quote(str) # Escapes quotes within a quoted string. def escape_quotes(str) - str.gsub(/"/, '\\"') + String(str).gsub(/"/, '\\"') end # Unescapes quotes within a quoted string def unescape_quotes(str) - str.gsub(%r{\\}, '') + String(str).gsub(%r{\\}, '') end end end diff --git a/spec/webmachine/quoted_string_spec.rb b/spec/webmachine/quoted_string_spec.rb new file mode 100644 index 00000000..c5e4efd8 --- /dev/null +++ b/spec/webmachine/quoted_string_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe Webmachine::QuotedString do + include Webmachine::QuotedString + + describe '#quote' do + context 'when the string is present and nonempty, has no quotes' do + subject { quote('123') } + + it { is_expected.to eq('"123"') } + end + + context 'when the string is present and nonempty, has embedded quotes' do + let(:string) { '1"2"3' } + subject { quote('1"2"3') } + + it { is_expected.to eq('"1\"2\"3"') } + end + + context 'when the string is present and empty' do + subject { quote('') } + + it { is_expected.to eq("\"\"") } + end + + context 'when the string is nil' do + subject { quote(nil) } + + it { is_expected.to eq("\"\"") } + end + end + + describe '#unquote' do + context 'when the string is present and nonempty, has no quotes' do + subject { unquote('123') } + + it { is_expected.to eq('123') } + end + + context 'when the string is present and nonempty, has embedded quotes' do + let(:string) { '"1\"2\"3"' } + subject { unquote(string) } + + it { is_expected.to eq('1"2"3') } + end + + context 'when the string is present and empty' do + subject { unquote('') } + + it { is_expected.to eq('') } + end + + context 'when the string is nil' do + subject { unquote(nil) } + + it { is_expected.to eq(nil) } + end + end +end