Skip to content

Commit 928868f

Browse files
committed
Add unit test, using factorio-unit-test system
1 parent e246f64 commit 928868f

5 files changed

Lines changed: 152 additions & 5 deletions

File tree

scripts/remote.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
remote.add_interface("factory-search", {
2+
---@param player LuaPlayer
3+
---@param state SearchGuiState
4+
set_search_state = function(player, state)
5+
SearchGui.open(player, storage.players[player.index])
6+
local player_data = storage.players[player.index]
7+
local refs = player_data.refs
8+
SearchGui.set_state(refs, state)
9+
end,
10+
211
---@param player LuaPlayer
312
---@param search_value SignalID
413
search = function(player, search_value)
514
SearchGui.open(player, storage.players[player.index])
615
local player_data = storage.players[player.index]
716
player_data.refs.item_select.elem_value = search_value
8-
SearchGui.start_search(player, player_data)
17+
SearchGui.start_search_immediate(player, player_data)
918
end
1019
})

scripts/search-gui.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ end
343343
function SearchGui.show_search_progress(refs, progress)
344344
refs.searching_label.visible = true
345345
refs.search_progressbar.visible = progress ~= nil
346-
346+
347347
if progress ~= nil then
348348
refs.search_progressbar.value = progress
349349
refs.search_progressbar.tooltip = {'', math.floor(progress * 100), '%'}
@@ -758,6 +758,23 @@ local function generate_state(refs)
758758
}
759759
end
760760

761+
---@param refs SearchGuiRefs
762+
---@param state SearchGuiState
763+
function SearchGui.set_state(refs, state)
764+
refs.all_qualities.state = state.all_qualities or refs.all_qualities.state
765+
refs.all_surfaces.state = state.all_surfaces or refs.all_surfaces.state
766+
refs.include_consumers.state = state.consumers or refs.include_consumers.state
767+
refs.include_machines.state = state.producers or refs.include_machines.state
768+
refs.include_inventories.state = state.storage or refs.include_inventories.state
769+
refs.include_logistics.state = state.logistics or refs.include_logistics.state
770+
refs.include_modules.state = state.modules or refs.include_modules.state
771+
refs.include_requesters.state = state.requesters or refs.include_requesters.state
772+
refs.include_ground_items.state = state.ground_items or refs.include_ground_items.state
773+
refs.include_entities.state = state.entities or refs.include_entities.state
774+
refs.include_signals.state = state.signals or refs.include_signals.state
775+
refs.include_map_tags.state = state.map_tags or refs.include_map_tags.state
776+
end
777+
761778
---@param state SearchGuiState
762779
---@return boolean
763780
local function is_valid_state(state)
@@ -796,6 +813,7 @@ function SearchGui.start_search(player, player_data, _, _, immediate)
796813
else
797814
SearchGui.build_results({}, {}, refs.result_flow)
798815
end
816+
if immediate then Search.on_tick() end
799817
else
800818
SearchGui.build_invalid_state(refs.result_flow)
801819
storage.current_searches[player.index] = nil

