File: parse_options_test.rb

package info (click to toggle)
ruby-multi-xml 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 472 kB
  • sloc: ruby: 2,822; sh: 4; makefile: 2
file content (183 lines) | stat: -rw-r--r-- 5,395 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require "test_helper"

# Tests for ParseOptionsTest
class ParseOptionsTest < Minitest::Test
  cover "MultiXml*"

  def setup
    @original_parser = MultiXml.instance_variable_get(:@parser)
    MultiXml.parser = best_available_parser
  end

  def teardown
    return unless @original_parser

    MultiXml.instance_variable_set(:@parser, @original_parser)
  end

  def test_parse_with_typecast_xml_value_true
    result = MultiXml.parse('<tag type="integer">42</tag>', typecast_xml_value: true)

    assert_equal 42, result["tag"]
  end

  def test_parse_with_typecast_xml_value_false
    result = MultiXml.parse('<tag type="integer">42</tag>', typecast_xml_value: false)

    assert_equal({"type" => "integer", "__content__" => "42"}, result["tag"])
  end

  def test_parse_with_symbolize_keys_true
    result = MultiXml.parse("<root><name>John</name></root>", symbolize_keys: true)

    assert_equal({root: {name: "John"}}, result)
  end

  def test_parse_with_symbolize_keys_false
    result = MultiXml.parse("<root><name>John</name></root>", symbolize_keys: false)

    assert_equal({"root" => {"name" => "John"}}, result)
  end

  def test_parse_with_disallowed_types_empty_allows_yaml
    result = MultiXml.parse('<tag type="yaml">--- test</tag>', disallowed_types: [])

    assert_equal "test", result["tag"]
  end

  def test_parse_with_custom_disallowed_types
    assert_raises(MultiXml::DisallowedTypeError) do
      MultiXml.parse('<tag type="integer">42</tag>', disallowed_types: ["integer"])
    end
  end

  def test_parse_uses_parser_option_when_provided
    MultiXml.parser = :rexml
    result = MultiXml.parse("<root>test</root>", parser: :nokogiri)

    assert_equal({"root" => "test"}, result)
  end

  def test_parse_uses_class_parser_when_parser_option_nil
    MultiXml.parser = best_available_parser
    # When options[:parser] is nil (falsy), should use class-level parser
    result = MultiXml.parse("<root>test</root>", parser: nil)

    assert_equal({"root" => "test"}, result)
  end
end

# Tests for ParseWithParserOptionTest
class ParseWithParserOptionTest < Minitest::Test
  cover "MultiXml*"

  def setup
    @original_parser = MultiXml.instance_variable_get(:@parser)
    MultiXml.parser = :rexml
  end

  def teardown
    return unless @original_parser

    MultiXml.instance_variable_set(:@parser, @original_parser)
  end

  def test_parse_uses_parser_option_when_truthy
    result = MultiXml.parse("<root>test</root>", parser: :nokogiri)

    assert_equal({"root" => "test"}, result)
  end

  def test_parse_uses_class_parser_when_parser_option_nil
    result = MultiXml.parse("<root>test</root>", parser: nil)

    # Should use REXML (the class parser we set)
    assert_equal({"root" => "test"}, result)
  end

  def test_parse_uses_class_parser_when_parser_option_false
    result = MultiXml.parse("<root>test</root>", parser: false)

    # Should use REXML (the class parser we set)
    assert_equal({"root" => "test"}, result)
  end

  def test_parse_with_explicit_parser_option
    MultiXml.parser = :rexml
    result = MultiXml.parse("<root>value</root>", parser: :nokogiri)

    # Should use Nokogiri, not REXML
    assert_equal({"root" => "value"}, result)
  end
end

# Tests parse option access behavior
class ParseOptionsAccessTest < Minitest::Test
  cover "MultiXml*"

  def setup
    @original_parser = MultiXml.instance_variable_get(:@parser)
  end

  def teardown
    return unless @original_parser

    MultiXml.instance_variable_set(:@parser, @original_parser)
  end

  def test_parse_accesses_parser_option_with_bracket
    # With fetch, missing key raises, with [] returns nil
    MultiXml.parser = best_available_parser

    # options without :parser key should work (use class-level parser)
    result = MultiXml.parse("<test>value</test>", symbolize_keys: false)

    assert_equal({"test" => "value"}, result)
  end

  def test_parse_uses_truthy_check_for_parser_option
    MultiXml.parser = :rexml

    # nil parser option should fall back to class parser
    result = MultiXml.parse("<r>v</r>", parser: nil)

    assert_equal({"r" => "v"}, result)
  end

  def test_parse_uses_provided_parser_when_truthy
    MultiXml.parser = :rexml

    # Truthy parser option should be used
    result = MultiXml.parse("<r>v</r>", parser: :nokogiri)

    assert_equal({"r" => "v"}, result)
  end

  def test_parse_accesses_typecast_option_correctly
    MultiXml.parser = best_available_parser

    result_with = MultiXml.parse('<n type="integer">42</n>', typecast_xml_value: true)
    result_without = MultiXml.parse('<n type="integer">42</n>', typecast_xml_value: false)

    assert_equal 42, result_with["n"]
    assert_equal({"type" => "integer", "__content__" => "42"}, result_without["n"])
  end

  def test_parse_accesses_symbolize_keys_option_correctly
    MultiXml.parser = best_available_parser

    result_with = MultiXml.parse("<root><name>v</name></root>", symbolize_keys: true)
    result_without = MultiXml.parse("<root><name>v</name></root>", symbolize_keys: false)

    assert_equal({root: {name: "v"}}, result_with)
    assert_equal({"root" => {"name" => "v"}}, result_without)
  end

  def test_parse_accesses_disallowed_types_option_correctly
    MultiXml.parser = best_available_parser

    assert_raises(MultiXml::DisallowedTypeError) do
      MultiXml.parse('<n type="yaml">test</n>', disallowed_types: ["yaml"])
    end
  end
end