Annotate

spec/util_datamapper_spec.lua @ 12391:a15647d42880 0.12

mod_invites_register: Push invitee contact entry to inviter Invitee would not show up in the roster of the invite creator unless they fetch their roster afterwards. Fixes #1715 Thanks gerald
author Kim Alvefur <zash@zash.se>
date Tue, 15 Mar 2022 21:59:51 +0100
parent 12134:912614c4bf3e
child 12580:a9dbf657c894
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11476
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
1 local st
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local xml
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local map
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 setup(function()
11476
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
6 st = require "util.stanza";
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 xml = require "util.xml";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 map = require "util.datamapper";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
12134
912614c4bf3e util_datamapper: Fix typo in unit tests
Kim Alvefur <zash@zash.se>
parents: 12133
diff changeset
11 describe("util.datamapper", function()
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local s, x, d
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
14 local disco, disco_info, disco_schema
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 setup(function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
11487
b104c10ffce7 util.datamapper: Use attribute convenience function throughout
Kim Alvefur <zash@zash.se>
parents: 11480
diff changeset
17 -- a convenience function for simple attributes, there's a few of them
12133
11060c8919b6 util.datamapper: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11494
diff changeset
18 local function attr() return {["$ref"]="#/$defs/attr"} end
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 s = {
12133
11060c8919b6 util.datamapper: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11494
diff changeset
20 ["$defs"] = { attr = { type = "string"; xml = { attribute = true } } };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 type = "object";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 xml = {name = "message"; namespace = "jabber:client"};
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 properties = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 to = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 from = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 type = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 id = attr();
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 body = "string";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 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
30 delay = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 type = "object";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 xml = {namespace = "urn:xmpp:delay"; name = "delay"};
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 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
34 };
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
35 state = {
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
36 type = "string";
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
37 enum = {
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
38 "active",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
39 "inactive",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
40 "gone",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
41 "composing",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
42 "paused",
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
43 };
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
44 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
45 };
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
46 fallback = {
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
47 type = "boolean";
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
48 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
49 };
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
50 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
51 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
52 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
53 };
11462
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
54 react = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
55 type = "object";
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
56 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
57 properties = {
11488
332c9291f4d5 util.datamapper: Revert one special attribute to longer form
Kim Alvefur <zash@zash.se>
parents: 11487
diff changeset
58 to = {type = "string"; xml = {attribute = true; name = "id"}};
11462
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
59 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
60 };
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
61 };
11480
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
62 stanza_ids = {
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
63 type = "array";
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
64 items = {
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
65 xml = {name = "stanza-id"; namespace = "urn:xmpp:sid:0"};
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
66 type = "object";
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
67 properties = {
11487
b104c10ffce7 util.datamapper: Use attribute convenience function throughout
Kim Alvefur <zash@zash.se>
parents: 11480
diff changeset
68 id = attr();
b104c10ffce7 util.datamapper: Use attribute convenience function throughout
Kim Alvefur <zash@zash.se>
parents: 11480
diff changeset
69 by = attr();
11480
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
70 };
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
71 };
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
72 };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 };
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 };
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 x = xml.parse [[
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
77 <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
78 <body>Hello</body>
11494
284c822d4c3d util.datamapper: Fix spelling in tests
Kim Alvefur <zash@zash.se>
parents: 11488
diff changeset
79 <delay xmlns='urn:xmpp:delay' from='test' stamp='2021-03-07T15:59:08+00:00'>Because</delay>
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
80 <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
81 <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
82 <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
83 <origin-id xmlns='urn:xmpp:sid:0' id='qgkmMdPB'/>
11480
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
84 <stanza-id xmlns='urn:xmpp:sid:0' id='abc1' by='muc'/>
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
85 <stanza-id xmlns='urn:xmpp:sid:0' id='xyz2' by='host'/>
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
86 <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
87 <reaction>👋</reaction>
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
88 <reaction>🐢</reaction>
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
89 </reactions>
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
90 </message>
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
91 ]];
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 d = {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 to = "a@test";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 from = "b@test";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 type = "chat";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 id = "1";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 lang = "en";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 body = "Hello";
11494
284c822d4c3d util.datamapper: Fix spelling in tests
Kim Alvefur <zash@zash.se>
parents: 11488
diff changeset
100 delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Because"};
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
101 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
102 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
103 origin_id = "qgkmMdPB";
11480
0aa2971380e9 util.datamapper: Add test coverage of unwrapped arrays of objects
Kim Alvefur <zash@zash.se>
parents: 11476
diff changeset
104 stanza_ids = {{id = "abc1"; by = "muc"}; {id = "xyz2"; by = "host"}};
11462
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
105 react = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
106 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
107 reactions = {
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
108 "👋",
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
109 "🐢",
d1982b7eb00d util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents: 11458
diff changeset
110 };
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
111 };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 };
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
113
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
114 disco_schema = {
12133
11060c8919b6 util.datamapper: Add support for $ref pointers
Kim Alvefur <zash@zash.se>
parents: 11494
diff changeset
115 ["$defs"] = { attr = { type = "string"; xml = { attribute = true } } };
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
116 type = "object";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
117 xml = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
118 name = "iq";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
119 namespace = "jabber:client"
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
120 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
121 properties = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
122 to = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
123 from = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
124 type = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
125 id = attr();
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
126 disco = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
127 type = "object";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
128 xml = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
129 name = "query";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
130 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
131 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
132 properties = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
133 features = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
134 type = "array";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
135 items = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
136 type = "string";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
137 xml = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
138 name = "feature";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
139 x_single_attribute = "var";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
140 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
141 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
142 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
143 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
144 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
145 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
146 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
147
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
148 disco_info = xml.parse[[
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
149 <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
150 <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
151 <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
152 <feature var="urn:example:feature:2"/>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
153 <feature var="urn:example:feature:3"/>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
154 <unrelated var="urn:example:feature:not"/>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
155 </query>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
156 </iq>
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
157 ]];
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 disco = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
160 type="result";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
161 id="disco1";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
162 from="example.com";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
163 disco = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
164 features = {
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
165 "urn:example:feature:1";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
166 "urn:example:feature:2";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
167 "urn:example:feature:3";
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
168 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
169 };
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
170 };
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 describe("parse", function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 it("works", function()
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 assert.same(d, map.parse(s, x));
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 end);
11466
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
177
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
178 it("handles arrays", function ()
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
179 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
180 end);
c098d07e6717 util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11462
diff changeset
181
11476
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
182 it("deals with locally built stanzas", function()
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
183 -- FIXME this could also be argued to be a util.stanza problem
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
184 local ver_schema = {
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
185 type = "object";
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
186 xml = {name = "iq"};
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
187 properties = {
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
188 type = {type = "string"; xml = {attribute = true}};
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
189 id = {type = "string"; xml = {attribute = true}};
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
190 version = {
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
191 type = "object";
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
192 xml = {name = "query"; namespace = "jabber:iq:version"};
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
193 properties = {name = "string"; version = "string"; os = "string"};
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
194 };
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
195 };
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
196 };
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
197 local ver_st = st.iq({type = "result"; id = "v1"})
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
198 :query("jabber:iq:version")
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
199 :text_tag("name", "Prosody")
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
200 :text_tag("version", "trunk")
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
201 :text_tag("os", "Lua 5.3")
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
202 :reset();
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
203
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
204 local data = {type = "result"; id = "v1"; version = {name = "Prosody"; version = "trunk"; os = "Lua 5.3"}}
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
205 assert.same(data, map.parse(ver_schema, ver_st));
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
206 end);
83e127eb91f9 util.datamapper: Deal with locally built stanzas missing xmlns
Kim Alvefur <zash@zash.se>
parents: 11468
diff changeset
207
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 end);
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
209
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
210 describe("unparse", function()
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
211 it("works", function()
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
212 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
213 assert.equal("message", u.name);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 if tag.name ~= "UNRELATED" then
11457
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
223 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
224 end
6a51749af7f4 util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
225 end
11458
0e00fa518688 util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents: 11457
diff changeset
226 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
227
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
228 end);
11468
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
229
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
230 it("handles arrays", function ()
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
231 local u = map.unparse(disco_schema, disco);
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
232 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
233 local n = 0;
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
234 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
235 n = n + 1;
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
236 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
237 end
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
238 end);
348b191cd850 util.datamapper: Complete array building support
Kim Alvefur <zash@zash.se>
parents: 11466
diff changeset
239
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
240 end);
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 end)