scripts/search.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ function Search.blocking_search(force, state, target_item, surface_list, type_li
819819
storage.current_searches[player.index] = nil
820820
end
821821

822-
function on_tick()
822+
function Search.on_tick()
823823
local player_index, search_data = next(storage.current_searches)
824824
if not player_index or not search_data then return end
825825

@@ -1079,7 +1079,7 @@ function Search.find_machines(target_item, force, state, player, immediate)
10791079
end
10801080
search_data = {
10811081
blocking = not non_blocking_search,
1082-
tick_triggered = game.tick - (immediate and DEBOUNCE_TICKS or 0),
1082+
tick_triggered = immediate and -10000 or game.tick,
10831083
force = force,
10841084
state = state,
10851085
target_item = target_item,
@@ -1098,7 +1098,7 @@ function Search.find_machines(target_item, force, state, player, immediate)
10981098
end
10991099

11001100
Search.events = {
1101-
[defines.events.on_tick] = on_tick,
1101+
[defines.events.on_tick] = Search.on_tick,
11021102
}
11031103

11041104
return Search

unit-test-config.jsonnet

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
defaultSettings: {},
3+
configurations:
4+
{
5+
"Vanilla (Blocking)":
6+
{
7+
"runtime-global": {
8+
startup: {
9+
"fs-non-blocking-search": false,
10+
},
11+
},
12+
mods: [
13+
"FactorySearch",
14+
],
15+
},
16+
/*"Vanilla (Non-blocking)":
17+
{
18+
"runtime-global": {
19+
startup: {
20+
"fs-non-blocking-search": true,
21+
},
22+
},
23+
mods: [
24+
"FactorySearch",
25+
],
26+
},
27+
"Space Age":
28+
{
29+
settings: {},
30+
mods: [
31+
"FactorySearch",
32+
"space-age",
33+
"elevated-rails",
34+
"quality",
35+
],
36+
},*/
37+
},
38+
tests:
39+
{
40+
"test-search": {},
41+
}
42+
}

unit-tests/test-search.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
local unit_test_functions = require("unit-test-functions")
2+
3+
local LOG = unit_test_functions.print_msg
4+
local SUCCESS = unit_test_functions.test_successful
5+
local FAIL = unit_test_functions.test_failed
6+
local INVALID = unit_test_functions.test_invalid
7+
local ASSERT = unit_test_functions.assert
8+
local ASSERT_EQUAL = unit_test_functions.assert_equal
9+
10+
local test_search = function(tick)
11+
LOG("Unit testing mod configuration:")
12+
for mod_name, mod_version in pairs(script.active_mods) do
13+
LOG(mod_name .. " version " .. mod_version)
14+
end
15+
16+
-- Setup
17+
local player = game.get_player(1) ---@cast player -?
18+
player.exit_cutscene()
19+
20+
local surface = player.surface
21+
22+
--surface.clear(true)
23+
local chest = surface.create_entity{
24+
name = "steel-chest",
25+
position = {5, 5},
26+
force = player.force,
27+
} ---@cast chest -?
28+
29+
chest.insert({name = "iron-plate", count = 100})
30+
31+
-- Run test
32+
33+
remote.call("factory-search", "set_search_state", player,
34+
---@diagnostic disable-next-line: missing-fields
35+
{
36+
all_qualities = true,
37+
all_surfaces = true,
38+
consumers = false,
39+
producers = false,
40+
storage = true,
41+
logistics = false,
42+
modules = false,
43+
requesters = false,
44+
ground_items = false,
45+
entities = false,
46+
signals = false,
47+
map_tags = false,
48+
})
49+
remote.call("factory-search", "search", player, {name = "iron-plate"})
50+
51+
-- Check result
52+
local search_frame = player.gui.screen["fs_frame"]
53+
local results_flow = search_frame
54+
.children[2] -- inside_shallow_frame
55+
.children[2] -- horizontal flow
56+
.children[2] -- scroll pane
57+
.children[1] -- vertical flow
58+
.children[2] -- result_flow
59+
60+
local surface_counts_flow = results_flow.children[1] -- [2] when include_surface_name
61+
local item_count_label = surface_counts_flow.children[1]
62+
ASSERT(item_count_label.caption[5] == "100", "Item count label does not show the correct number of items.")
63+
64+
local results_table = results_flow
65+
.children[2] -- vertical frame (slot_button_deep_frame) ([3] when include_surface_name)
66+
.children[3] -- table (storage results)
67+
68+
ASSERT(#results_table.children == 1, "No results found in search table, test failed.")
69+
local result_button = results_table.children[1]
70+
ASSERT(result_button.sprite == "entity/steel-chest", "Result button does not have the correct sprite.")
71+
ASSERT(result_button.number == 1, "Result button does not have the correct number of items.")
72+
ASSERT(result_button.tags.position.x == 5.5 and result_button.tags.position.y == 5.5,
73+
"Result button does not have the correct position tags.")
74+
ASSERT(result_button.tags.surface == surface.name, "Result button does not have the correct surface tag.")
75+
return SUCCESS
76+
end
77+
78+
return {test_search = test_search}

0 commit comments

Comments
 (0)