diff --git a/assert-gooder.lua b/assert-gooder.lua index 7712233..ea0d2e7 100644 --- a/assert-gooder.lua +++ b/assert-gooder.lua @@ -59,6 +59,10 @@ local function parse (tokens) left = get_value_token(tokens[1]), right = get_value_token(tokens[3]) } + elseif #tokens == 3 and tokens[1].token == 'IDENTIFIER' and tokens[2].token == 'DOT' and tokens[3].token == 'IDENTIFIER' then + return { exp = 'LVALUE', tokens[1].text, { exp = 'STRING', value = tokens[3].text } } + elseif #tokens == 4 and tokens[1].token == 'IDENTIFIER' and tokens[2].token == 'LBRACK' and VALUE_TOKEN[tokens[3].token] and tokens[4].token == 'RBRACK' then + return { exp = 'LVALUE', tokens[1].text, get_value_token(tokens[3]) } elseif #tokens == 1 then return get_value_token(tokens[1]) else @@ -151,9 +155,12 @@ local function fmt_val (val) end end -local function fmt_lvalue (lvalue) +local function fmt_lvalue (lvalue, var_scope) assert(type(lvalue) == 'table' and lvalue.exp == 'LVALUE') - return table.concat(lvalue, '.') + if #lvalue == 1 then return string.format('%s \'%s\'', var_scope, lvalue[1]) + elseif #lvalue == 2 then return string.format('key %s in %s \'%s\'', fmt_val(lvalue[2].value), var_scope, lvalue[1]) + else error 'Not implemented yet!' + end end local function get_variable_and_prefix (lvalue, level) @@ -161,9 +168,12 @@ local function get_variable_and_prefix (lvalue, level) assert(type(level) == 'number') -- local base_value, var_scope, in_func = get_variable(lvalue[1], level + 1) - local value = base_value -- TODO: Generalize to any lvalue + -- Determine value of variable + local value = base_value + for i = 2, #lvalue do value = value[lvalue[i].value] end + -- local func_name = in_func and (' to '..get_function_name(debug.getinfo(level + 1))) or '' - return value, ('assertion failed! bad %s \'%s\'%s'):format(var_scope, fmt_lvalue(lvalue), func_name) + return value, ('assertion failed! bad %s%s'):format(fmt_lvalue(lvalue, var_scope), func_name) end diff --git a/test/test_assert-gooder.lua b/test/test_assert-gooder.lua index 8652f6c..4138677 100644 --- a/test/test_assert-gooder.lua +++ b/test/test_assert-gooder.lua @@ -131,6 +131,22 @@ SUITE:addTest('truthy indexing', function () assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key "b" in local \'a\' (truthy expected, but got false)', msg) end) +SUITE:addTest('truthy subscript', function () + local _, msg = pcall(function () + local a = {} + assert(a[6]) + end) + assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key 6 in local \'a\' (truthy expected, but got nil)', msg) +end) + +SUITE:addTest('truthy variable subscript', function () + local _, msg = pcall(function () + local a, i = {}, 3 + assert(a[i]) + end) + assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key 3 in local \'a\' (truthy expected, but got nil)', msg) +end) + SUITE:addTest('not equal', function (constant_value, msg_in_pars) local func = loadstring (("return function() local a = %s; assert(a ~= %s) end"):format(constant_value, constant_value)) ()