File: to_hash_document_spec.rb

package info (click to toggle)
ruby-fog-xml 0.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 204 kB
  • sloc: ruby: 371; makefile: 2
file content (142 lines) | stat: -rw-r--r-- 3,660 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# frozen_string_literal: true

require 'minitest_helper'
require 'fog/xml'

# We expose accessors just for testing purposes
Fog::ToHashDocument.attr_accessor(:value, :stack)

describe Fog::ToHashDocument do
  before do
    @document = Fog::ToHashDocument.new
  end

  describe '#characters' do
    it 'appends characters to @value' do
      @document.characters('some text')
      _(@document.value).must_equal 'some text'
    end

    it 'strips whitespace from characters' do
      @document.characters('  some text  ')
      _(@document.value).must_equal 'some text'
    end
  end

  describe '#end_element' do
    before do
      @document.stack << {}
      @document.characters('some text')
    end

    it 'adds element with text content to the stack' do
      @document.end_element('element')

      expected = { element: 'some text' }
      _(@document.stack.last).must_equal(expected)
    end

    it 'can mutate the new empty value' do
      @document.end_element('element')

      _(@document.value).must_equal('')

      # Mutate the new empty value even when frozen string literals are enabled
      _(@document.characters('one'))
    end

    it 'adds empty string if element is empty and value is empty' do
      @document.value = ''

      @document.end_element('element')

      expected = { element: '' }
      _(@document.stack.last).must_equal(expected)
    end

    it 'adds nil if element has :i_nil attribute' do
      @document.stack.last[:i_nil] = 'true'
      @document.value = ''

      @document.end_element('element')

      expected = { element: nil }
      _(@document.stack.last).must_equal(expected)
    end
  end

  describe '#body' do
    it 'returns the first element of the stack' do
      @document.stack << { key: 'value' }

      expected = { key: 'value' }
      _(@document.body).must_equal(expected)
    end
  end

  describe '#response' do
    it 'returns the body' do
      @document.stack << { key: 'value' }

      expected = { key: 'value' }
      _(@document.response).must_equal(expected)
    end
  end

  describe '#start_element' do
    it 'parses attributes correctly' do
      @document.start_element('element', [%w[key value]])

      expected = { key: 'value' }
      _(@document.stack.last).must_equal(expected)
    end

    it 'handles elements without attributes' do
      @document.start_element('element')

      _(@document.stack.last).must_equal({})
    end

    it 'adds nested elements to the stack' do
      @document.start_element('parent')
      @document.start_element('child')

      _(@document.stack).must_equal([{ child: {} }, { child: {} }, {}])
    end

    it 'adds nested elements with attributes to the stack' do
      @document.start_element('parent')
      @document.start_element('child', [%w[key value]])
      expected = [
        { child: { key: 'value' } },
        { child: { key: 'value' } },
        { key: 'value' }
      ]

      _(@document.stack).must_equal(expected)
    end

    it 'handles multiple children elements correctly' do
      @document.start_element('parent')
      @document.start_element('child1')
      @document.end_element('child1')
      @document.start_element('child2', [%w[key value]])
      @document.end_element('child2')
      expected = {
        child1: '',
        child2: { key: 'value' }
      }

      _(@document.stack.first).must_equal(expected)
    end

    it 'handles text content within elements' do
      @document.start_element('parent')
      @document.characters('some text')
      @document.end_element('parent')

      expected = { parent: 'some text' }
      _(@document.stack.first).must_equal(expected)
    end
  end
end