diff options
Diffstat (limited to 'spec/util_dataforms_spec.lua')
-rw-r--r-- | spec/util_dataforms_spec.lua | 123 |
1 files changed, 104 insertions, 19 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua index 89759035..e0a9bb9b 100644 --- a/spec/util_dataforms_spec.lua +++ b/spec/util_dataforms_spec.lua @@ -106,11 +106,26 @@ describe("util.dataforms", function () name = "text-single-field", value = "text-single-value", }, + { + -- XEP-0221 + -- TODO Validate the XML produced by this. + type = "text-single", + label = "text-single-with-media-label", + name = "text-single-with-media-field", + media = { + height = 24, + width = 32, + { + type = "image/png", + uri = "data:", + }, + }, + }, }); xform = some_form:form(); end); - it("works", function () + it("XML serialization looks like it should", function () assert.truthy(xform); assert.truthy(st.is_stanza(xform)); assert.equal("x", xform.name); @@ -316,7 +331,7 @@ describe("util.dataforms", function () end); describe(":data", function () - it("works", function () + it("returns something", function () assert.truthy(some_form:data(xform)); end); end); @@ -402,25 +417,95 @@ describe("util.dataforms", function () end); end); - describe("validation", function () - local f = dataforms.new { - { - name = "number", - type = "text-single", - datatype = "xs:integer", - }, - }; - - it("works", function () - local d = f:data(f:form({number = 1})); - assert.equal(1, d.number); + describe("number handling", function() + it("handles numbers as booleans", function() + local f = dataforms.new { { name = "boolean"; type = "boolean" } }; + local x = f:form({ boolean = 0 }); + assert.equal("0", x:find "field/value#"); + x = f:form({ boolean = 1 }); + assert.equal("1", x:find "field/value#"); end); + end) + + describe("datatype validation", function () + describe("integer", function () + + local f = dataforms.new { + { + name = "number", + type = "text-single", + datatype = "xs:integer", + range_min = -10, + range_max = 10, + }, + }; + + it("roundtrip works", function () + local d = f:data(f:form({number = 1})); + assert.equal(1, d.number); + end); + + it("error handling works", function () + local d,e = f:data(f:form({number = "nan"})); + assert.not_equal(1, d.number); + assert.table(e); + assert.string(e.number); + end); + + it("bounds-cheking work works", function () + local d,e = f:data(f:form({number = 100})); + assert.not_equal(100, d.number); + assert.table(e); + assert.string(e.number); + end); + + it("serializes largeer ints okay", function () + local x = f:form{number=1125899906842624} + assert.equal("1125899906842624", x:find("field/value#")) + end); + + end) + + describe("datetime", function () + local f = dataforms.new { { name = "when"; type = "text-single"; datatype = "xs:dateTime" } } + + it("works", function () + local x = f:form({ when = 1219439340 }); + assert.equal("2008-08-22T21:09:00Z", x:find("field/value#")) + local d, e = f:data(x); + assert.is_nil(e); + assert.same({ when = 1219439340 }, d); + end); + + end) + + end); + describe("media element", function () + it("produced media element correctly", function () + local f; + for field in xform:childtags("field") do + if field.attr.var == "text-single-with-media-field" then + f = field; + break; + end + end - it("works", function () - local d,e = f:data(f:form({number = "nan"})); - assert.not_equal(1, d.number); - assert.table(e); - assert.string(e.number); + assert.truthy(st.is_stanza(f)); + assert.equal("text-single-with-media-field", f.attr.var); + assert.equal("text-single", f.attr.type); + assert.equal("text-single-with-media-label", f.attr.label); + assert.equal(0, iter.count(f:childtags("value"))); + + local m = f:get_child("media", "urn:xmpp:media-element"); + assert.truthy(st.is_stanza(m)); + assert.equal("24", m.attr.height); + assert.equal("32", m.attr.width); + assert.equal(1, iter.count(m:childtags("uri"))); + + local u = m:get_child("uri"); + assert.truthy(st.is_stanza(u)); + assert.equal("image/png", u.attr.type); + assert.equal("data:", u:get_text()); end); end); end); |