-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When attempting to access array values via the
json
resource:
``` describe json('/tmp/test.json') do its(['array',0]) { should eq "zero" } end ``` ... the resulting data would be an array of the size of the original array with all the values replaced with nils: ``` expected: "zero" got: [nil, nil, nil] ``` This was due to a bug in the ObjectTraverser mixin that mapped array values back through `extract_value` rather than properly handling the passed-in key(s). This worked fine for the specific data format created by the `csv` resource but did not work `json` or any other resource that subclassed the `JsonConfig` resource. This change fixes the logic when dealing with an array when it's encountered, and fixes up the `csv` resource with its own `value` method. This change also adds tests for ObjectTraverser. Signed-off-by: Adam Leff <[email protected]>
- Loading branch information
Showing
5 changed files
with
110 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# encoding: utf-8 | ||
# author: Adam Leff | ||
|
||
require 'helper' | ||
|
||
class Tester | ||
include ObjectTraverser | ||
end | ||
|
||
describe ObjectTraverser do | ||
let(:subject) { Tester.new } | ||
let(:sample_data) do | ||
{ | ||
'string1' => 'value1', | ||
'string2' => 'value2', | ||
'number1' => 2468, | ||
'hash1' => { 'key1' => 'value1' }, | ||
'hash2' => { | ||
'hash1string1' => 'value3', | ||
'hash1number1' => 123, | ||
'hash1subhash' => { 'key1' => 1, 'key2' => 2 }, | ||
}, | ||
'array1' => %w(word1 word2 word3), | ||
'array2' => [ | ||
123, | ||
456, | ||
{ 'array1hashkey1' => 1, 'array1hashkey2' => 2 }, | ||
] | ||
} | ||
end | ||
|
||
it 'returns values from the top-level' do | ||
subject.extract_value(['string1'], sample_data).must_equal('value1') | ||
subject.extract_value(['string2'], sample_data).must_equal('value2') | ||
subject.extract_value(['number1'], sample_data).must_equal(2468) | ||
end | ||
|
||
it 'returns a full hash from the top-level' do | ||
subject.extract_value(['hash1'], sample_data).must_equal({ 'key1' => 'value1' }) | ||
end | ||
|
||
it 'returns values from a hash' do | ||
subject.extract_value(['hash2', 'hash1string1'], sample_data).must_equal('value3') | ||
subject.extract_value(['hash2', 'hash1number1'], sample_data).must_equal(123) | ||
end | ||
|
||
it 'returns values from a nested hash' do | ||
subject.extract_value(['hash2', 'hash1subhash', 'key1'], sample_data).must_equal(1) | ||
subject.extract_value(['hash2', 'hash1subhash', 'key2'], sample_data).must_equal(2) | ||
end | ||
|
||
it 'returns a full array from the top level' do | ||
subject.extract_value(['array1'], sample_data).must_equal(%w(word1 word2 word3)) | ||
end | ||
|
||
it 'returns values from the array using index numbers' do | ||
subject.extract_value(['array1', 0], sample_data).must_equal('word1') | ||
subject.extract_value(['array1', 1], sample_data).must_equal('word2') | ||
subject.extract_value(['array1', 2], sample_data).must_equal('word3') | ||
end | ||
|
||
it 'returns values from the array using methods' do | ||
subject.extract_value(['array1', 'first'], sample_data).must_equal('word1') | ||
subject.extract_value(['array1', 'last'], sample_data).must_equal('word3') | ||
end | ||
|
||
it 'returns nil when fetching from an array when it does not match a method' do | ||
subject.extract_value(['array1', 'not_a_valid_method'], sample_data).must_be_nil | ||
end | ||
|
||
it 'returns values from a nested hash within an array, accessing the array using numbers' do | ||
subject.extract_value(['array2', 2, 'array1hashkey1'], sample_data).must_equal(1) | ||
subject.extract_value(['array2', 2, 'array1hashkey2'], sample_data).must_equal(2) | ||
end | ||
|
||
it 'returns values from a nested hash within an array, accessing the array using methods' do | ||
subject.extract_value(['array2', 'last', 'array1hashkey1'], sample_data).must_equal(1) | ||
subject.extract_value(['array2', 'last', 'array1hashkey2'], sample_data).must_equal(2) | ||
end | ||
end |