-
Notifications
You must be signed in to change notification settings - Fork 4
/
map_to_xml_test.exs
125 lines (115 loc) · 4.19 KB
/
map_to_xml_test.exs
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
defmodule MapToXmlTest do
use ExUnit.Case
describe "basic functionality" do
test "one tag" do
assert MapToXml.from_map(%{
"Tag1" => "Value1"
}) == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1>Value1</Tag1>"
end
test "multiple tags" do
assert MapToXml.from_map(%{
"Tag1" => "Value1",
"Tag2" => "Value2",
"Tag3" => "Value3"
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1>Value1</Tag1>\n<Tag2>Value2</Tag2>\n<Tag3>Value3</Tag3>"
end
test "nested maps becomes nested tags" do
assert MapToXml.from_map(%{
"Tag1" => %{
"Tag2" => %{
"Tag3" => "Value"
}
}
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1>\n <Tag2>\n <Tag3>Value</Tag3>\n </Tag2>\n</Tag1>"
end
test "maps with lists become repeated child tags" do
assert MapToXml.from_map(%{
"Tags" => %{
"Tag1" => [
%{"Sub1" => "Val1"},
%{"Sub1" => "Val2"},
%{"Sub1" => "Val3"}
]
}
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tags>\n <Tag1>\n <Sub1>Val1</Sub1>\n </Tag1>\n <Tag1>\n <Sub1>Val2</Sub1>\n </Tag1>\n <Tag1>\n <Sub1>Val3</Sub1>\n </Tag1>\n</Tags>"
end
test "numeric values" do
assert MapToXml.from_map(%{
"Tag1" => 1234,
"Tag2" => 12.15
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1>1234</Tag1>\n<Tag2>12.15</Tag2>"
end
test "boolean values" do
assert MapToXml.from_map(%{
"Tag1" => true,
"Tag2" => false
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1>true</Tag1>\n<Tag2>false</Tag2>"
end
end
describe "attribute support" do
test "simple value" do
assert MapToXml.from_map(%{
"Tag1" => %{
"#content" => "some value",
"-id" => 123,
"-something" => "111"
}
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1 id=\"123\" something=\"111\">some value</Tag1>"
end
test "nested map as value" do
assert MapToXml.from_map(%{
"Tag1" => %{
"#content" => %{
"Tag2" => %{
"Tag3" => "value"
}
},
"-id" => 123,
"-something" => "111"
}
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1 id=\"123\" something=\"111\">\n <Tag2>\n <Tag3>value</Tag3>\n </Tag2>\n</Tag1>"
end
test "nested maps with attributes" do
assert MapToXml.from_map(%{
"Tag1" => %{
"#content" => %{
"Tag2" => %{
"#content" => %{
"Tag3" => %{
"#content" => "final value",
"-id" => "1234",
"-something" => "value"
}
},
"-id" => "1234",
"-something" => "value"
}
},
"-id" => "1234",
"-something" => "value"
}
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1 id=\"1234\" something=\"value\">\n <Tag2 id=\"1234\" something=\"value\">\n <Tag3 id=\"1234\" something=\"value\">final value</Tag3>\n </Tag2>\n</Tag1>"
end
test "lists" do
assert MapToXml.from_map(%{
"Tag1" => %{
"#content" => [
"value1",
"value2"
],
"-id" => "1234",
"-something" => "value"
}
}) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Tag1 id=\"1234\" something=\"value\">value1</Tag1>\n<Tag1 id=\"1234\" something=\"value\">value2</Tag1>"
end
end
end