Annotate

spec/util_datamapper_spec.lua @ 11470:5ebad952ebf7

util.datamapper: Fix to skip parsing wrapped arrays that aren't there Turns out the unreachable error is reachable :D
author Kim Alvefur <zash@zash.se>
date Mon, 22 Mar 2021 10:03:32 +0100
parent 11468:348b191cd850
child 11476:83e127eb91f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local xml
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local map
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 setup(function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 xml = require "util.xml";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 map = require "util.datamapper";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 end);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 describe("util.datampper", function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local s, x, d
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
12 local disco, disco_info, disco_schema
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 setup(function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local function attr() return {type = "string"; xml = {attribute = true}} end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 s = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 type = "object";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 xml = {name = "message"; namespace = "jabber:client"};
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 properties = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 to = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 from = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 type = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 id = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 body = "string";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 lang = {type = "string"; xml = {attribute = true; prefix = "xml"}};
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 delay = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 type = "object";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 xml = {namespace = "urn:xmpp:delay"; name = "delay"};
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 properties = {stamp = attr(); from = attr(); reason = {type = "string"; xml = {text = true}}};
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 };
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
31 state = {
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
32 type = "string";
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
33 enum = {
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
34 "active",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
35 "inactive",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
36 "gone",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
37 "composing",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
38 "paused",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
39 };
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
40 xml = {x_name_is_value = true; namespace = "http://jabber.org/protocol/chatstates"};
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
41 };
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
42 fallback = {
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
43 type = "boolean";
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
44 xml = {x_name_is_value = true; name = "fallback"; namespace = "urn:xmpp:fallback:0"};
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
45 };
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
46 origin_id = {
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
47 type = "string";
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
48 xml = {name = "origin-id"; namespace = "urn:xmpp:sid:0"; x_single_attribute = "id"};
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
49 };
11462
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
50 react = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
51 type = "object";
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
52 xml = {namespace = "urn:xmpp:reactions:0"; name = "reactions"};
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
53 properties = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
54 to = {type = "string"; xml = {attribute = true; name = "id"}};
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
55 reactions = {type = "array"; items = {type = "string"; xml = {name = "reaction"}}};
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
56 };
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
57 };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 };
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 };
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 x = xml.parse [[
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
62 <message xmlns="jabber:client" xml:lang="en" to="a@test" from="b@test" type="chat" id="1">
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
63 <body>Hello</body>
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
64 <delay xmlns='urn:xmpp:delay' from='test' stamp='2021-03-07T15:59:08+00:00'>Becasue</delay>
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
65 <UNRELATED xmlns='http://jabber.org/protocol/chatstates'/>
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
66 <active xmlns='http://jabber.org/protocol/chatstates'/>
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
67 <fallback xmlns='urn:xmpp:fallback:0'/>
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
68 <origin-id xmlns='urn:xmpp:sid:0' id='qgkmMdPB'/>
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
69 <reactions id='744f6e18-a57a-11e9-a656-4889e7820c76' xmlns='urn:xmpp:reactions:0'>
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
70 <reaction>👋</reaction>
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
71 <reaction>🐢</reaction>
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
72 </reactions>
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
73 </message>
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
74 ]];
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 d = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 to = "a@test";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 from = "b@test";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 type = "chat";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 id = "1";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 lang = "en";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 body = "Hello";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Becasue"};
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
84 state = "active";
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
85 fallback = true;
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
86 origin_id = "qgkmMdPB";
11462
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
87 react = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
88 to = "744f6e18-a57a-11e9-a656-4889e7820c76";
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
89 reactions = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
90 "👋",
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
91 "🐢",
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
92 };
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
93 };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 };
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
95
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
96 disco_schema = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
97 type = "object";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
98 xml = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
99 name = "iq";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
100 namespace = "jabber:client"
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
101 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
102 properties = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
103 to = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
104 from = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
105 type = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
106 id = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
107 disco = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
108 type = "object";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
109 xml = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
110 name = "query";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
111 namespace = "http://jabber.org/protocol/disco#info"
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
112 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
113 properties = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
114 features = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
115 type = "array";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
116 items = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
117 type = "string";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
118 xml = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
119 name = "feature";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
120 x_single_attribute = "var";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
121 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
122 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
123 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
124 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
125 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
126 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
127 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
128
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
129 disco_info = xml.parse[[
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
130 <iq type="result" id="disco1" from="example.com">
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
131 <query xmlns="http://jabber.org/protocol/disco#info">
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
132 <feature var="urn:example:feature:1">wrong</feature>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
133 <feature var="urn:example:feature:2"/>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
134 <feature var="urn:example:feature:3"/>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
135 <unrelated var="urn:example:feature:not"/>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
136 </query>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
137 </iq>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
138 ]];
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
139
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
140 disco = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
141 type="result";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
142 id="disco1";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
143 from="example.com";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
144 disco = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
145 features = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
146 "urn:example:feature:1";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
147 "urn:example:feature:2";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
148 "urn:example:feature:3";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
149 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
150 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
151 };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 end);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 describe("parse", function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 it("works", function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 assert.same(d, map.parse(s, x));
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 end);
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
158
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
159 it("handles arrays", function ()
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
160 assert.same(disco, map.parse(disco_schema, disco_info));
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
161 end);
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
162
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 end);
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
164
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
165 describe("unparse", function()
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
166 it("works", function()
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
167 local u = map.unparse(s, d);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
168 assert.equal("message", u.name);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
169 assert.same(x.attr, u.attr);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
170 assert.equal(x:get_child_text("body"), u:get_child_text("body"));
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
171 assert.equal(x:get_child_text("delay", "urn:xmpp:delay"), u:get_child_text("delay", "urn:xmpp:delay"));
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
172 assert.same(x:get_child("delay", "urn:xmpp:delay").attr, u:get_child("delay", "urn:xmpp:delay").attr);
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
173 assert.same(x:get_child("origin-id", "urn:xmpp:sid:0").attr, u:get_child("origin-id", "urn:xmpp:sid:0").attr);
11462
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
174 assert.same(x:get_child("reactions", "urn:xmpp:reactions:0").attr, u:get_child("reactions", "urn:xmpp:reactions:0").attr);
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
175 assert.same(2, #u:get_child("reactions", "urn:xmpp:reactions:0").tags);
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
176 for _, tag in ipairs(x.tags) do
11458
0e00fa518688 util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents: 11457
diff changeset
177 if tag.name ~= "UNRELATED" then
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
178 assert.truthy(u:get_child(tag.name, tag.attr.xmlns) or u:get_child(tag.name), tag:top_tag())
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
179 end
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
180 end
11458
0e00fa518688 util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents: 11457
diff changeset
181 assert.equal(#x.tags-1, #u.tags)
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
182
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
183 end);
11468
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
184
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
185 it("handles arrays", function ()
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
186 local u = map.unparse(disco_schema, disco);
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
187 assert.equal("urn:example:feature:1", u:find("{http://jabber.org/protocol/disco#info}query/feature/@var"))
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
188 local n = 0;
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
189 for child in u:get_child("query", "http://jabber.org/protocol/disco#info"):childtags("feature") do
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
190 n = n + 1;
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
191 assert.equal(string.format("urn:example:feature:%d", n), child.attr.var);
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
192 end
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
193 end);
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
194
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
195 end);
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 end)