spec/core_moduleapi_spec.lua
changeset 8239 4878e4159e12
child 8565 a6188f5d5bb5
equal deleted inserted replaced
8238:7d9a2c200736 8239:4878e4159e12
       
     1 
       
     2 package.loaded["core.configmanager"] = {};
       
     3 package.loaded["core.statsmanager"] = {};
       
     4 package.loaded["net.server"] = {};
       
     5 
       
     6 local set = require "util.set";
       
     7 
       
     8 _G.prosody = { hosts = {}, core_post_stanza = true };
       
     9 
       
    10 local api = require "core.moduleapi";
       
    11 
       
    12 local module = setmetatable({}, {__index = api});
       
    13 local opt = nil;
       
    14 function module:log() end
       
    15 function module:get_option(name)
       
    16 	if name == "opt" then
       
    17 		return opt;
       
    18 	else
       
    19 		return nil;
       
    20 	end
       
    21 end
       
    22 
       
    23 function test_option_value(value, returns)
       
    24 	opt = value;
       
    25 	assert(module:get_option_number("opt") == returns.number, "number doesn't match");
       
    26 	assert(module:get_option_string("opt") == returns.string, "string doesn't match");
       
    27 	assert(module:get_option_boolean("opt") == returns.boolean, "boolean doesn't match");
       
    28 
       
    29 	if type(returns.array) == "table" then
       
    30 		local target_array, returned_array = returns.array, module:get_option_array("opt");
       
    31 		assert(#target_array == #returned_array, "array length doesn't match");
       
    32 		for i=1,#target_array do
       
    33 			assert(target_array[i] == returned_array[i], "array item doesn't match");
       
    34 		end
       
    35 	else
       
    36 		assert(module:get_option_array("opt") == returns.array, "array is returned (not nil)");
       
    37 	end
       
    38 
       
    39 	if type(returns.set) == "table" then
       
    40 		local target_items, returned_items = set.new(returns.set), module:get_option_set("opt");
       
    41 		assert(target_items == returned_items, "set doesn't match");
       
    42 	else
       
    43 		assert(module:get_option_set("opt") == returns.set, "set is returned (not nil)");
       
    44 	end
       
    45 end
       
    46 
       
    47 describe("core.moduleapi", function()
       
    48 	describe("#get_option_*()", function()
       
    49 		it("should handle missing options", function()
       
    50 			test_option_value(nil, {});
       
    51 		end);
       
    52 
       
    53 		it("should return correctly handle boolean options", function()
       
    54 			test_option_value(true, { boolean = true, string = "true", array = {true}, set = {true} });
       
    55 			test_option_value(false, { boolean = false, string = "false", array = {false}, set = {false} });
       
    56 			test_option_value("true", { boolean = true, string = "true", array = {"true"}, set = {"true"} });
       
    57 			test_option_value("false", { boolean = false, string = "false", array = {"false"}, set = {"false"} });
       
    58 			test_option_value(1, { boolean = true, string = "1", array = {1}, set = {1}, number = 1 });
       
    59 			test_option_value(0, { boolean = false, string = "0", array = {0}, set = {0}, number = 0 });
       
    60 		end);
       
    61 
       
    62 		it("should return handle strings", function()
       
    63 			test_option_value("hello world", { string = "hello world", array = {"hello world"}, set = {"hello world"} });
       
    64 		end);
       
    65 
       
    66 		it("should return handle numbers", function()
       
    67 			test_option_value(1234, { string = "1234", number = 1234, array = {1234}, set = {1234} });
       
    68 		end);
       
    69 
       
    70 		it("should return handle arrays", function()
       
    71 			test_option_value({1, 2, 3}, { boolean = true, string = "1", number = 1, array = {1, 2, 3}, set = {1, 2, 3} });
       
    72 			test_option_value({1, 2, 3, 3, 4}, {boolean = true, string = "1", number = 1, array = {1, 2, 3, 3, 4}, set = {1, 2, 3, 4} });
       
    73 			test_option_value({0, 1, 2, 3}, { boolean = false, string = "0", number = 0, array = {0, 1, 2, 3}, set = {0, 1, 2, 3} });	
       
    74 		end);
       
    75 	end)
       
    76 end)