scansion.xml: Add 'scansion' namespace, and tests

Thu, 06 Sep 2018 18:11:16 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 06 Sep 2018 18:11:16 +0100
changeset 97
bc4c2d9765f1
parent 96
de4dcef0b0f0
child 98
88a3e03f4b9f

scansion.xml: Add 'scansion' namespace, and tests

scansion/xml.lua file | annotate | diff | comparison | revisions
spec/xml_spec.lua file | annotate | diff | comparison | revisions
--- a/scansion/xml.lua	Tue Jul 10 11:15:04 2018 +0100
+++ b/scansion/xml.lua	Thu Sep 06 18:11:16 2018 +0100
@@ -4,9 +4,12 @@
 
 local _ENV = nil;
 
+local xmlns_scansion = "http://prosody.im/scansion";
+
 local parse_xml = (function()
 	local ns_prefixes = {
 		["http://www.w3.org/XML/1998/namespace"] = "xml";
+		[xmlns_scansion] = "scansion";
 	};
 	local ns_separator = "\1";
 	local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
@@ -43,11 +46,11 @@
 			stanza:up();
 		end
 		local parser = lxp.new(handler, "\1");
-		local ok, err, line, col = parser:parse(xml);
+		local ok, err, line, col = parser:parse("<scansion xmlns:scansion='"..xmlns_scansion.."'>"..xml.."</scansion>");
 		if ok then ok, err, line, col = parser:parse(); end
 		--parser:close();
 		if ok then
-			return stanza.tags[1];
+			return stanza.tags[1].tags[1];
 		else
 			return ok, err.." (line "..line..", col "..col..")";
 		end
@@ -56,4 +59,5 @@
 
 return {
 	parse = parse_xml;
+	xmlns_scansion = xmlns_scansion;
 };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/xml_spec.lua	Thu Sep 06 18:11:16 2018 +0100
@@ -0,0 +1,55 @@
+local stanzacmp = require "scansion.stanzacmp";
+local verse = 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("scansion.xml", function ()
+	it("should work", function ()
+		local s = parse("<test foo='bar'/>");
+		assert.equal("test", s.name);
+		assert.equal("bar", s.attr.foo);
+		yes(s, st.stanza("test", { foo = "bar" }));
+	end);
+	it("should add a scansion namespace", function ()
+		local s = parse("<scansion:test/>");
+		assert.equal("test", s.name);
+		assert.equal(xml.xmlns_scansion, s.attr.xmlns);
+
+		local s = parse("<test scansion:test='foo'/>");
+		assert.equal("test", s.name);
+		assert.equal("foo", s.attr["scansion:test"]);
+	end);
+end);

mercurial