Модуль:Util/testcases

Материал из Path of Exile Wiki
Перейти к: навигация, поиск

N7 tests failed.

Name Expected Actual
YesY test_args_from_cargo_maps__cast_valid_fields
YesY test_args_from_cargo_maps__default_function
YesY test_args_from_cargo_maps__default_value
YesY test_args_from_cargo_maps__error_missing_in_fields
YesY test_args_from_cargo_maps__error_missing_in_order
YesY test_args_from_cargo_maps__func
YesY test_args_from_cargo_maps__name
YesY test_args_stats__errors
YesY test_args_stats__min_max
YesY test_args_stats__value
N test_cargo_parse_field__list Lua error -- Модуль:Util/testcases:295: attempt to index field 'cargo' (a nil value)
N test_cargo_parse_field__list_with_parameters Lua error -- Модуль:Util/testcases:324: attempt to index field 'cargo' (a nil value)
N test_cargo_parse_field__paramater_whitespace Lua error -- Модуль:Util/testcases:348: attempt to index field 'cargo' (a nil value)
N test_cargo_parse_field__parameter Lua error -- Модуль:Util/testcases:309: attempt to index field 'cargo' (a nil value)
N test_cargo_parse_field__simple Lua error -- Модуль:Util/testcases:284: attempt to index field 'cargo' (a nil value)
N test_cargo_parse_field__simple_whitespace Lua error -- Модуль:Util/testcases:334: attempt to index field 'cargo' (a nil value)
YesY test_cast_boolean
YesY test_cast_number
YesY test_cast_number__error_invalid_number
YesY test_cast_number__error_max
YesY test_cast_number__error_min
YesY test_cast_number__invalid_with_default
YesY test_html_abbr
N test_html_error <span class="module-error">Ошибка модуля: Test</span> <strong class="error"><span class="module-error">Ошибка модуля: Test</span></strong>
YesY test_misc_add_category
YesY test_misc_get_frame
YesY test_misc_is_frame
YesY test_smw_safeguard
YesY test_string_split_args
YesY test_table_find_in_nested_array__assoc
local util = require('Module:Util') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

-- ----------------------------------------------------------------------------
-- Tests: util.cast 
-- ----------------------------------------------------------------------------

function suite:test_cast_boolean()
    self:assertEquals(true, util.cast.boolean('true'))
    self:assertEquals(true, util.cast.boolean(5))
    self:assertEquals(true, util.cast.boolean(true))
    self:assertEquals(false, util.cast.boolean('false'))
    self:assertEquals(false, util.cast.boolean(0))
    self:assertEquals(false, util.cast.boolean(false))
    self:assertEquals(false, util.cast.boolean(nil))
end

function suite:test_cast_number()
    -- boolean
    self:assertEquals(1, util.cast.number(true))
    self:assertEquals(0, util.cast.number(false))
    -- number
    self:assertEquals(5, 5)
    self:assertEquals(5, 5.0)
    -- string
    self:assertEquals(5, util.cast.number('5'))
    self:assertEquals(5, util.cast.number('5.0'))
    self:assertEquals(5, util.cast.number('5e0'))
end

function suite:test_cast_number__error_invalid_number()
    self:assertFalse(pcall(util.cast.number, 'wrong'))
end

function suite:test_cast_number__error_min()
    self:assertFalse(pcall(util.cast.number, 5, {min=6}))
end

function suite:test_cast_number__error_max()
    self:assertFalse(pcall(util.cast.number, 5, {max=4}))
end

function suite:test_cast_number__invalid_with_default()
    self:assertEquals(5, util.cast.number('wrong', {default=5}))
end

-- ----------------------------------------------------------------------------
-- Tests: util.args
-- ----------------------------------------------------------------------------

function suite:test_args_stats__value()
    local args = {stat1_id='stat1', stat1_value='1'}
    util.args.stats(args, {})
    self:assertEquals(args.stat1_value, 1)
end

function suite:test_args_stats__min_max()
    local args = {stat1_id='stat1', stat1_min='1', stat1_max='2'}
    util.args.stats(args, {})
    self:assertEquals(args.stat1_min, 1)
    self:assertEquals(args.stat1_max, 2)
    self:assertEquals(args.stat1_avg, 1.5)
end

function suite:test_args_stats__errors()
    local args = {stat1_id='stat1', stat1_value='1', stat1_min='1'}
    self:assertFalse(pcall(util.args.stats, args, {}))
    
    args = {stat1_id='stat1', stat1_min='1'}
    self:assertFalse(pcall(util.args.stats, args, {}))
