forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_location_test.cpp
66 lines (63 loc) · 1.87 KB
/
item_location_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <functional>
#include <memory>
#include <string>
#include "catch/catch.hpp"
#include "game.h"
#include "item.h"
#include "map_helpers.h"
#include "rng.h"
#include "item_location.h"
#include "map.h"
#include "map_selector.h"
#include "optional.h"
#include "point.h"
#include "visitable.h"
TEST_CASE( "item_location_can_maintain_reference_despite_item_removal", "[item][item_location]" )
{
clear_map();
map &m = g->m;
tripoint pos( 60, 60, 0 );
m.i_clear( pos );
m.add_item( pos, item( "jeans" ) );
m.add_item( pos, item( "jeans" ) );
m.add_item( pos, item( "tshirt" ) );
m.add_item( pos, item( "jeans" ) );
m.add_item( pos, item( "jeans" ) );
map_cursor cursor( pos );
item *tshirt = nullptr;
cursor.visit_items( [&tshirt]( item * i ) {
if( i->typeId() == "tshirt" ) {
tshirt = i;
return VisitResponse::ABORT;
}
return VisitResponse::NEXT;
} );
REQUIRE( tshirt != nullptr );
item_location item_loc( cursor, tshirt );
REQUIRE( item_loc->typeId() == "tshirt" );
for( int j = 0; j < 4; ++j ) {
// Delete up to 4 random jeans
map_stack stack = m.i_at( pos );
REQUIRE( !stack.empty() );
item *i = &random_entry_opt( stack )->get();
if( i->typeId() == "jeans" ) {
m.i_rem( pos, i );
}
}
CAPTURE( m.i_at( pos ) );
REQUIRE( item_loc );
CHECK( item_loc->typeId() == "tshirt" );
}
TEST_CASE( "item_location_doesnt_return_stale_map_item", "[item][item_location]" )
{
clear_map();
map &m = g->m;
tripoint pos( 60, 60, 0 );
m.i_clear( pos );
m.add_item( pos, item( "tshirt" ) );
item_location item_loc( map_cursor( pos ), &m.i_at( pos ).only_item() );
REQUIRE( item_loc->typeId() == "tshirt" );
m.i_rem( pos, &*item_loc );
m.add_item( pos, item( "jeans" ) );
CHECK( !item_loc );
}