From 7aed0f2d4d980c6affeb32cf86e3e7243be305a9 Mon Sep 17 00:00:00 2001 From: Dan Abel Date: Tue, 15 Oct 2013 16:35:06 +0100 Subject: [PATCH] [vcloud_director] tests for #ensure_list! --- lib/fog/vcloud_director/compute.rb | 34 ++++++------- tests/vcloud_director/ensure_list_tests.rb | 56 ++++++++++++++++++++++ 2 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 tests/vcloud_director/ensure_list_tests.rb diff --git a/lib/fog/vcloud_director/compute.rb b/lib/fog/vcloud_director/compute.rb index e38fc7a0d..f168a5745 100644 --- a/lib/fog/vcloud_director/compute.rb +++ b/lib/fog/vcloud_director/compute.rb @@ -414,6 +414,24 @@ module Fog data[:id] = data[:href].split('/').last end + # Compensate for Fog::ToHashDocument shortcomings. + # @api private + # @param [Hash] hash + # @param [String,Symbol] key1 + # @param [String,Symbol] key2 + # @return [Hash] + def ensure_list!(hash, key1, key2=nil) + if key2.nil? + hash[key1] ||= [] + hash[key1] = [hash[key1]] if hash[key1].is_a?(Hash) + else + hash[key1] ||= {key2 => []} + hash[key1] = {key2 => []} if hash[key1].empty? + hash[key1][key2] = [hash[key1][key2]] if hash[key1][key2].is_a?(Hash) + end + hash + end + private def login @@ -433,22 +451,6 @@ module Fog @org_name = nil end - # Compensate for Fog::ToHashDocument shortcomings. - # @param [Hash] hash - # @param [String,Symbol] key1 - # @param [String,Symbol] key2 - # @return [Hash] - def ensure_list!(hash, key1, key2=nil) - if key2.nil? - hash[key1] ||= [] - hash[key1] = [hash[key1]] if hash[key1].is_a?(Hash) - else - hash[key1] ||= {key2 => []} - hash[key1] = {key2 => []} if hash[key1].empty? - hash[key1][key2] = [hash[key1][key2]] if hash[key1][key2].is_a?(Hash) - end - hash - end end class Mock diff --git a/tests/vcloud_director/ensure_list_tests.rb b/tests/vcloud_director/ensure_list_tests.rb new file mode 100644 index 000000000..1d27f7c68 --- /dev/null +++ b/tests/vcloud_director/ensure_list_tests.rb @@ -0,0 +1,56 @@ +Shindo.tests('Compute::VcloudDirector | ensure_list!', ['vclouddirector']) do + + # ensure list is not available in mocking mode + unless Fog.mocking? + + @service = Fog::Compute::VcloudDirector.new + + tests('#ensure_list! for single key') do + tests('for key with a hash').returns(Array) do + testdata = {:k => {:A => '1'}} + @service.ensure_list!(testdata, :k) + testdata[:k].class + end + + tests('for key with empty array').returns(Array) do + testdata = {:k => []} + @service.ensure_list!(testdata, :k) + testdata[:k].class + end + + tests('for key with nothing').returns(Array) do + testdata = {} + @service.ensure_list!(testdata, :k) + testdata[:k].class + end + + tests('for key with non-empty array').returns(Array) do + testdata = {:k => ['one', 'two']} + @service.ensure_list!(testdata, :k) + testdata[:k].class + end + end + + tests ('#ensure_list! for nested keys') do + tests('with no key').returns(Array) do + testdata = {} + @service.ensure_list!(testdata, :keys, :key) + testdata[:keys][:key].class + end + + tests('with empty string').returns(Array) do + testdata = {:keys => ''} + @service.ensure_list!(testdata, :keys, :key) + testdata[:keys][:key].class + end + + tests('with nested hashes').returns(Array) do + testdata = {:keys => {:key => {:a => '1'}}} + @service.ensure_list!(testdata, :keys, :key) + testdata[:keys][:key].class + end + end + + end + +end