From 9ec1488f1b730a3010c117bb9015c98385b2134a Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 19 Jul 2021 11:43:09 -0700 Subject: [PATCH] add unit tests --- test/plugins/orders.lua | 142 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 129 insertions(+), 13 deletions(-) diff --git a/test/plugins/orders.lua b/test/plugins/orders.lua index e07bdc032..0982de807 100644 --- a/test/plugins/orders.lua +++ b/test/plugins/orders.lua @@ -9,38 +9,61 @@ local TMP_FILE_NAME = 'tmp-test' local TMP_FILE_PATH = FILE_PATH_PATTERN:format(TMP_FILE_NAME) local function test_wrapper(test_fn) - -- save active orders + -- backup and clear active orders dfhack.run_command_silent{'orders', 'export', BACKUP_FILE_NAME} + dfhack.run_command_silent{'orders', 'clear'} + df.global.world.manager_order_next_id = 0 return dfhack.with_finalize( function() - -- clear test orders, restore original orders, remove temp file + -- clear test orders, restore original orders, remove temp files dfhack.run_command_silent{'orders', 'clear'} - print('reimporting') dfhack.run_command_silent{'orders', 'import', BACKUP_FILE_NAME} + df.global.world.manager_order_next_id = + #df.global.world.manager_orders os.remove(BACKUP_FILE_PATH) + os.remove(TMP_FILE_PATH) end, test_fn) end config.wrapper = test_wrapper +-- returns export command result and exported file content +function run_orders_export() + local _, result = dfhack.run_command_silent{'orders', 'export', + TMP_FILE_NAME} + local f = io.open(TMP_FILE_PATH, 'r') + return dfhack.with_finalize( + function() f:close() end, + function() return result, f:read('*all') end) +end + function run_orders_import(file_content) local f = io.open(TMP_FILE_PATH, 'w') f:write(file_content) f:close() + return dfhack.run_command_silent{'orders', 'import', TMP_FILE_NAME} +end - return dfhack.with_finalize( - function() - os.remove(TMP_FILE_PATH) - end, - function() - return dfhack.run_command_silent{'orders', 'import', TMP_FILE_NAME} - end - ) +local function normalize_whitespace(str) + return str:gsub('%s+', ' '):trim() +end + +function check_export_success(expected_file_content) + local result, file_content = run_orders_export() + expect.eq(result, CR_OK) + + -- ignore whitespace (otherwise the expected file content is impossible to + -- format properly in this file) + expect.eq(normalize_whitespace(expected_file_content), + normalize_whitespace(file_content)) end -function check_import_success(file_content) +function check_import_success(file_content, comment, num_expected_orders) + local prev_num_orders = #df.global.world.manager_orders local output, result = run_orders_import(file_content) expect.eq(result, CR_OK) + expect.eq(prev_num_orders + num_expected_orders, + #df.global.world.manager_orders) end function check_import_fail(file_content, comment, prefix) @@ -55,7 +78,7 @@ function check_import_fail(file_content, comment, prefix) end function test.import_empty() - check_import_success('[]') + check_import_success('[]', 'empty input', 0) end function test.import_non_array() @@ -141,3 +164,96 @@ function test.import_valid_and_invalid_orders() ] ]], 'empty order before valid order') end + +function test.import_export_reaction_condition() + local file_content = [[ + [ + { + "amount_left" : 1, + "amount_total" : 1, + "frequency" : "Daily", + "id" : 0, + "is_active" : false, + "is_validated" : false, + "item_conditions" : + [ + { + "condition" : "AtLeast", + "contains" : + [ + "lye" + ], + "reaction_id" : "MAKE_SOAP_FROM_TALLOW", + "value" : 5 + } + ], + "job" : "CustomReaction", + "reaction" : "MAKE_SOAP_FROM_TALLOW" + } + ] + ]] + check_import_success(file_content, 'valid reaction condition', 1) + check_export_success(file_content) +end + +local function get_last_order() + return df.global.world.manager_orders[#df.global.world.manager_orders-1] +end + +function test.import_invalid_reaction_conditions() + check_import_success([[ + [ + { + "amount_left" : 1, + "amount_total" : 1, + "frequency" : "OneTime", + "id" : 0, + "is_active" : false, + "is_validated" : true, + "item_conditions" : + [ + { + "condition" : "AtLeast", + "contains" : + [ + "lye" + ], + "reaction_id" : "MAKE_SOAP_FROM_TALLOW_xxx", + "value" : 5 + } + ], + "job" : "CustomReaction", + "reaction" : "MAKE_SOAP_FROM_TALLOW" + } + ] + ]], 'condition ignored for bad reaction id', 1) + expect.eq(0, #get_last_order().item_conditions) + + check_import_success([[ + [ + { + "amount_left" : 1, + "amount_total" : 1, + "frequency" : "OneTime", + "id" : 0, + "is_active" : false, + "is_validated" : true, + "item_conditions" : + [ + { + "condition" : "AtLeast", + "contains" : + [ + "lye_xxx" + ], + "reaction_id" : "MAKE_SOAP_FROM_TALLOW", + "value" : 5 + } + ], + "job" : "CustomReaction", + "reaction" : "MAKE_SOAP_FROM_TALLOW" + } + ] + ]], 'condition ignored for bad reagent name', 1) + expect.eq(0, #get_last_order().item_conditions) +end