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