-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2json_spec.rb
105 lines (91 loc) · 2.8 KB
/
xml2json_spec.rb
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
# frozen_string_literal: true
# rubocop:disable Metrics/BlockLength
require "xml2json"
RSpec.describe Xml2Json do
it "has a version number" do
expect(Xml2Json::VERSION).not_to be nil
end
end
RSpec.describe Xml2Json::Xml do
it "converts json to xml" do
expect(Xml2Json::Xml.build('{"a": 1, "b": ["2"]}')).to eq "<?xml version=\"1.0\"?><root><a>1</a><b>2</b></root>"
end
it "converts json into pretty xml" do
expect(Xml2Json::Xml.build_pretty('{"a": 1, "b": ["2"]}')).to eq <<~XML.chomp
<?xml version="1.0"?>
<root>
<a>1</a>
<b>2</b>
</root>
XML
end
it "raises on incorrect input" do
expect { Xml2Json::Xml.build('{"a": 1, ') }.to raise_error(RuntimeError)
expect { Xml2Json::Xml.build('{"a": 1}', indent_char: "🐈") }.to raise_error(TypeError)
end
it "accepts config options" do
expect(
Xml2Json::Xml.build(
'{"book":{"^":{"category":"fantasy"},"title":{"_":"The Name of the Wind","^":{"lang":"en"}},' \
'"author":"Patrick Rothfuss","year":"2007"}}',
indent_char: " ", indent_size: 2,
version: "1.0", encoding: :UTF8, standalone: true,
attrkey: "^", root_name: "store"
)
).to eq '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<store>
<book category="fantasy">
<title lang="en">The Name of the Wind</title>
<author>Patrick Rothfuss</author>
<year>2007</year>
</book>
</store>'
end
end
RSpec.describe Xml2Json::Xml::Encoding do
it "stores constants" do
should be_const_defined(:UTF8)
end
end
RSpec.describe Xml2Json::Xml::Version do
it "stores constants" do
should be_const_defined(:XML10)
should be_const_defined(:XML11)
end
end
RSpec.describe Xml2Json::Json do
it "converts xml to json" do
expect(Xml2Json::Json.build("<root><a>1</a><b>2</b></root>")).to eq '{"root":{"a":["1"],"b":["2"]}}'
end
it "converts xml into pretty json" do
expect(Xml2Json::Json.build_pretty("<root><a>1</a><b>2</b></root>")).to eq <<~JSON.chomp
{
"root": {
"a": [
"1"
],
"b": [
"2"
]
}
}
JSON
end
it "raises on incorrect input" do
expect { Xml2Json::Json.build("<root><a>1</") }.to raise_error(RuntimeError)
end
it "may ignore incorrect input" do
expect(Xml2Json::Json.build("<root><a>1</a")).to eq "null"
end
it "accepts config options" do
expect(
Xml2Json::Json.build(
'<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title>' \
"<author>Patrick Rothfuss</author><year>2007</year></book>",
ignore_attrs: true,
explicit_array: false
)
).to eq '{"book":{"title":"The Name of the Wind","author":"Patrick Rothfuss","year":"2007"}}'
end
end
# rubocop:enable Metrics/BlockLength