From 65f4b853a0d32f8b390f526d2044ba66fb20a603 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 18 Mar 2018 11:32:00 +0000 Subject: util.stanza: Switch from asserts to if's, improve performance, errors and tests --- spec/util_stanza_spec.lua | 63 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 26 deletions(-) (limited to 'spec') diff --git a/spec/util_stanza_spec.lua b/spec/util_stanza_spec.lua index f7759385..cd5a0dc3 100644 --- a/spec/util_stanza_spec.lua +++ b/spec/util_stanza_spec.lua @@ -165,38 +165,49 @@ describe("util.stanza", function() end); end); - describe("#invalid", function () - it("name should be rejected", function () - assert.has_error(function () - st.stanza(1234); + describe("should reject #invalid", function () + local invalid_names = { + ["empty string"] = "", ["characters"] = "<>"; + } + local invalid_data = { + ["number"] = 1234, ["table"] = {}; + ["utf8"] = string.char(0xF4, 0x90, 0x80, 0x80); + }; + + for value_type, value in pairs(invalid_names) do + it(value_type.." in tag names", function () + assert.error_matches(function () + st.stanza(value); + end, value_type); end); - assert.has_error(function () - st.stanza({}); + it(value_type.." in attribute names", function () + assert.error_matches(function () + st.stanza("valid", { [value] = "valid" }); + end, value_type); end); - assert.has_error(function () - st.stanza(); + end + for value_type, value in pairs(invalid_data) do + it(value_type.." in tag names", function () + assert.error_matches(function () + st.stanza(value); + end, value_type); end); - assert.has_error(function () - st.stanza(""); + it(value_type.." in attribute names", function () + assert.error_matches(function () + st.stanza("valid", { [value] = "valid" }); + end, value_type); end); - assert.has_error(function () - st.stanza(string.char(0xC0)); + it(value_type.." in attribute values", function () + assert.error_matches(function () + st.stanza("valid", { valid = value }); + end, value_type); end); - assert.has_error(function () - st.stanza(string.char(0xF4, 0x90, 0x80, 0x80)); + it(value_type.." in text node", function () + assert.error_matches(function () + st.stanza("valid"):text(value); + end, value_type); end); - assert.has_error(function () - st.stanza("<>"); - end); - assert.has_error(function () - st.stanza("&"); - end); - end); - it("UTF-8 should be rejected", function () - assert.has_error(function () - st.stanza("tag"):text("hello "..string.char(0xF4, 0x90, 0x80, 0x80).." world"); - end); - end); + end end); describe("#is_stanza", function () -- cgit v1.2.3