end

-- 
-- Tests: util.args.from_cargo_map
-- 
function suite:test_args_from_cargo_maps__cast_valid_fields()
    local tpl_args = {
        string = 'string',
        integer = '12345',
        float = '12.345',
        boolean_true = 'true',
        boolean_false = 'false',
    }
    
    local tpl_args_expected = {
        string = 'string',
        integer = 12345,
        float = 12.345,
        boolean_true = true,
        boolean_false = false,
    }
    
    util.args.from_cargo_map{
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {'string', 'integer', 'float', 'boolean_true', 'boolean_false'},
            fields = {
                string = {
                    type = 'String',
                    field = 'string',
                },
                integer = {
                    type = 'Integer',
                    field = 'integer',
                },
                float = {
                    type = 'Float',
                    field = 'float',
                },
                boolean_true = {
                    type = 'Boolean',
                    field = 'boolean_true',
                },
                boolean_false = {
                    type = 'Boolean',
                    field = 'boolean_false',
                },
            },
        }
    }
    
    self:assertDeepEquals(tpl_args_expected, tpl_args, 'Casting / parsing of valid fields failed')
end

function suite:test_args_from_cargo_maps__default_value()
    local tpl_args = {
    }
    local tpl_args_expected = {
        default = 5,
    }
    util.args.from_cargo_map{
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {'default'},
            fields = {
                default = {
                    type = 'Integer',
                    field = 'default',
                    default = 5
                },
            },
        }
    }
    
    self:assertDeepEquals(tpl_args_expected, tpl_args, 'Setting default values for nil fields failed')
end

function suite:test_args_from_cargo_maps__default_function()
    local tpl_args = {
    }
    local tpl_args_expected = {
        default = 5,
    }
    util.args.from_cargo_map{
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {'default'},
            fields = {
                default = {
                    type = 'Integer',
                    field = 'default',
                    default = function (tpl_args, frame)
                        return 5
                    end
                },
            },
        }
    }
    
    self:assertDeepEquals(tpl_args_expected, tpl_args, 'Setting default values via function for nil fields failed')
end

function suite:test_args_from_cargo_maps__func()
    local tpl_args = {
        custom = 10
    }
    local tpl_args_expected = {
        custom = 5,
    }
    util.args.from_cargo_map{
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {'custom'},
            fields = {
                custom = {
                    type = 'Integer',
                    field = 'custom',
                    func = function (tpl_args, frame)
                        return tpl_args.custom / 2
                    end
                },
            },
        }
    }
    
    self:assertDeepEquals(tpl_args_expected, tpl_args, 'Parsing via func failed')
end

function suite:test_args_from_cargo_maps__name()
    local tpl_args = {
        different_name = '5'
    }
    local tpl_args_expected = {
        different_name = '5',
        custom = 5,
    }
    util.args.from_cargo_map{
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {'custom'},
            fields = {
                custom = {
                    type = 'Integer',
                    field = 'custom',
                    name = 'different_name',
                },
            },
        }
    }
    
    self:assertDeepEquals(tpl_args_expected, tpl_args, 'Parsing from a different name in tpl_args failed')
end

function suite:test_args_from_cargo_maps__error_missing_in_order()
    local tpl_args = {
        custom = '5',
    }
    self:assertFalse(pcall(util.args.from_cargo_map, {}, {
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {},
            fields = {
                custom = {
                    type = 'Integer',
                    field = 'custom',
                },
            },
        }
    }), 'Field that is present in fields but missing in order should raise an error')
end

function suite:test_args_from_cargo_maps__error_missing_in_fields()
    local tpl_args = {
        custom = '5',
    }
    self:assertFalse(pcall(util.args.from_cargo_map, {}, {
        tpl_args=tpl_args,
        frame=mw.getCurrentFrame(),
        table_map={
            table = 'test',
            order = {'custom'},
            fields = {
            },
        }
    }), 'Field that is present in order but missing in fields should raise an error')
end

-- ----------------------------------------------------------------------------
-- Tests: util.cargo
-- ----------------------------------------------------------------------------

-- 
-- Tests: util.cargo.parse_field
-- 

function suite:test_cargo_parse_field__simple()
    self:assertDeepEquals(
        {
            type='String'
        }, 
        util.cargo.parse_field{field='String'}, 
        'Simple type declaration failed'
    )
