Mon, 10 Sep 2018 14:15:22 +0100
stanzacmp: Don't look for scansion control attrs in comparison stanza
--luacheck: std +busted local stanzacmp = require "scansion.stanzacmp"; require "verse"; local st = require "util.stanza"; local xml = require "scansion.xml"; local function parse(...) local n = select("#", ...); local out = {}; for i = 1, n do local v = (select(i, ...)); if type(v) == "string" then out[i] = xml.parse(v); else out[i] = v; end end return table.unpack(out, 1, n); end local function yes(s1, s2) s1, s2 = parse(s1, s2); if not stanzacmp.stanzas_match(s1, s2) then print("s1", s1) print("s2", s2) print("literal", tostring(s1) == tostring(s2)); assert(false, "No match, but they should."); end end local function no(s1, s2) s1, s2 = parse(s1, s2); if stanzacmp.stanzas_match(s1, s2) then print("s1", s1) print("s2", s2) assert(false, "Match, but they should not."); end end describe("stanzacmp", function () it("should work", function () local s1 = st.message({ to = "foo", from = "bar"}); local s2 = st.message({ to = "foo", from = "bar"}); yes(s1, s2); yes(s2, s1); s1.attr.to = nil; yes(s1, s2); no(s2, s1); s1.attr.to = "foo"; yes(s1, s2); s2:tag("blah", { xmlns = "foobar" }); yes(s1, s2); no(s2, s1); s1:tag("blah", { xmlns = "foobar" }); yes(s1, s2); yes(s2, s1); s1:tag("a"); no(s1, s2); s2:tag("a"); yes(s1, s2); s2:up():up(); s2:tag("extension", { xmlns = "blah" }); yes(s1, s2); s2:tag("b"); yes(s1, s2); s1:up():up(); s1:tag("extension", { xmlns = "blah" }); no(s1, s2); s1:tag("b"); yes(s1, s2); s1:tag("c", { n = "1" }); no(s1, s2); end); it("should allow matching any attribute value with {scansion:any}", function () --[[ <message from="user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b" type="chat"> <body>Hello Juliet, are you there?</body> <delay xmlns="urn:xmpp:delay" from="localhost"/> </message> ]] local s01 = st.message({ from = "user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b", type = "chat" }) :tag("body"):text("Hello Juliet, are you there?"):up() :tag("delay", { xmlns = "urn:xmpp:delay", from = "localhost", stamp = "{scansion:any}" }):up(); --[[ <message to="juliet@localhost" type="chat" from="user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b"> <body>Hello Juliet, are you there?</body> <stanza-id xmlns="urn:xmpp:sid:0" by="user@localhost" id="eb9eaf13-384e-47d1-a6c0-303ef4f1ac70"/> <stanza-id xmlns="urn:xmpp:sid:0" by="juliet@localhost" id="f6c6f61d-0b15-46d6-9f69-bca1640401d1"/> <delay xmlns="urn:xmpp:delay" stamp="2017-05-07T09:46:21Z" from="localhost"/> </message> ]] local s02 = st.message({ to = "juliet@localhost", from = "user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b", type = "chat" }) :tag("body"):text("Hello Juliet, are you there?"):up() :tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = "user@localhost", id = "eb9eaf13-384e-47d1-a6c0-303ef4f1ac70" }):up() :tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = "juliet@localhost", id = "f6c6f61d-0b15-46d6-9f69-bca1640401d1" }):up() :tag("delay", { xmlns = "urn:xmpp:delay", stamp = "2017-05-07T09:46:21Z", from = "localhost" }):up(); yes(s01, s02); end); it("should match unordered children at the top level", function () yes("<foo><one/><two/></foo>", "<foo><two/><one/></foo>"); end); it("should not match unordered children", function () no("<foo><one><a/><b/></one></foo>", "<foo><one><b/><a/></one></foo>"); end); it("should be possible to disable ordered matching", function () yes("<foo><one scansion:strict='no'><a/><b/></one></foo>", "<foo><one><b/><a/></one></foo>"); end); it("should match when there are many children", function () yes([[<s> <x> <a/> <b/> <c/> </x> </s>]] , [[<s> <x> <a/> <b/> <c/> </x> </s>]]); end); end);