From 2cb6df163319cdef371607ad527558e09d707129 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 15 Dec 2013 12:30:03 +0100
Subject: Sluift: Fix create_submission dropping values from multi fields

Form->Lua conversion no longer relies on the type to decide how to
convert values, and blindly converts value arrays.
This also fixes issues if Sluift would be used on the receiving
side of a form.

Change-Id: I92c346795e1c67c74ccf542d16f79fe8010508f7

diff --git a/Sluift/ElementConvertors/FormConvertor.cpp b/Sluift/ElementConvertors/FormConvertor.cpp
index 9791bfa..26a7d29 100644
--- a/Sluift/ElementConvertors/FormConvertor.cpp
+++ b/Sluift/ElementConvertors/FormConvertor.cpp
@@ -90,17 +90,15 @@ namespace {
 		if (!field->getDescription().empty()) {
 			luaField["description"] = Lua::valueRef(field->getDescription());
 		}
-
-		if (field->getType() == FormField::ListMultiType || field->getType() == FormField::JIDMultiType || field->getType() == FormField::TextMultiType) {
-			luaField["value"] = Lua::valueRef(Lua::Array(field->getValues().begin(), field->getValues().end()));
-		}
-		else if (field->getType() == FormField::BooleanType) {
+		if (field->getType() == FormField::BooleanType) {
 			luaField["value"] = Lua::boolRef(field->getBoolValue());
 		}
-		else if (!field->getValues().empty()) {
+		else if (field->getValues().size() > 1) {
+			luaField["value"] = Lua::valueRef(Lua::Array(field->getValues().begin(), field->getValues().end()));
+		}
+		else if (field->getValues().size() == 1) {
 			luaField["value"] = Lua::valueRef(field->getValues()[0]);
 		}
-
 		if (!field->getOptions().empty()) {
 			Lua::Array options;
 			foreach(const FormField::Option& option, field->getOptions()) {
diff --git a/Sluift/Tests/FormTest.lua b/Sluift/Tests/FormTest.lua
index b2ec111..3b311da 100644
--- a/Sluift/Tests/FormTest.lua
+++ b/Sluift/Tests/FormTest.lua
@@ -13,7 +13,10 @@ example_form = [[
 	</field>
 	<field type='fixed'><value>Section 1: Bot Info</value></field>
 	<field type='text-single' label='The name of your bot' var='botname'/>
-	<field type='text-multi' label='Helpful description of your bot' var='description'/>
+	<field type='text-multi' label='Helpful description of your bot' var='description'>
+		<value>This is</value>
+		<value>my bot</value>
+	</field>
 	<field type='boolean' label='Public bot?' var='public'>
 		<required/>
 	</field>
@@ -71,9 +74,19 @@ assert(form['FORM_TYPE']['value'] == 'jabber:bot')
 -- Test response form
 submission = form:create_submission()
 assert(#(submission.fields) == 8)
+description_submit_value = submission['description']['value']
+assert(type(description_submit_value) == 'table')
+assert(description_submit_value[1] == 'This is')
+assert(description_submit_value[2] == 'my bot')
+
 submission['description'] = 'my description'
 assert(submission['description']['value'] == 'my description')
 submission['type'] = 'cancel'
-assert(#(submission.fields) == 8)
+
+-- Test text-multi field
+text_multi_field = form['fields'][4]
+assert(text_multi_field['name'] == 'description')
+assert(type(text_multi_field['value']) == 'table')
+
 
 --print(sluift.to_xml({type = 'form', data = form}))
-- 
cgit v0.10.2-6-g49f6