|
12 | 12 | expect(ces).to be_instance_of(described_class) |
13 | 13 | end |
14 | 14 |
|
| 15 | + # --------------------------------------------------------------------------- |
| 16 | + # Hash-like methods returning Collections (issue #37) |
| 17 | + # --------------------------------------------------------------------------- |
| 18 | + describe 'Hash-like methods that return Collections' do |
| 19 | + subject(:ces) { described_class.new(ComplianceEngine::Data.new(ComplianceEngine::DataLoader.new(compliance_data))) } |
| 20 | + |
| 21 | + let(:compliance_data) do |
| 22 | + { |
| 23 | + 'version' => '2.0.0', |
| 24 | + 'ce' => { |
| 25 | + 'ce_one' => { 'title' => 'CE One' }, |
| 26 | + 'ce_two' => { 'title' => 'CE Two' }, |
| 27 | + 'ce_three' => { 'title' => 'CE Three' }, |
| 28 | + }, |
| 29 | + } |
| 30 | + end |
| 31 | + |
| 32 | + describe '#select' do |
| 33 | + it 'returns a Collection of the same type' do |
| 34 | + result = ces.select { |k, _| k == 'ce_one' } |
| 35 | + expect(result).to be_instance_of(described_class) |
| 36 | + end |
| 37 | + |
| 38 | + it 'contains only the selected keys' do |
| 39 | + result = ces.select { |k, _| k == 'ce_one' } |
| 40 | + expect(result.keys).to eq(['ce_one']) |
| 41 | + end |
| 42 | + |
| 43 | + it 'does not modify the original collection' do |
| 44 | + ces.select { |k, _| k == 'ce_one' } |
| 45 | + expect(ces.keys).to contain_exactly('ce_one', 'ce_two', 'ce_three') |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe '#reject' do |
| 50 | + it 'returns a Collection of the same type' do |
| 51 | + result = ces.reject { |k, _| k == 'ce_one' } |
| 52 | + expect(result).to be_instance_of(described_class) |
| 53 | + end |
| 54 | + |
| 55 | + it 'excludes the rejected keys' do |
| 56 | + result = ces.reject { |k, _| k == 'ce_one' } |
| 57 | + expect(result.keys).to contain_exactly('ce_two', 'ce_three') |
| 58 | + end |
| 59 | + |
| 60 | + it 'does not modify the original collection' do |
| 61 | + ces.reject { |k, _| k == 'ce_one' } |
| 62 | + expect(ces.keys).to contain_exactly('ce_one', 'ce_two', 'ce_three') |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe '#transform_values' do |
| 67 | + it 'returns a Hash' do |
| 68 | + result = ces.transform_values(&:title) |
| 69 | + expect(result).to be_instance_of(Hash) |
| 70 | + end |
| 71 | + |
| 72 | + it 'maps each component to its transformed value' do |
| 73 | + result = ces.transform_values(&:title) |
| 74 | + expect(result).to eq('ce_one' => 'CE One', 'ce_two' => 'CE Two', 'ce_three' => 'CE Three') |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
15 | 79 | # --------------------------------------------------------------------------- |
16 | 80 | # clone/dup isolation (Collection behavior) |
17 | 81 | # |
|
0 commit comments