end

function suite:test_cargo_parse_field__list()
    self:assertDeepEquals(
        {
            type='String',
            list=',',
        }, 
        util.cargo.parse_field{field='List (,) of String'}, 
        'List declaration type failed'
    )
end

function suite:test_cargo_parse_field__parameter()
    self:assertDeepEquals(
        {
            type='String',
            parameters = {
                size = '300',
                unique = true,
            },
        }, 
        util.cargo.parse_field{field='String(size=300;unique)'}, 
        'Declaration with parameters failed'
    )
end

function suite:test_cargo_parse_field__list_with_parameters()
    self:assertDeepEquals(
        {
            type='String',
            list=',',
            parameters = {
                size = '300',
                unique = true,
            },
        }, 
        util.cargo.parse_field{field='List (,) of String(size=300;unique)'}, 
        'List declaration with parameters failed'
    )
end

function suite:test_cargo_parse_field__simple_whitespace()
    self:assertDeepEquals(
        {
            type='String'
        }, 
        util.cargo.parse_field{field='  String  '}, 
        'Simple type declaration with whitespace removal failed'
    )
end

function suite:test_cargo_parse_field__paramater_whitespace()
    self:assertDeepEquals(
        {
            type='String',
            parameters = {
                size = '300',
                unique = true,
            },
        }, 
        util.cargo.parse_field{field='String( size=300  ; unique  )'}, 
        'Declaration with parameters wih whitespace removal failed'
    )
end

-- ----------------------------------------------------------------------------
-- Tests: util.html
-- ----------------------------------------------------------------------------

function suite:test_html_abbr()
    self:assertEquals('<abbr title="content" class="cls">test</abbr>', util.html.abbr('test', 'content', 'cls'))
end

function suite:test_html_error()
    self:assertEquals('<span class="module-error">Ошибка модуля: Test</span>', util.html.error{msg='Test'})
    self:assertEquals('<span class="module-error">Ошибка модуля: </span>', util.html.error())
end

-- ----------------------------------------------------------------------------
-- Tests: util.misc
-- ----------------------------------------------------------------------------

function suite:test_misc_is_frame()
    self:assertFalse(util.misc.is_frame(), 'Test failed for nil')
    self:assertFalse(util.misc.is_frame(0), 'Test failed for number')
    self:assertFalse(util.misc.is_frame({}), 'Test failed for unrelated table')
    self:assertTrue(util.misc.is_frame(mw.getCurrentFrame()), 'Test failed for correct frame object')
end

function suite:test_misc_get_frame()
    util.misc.get_frame()
end

function suite:test_misc_add_category()
    self:assertEquals('[[Category:test]]', util.misc.add_category({'test'}, {ingore_blacklist=true}))
end

-- ----------------------------------------------------------------------------
-- Tests: util.smw
-- ----------------------------------------------------------------------------

function suite:test_smw_safeguard()
    self:assertTrue(util.smw.safeguard(), 'Should return true for module namespace')
end

-- ----------------------------------------------------------------------------
-- Tests: util.string
-- ----------------------------------------------------------------------------

function suite:test_string_split_args()
    self:assertDeepEquals({'a','b',x='c','d', y='e'}, util.string.split_args('a,b,x=c,d,y=e'), 'Basic functionality test failed.')
    self:assertDeepEquals({'a','b',x='c','d', y='e'}, util.string.split_args('a;b;x=c;d;y=e', {sep=';'}), 'Separator failed.')
    self:assertFalse(pcall(util.string.split_args, 'a,b=1=1'), 'Testing errornous string failed (duplicate =).')
end

-- ----------------------------------------------------------------------------
-- Tests: util.table
-- ----------------------------------------------------------------------------

local data_array = {
    { 5, 10 },
    { 6, 12 },
}

local data_assoc = {
    {
        a = 5,
        b = 10,
    },
    {
        a = 6,
        b = 12,
    },
}

function suite:test_table_find_in_nested_array__assoc()
    self:assertEquals(6, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6})
    self:assertEquals(nil, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=0})
    self:assertEquals(data_assoc[2], util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6, rtrvalue=false})
    self:assertEquals(12, util.table.find_in_nested_array{tbl=data_assoc, key='a', value=6, rtrkey='b'})
    self:assertEquals(6, util.table.find_in_nested_array{tbl=data_assoc, key={'a', 'b'}, value=12, rtrkey='a'})
end

-- ----------------------------------------------------------------------------

return suite