Thu, 06 Sep 2018 18:26:01 +0100
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
93 | 1 | local stanzacmp = require "scansion.stanzacmp"; |
2 | local verse = require "verse"; | |
3 | local st = require "util.stanza"; | |
98
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
4 | local xml = require "scansion.xml"; |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
5 | |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
6 | local function parse(...) |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
7 | local n = select("#", ...); |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
8 | local out = {}; |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
9 | for i = 1, n do |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
10 | local v = (select(i, ...)); |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
11 | if type(v) == "string" then |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
12 | out[i] = xml.parse(v); |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
13 | else |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
14 | out[i] = v; |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
15 | end |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
16 | end |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
17 | return table.unpack(out, 1, n); |
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
18 | end |
93 | 19 | |
20 | local function yes(s1, s2) | |
98
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
21 | s1, s2 = parse(s1, s2); |
93 | 22 | if not stanzacmp.stanzas_match(s1, s2) then |
23 | print("s1", s1) | |
24 | print("s2", s2) | |
25 | print("literal", tostring(s1) == tostring(s2)); | |
26 | assert(false, "No match, but they should."); | |
27 | end | |
28 | end | |
29 | ||
30 | local function no(s1, s2) | |
98
88a3e03f4b9f
spec/stanzacmp: Allow passing XML strings to yes()/no() to avoid manual stanza building
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
31 | s1, s2 = parse(s1, s2); |
93 | 32 | if stanzacmp.stanzas_match(s1, s2) then |
33 | print("s1", s1) | |
34 | print("s2", s2) | |
35 | assert(false, "Match, but they should not."); | |
36 | end | |
37 | end | |
38 | ||
39 | describe("stanzacmp", function () | |
40 | it("should work", function () | |
41 | local s1 = st.message({ to = "foo", from = "bar"}); | |
42 | local s2 = st.message({ to = "foo", from = "bar"}); | |
43 | ||
44 | yes(s1, s2); | |
45 | yes(s2, s1); | |
46 | ||
47 | s1.attr.to = nil; | |
48 | ||
49 | yes(s1, s2); | |
50 | no(s2, s1); | |
51 | ||
52 | s1.attr.to = "foo"; | |
53 | yes(s1, s2); | |
54 | ||
55 | s2:tag("blah", { xmlns = "foobar" }); | |
56 | ||
57 | yes(s1, s2); | |
58 | no(s2, s1); | |
59 | ||
60 | s1:tag("blah", { xmlns = "foobar" }); | |
61 | yes(s1, s2); | |
62 | yes(s2, s1); | |
63 | ||
64 | s1:tag("a"); | |
65 | no(s1, s2); | |
66 | s2:tag("a"); | |
67 | yes(s1, s2); | |
68 | s2:up():up(); | |
69 | s2:tag("extension", { xmlns = "blah" }); | |
70 | yes(s1, s2); | |
71 | s2:tag("b"); | |
72 | yes(s1, s2); | |
73 | s1:up():up(); | |
74 | s1:tag("extension", { xmlns = "blah" }); | |
75 | no(s1, s2); | |
76 | s1:tag("b"); | |
77 | yes(s1, s2); | |
78 | s1:tag("c", { n = "1" }); | |
79 | no(s1, s2); | |
80 | end); | |
95
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
81 | it("should allow matching any attribute value with {scansion:any}", function () |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
82 | --[[ |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
83 | <message from="user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b" type="chat"> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
84 | <body>Hello Juliet, are you there?</body> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
85 | <delay xmlns="urn:xmpp:delay" from="localhost"/> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
86 | </message> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
87 | ]] |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
88 | |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
89 | local s01 = st.message({ from = "user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b", type = "chat" }) |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
90 | :tag("body"):text("Hello Juliet, are you there?"):up() |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
91 | :tag("delay", { xmlns = "urn:xmpp:delay", from = "localhost", stamp = "{scansion:any}" }):up(); |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
92 | |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
93 | --[[ |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
94 | <message to="juliet@localhost" type="chat" from="user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b"> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
95 | <body>Hello Juliet, are you there?</body> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
96 | <stanza-id xmlns="urn:xmpp:sid:0" by="user@localhost" id="eb9eaf13-384e-47d1-a6c0-303ef4f1ac70"/> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
97 | <stanza-id xmlns="urn:xmpp:sid:0" by="juliet@localhost" id="f6c6f61d-0b15-46d6-9f69-bca1640401d1"/> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
98 | <delay xmlns="urn:xmpp:delay" stamp="2017-05-07T09:46:21Z" from="localhost"/> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
99 | </message> |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
100 | ]] |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
101 | |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
102 | local s02 = st.message({ to = "juliet@localhost", from = "user@localhost/71652fc8-93cb-40c7-8220-47ce80cc417b", type = "chat" }) |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
103 | :tag("body"):text("Hello Juliet, are you there?"):up() |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
104 | :tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = "user@localhost", id = "eb9eaf13-384e-47d1-a6c0-303ef4f1ac70" }):up() |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
105 | :tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = "juliet@localhost", id = "f6c6f61d-0b15-46d6-9f69-bca1640401d1" }):up() |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
106 | :tag("delay", { xmlns = "urn:xmpp:delay", stamp = "2017-05-07T09:46:21Z", from = "localhost" }):up(); |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
107 | |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
108 | |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
109 | |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
110 | yes(s01, s02); |
a1e7ad9336db
tests: Add test for scansion:any
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
111 | end); |
99
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
112 | |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
113 | it("should match unordered children at the top level", function () |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
114 | yes("<foo><one/><two/></foo>", "<foo><two/><one/></foo>"); |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
115 | end); |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
116 | |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
117 | it("should not match unordered children", function () |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
118 | no("<foo><one><a/><b/></one></foo>", "<foo><one><b/><a/></one></foo>"); |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
119 | end); |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
120 | |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
121 | it("should be possible to disable ordered matching", function () |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
122 | yes("<foo><one scansion:strict='no'><a/><b/></one></foo>", "<foo><one><b/><a/></one></foo>"); |
dc56d434e406
stanzacmp+tests: Add support for scansion:strict to allow override of default behaviour
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
123 | end); |
93 | 124 | end); |