From 403f8225202afed171475d87d9b5728916c97276 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 17 Apr 2021 00:59:56 -0400 Subject: [PATCH] Add tests for patch() where patching raises an error --- test/library/test_util/mock.lua | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/library/test_util/mock.lua b/test/library/test_util/mock.lua index 5681e7afb..a605abc8a 100644 --- a/test/library/test_util/mock.lua +++ b/test/library/test_util/mock.lua @@ -99,6 +99,51 @@ function test.patch_callback_return_value() expect.eq(b, 4) end +function test.patch_invalid_value() + dfhack.with_temp_object(df.new('int8_t'), function(i) + i.value = 1 + local called = false + expect.error_match('integer expected', function() + mock.patch(i, 'value', 2, function() + expect.eq(i.value, 2) + called = true + i.value = 'a' + end) + end) + expect.true_(called) + expect.eq(i.value, 1) + end) +end + +function test.patch_invalid_value_initial() + dfhack.with_temp_object(df.new('int8_t'), function(i) + i.value = 1 + expect.error_match('integer expected', function() + mock.patch(i, 'value', 'a', function() + expect.fail('patch() callback called unexpectedly') + end) + end) + expect.eq(i.value, 1) + end) +end + +function test.patch_invalid_value_initial_multiple() + dfhack.with_temp_object(df.new('int8_t', 2), function(i) + i[0] = 1 + i[1] = 2 + expect.error_match('integer expected', function() + mock.patch({ + {i, 0, 3}, + {i, 1, 'a'}, + }, function() + expect.fail('patch() callback called unexpectedly') + end) + end) + expect.eq(i[0], 1) + expect.eq(i[1], 2) + end) +end + function test.restore_single() local t = {k = 1} mock.restore(t, 'k', function()