Software / code / prosody
Annotate
spec/util_dataforms_spec.lua @ 9046:7cdc718312c8
util.dataforms: Include a fixed field in test
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 15 Jul 2018 22:35:42 +0200 |
| parent | 8864:cf2f66b233d1 |
| child | 9083:5d3639e415bd |
| rev | line source |
|---|---|
| 8862 | 1 local dataforms = require "util.dataforms"; |
| 2 local st = require "util.stanza"; | |
| 3 local jid = require "util.jid"; | |
| 4 local iter = require "util.iterators"; | |
| 5 | |
| 6 describe("util.dataforms", function () | |
| 7 local some_form, xform; | |
| 8 setup(function () | |
| 9 some_form = dataforms.new({ | |
| 10 title = "form-title", | |
| 11 instructions = "form-instructions", | |
| 12 { | |
| 13 type = "hidden", | |
| 14 name = "FORM_TYPE", | |
| 15 value = "xmpp:prosody.im/spec/util.dataforms#1", | |
| 16 }; | |
| 17 { | |
|
9046
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
18 type = "fixed"; |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
19 value = "Fixed field"; |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
20 }, |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
21 { |
| 8862 | 22 type = "boolean", |
| 23 label = "boolean-label", | |
| 24 name = "boolean-field", | |
| 25 value = true, | |
| 26 }, | |
| 27 { | |
| 28 type = "fixed", | |
| 29 label = "fixed-label", | |
| 30 name = "fixed-field", | |
| 31 value = "fixed-value", | |
| 32 }, | |
| 33 { | |
| 34 type = "hidden", | |
| 35 label = "hidden-label", | |
| 36 name = "hidden-field", | |
| 37 value = "hidden-value", | |
| 38 }, | |
| 39 { | |
| 40 type = "jid-multi", | |
| 41 label = "jid-multi-label", | |
| 42 name = "jid-multi-field", | |
| 43 value = { | |
| 44 "jid@multi/value#1", | |
| 45 "jid@multi/value#2", | |
| 46 }, | |
| 47 }, | |
| 48 { | |
| 49 type = "jid-single", | |
| 50 label = "jid-single-label", | |
| 51 name = "jid-single-field", | |
| 52 value = "jid@single/value", | |
| 53 }, | |
| 54 { | |
| 55 type = "list-multi", | |
| 56 label = "list-multi-label", | |
| 57 name = "list-multi-field", | |
| 58 value = { | |
| 59 "list-multi-option-value#1", | |
| 60 "list-multi-option-value#3", | |
| 61 }, | |
| 62 options = { | |
| 63 { | |
| 64 label = "list-multi-option-label#1", | |
| 65 value = "list-multi-option-value#1", | |
| 66 default = true, | |
| 67 }, | |
| 68 { | |
| 69 label = "list-multi-option-label#2", | |
| 70 value = "list-multi-option-value#2", | |
| 71 default = false, | |
| 72 }, | |
| 73 { | |
| 74 label = "list-multi-option-label#3", | |
| 75 value = "list-multi-option-value#3", | |
| 76 default = true, | |
| 77 }, | |
| 78 } | |
| 79 }, | |
| 80 { | |
| 81 type = "list-single", | |
| 82 label = "list-single-label", | |
| 83 name = "list-single-field", | |
| 84 value = "list-single-value", | |
| 85 options = { | |
| 86 "list-single-value", | |
| 87 "list-single-value#2", | |
| 88 "list-single-value#3", | |
| 89 } | |
| 90 }, | |
| 91 { | |
| 92 type = "text-multi", | |
| 93 label = "text-multi-label", | |
| 94 name = "text-multi-field", | |
| 95 value = "text\nmulti\nvalue", | |
| 96 }, | |
| 97 { | |
| 98 type = "text-private", | |
| 99 label = "text-private-label", | |
| 100 name = "text-private-field", | |
| 101 value = "text-private-value", | |
| 102 }, | |
| 103 { | |
| 104 type = "text-single", | |
| 105 label = "text-single-label", | |
| 106 name = "text-single-field", | |
| 107 value = "text-single-value", | |
| 108 }, | |
| 109 }); | |
| 110 xform = some_form:form(); | |
| 111 end); | |
| 112 | |
| 113 it("works", function () | |
| 114 assert.truthy(xform); | |
| 115 assert.truthy(st.is_stanza(xform)); | |
| 116 assert.equal("x", xform.name); | |
| 117 assert.equal("jabber:x:data", xform.attr.xmlns); | |
| 118 assert.equal("FORM_TYPE", xform:find("field@var")); | |
| 119 assert.equal("xmpp:prosody.im/spec/util.dataforms#1", xform:find("field/value#")); | |
| 120 local allowed_direct_children = { | |
| 121 title = true, | |
| 122 instructions = true, | |
| 123 field = true, | |
| 124 } | |
| 125 for tag in xform:childtags() do | |
| 126 assert.truthy(allowed_direct_children[tag.name], "unknown direct child"); | |
| 127 end | |
| 128 end); | |
| 129 | |
| 130 it("produced boolean field correctly", function () | |
| 131 local f; | |
| 132 for field in xform:childtags("field") do | |
| 133 if field.attr.var == "boolean-field" then | |
| 134 f = field; | |
| 135 break; | |
| 136 end | |
| 137 end | |
| 138 | |
| 139 assert.truthy(st.is_stanza(f)); | |
| 140 assert.equal("boolean-field", f.attr.var); | |
| 141 assert.equal("boolean", f.attr.type); | |
| 142 assert.equal("boolean-label", f.attr.label); | |
| 143 assert.equal(1, iter.count(f:childtags("value"))); | |
| 144 local val = f:get_child_text("value"); | |
| 145 assert.truthy(val == "true" or val == "1"); | |
| 146 end); | |
| 147 | |
| 148 it("produced fixed field correctly", function () | |
| 149 local f; | |
| 150 for field in xform:childtags("field") do | |
| 151 if field.attr.var == "fixed-field" then | |
| 152 f = field; | |
| 153 break; | |
| 154 end | |
| 155 end | |
| 156 | |
| 157 assert.truthy(st.is_stanza(f)); | |
| 158 assert.equal("fixed-field", f.attr.var); | |
| 159 assert.equal("fixed", f.attr.type); | |
| 160 assert.equal("fixed-label", f.attr.label); | |
| 161 assert.equal(1, iter.count(f:childtags("value"))); | |
| 162 assert.equal("fixed-value", f:get_child_text("value")); | |
| 163 end); | |
| 164 | |
| 165 it("produced hidden field correctly", function () | |
| 166 local f; | |
| 167 for field in xform:childtags("field") do | |
| 168 if field.attr.var == "hidden-field" then | |
| 169 f = field; | |
| 170 break; | |
| 171 end | |
| 172 end | |
| 173 | |
| 174 assert.truthy(st.is_stanza(f)); | |
| 175 assert.equal("hidden-field", f.attr.var); | |
| 176 assert.equal("hidden", f.attr.type); | |
| 177 assert.equal("hidden-label", f.attr.label); | |
| 178 assert.equal(1, iter.count(f:childtags("value"))); | |
| 179 assert.equal("hidden-value", f:get_child_text("value")); | |
| 180 end); | |
| 181 | |
| 182 it("produced jid-multi field correctly", function () | |
| 183 local f; | |
| 184 for field in xform:childtags("field") do | |
| 185 if field.attr.var == "jid-multi-field" then | |
| 186 f = field; | |
| 187 break; | |
| 188 end | |
| 189 end | |
| 190 | |
| 191 assert.truthy(st.is_stanza(f)); | |
| 192 assert.equal("jid-multi-field", f.attr.var); | |
| 193 assert.equal("jid-multi", f.attr.type); | |
| 194 assert.equal("jid-multi-label", f.attr.label); | |
| 195 assert.equal(2, iter.count(f:childtags("value"))); | |
| 196 | |
| 197 local i = 0; | |
| 198 for value in f:childtags("value") do | |
| 199 i = i + 1; | |
| 200 assert.equal(("jid@multi/value#%d"):format(i), value:get_text()); | |
| 201 end | |
| 202 end); | |
| 203 | |
| 204 it("produced jid-single field correctly", function () | |
| 205 local f; | |
| 206 for field in xform:childtags("field") do | |
| 207 if field.attr.var == "jid-single-field" then | |
| 208 f = field; | |
| 209 break; | |
| 210 end | |
| 211 end | |
| 212 | |
| 213 assert.truthy(st.is_stanza(f)); | |
| 214 assert.equal("jid-single-field", f.attr.var); | |
| 215 assert.equal("jid-single", f.attr.type); | |
| 216 assert.equal("jid-single-label", f.attr.label); | |
| 217 assert.equal(1, iter.count(f:childtags("value"))); | |
| 218 assert.equal("jid@single/value", f:get_child_text("value")); | |
| 219 assert.truthy(jid.prep(f:get_child_text("value"))); | |
| 220 end); | |
| 221 | |
| 222 it("produced list-multi field correctly", function () | |
| 223 local f; | |
| 224 for field in xform:childtags("field") do | |
| 225 if field.attr.var == "list-multi-field" then | |
| 226 f = field; | |
| 227 break; | |
| 228 end | |
| 229 end | |
| 230 | |
| 231 assert.truthy(st.is_stanza(f)); | |
| 232 assert.equal("list-multi-field", f.attr.var); | |
| 233 assert.equal("list-multi", f.attr.type); | |
| 234 assert.equal("list-multi-label", f.attr.label); | |
| 235 assert.equal(2, iter.count(f:childtags("value"))); | |
| 236 assert.equal("list-multi-option-value#1", f:get_child_text("value")); | |
| 237 assert.equal(3, iter.count(f:childtags("option"))); | |
| 238 end); | |
| 239 | |
| 240 it("produced list-single field correctly", function () | |
| 241 local f; | |
| 242 for field in xform:childtags("field") do | |
| 243 if field.attr.var == "list-single-field" then | |
| 244 f = field; | |
| 245 break; | |
| 246 end | |
| 247 end | |
| 248 | |
| 249 assert.truthy(st.is_stanza(f)); | |
| 250 assert.equal("list-single-field", f.attr.var); | |
| 251 assert.equal("list-single", f.attr.type); | |
| 252 assert.equal("list-single-label", f.attr.label); | |
| 253 assert.equal(1, iter.count(f:childtags("value"))); | |
| 254 assert.equal("list-single-value", f:get_child_text("value")); | |
| 255 assert.equal(3, iter.count(f:childtags("option"))); | |
| 256 end); | |
| 257 | |
| 258 it("produced text-multi field correctly", function () | |
| 259 local f; | |
| 260 for field in xform:childtags("field") do | |
| 261 if field.attr.var == "text-multi-field" then | |
| 262 f = field; | |
| 263 break; | |
| 264 end | |
| 265 end | |
| 266 | |
| 267 assert.truthy(st.is_stanza(f)); | |
| 268 assert.equal("text-multi-field", f.attr.var); | |
| 269 assert.equal("text-multi", f.attr.type); | |
| 270 assert.equal("text-multi-label", f.attr.label); | |
| 271 assert.equal(3, iter.count(f:childtags("value"))); | |
| 272 end); | |
| 273 | |
| 274 it("produced text-private field correctly", function () | |
| 275 local f; | |
| 276 for field in xform:childtags("field") do | |
| 277 if field.attr.var == "text-private-field" then | |
| 278 f = field; | |
| 279 break; | |
| 280 end | |
| 281 end | |
| 282 | |
| 283 assert.truthy(st.is_stanza(f)); | |
| 284 assert.equal("text-private-field", f.attr.var); | |
| 285 assert.equal("text-private", f.attr.type); | |
| 286 assert.equal("text-private-label", f.attr.label); | |
| 287 assert.equal(1, iter.count(f:childtags("value"))); | |
| 288 assert.equal("text-private-value", f:get_child_text("value")); | |
| 289 end); | |
| 290 | |
| 291 it("produced text-single field correctly", function () | |
| 292 local f; | |
| 293 for field in xform:childtags("field") do | |
| 294 if field.attr.var == "text-single-field" then | |
| 295 f = field; | |
| 296 break; | |
| 297 end | |
| 298 end | |
| 299 | |
| 300 assert.truthy(st.is_stanza(f)); | |
| 301 assert.equal("text-single-field", f.attr.var); | |
| 302 assert.equal("text-single", f.attr.type); | |
| 303 assert.equal("text-single-label", f.attr.label); | |
| 304 assert.equal(1, iter.count(f:childtags("value"))); | |
| 305 assert.equal("text-single-value", f:get_child_text("value")); | |
| 306 end); | |
| 307 | |
|
8864
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
308 describe("get_type()", function () |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
309 it("identifes dataforms", function () |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
310 assert.equal(nil, dataforms.get_type(nil)); |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
311 assert.equal(nil, dataforms.get_type("")); |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
312 assert.equal(nil, dataforms.get_type({})); |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
313 assert.equal(nil, dataforms.get_type(st.stanza("no-a-form"))); |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
314 assert.equal("xmpp:prosody.im/spec/util.dataforms#1", dataforms.get_type(xform)); |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
315 end); |
|
cf2f66b233d1
util.dataforms: Add a simple function for identifying form types
Kim Alvefur <zash@zash.se>
parents:
8862
diff
changeset
|
316 end); |
|
9046
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
317 |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
318 describe(":data", function () |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
319 it("works", function () |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
320 assert.truthy(some_form:data(xform)); |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
321 end); |
|
7cdc718312c8
util.dataforms: Include a fixed field in test
Kim Alvefur <zash@zash.se>
parents:
8864
diff
changeset
|
322 end); |
| 8862 | 323 end); |
| 324 |