Mercurial > prosody-hg
comparison spec/util_datamapper_spec.lua @ 11457:6a51749af7f4
util.datamapper: Add initial support for parsing arrays
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 18 Mar 2021 12:57:25 +0100 |
| parents | f0037234b2e9 |
| children | 0e00fa518688 |
comparison
equal
deleted
inserted
replaced
| 11456:4e376a43fe40 | 11457:6a51749af7f4 |
|---|---|
| 44 }; | 44 }; |
| 45 origin_id = { | 45 origin_id = { |
| 46 type = "string"; | 46 type = "string"; |
| 47 xml = {name = "origin-id"; namespace = "urn:xmpp:sid:0"; x_single_attribute = "id"}; | 47 xml = {name = "origin-id"; namespace = "urn:xmpp:sid:0"; x_single_attribute = "id"}; |
| 48 }; | 48 }; |
| 49 reactions = { | |
| 50 type = "array"; | |
| 51 xml = {namespace = "urn:xmpp:reactions:0"; wrapped = true}; | |
| 52 items = {type = "string"; xml = {name = "reaction"}}; | |
| 53 }; | |
| 49 }; | 54 }; |
| 50 }; | 55 }; |
| 51 | 56 |
| 52 x = xml.parse [[ | 57 x = xml.parse [[ |
| 53 <message xmlns="jabber:client" xml:lang="en" to="a@test" from="b@test" type="chat" id="1"> | 58 <message xmlns="jabber:client" xml:lang="en" to="a@test" from="b@test" type="chat" id="1"> |
| 55 <delay xmlns='urn:xmpp:delay' from='test' stamp='2021-03-07T15:59:08+00:00'>Becasue</delay> | 60 <delay xmlns='urn:xmpp:delay' from='test' stamp='2021-03-07T15:59:08+00:00'>Becasue</delay> |
| 56 <UNRELATED xmlns='http://jabber.org/protocol/chatstates'/> | 61 <UNRELATED xmlns='http://jabber.org/protocol/chatstates'/> |
| 57 <active xmlns='http://jabber.org/protocol/chatstates'/> | 62 <active xmlns='http://jabber.org/protocol/chatstates'/> |
| 58 <fallback xmlns='urn:xmpp:fallback:0'/> | 63 <fallback xmlns='urn:xmpp:fallback:0'/> |
| 59 <origin-id xmlns='urn:xmpp:sid:0' id='qgkmMdPB'/> | 64 <origin-id xmlns='urn:xmpp:sid:0' id='qgkmMdPB'/> |
| 65 <reactions id='744f6e18-a57a-11e9-a656-4889e7820c76' xmlns='urn:xmpp:reactions:0'> | |
| 66 <reaction>👋</reaction> | |
| 67 <reaction>🐢</reaction> | |
| 68 </reactions> | |
| 60 </message> | 69 </message> |
| 61 ]]; | 70 ]]; |
| 62 | 71 |
| 63 d = { | 72 d = { |
| 64 to = "a@test"; | 73 to = "a@test"; |
| 69 body = "Hello"; | 78 body = "Hello"; |
| 70 delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Becasue"}; | 79 delay = {from = "test"; stamp = "2021-03-07T15:59:08+00:00"; reason = "Becasue"}; |
| 71 state = "active"; | 80 state = "active"; |
| 72 fallback = true; | 81 fallback = true; |
| 73 origin_id = "qgkmMdPB"; | 82 origin_id = "qgkmMdPB"; |
| 83 reactions = { | |
| 84 "👋", | |
| 85 "🐢", | |
| 86 }; | |
| 74 }; | 87 }; |
| 75 end); | 88 end); |
| 76 | 89 |
| 77 describe("parse", function() | 90 describe("parse", function() |
| 78 it("works", function() | 91 it("works", function() |
| 83 describe("unparse", function() | 96 describe("unparse", function() |
| 84 it("works", function() | 97 it("works", function() |
| 85 local u = map.unparse(s, d); | 98 local u = map.unparse(s, d); |
| 86 assert.equal("message", u.name); | 99 assert.equal("message", u.name); |
| 87 assert.same(x.attr, u.attr); | 100 assert.same(x.attr, u.attr); |
| 88 assert.equal(#x.tags-1, #u.tags) | |
| 89 assert.equal(x:get_child_text("body"), u:get_child_text("body")); | 101 assert.equal(x:get_child_text("body"), u:get_child_text("body")); |
| 90 assert.equal(x:get_child_text("delay", "urn:xmpp:delay"), u:get_child_text("delay", "urn:xmpp:delay")); | 102 assert.equal(x:get_child_text("delay", "urn:xmpp:delay"), u:get_child_text("delay", "urn:xmpp:delay")); |
| 91 assert.same(x:get_child("delay", "urn:xmpp:delay").attr, u:get_child("delay", "urn:xmpp:delay").attr); | 103 assert.same(x:get_child("delay", "urn:xmpp:delay").attr, u:get_child("delay", "urn:xmpp:delay").attr); |
| 92 assert.same(x:get_child("origin-id", "urn:xmpp:sid:0").attr, u:get_child("origin-id", "urn:xmpp:sid:0").attr); | 104 assert.same(x:get_child("origin-id", "urn:xmpp:sid:0").attr, u:get_child("origin-id", "urn:xmpp:sid:0").attr); |
| 105 for _, tag in ipairs(x.tags) do | |
| 106 if tag.name ~= "UNRELATED" and tag.name ~= "reactions" then | |
| 107 assert.truthy(u:get_child(tag.name, tag.attr.xmlns) or u:get_child(tag.name), tag:top_tag()) | |
| 108 end | |
| 109 end | |
| 110 assert.equal(#x.tags-2, #u.tags) | |
| 111 | |
| 112 pending("arrays", function () | |
| 113 assert.truthy(u:get_child("reactions", "urn:xmpp:reactions:0")) | |
| 114 end); | |
| 115 | |
| 93 end); | 116 end); |
| 94 end); | 117 end); |
| 95 end) | 118 end) |
