From f25b8a0d143b3a27e4fe778d5877b296aa175545 Mon Sep 17 00:00:00 2001 From: lethosor Date: Fri, 9 Apr 2021 00:35:54 -0400 Subject: [PATCH] Fix patching value with nil --- library/lua/test_util/mock.lua | 16 +++++------ test/library/test_util/mock.lua | 51 +++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/library/lua/test_util/mock.lua b/library/lua/test_util/mock.lua index 1e4609f10..dde1b1ee2 100644 --- a/library/lua/test_util/mock.lua +++ b/library/lua/test_util/mock.lua @@ -5,15 +5,15 @@ local mock = mkmodule('test_util.mock') Usage: patch(table, key, value, callback) patch({ - [{table, key}] = value, - [{table2, key2}] = value2 + {table, key, value}, + {table2, key2, value2} }, callback) ]] function mock.patch(...) local args = {...} if #args == 4 then args = {{ - [{args[1], args[2]}] = args[3] + {args[1], args[2], args[3]} }, args[4]} end if #args ~= 2 then @@ -22,15 +22,15 @@ function mock.patch(...) local callback = args[2] local patches = {} - for k, v in pairs(args[1]) do + for _, v in ipairs(args[1]) do local p = { - table = k[1], - key = k[2], - new_value = v, + table = v[1], + key = v[2], + new_value = v[3], } p.old_value = p.table[p.key] -- no-op to ensure that the value can be restored by the finalizer below - p.table[p.key] = p.table[p.key] + p.table[p.key] = p.old_value table.insert(patches, p) end diff --git a/test/library/test_util/mock.lua b/test/library/test_util/mock.lua index f43fcbd65..40f80b165 100644 --- a/test/library/test_util/mock.lua +++ b/test/library/test_util/mock.lua @@ -33,8 +33,8 @@ function test.patch_multiple() expect.eq(test_table.func1(), 1) expect.eq(test_table.func2(), 2) mock.patch({ - [{test_table, 'func1'}] = function() return 3 end, - [{test_table, 'func2'}] = function() return 4 end, + {test_table, 'func1', function() return 3 end}, + {test_table, 'func2', function() return 4 end}, }, function() expect.eq(test_table.func1(), 3) expect.eq(test_table.func2(), 4) @@ -43,3 +43,50 @@ function test.patch_multiple() expect.eq(test_table.func2(), 2) end +function test.patch_nil_old_value() + local t = {} + mock.patch(t, 1, 2, function() + expect.eq(t[1], 2) + end) + expect.eq(t[1], nil) + expect.eq(#t, 0) +end + +function test.patch_nil_new_value() + local t = {2} + mock.patch(t, 1, nil, function() + expect.eq(t[1], nil) + expect.eq(#t, 0) + end) + expect.eq(t[1], 2) +end + +function test.patch_nil_key() + local called = false + expect.error_match('table index is nil', function() + mock.patch({}, nil, 'value', function() + called = true + end) + end) + expect.false_(called) +end + +function test.patch_nil_table() + local called = false + expect.error(function() + mock.patch(nil, 1, 2, function() + called = true + end) + end) + expect.false_(called) +end + +function test.patch_complex_key() + local key = {'key'} + local t = {[key] = 'value'} + expect.eq(t[key], 'value') + mock.patch(t, key, 2, function() + expect.eq(t[key], 2) + end) + expect.eq(t[key], 'value') +end