|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'minitest_helper' |
| 4 | +require 'fog/xml' |
| 5 | + |
| 6 | +# We expose accessors just for testing purposes |
| 7 | +Fog::ToHashDocument.attr_accessor(:value, :stack) |
| 8 | + |
| 9 | +describe Fog::ToHashDocument do |
| 10 | + before do |
| 11 | + @document = Fog::ToHashDocument.new |
| 12 | + end |
| 13 | + |
| 14 | + describe '#characters' do |
| 15 | + it 'appends characters to @value' do |
| 16 | + @document.characters('some text') |
| 17 | + _(@document.value).must_equal 'some text' |
| 18 | + end |
| 19 | + |
| 20 | + it 'strips whitespace from characters' do |
| 21 | + @document.characters(' some text ') |
| 22 | + _(@document.value).must_equal 'some text' |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe '#end_element' do |
| 27 | + before do |
| 28 | + @document.stack << {} |
| 29 | + @document.characters('some text') |
| 30 | + end |
| 31 | + |
| 32 | + it 'adds element with text content to the stack' do |
| 33 | + @document.end_element('element') |
| 34 | + |
| 35 | + expected = { element: 'some text' } |
| 36 | + _(@document.stack.last).must_equal(expected) |
| 37 | + end |
| 38 | + |
| 39 | + it 'can mutate the new empty value' do |
| 40 | + @document.end_element('element') |
| 41 | + |
| 42 | + _(@document.value).must_equal('') |
| 43 | + |
| 44 | + # Mutate the new empty value even when frozen string literals are enabled |
| 45 | + _(@document.characters('one')) |
| 46 | + end |
| 47 | + |
| 48 | + it 'adds empty string if element is empty and value is empty' do |
| 49 | + @document.value = '' |
| 50 | + |
| 51 | + @document.end_element('element') |
| 52 | + |
| 53 | + expected = { element: '' } |
| 54 | + _(@document.stack.last).must_equal(expected) |
| 55 | + end |
| 56 | + |
| 57 | + it 'adds nil if element has :i_nil attribute' do |
| 58 | + @document.stack.last[:i_nil] = 'true' |
| 59 | + @document.value = '' |
| 60 | + |
| 61 | + @document.end_element('element') |
| 62 | + |
| 63 | + expected = { element: nil } |
| 64 | + _(@document.stack.last).must_equal(expected) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe '#body' do |
| 69 | + it 'returns the first element of the stack' do |
| 70 | + @document.stack << { key: 'value' } |
| 71 | + |
| 72 | + expected = { key: 'value' } |
| 73 | + _(@document.body).must_equal(expected) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe '#response' do |
| 78 | + it 'returns the body' do |
| 79 | + @document.stack << { key: 'value' } |
| 80 | + |
| 81 | + expected = { key: 'value' } |
| 82 | + _(@document.response).must_equal(expected) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + describe '#start_element' do |
| 87 | + it 'parses attributes correctly' do |
| 88 | + @document.start_element('element', [%w[key value]]) |
| 89 | + |
| 90 | + expected = { key: 'value' } |
| 91 | + _(@document.stack.last).must_equal(expected) |
| 92 | + end |
| 93 | + |
| 94 | + it 'handles elements without attributes' do |
| 95 | + @document.start_element('element') |
| 96 | + |
| 97 | + _(@document.stack.last).must_equal({}) |
| 98 | + end |
| 99 | + |
| 100 | + it 'adds nested elements to the stack' do |
| 101 | + @document.start_element('parent') |
| 102 | + @document.start_element('child') |
| 103 | + |
| 104 | + _(@document.stack).must_equal([{ child: {} }, { child: {} }, {}]) |
| 105 | + end |
| 106 | + |
| 107 | + it 'adds nested elements with attributes to the stack' do |
| 108 | + @document.start_element('parent') |
| 109 | + @document.start_element('child', [%w[key value]]) |
| 110 | + expected = [ |
| 111 | + { child: { key: 'value' } }, |
| 112 | + { child: { key: 'value' } }, |
| 113 | + { key: 'value' } |
| 114 | + ] |
| 115 | + |
| 116 | + _(@document.stack).must_equal(expected) |
| 117 | + end |
| 118 | + |
| 119 | + it 'handles multiple children elements correctly' do |
| 120 | + @document.start_element('parent') |
| 121 | + @document.start_element('child1') |
| 122 | + @document.end_element('child1') |
| 123 | + @document.start_element('child2', [%w[key value]]) |
| 124 | + @document.end_element('child2') |
| 125 | + expected = { |
| 126 | + child1: '', |
| 127 | + child2: { key: 'value' } |
| 128 | + } |
| 129 | + |
| 130 | + _(@document.stack.first).must_equal(expected) |
| 131 | + end |
| 132 | + |
| 133 | + it 'handles text content within elements' do |
| 134 | + @document.start_element('parent') |
| 135 | + @document.characters('some text') |
| 136 | + @document.end_element('parent') |
| 137 | + |
| 138 | + expected = { parent: 'some text' } |
| 139 | + _(@document.stack.first).must_equal(expected) |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments