1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-22 03:39:09 +02:00

More Landfill tests plus fix entity filter blacklist

This commit is contained in:
James Gillham 2020-10-03 21:10:25 +01:00
parent 031517fb85
commit 980ca7672b
2 changed files with 269 additions and 4 deletions

View File

@ -56,9 +56,7 @@ local function can_select_landfill_tiles(cursor, surface, area)
entity_filters[#entity_filters + 1] = 'character' entity_filters[#entity_filters + 1] = 'character'
end end
if surface.count_entities_filtered({area = area, name = entity_filters, invert = invert, limit = 1}) > 0 then return surface.count_entities_filtered({area = area, name = entity_filters, invert = invert, limit = 1}) == 0
return false
end
end end
local function within_reach(tile_position, player_position, radius_squared) local function within_reach(tile_position, player_position, radius_squared)

View File

@ -15,7 +15,7 @@ local tile_items = {
} }
Declare.module( Declare.module(
'landfill remover', {'features', 'landfill remover'},
function() function()
local teardown local teardown
@ -31,6 +31,18 @@ Declare.module(
end end
) )
local function setup_player_with_default_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory)
inventory.clear()
inventory.insert('deconstruction-planner')
local stack = inventory.find_item_stack('deconstruction-planner')
local cursor = player.cursor_stack
cursor.set_stack(stack)
return cursor
end
local function setup_player_with_valid_deconstruction_planner(player) local function setup_player_with_valid_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory) local inventory = player.get_inventory(main_inventory)
inventory.clear() inventory.clear()
@ -45,6 +57,34 @@ Declare.module(
return cursor return cursor
end end
local function setup_player_with_tile_filter_whitelist_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory)
inventory.clear()
inventory.insert('deconstruction-planner')
local stack = inventory.find_item_stack('deconstruction-planner')
stack.set_tile_filter(1, 'landfill')
stack.tile_filter_mode = defines.deconstruction_item.tile_filter_mode.whitelist
local cursor = player.cursor_stack
cursor.set_stack(stack)
return cursor
end
local function setup_player_with_tile_filter_blacklist_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory)
inventory.clear()
inventory.insert('deconstruction-planner')
local stack = inventory.find_item_stack('deconstruction-planner')
stack.set_tile_filter(1, 'landfill')
stack.tile_filter_mode = defines.deconstruction_item.tile_filter_mode.blacklist
local cursor = player.cursor_stack
cursor.set_stack(stack)
return cursor
end
local function setup_player_with_normal_selection_mode_deconstruction_planner(player) local function setup_player_with_normal_selection_mode_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory) local inventory = player.get_inventory(main_inventory)
inventory.clear() inventory.clear()
@ -100,6 +140,50 @@ Declare.module(
return cursor return cursor
end end
local function setup_player_with_trees_and_rocks_only_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory)
inventory.clear()
inventory.insert('deconstruction-planner')
local stack = inventory.find_item_stack('deconstruction-planner')
stack.set_tile_filter(1, 'landfill')
stack.trees_and_rocks_only = true
local cursor = player.cursor_stack
cursor.set_stack(stack)
return cursor
end
local function setup_player_with_entity_filter_whitelist_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory)
inventory.clear()
inventory.insert('deconstruction-planner')
local stack = inventory.find_item_stack('deconstruction-planner')
stack.set_tile_filter(1, 'landfill')
stack.set_entity_filter(1, 'iron-chest')
stack.entity_filter_mode = defines.deconstruction_item.entity_filter_mode.whitelist
local cursor = player.cursor_stack
cursor.set_stack(stack)
return cursor
end
local function setup_player_with_entity_filter_blacklist_deconstruction_planner(player)
local inventory = player.get_inventory(main_inventory)
inventory.clear()
inventory.insert('deconstruction-planner')
local stack = inventory.find_item_stack('deconstruction-planner')
stack.set_tile_filter(1, 'landfill')
stack.set_entity_filter(1, 'iron-chest')
stack.entity_filter_mode = defines.deconstruction_item.entity_filter_mode.blacklist
local cursor = player.cursor_stack
cursor.set_stack(stack)
return cursor
end
Declare.test( Declare.test(
'can remove landfill', 'can remove landfill',
function(context) function(context)
@ -276,6 +360,46 @@ Declare.module(
) )
end end
Declare.test(
'does not remove landfill when trees and rocks only',
function(context)
-- Arrange
local player = context.player
local surface = player.surface
local position = {2, 2}
local area = {{2.1, 2.1}, {2.9, 2.9}}
surface.set_tiles({{name = 'landfill', position = position}})
local cursor = setup_player_with_trees_and_rocks_only_deconstruction_planner(player)
-- Act
EventFactory.do_player_deconstruct_area(cursor, player, area)
-- Assert
local tile = surface.get_tile(position[1], position[2])
Assert.equal('landfill', tile.name)
end
)
Declare.test(
'does not remove landfill when default deconstruction planner',
function(context)
-- Arrange
local player = context.player
local surface = player.surface
local position = {2, 2}
local area = {{2.1, 2.1}, {2.9, 2.9}}
surface.set_tiles({{name = 'landfill', position = position}})
local cursor = setup_player_with_default_deconstruction_planner(player)
-- Act
EventFactory.do_player_deconstruct_area(cursor, player, area)
-- Assert
local tile = surface.get_tile(position[1], position[2])
Assert.equal('landfill', tile.name)
end
)
local tile_mode_test_cases = { local tile_mode_test_cases = {
{ {
name = 'only', name = 'only',
@ -328,6 +452,43 @@ Declare.module(
) )
end end
local tile_filter_test_cases = {
{
name = 'whitelist',
setup = setup_player_with_tile_filter_whitelist_deconstruction_planner,
should_remove = true
},
{
name = 'blacklist',
setup = setup_player_with_tile_filter_blacklist_deconstruction_planner,
should_remove = false
}
}
for _, test_case in pairs(tile_filter_test_cases) do
Declare.test(
'tile filter ' ..
test_case.name .. ' should ' .. (test_case.should_remove and '' or 'not ') .. 'remove landfill',
function(context)
-- Arrange
local player = context.player
local surface = player.surface
local cursor = test_case.setup(player)
local position = {2, 2}
local area = {{2.1, 2.1}, {2.9, 2.9}}
surface.set_tiles({{name = 'landfill', position = position}})
local expected_tile = test_case.should_remove and config.revert_tile or 'landfill'
-- Act
EventFactory.do_player_deconstruct_area(cursor, player, area)
-- Assert
local tile = surface.get_tile(position[1], position[2])
Assert.equal(expected_tile, tile.name)
end
)
end
local tile_mode_with_entity_test_cases = { local tile_mode_with_entity_test_cases = {
{ {
name = 'only', name = 'only',
@ -396,6 +557,112 @@ Declare.module(
) )
end end
local tile_filter_with_entity_test_cases = {
{
name = 'whitelist',
setup = setup_player_with_tile_filter_whitelist_deconstruction_planner,
should_remove = false
},
{
name = 'blacklist',
setup = setup_player_with_tile_filter_blacklist_deconstruction_planner,
should_remove = false
}
}
for _, test_case in pairs(tile_filter_with_entity_test_cases) do
Declare.test(
'tile mode ' ..
test_case.name ..
' with entity should ' .. (test_case.should_remove and '' or 'not ') .. 'remove landfill',
function(context)
-- Arrange
local player = context.player
local surface = player.surface
local position1 = {2, 2}
local position2 = {3, 2}
local area = {{2.1, 2.1}, {3.9, 2.9}}
surface.set_tiles(
{{name = 'landfill', position = position1}, {name = 'landfill', position = position2}}
)
local expected_tile = test_case.should_remove and config.revert_tile or 'landfill'
-- Place entity.
local cursor = player.cursor_stack
cursor.set_stack('iron-chest')
player.build_from_cursor({position = position1})
cursor = test_case.setup(player)
-- Act
EventFactory.do_player_deconstruct_area(cursor, player, area)
-- Assert
local tile = surface.get_tile(position2[1], position2[2])
Assert.equal(expected_tile, tile.name)
local entities = surface.find_entities(area)
local entity = entities[1]
Assert.is_lua_object_with_name(entity, 'iron-chest', 'iron-chest was not valid.')
entity.destroy()
end
)
end
local entity_filter_with_entity_test_cases = {
{
name = 'whitelist',
setup = setup_player_with_entity_filter_whitelist_deconstruction_planner,
should_remove = false
},
{
name = 'blacklist',
setup = setup_player_with_entity_filter_blacklist_deconstruction_planner,
should_remove = true
}
}
for _, test_case in pairs(entity_filter_with_entity_test_cases) do
Declare.test(
'entity filter ' ..
test_case.name ..
' with entity should ' .. (test_case.should_remove and '' or 'not ') .. 'remove landfill',
function(context)
-- Arrange
local player = context.player
local surface = player.surface
local position1 = {2, 2}
local position2 = {3, 2}
local area = {{2.1, 2.1}, {3.9, 2.9}}
surface.set_tiles(
{{name = 'landfill', position = position1}, {name = 'landfill', position = position2}}
)
local expected_tile = test_case.should_remove and config.revert_tile or 'landfill'
-- Place entity.
local cursor = player.cursor_stack
cursor.set_stack('iron-chest')
player.build_from_cursor({position = position1})
cursor = test_case.setup(player)
-- Act
EventFactory.do_player_deconstruct_area(cursor, player, area)
-- Assert
local tile = surface.get_tile(position2[1], position2[2])
Assert.equal(expected_tile, tile.name)
local entities = surface.find_entities(area)
local entity = entities[1]
Assert.is_lua_object_with_name(entity, 'iron-chest', 'iron-chest was not valid.')
entity.destroy()
end
)
end
Declare.test( Declare.test(
'ignore character when removing landfill', 'ignore character when removing landfill',
function(context) function(context)