Merge 0.10->trunk
[prosody.git] / tests / modulemanager_option_conversion.lua
1 package.path = "../?.lua;"..package.path;
2
3 local api = require "core.modulemanager".api;
4
5 local module = setmetatable({}, {__index = api});
6 local opt = nil;
7 function module:log() end
8 function module:get_option(name)
9         if name == "opt" then
10                 return opt;
11         else
12                 return nil;
13         end
14 end
15
16 function test_value(value, returns)
17         opt = value;
18         assert(module:get_option_number("opt") == returns.number, "number doesn't match");
19         assert(module:get_option_string("opt") == returns.string, "string doesn't match");
20         assert(module:get_option_boolean("opt") == returns.boolean, "boolean doesn't match");
21
22         if type(returns.array) == "table" then
23                 local target_array, returned_array = returns.array, module:get_option_array("opt");
24                 assert(#target_array == #returned_array, "array length doesn't match");
25                 for i=1,#target_array do
26                         assert(target_array[i] == returned_array[i], "array item doesn't match");
27                 end
28         else
29                 assert(module:get_option_array("opt") == returns.array, "array is returned (not nil)");
30         end
31
32         if type(returns.set) == "table" then
33                 local target_items, returned_items = set.new(returns.set), module:get_option_set("opt");
34                 assert(target_items == returned_items, "set doesn't match");
35         else
36                 assert(module:get_option_set("opt") == returns.set, "set is returned (not nil)");
37         end
38 end
39
40 test_value(nil, {});
41
42 test_value(true, { boolean = true, string = "true", array = {true}, set = {true} });
43 test_value(false, { boolean = false, string = "false", array = {false}, set = {false} });
44 test_value("true", { boolean = true, string = "true", array = {"true"}, set = {"true"} });
45 test_value("false", { boolean = false, string = "false", array = {"false"}, set = {"false"} });
46 test_value(1, { boolean = true, string = "1", array = {1}, set = {1}, number = 1 });
47 test_value(0, { boolean = false, string = "0", array = {0}, set = {0}, number = 0 });
48
49 test_value("hello world", { string = "hello world", array = {"hello world"}, set = {"hello world"} });
50 test_value(1234, { string = "1234", number = 1234, array = {1234}, set = {1234} });
51
52 test_value({1, 2, 3}, { boolean = true, string = "1", number = 1, array = {1, 2, 3}, set = {1, 2, 3} });
53 test_value({1, 2, 3, 3, 4}, {boolean = true, string = "1", number = 1, array = {1, 2, 3, 3, 4}, set = {1, 2, 3, 4} });
54 test_value({0, 1, 2, 3}, { boolean = false, string = "0", number = 0, array = {0, 1, 2, 3}, set = {0, 1, 2, 3} });
55