forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_pocket_test.cpp
2930 lines (2600 loc) · 120 KB
/
item_pocket_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <functional>
#include <iosfwd>
#include <map>
#include <memory>
#include <new>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "activity_actor_definitions.h"
#include "avatar.h"
#include "calendar.h"
#include "cata_catch.h"
#include "character.h"
#include "debug.h"
#include "enums.h"
#include "flag.h"
#include "item.h"
#include "item_category.h"
#include "item_factory.h"
#include "item_location.h"
#include "item_pocket.h"
#include "itype.h"
#include "iuse_actor.h"
#include "map.h"
#include "map_helpers.h"
#include "mapgen_helpers.h"
#include "player_helpers.h"
#include "ret_val.h"
#include "test_data.h"
#include "type_id.h"
#include "units.h"
#include "value_ptr.h"
#include "weather.h"
static const ammotype ammo_test_9mm( "test_9mm" );
static const item_group_id Item_spawn_data_wallet_duct_tape_full( "wallet_duct_tape_full" );
static const item_group_id Item_spawn_data_wallet_full( "wallet_full" );
static const item_group_id Item_spawn_data_wallet_industrial_full( "wallet_industrial_full" );
static const item_group_id
Item_spawn_data_wallet_industrial_leather_full( "wallet_industrial_leather_full" );
static const item_group_id Item_spawn_data_wallet_large_full( "wallet_large_full" );
static const item_group_id Item_spawn_data_wallet_leather_full( "wallet_leather_full" );
static const item_group_id Item_spawn_data_wallet_military_full( "wallet_military_full" );
static const item_group_id
Item_spawn_data_wallet_military_leather_full( "wallet_military_leather_full" );
static const item_group_id Item_spawn_data_wallet_science_full( "wallet_science_full" );
static const item_group_id
Item_spawn_data_wallet_science_leather_full( "wallet_science_leather_full" );
static const item_group_id
Item_spawn_data_wallet_science_stylish_full( "wallet_science_stylish_full" );
static const item_group_id Item_spawn_data_wallet_stylish_full( "wallet_stylish_full" );
static const itype_id itype_test_backpack( "test_backpack" );
static const itype_id itype_test_jug_plastic( "test_jug_plastic" );
static const itype_id itype_test_socks( "test_socks" );
static const itype_id
itype_test_watertight_open_sealed_container_1L( "test_watertight_open_sealed_container_1L" );
static const nested_mapgen_id nested_mapgen_auto_wl_test( "auto_wl_test" );
static const pocket_type pocket_container = pocket_type::CONTAINER;
// Pocket Tests
// ------------
//
// These tests are focused on the `item_pocket` and `pocket_data` classes, and how they implement
// the "pocket_data" section of JSON item definitions (as documented in doc/JSON_FLAGS.md).
//
// To run all tests in this file:
//
// tests/cata_test [pocket]
//
// Other tags used include: [holster], [magazine], [airtight], [watertight], [rigid], and more.
//
// Most of the items used for testing here are defined in data/mods/TEST_DATA/items.json, to have
// a predictable set of data unaffected by in-game balance changes.
// TODO: Add tests focusing on these JSON pocket_data fields:
// - pocket_type
// - spoil_multiplier
// - weight_multiplier
// - magazine_well
// - fire_protection
// - open_container
// - sealed_data
// - moves
// FIXME: The failure messaging from these helper functions is pretty terrible, due to the multiple
// CHECKs in each function. Capturing it.tname() helps only slightly. The `rate_can.value()` is an
// integer, so it is difficult to see when a failure reason is returned instead of success.
// Expect pocket.can_contain( it ) to be successful (contain_code::SUCCESS)
static void expect_can_contain( const item_pocket &pocket, const item &it )
{
CAPTURE( it.tname() );
const ret_val<item_pocket::contain_code> rate_can = pocket.can_contain( it );
INFO( rate_can.str() );
CHECK( rate_can.success() );
CHECK( rate_can.str().empty() );
CHECK( rate_can.value() == item_pocket::contain_code::SUCCESS );
}
// Expect pocket.can_contain( it ) to fail, with an expected reason and contain_code
static void expect_cannot_contain( const item_pocket &pocket, const item &it,
const std::string &expect_reason,
item_pocket::contain_code expect_code )
{
CAPTURE( it.tname() );
ret_val<item_pocket::contain_code> rate_can = pocket.can_contain( it );
CHECK_FALSE( rate_can.success() );
CHECK( rate_can.str() == expect_reason );
CHECK( rate_can.value() == expect_code );
}
// Call pocket.insert_item( it ) and expect it to be successful (contain_code::SUCCESS)
static void expect_can_insert( item_pocket &pocket, const item &it )
{
CAPTURE( it.tname() );
CHECK( pocket.can_contain( it ).value() == item_pocket::contain_code::SUCCESS );
ret_val<item *> rate_can = pocket.insert_item( it );
CHECK( rate_can.success() );
CHECK( rate_can.str().empty() );
REQUIRE( rate_can.value() != nullptr );
CHECK( rate_can.value()->stacks_with( it ) );
}
// Call pocket.insert_item( it ) and expect it to fail, with an expected reason and contain_code
static void expect_cannot_insert( item_pocket &pocket, const item &it,
const std::string &expect_reason,
item_pocket::contain_code expect_code )
{
CAPTURE( it.tname() );
CHECK( pocket.can_contain( it ).value() == expect_code );
ret_val<item *> rate_can = pocket.insert_item( it );
CHECK_FALSE( rate_can.success() );
CHECK( rate_can.str() == expect_reason );
CHECK( rate_can.value() == nullptr );
}
// Max item length
// ---------------
// Pocket "max_item_length" is the biggest item (by longest side) that can fit.
//
// Related JSON fields:
// "max_item_length"
//
// Functions:
// item_pocket::can_contain
//
TEST_CASE( "max_item_length", "[pocket][max_item_length]" )
{
// Test items with different lengths
item screwdriver( "test_screwdriver" );
item sonic( "test_sonic_screwdriver" );
item sword( "test_clumsy_sword" );
// Sheath that may contain items
pocket_data data_sheath( pocket_type::CONTAINER );
// Has plenty of weight/ volume, since we're only testing length
data_sheath.volume_capacity = 10_liter;
data_sheath.max_contains_weight = 10_kilogram;
GIVEN( "pocket has max_item_length enough for some items, but not others" ) {
// Sheath is just long enough for sonic screwdriver
const units::length max_length = sonic.length();
data_sheath.max_item_length = max_length;
item_pocket pocket_sheath( &data_sheath );
THEN( "it can contain an item with longest_side shorter than max_item_length" ) {
REQUIRE( screwdriver.length() < max_length );
expect_can_contain( pocket_sheath, screwdriver );
}
THEN( "it can contain an item with longest_side equal to max_item_length" ) {
REQUIRE( sonic.length() == max_length );
expect_can_contain( pocket_sheath, sonic );
}
THEN( "it cannot contain an item with longest_side longer than max_item_length" ) {
REQUIRE( sword.length() > max_length );
expect_cannot_contain( pocket_sheath, sword, "item is too long",
item_pocket::contain_code::ERR_TOO_BIG );
}
}
// Default max_item_length assumes pocket is a cube, limiting max length to the diagonal
// dimension of one side of the cube (the opening into the box, so to speak).
//
// For a 1-liter volume, a 10 cm cube encloses it, so the longest diagonal is:
//
// 10 cm * sqrt(2) =~ 14.14 cm
//
// This test ensures that a 1-liter box with unspecified max_item_length can indeed contain
// items up to 14 cm in length, but not ones that are 15 cm.
//
// NOTE: In theory, the interior space of the cube can accommodate a longer item between its
// opposite diagonals, assuming it is infinitely thin:
//
// 10 cm * sqrt(3) =~ 17.32 cm
//
// Items of such length are not currently allowed in a 1-liter pocket.
GIVEN( "a 1-liter box without a defined max_item_length" ) {
item box( "test_box" );
REQUIRE( box.get_total_capacity() == 1_liter );
THEN( "it can hold an item 14 cm in length" ) {
item rod_14( "test_rod_14cm" );
REQUIRE( rod_14.length() == 14_cm );
REQUIRE( box.is_container_empty() );
box.put_in( rod_14, pocket_type::CONTAINER );
// Item went into the box
CHECK_FALSE( box.is_container_empty() );
}
THEN( "it cannot hold an item 15 cm in length" ) {
item rod_15( "test_rod_15cm" );
REQUIRE( rod_15.length() == 15_cm );
REQUIRE( box.is_container_empty() );
std::string dmsg = capture_debugmsg_during( [&box, &rod_15]() {
ret_val<void> result = box.put_in( rod_15, pocket_type::CONTAINER );
CHECK_FALSE( result.success() );
} );
CHECK_THAT( dmsg, Catch::EndsWith( "item is too long" ) );
// Box should still be empty
CHECK( box.is_container_empty() );
}
}
}
// Max item volume
// ---------------
// "max_item_volume" is the size of the mouth or opening into the pocket. Liquids and gases, as well
// as "soft" items, can always fit through. All other items must fit within "max_item_volume" to be
// put in the pocket.
//
// Related JSON fields:
// "max_item_volume"
//
// Functions:
// item_pocket::can_contain
//
TEST_CASE( "max_item_volume", "[pocket][max_item_volume]" )
{
// Test items
item screwdriver( "test_screwdriver" );
item rag( "test_rag" );
item rock( "test_rock" );
item gas( "test_gas" );
item liquid( "test_liquid" );
// Air-tight, water-tight, jug-style container
pocket_data data_jug( pocket_type::CONTAINER );
data_jug.airtight = true;
data_jug.watertight = true;
// No significant limits on length or weight
data_jug.max_item_length = 1_meter;
data_jug.max_contains_weight = 10_kilogram;
// Jug capacity is more than enough for several rocks, rags, or screwdrivers
data_jug.volume_capacity = 2_liter;
REQUIRE( data_jug.volume_capacity > 5 * rock.volume() );
REQUIRE( data_jug.volume_capacity > 5 * rag.volume() );
REQUIRE( data_jug.volume_capacity > 10 * screwdriver.volume() );
// But it has a narrow mouth, just barely enough to fit a screwdriver
const units::volume max_volume = screwdriver.volume();
data_jug.max_item_volume = max_volume;
// The screwdriver will fit
REQUIRE( screwdriver.volume() <= max_volume );
// All other items have too much volume to fit the opening
REQUIRE( rag.volume() > max_volume );
REQUIRE( rock.volume() > max_volume );
REQUIRE( gas.volume() > max_volume );
REQUIRE( liquid.volume() > max_volume );
GIVEN( "large airtight and watertight pocket with a narrow opening" ) {
item_pocket pocket_jug( &data_jug );
THEN( "it can contain liquids" ) {
REQUIRE( liquid.made_of( phase_id::LIQUID ) );
expect_can_contain( pocket_jug, liquid );
}
THEN( "it can contain gases" ) {
REQUIRE( gas.made_of( phase_id::GAS ) );
expect_can_contain( pocket_jug, gas );
}
THEN( "it can contain solid items that fit through the opening" ) {
REQUIRE_FALSE( screwdriver.is_soft() );
expect_can_contain( pocket_jug, screwdriver );
}
THEN( "it can contain soft items larger than the opening" ) {
REQUIRE( rag.is_soft() );
expect_can_contain( pocket_jug, rag );
}
THEN( "it cannot contain solid items larger than the opening" ) {
REQUIRE_FALSE( rock.is_soft() );
expect_cannot_contain( pocket_jug, rock, "item is too big",
item_pocket::contain_code::ERR_TOO_BIG );
}
}
}
// Max container volume
// --------------------
// Normally, the "max_contains_volume" from pocket data JSON is the largest total volume of
// items the pocket can hold. It is returned by the pocket_data::max_contains_volume() function.
//
// However, pockets with an "ammo_restriction", the pocket_data::max_contains_volume() is derived
// from the size of the ammo given in the restriction.
//
// Related JSON fields:
// "max_contains_volume"
// "ammo_restriction"
//
// Functions:
// pocket_data::max_contains_volume
//
TEST_CASE( "max_container_volume", "[pocket][max_contains_volume]" )
{
// TODO: Add tests for having multiple ammo types in the ammo_restriction
WHEN( "pocket has no ammo_restriction" ) {
pocket_data data_box( pocket_type::CONTAINER );
// Just a normal 1-liter box
data_box.volume_capacity = 1_liter;
REQUIRE( data_box.ammo_restriction.empty() );
THEN( "max_contains_volume is the pocket's volume_capacity" ) {
CHECK( data_box.max_contains_volume() == 1_liter );
}
}
WHEN( "pocket has ammo_restriction" ) {
pocket_data data_ammo_box( pocket_type::CONTAINER );
// 9mm ammo is 50 rounds per 250ml (or 200 rounds per liter), so this ammo box
// should be exactly 1 liter in size, so it can contain this much ammo.
data_ammo_box.ammo_restriction.emplace( ammo_test_9mm, 200 );
REQUIRE_FALSE( data_ammo_box.ammo_restriction.empty() );
// And because actual volume is derived from ammo needs, this volume should be ignored.
data_ammo_box.volume_capacity = 1_ml;
THEN( "max_contains_volume is based on the pocket's ammo type" ) {
CHECK( data_ammo_box.max_contains_volume() == 1_liter );
}
}
}
// Ammo restriction
// ----------------
// Pockets with "ammo_restriction" defined have capacity based on those restrictions (from the
// type(s) of ammo and quantity of each. They can only contain ammo of those types and counts,
// and mixing different ammos in the same pocket is prohibited.
//
// Related JSON fields:
// "ammo_restriction"
//
// Functions:
// item_pocket::can_contain
// item_pocket::insert_item
//
TEST_CASE( "magazine_with_ammo_restriction", "[pocket][magazine][ammo_restriction]" )
{
pocket_data data_mag( pocket_type::MAGAZINE );
// ammo_restriction takes precedence over volume/length/weight,
// so it doesn't matter what these are set to - they can even be 0.
data_mag.volume_capacity = 0_liter;
data_mag.min_item_volume = 0_liter;
data_mag.max_item_length = 0_meter;
data_mag.max_contains_weight = 0_gram;
// TODO: Add tests for multiple ammo types in the same clip
GIVEN( "magazine has 9mm ammo_restriction" ) {
// Magazine ammo_restriction may be a single { ammotype, count } or a list of ammotypes and
// counts. This mag 10 rounds of 9mm, as if it were defined in JSON as:
//
// "ammo_restriction": { "9mm", 10 }
//
const int full_clip_qty = 10;
data_mag.ammo_restriction.emplace( ammo_test_9mm, full_clip_qty );
item_pocket pocket_mag( &data_mag );
WHEN( "it does not already contain any ammo" ) {
REQUIRE( pocket_mag.empty() );
THEN( "it can contain a full clip of 9mm ammo" ) {
const item ammo_9mm( "test_9mm_ammo", calendar::turn_zero, full_clip_qty );
expect_can_contain( pocket_mag, ammo_9mm );
}
THEN( "it cannot contain items of the wrong ammo type" ) {
item rock( "test_rock" );
item ammo_45( "test_45_ammo", calendar::turn_zero, 1 );
expect_cannot_contain( pocket_mag, rock, "item is not the correct ammo type",
item_pocket::contain_code::ERR_AMMO );
expect_cannot_contain( pocket_mag, ammo_45, "item is not the correct ammo type",
item_pocket::contain_code::ERR_AMMO );
}
THEN( "it cannot contain items that are not ammo" ) {
item rag( "test_rag" );
expect_cannot_contain( pocket_mag, rag, "item is not ammunition",
item_pocket::contain_code::ERR_AMMO );
}
}
WHEN( "it is partly full of ammo" ) {
const int half_clip_qty = full_clip_qty / 2;
item ammo_9mm_half_clip( "test_9mm_ammo", calendar::turn_zero, half_clip_qty );
expect_can_insert( pocket_mag, ammo_9mm_half_clip );
THEN( "it can contain more of the same ammo" ) {
item ammo_9mm_refill( "test_9mm_ammo", calendar::turn_zero,
full_clip_qty - half_clip_qty );
expect_can_contain( pocket_mag, ammo_9mm_refill );
}
THEN( "it cannot contain more ammo than ammo_restriction allows" ) {
item ammo_9mm_overfill( "test_9mm_ammo", calendar::turn_zero,
full_clip_qty - half_clip_qty + 1 );
expect_cannot_contain( pocket_mag, ammo_9mm_overfill,
"tried to put too many charges of ammo in item",
item_pocket::contain_code::ERR_NO_SPACE );
}
}
WHEN( "it is completely full of ammo" ) {
item ammo_9mm_full_clip( "test_9mm_ammo", calendar::turn_zero, full_clip_qty );
expect_can_insert( pocket_mag, ammo_9mm_full_clip );
THEN( "it cannot contain any more of the same ammo" ) {
item ammo_9mm_bullet( "test_9mm_ammo", calendar::turn_zero, 1 );
expect_cannot_contain( pocket_mag, ammo_9mm_bullet,
"tried to put too many charges of ammo in item",
item_pocket::contain_code::ERR_NO_SPACE );
}
}
}
}
// Flag restriction
// ----------------
// The "get_flag_restrictions" list from pocket data JSON gives compatible item flag(s) for this pocket.
// An item with any one of those tags may be inserted into the pocket.
//
// Related JSON fields:
// "flag_restriction"
// "min_item_volume"
// "max_contains_volume"
//
// Functions:
// item_pocket::can_contain
// pocket_data::max_contains_volume
//
TEST_CASE( "pocket_with_item_flag_restriction", "[pocket][flag_restriction]" )
{
// Items with BELT_CLIP flag
item screwdriver( "test_screwdriver" );
item sonic( "test_sonic_screwdriver" );
item halligan( "test_halligan" );
item axe( "test_fire_ax" );
// Items without BELT_CLIP flag
item rag( "test_rag" );
item rock( "test_rock" );
// Ensure the tools have expected volume relationships
// (too small, minimum size, maximum size, too big)
REQUIRE( screwdriver.volume() < sonic.volume() );
REQUIRE( sonic.volume() < halligan.volume() );
REQUIRE( halligan.volume() < axe.volume() );
// Test pocket with BELT_CLIP flag
pocket_data data_belt( pocket_type::CONTAINER );
// Sonic screwdriver is the smallest item it can hold
data_belt.min_item_volume = sonic.volume();
// Halligan bar is the largest item it can hold
data_belt.volume_capacity = halligan.volume();
// Plenty of length and weight, since we only care about flag restrictions here
data_belt.max_item_length = 10_meter;
data_belt.max_contains_weight = 10_kilogram;
// TODO: Add test for pocket with multiple flag_restriction entries, and ensure
// items with any of those flags can be inserted in the pocket.
GIVEN( "pocket with BELT_CLIP flag restriction" ) {
data_belt.add_flag_restriction( flag_BELT_CLIP );
item_pocket pocket_belt( &data_belt );
GIVEN( "item has BELT_CLIP flag" ) {
REQUIRE( screwdriver.has_flag( flag_BELT_CLIP ) );
REQUIRE( sonic.has_flag( flag_BELT_CLIP ) );
REQUIRE( halligan.has_flag( flag_BELT_CLIP ) );
REQUIRE( axe.has_flag( flag_BELT_CLIP ) );
WHEN( "item volume is less than min_item_volume" ) {
REQUIRE( screwdriver.volume() < data_belt.min_item_volume );
THEN( "pocket cannot contain it, because it is too small" ) {
expect_cannot_contain( pocket_belt, screwdriver, "item is too small",
item_pocket::contain_code::ERR_TOO_SMALL );
}
THEN( "item cannot be inserted into the pocket" ) {
expect_cannot_insert( pocket_belt, screwdriver, "item is too small",
item_pocket::contain_code::ERR_TOO_SMALL );
}
}
WHEN( "item volume is equal to min_item_volume" ) {
REQUIRE( sonic.volume() == data_belt.min_item_volume );
THEN( "pocket can contain it" ) {
expect_can_contain( pocket_belt, sonic );
}
THEN( "item can be inserted successfully" ) {
expect_can_insert( pocket_belt, sonic );
}
}
WHEN( "item volume is equal to max_contains_volume" ) {
REQUIRE( halligan.volume() == data_belt.max_contains_volume() );
THEN( "pocket can contain it" ) {
expect_can_contain( pocket_belt, halligan );
}
THEN( "item can be inserted successfully" ) {
expect_can_insert( pocket_belt, halligan );
}
}
WHEN( "item volume is greater than max_contains_volume" ) {
REQUIRE( axe.volume() > data_belt.max_contains_volume() );
THEN( "pocket cannot contain it, because it is too big" ) {
expect_cannot_contain( pocket_belt, axe, "item is too big",
item_pocket::contain_code::ERR_TOO_BIG );
}
THEN( "item cannot be inserted into the pocket" ) {
expect_cannot_insert( pocket_belt, axe, "item is too big",
item_pocket::contain_code::ERR_TOO_BIG );
}
}
}
GIVEN( "item without BELT_CLIP flag" ) {
REQUIRE_FALSE( rag.has_flag( flag_BELT_CLIP ) );
REQUIRE_FALSE( rock.has_flag( flag_BELT_CLIP ) );
// Ensure they are not too large otherwise
REQUIRE_FALSE( rag.volume() > data_belt.max_contains_volume() );
REQUIRE_FALSE( rock.volume() > data_belt.max_contains_volume() );
THEN( "pocket cannot contain it, because it does not have the flag" ) {
expect_cannot_contain( pocket_belt, rag, "holster does not accept this item type or form factor",
item_pocket::contain_code::ERR_FLAG );
expect_cannot_contain( pocket_belt, rock, "holster does not accept this item type or form factor",
item_pocket::contain_code::ERR_FLAG );
}
}
}
}
// Holster
// -------
// Pockets with the "holster" attribute set to true may hold only a single item or stack.
//
// Related JSON fields:
// "holster"
// "max_item_length"
// "max_contains_volume"
// "max_contains_weight"
//
// Functions:
// item_pocket::can_contain
// item_pocket::insert_item
//
TEST_CASE( "holster_can_contain_one_fitting_item", "[pocket][holster]" )
{
// Start with a basic test handgun from data/mods/TEST_DATA/items.json
item glock( "test_glock" );
// Construct data for a holster to perfectly fit this gun
pocket_data data_holster( pocket_type::CONTAINER );
data_holster.holster = true;
data_holster.volume_capacity = glock.volume();
data_holster.max_item_length = glock.length();
data_holster.max_contains_weight = glock.weight();
GIVEN( "holster has enough volume, length, and weight capacity" ) {
// Use the perfectly-fitted holster data
item_pocket pocket_holster( &data_holster );
THEN( "it can contain the item" ) {
expect_can_contain( pocket_holster, glock );
}
THEN( "item can be successfully inserted" ) {
expect_can_insert( pocket_holster, glock );
}
}
GIVEN( "holster has not enough weight capacity" ) {
// Reduce perfect holster weight capacity by 1 gram
data_holster.max_contains_weight = glock.weight() - 1_gram;
item_pocket pocket_holster( &data_holster );
THEN( "it cannot contain the item, because it is too heavy" ) {
expect_cannot_contain( pocket_holster, glock, "item is too heavy",
item_pocket::contain_code::ERR_TOO_HEAVY );
}
THEN( "item cannot be successfully inserted" ) {
expect_cannot_insert( pocket_holster, glock, "item is too heavy",
item_pocket::contain_code::ERR_TOO_HEAVY );
}
}
GIVEN( "holster has not enough volume capacity" ) {
// Reduce perfect holster volume capacity by 1 ml
data_holster.volume_capacity = glock.volume() - 1_ml;
item_pocket pocket_holster( &data_holster );
THEN( "it cannot contain the item, because it is too big" ) {
expect_cannot_contain( pocket_holster, glock, "item is too big",
item_pocket::contain_code::ERR_TOO_BIG );
}
THEN( "item cannot be successfully inserted" ) {
expect_cannot_insert( pocket_holster, glock, "item is too big",
item_pocket::contain_code::ERR_TOO_BIG );
}
}
// TODO: Add test for item with longest_side greater than max_item_length
GIVEN( "holster already contains an item" ) {
// Put another item in the holster first
item_pocket pocket_holster( &data_holster );
expect_can_insert( pocket_holster, item( "test_glock" ) );
THEN( "it cannot contain the item, because holster can only hold one item" ) {
expect_cannot_contain( pocket_holster, glock, "holster already contains an item",
item_pocket::contain_code::ERR_NO_SPACE );
}
THEN( "another item cannot be successfully inserted" ) {
expect_cannot_insert( pocket_holster, glock, "holster already contains an item",
item_pocket::contain_code::ERR_NO_SPACE );
}
}
}
// Watertight pockets
// ------------------
// To contain liquids, a pocket must have the "watertight" attribute set to true. After a pocket has
// been partially filled with a liquid, adding a different liquid to it is prohibited, since liquids
// can't mix.
//
// Related JSON fields:
// "watertight"
//
// Functions:
// item_pocket::watertight
//
TEST_CASE( "pockets_containing_liquids", "[pocket][watertight][liquid]" )
{
// Liquids
item ketchup( "ketchup", calendar::turn_zero, item::default_charges_tag{} );
item mustard( "mustard", calendar::turn_zero, item::default_charges_tag{} );
// Non-liquids
item rock( "test_rock" );
item glock( "test_glock" );
// Large watertight container
pocket_data data_bucket( pocket_type::CONTAINER );
data_bucket.watertight = true;
data_bucket.volume_capacity = 10_liter;
data_bucket.max_item_length = 1_meter;
data_bucket.max_contains_weight = 10_kilogram;
GIVEN( "pocket is watertight" ) {
item_pocket pocket_bucket( &data_bucket );
REQUIRE( pocket_bucket.watertight() );
WHEN( "pocket is empty" ) {
REQUIRE( pocket_bucket.empty() );
THEN( "it can contain liquid items" ) {
expect_can_contain( pocket_bucket, ketchup );
expect_can_contain( pocket_bucket, mustard );
}
THEN( "it can contain non-liquid items" ) {
expect_can_contain( pocket_bucket, rock );
expect_can_contain( pocket_bucket, glock );
}
}
WHEN( "pocket already contains some liquid" ) {
expect_can_insert( pocket_bucket, ketchup );
REQUIRE_FALSE( pocket_bucket.empty() );
THEN( "it can contain more of the same liquid" ) {
expect_can_contain( pocket_bucket, ketchup );
}
THEN( "it cannot contain a different liquid" ) {
expect_cannot_contain( pocket_bucket, mustard, "can't mix liquid with contained item",
item_pocket::contain_code::ERR_LIQUID );
}
THEN( "it cannot contain a non-liquid" ) {
expect_cannot_contain( pocket_bucket, rock, "can't put non liquid into pocket with liquid",
item_pocket::contain_code::ERR_LIQUID );
}
}
WHEN( "pocket already contains a non-liquid" ) {
expect_can_insert( pocket_bucket, rock );
REQUIRE_FALSE( pocket_bucket.empty() );
THEN( "it can contain other non-liquids" ) {
expect_can_contain( pocket_bucket, glock );
}
THEN( "it cannot contain a liquid" ) {
expect_cannot_contain( pocket_bucket, ketchup, "can't mix liquid with contained item",
item_pocket::contain_code::ERR_LIQUID );
expect_cannot_contain( pocket_bucket, mustard, "can't mix liquid with contained item",
item_pocket::contain_code::ERR_LIQUID );
}
}
}
GIVEN( "pocket is not watertight" ) {
// Poke a hole in the bucket
data_bucket.watertight = false;
item_pocket pocket_bucket( &data_bucket );
THEN( "it cannot contain liquid items" ) {
expect_cannot_contain( pocket_bucket, ketchup, "can't contain liquid",
item_pocket::contain_code::ERR_LIQUID );
expect_cannot_contain( pocket_bucket, mustard, "can't contain liquid",
item_pocket::contain_code::ERR_LIQUID );
}
THEN( "it can still contain non-liquid items" ) {
expect_can_contain( pocket_bucket, rock );
expect_can_contain( pocket_bucket, glock );
}
}
}
// Airtight pockets
// ----------------
// To contain gases, a pocket must have the "airtight" attribute set to true.
//
// Related JSON fields:
// "airtight"
//
// Functions:
// item_pocket::airtight
//
TEST_CASE( "pockets_containing_gases", "[pocket][airtight][gas]" )
{
item gas( "test_gas", calendar::turn_zero, item::default_charges_tag{} );
// A potentially airtight container
pocket_data data_balloon( pocket_type::CONTAINER );
// Has capacity for several charges of gas
data_balloon.volume_capacity = 4 * gas.volume();
data_balloon.max_contains_weight = 4 * gas.weight();
// Avoid any length restrictions
data_balloon.max_item_length = 1_meter;
GIVEN( "pocket is airtight" ) {
data_balloon.airtight = true;
item_pocket pocket_balloon( &data_balloon );
REQUIRE( pocket_balloon.airtight() );
THEN( "it can contain a gas" ) {
expect_can_contain( pocket_balloon, gas );
}
// TODO: Test that mixing gases in a pocket is prohibited, like it is for liquids.
}
GIVEN( "pocket is not airtight" ) {
// Leaky balloon!
data_balloon.airtight = false;
item_pocket pocket_balloon( &data_balloon );
REQUIRE_FALSE( pocket_balloon.airtight() );
THEN( "it cannot contain a gas" ) {
expect_cannot_contain( pocket_balloon, gas, "can't contain gas",
item_pocket::contain_code::ERR_GAS );
}
}
}
// Pocket rigidity
// ---------------
// When a pocket is "rigid", the total volume of the pocket itself does not change, like it were a
// glass jar (having the same outer volume whether full or empty).
//
// When "rigid" is false (the default), the pocket grows larger as items fill it up.
//
// Related JSON fields:
// "rigid"
//
// Functions:
// item_pocket::can_contain
// item_pocket::insert_item
// item_pocket::item_size_modifier
//
TEST_CASE( "rigid_and_non-rigid_or_flexible_pockets", "[pocket][rigid][flexible]" )
{
item rock( "test_rock" );
// Pocket with enough space for 2 rocks
pocket_data data_sock( pocket_type::CONTAINER );
data_sock.volume_capacity = 2 * rock.volume();
// Can hold plenty of length and weight (we only care about volume here)
data_sock.max_item_length = 5 * rock.length();
data_sock.max_contains_weight = 10 * rock.weight();
GIVEN( "a non-rigid pocket" ) {
// Sock is freshly washed, so it's non-rigid
data_sock.rigid = false;
item_pocket pocket_sock( &data_sock );
WHEN( "pocket is empty" ) {
REQUIRE( pocket_sock.empty() );
THEN( "it can contain an item" ) {
expect_can_contain( pocket_sock, rock );
}
THEN( "item_size_modifier is zero" ) {
CHECK( pocket_sock.item_size_modifier() == 0_ml );
}
}
WHEN( "pocket is partially filled" ) {
// One rock should fill the sock half-way
expect_can_insert( pocket_sock, item( "test_rock" ) );
REQUIRE_FALSE( pocket_sock.empty() );
THEN( "it can contain another item" ) {
expect_can_contain( pocket_sock, rock );
}
THEN( "item_size_modifier is the contained item volume" ) {
CHECK( pocket_sock.item_size_modifier() == rock.volume() );
}
}
WHEN( "pocket is full" ) {
// Two rocks should be enough to fill it
expect_can_insert( pocket_sock, item( "test_rock" ) );
expect_can_insert( pocket_sock, item( "test_rock" ) );
REQUIRE_FALSE( pocket_sock.empty() );
REQUIRE( pocket_sock.full( true ) );
THEN( "it cannot contain another item" ) {
expect_cannot_contain( pocket_sock, rock, "not enough space",
item_pocket::contain_code::ERR_NO_SPACE );
}
THEN( "item_size_modifier is the total of contained item volumes" ) {
CHECK( pocket_sock.item_size_modifier() == 2 * rock.volume() );
}
}
}
GIVEN( "a rigid pocket" ) {
// Our sock got all crusty with zombie guts and is rigid now.
data_sock.rigid = true;
item_pocket pocket_sock( &data_sock );
REQUIRE( pocket_sock.rigid() );
WHEN( "pocket is empty" ) {
REQUIRE( pocket_sock.empty() );
THEN( "it can contain an item" ) {
expect_can_contain( pocket_sock, rock );
}
THEN( "item_size_modifier is zero" ) {
CHECK( pocket_sock.item_size_modifier() == 0_ml );
}
}
WHEN( "pocket is full" ) {
expect_can_insert( pocket_sock, item( "test_rock" ) );
expect_can_insert( pocket_sock, item( "test_rock" ) );
REQUIRE_FALSE( pocket_sock.empty() );
REQUIRE( pocket_sock.full( true ) );
THEN( "it cannot contain another item" ) {
expect_cannot_contain( pocket_sock, rock, "not enough space",
item_pocket::contain_code::ERR_NO_SPACE );
}
THEN( "item_size_modifier is still zero" ) {
CHECK( pocket_sock.item_size_modifier() == 0_ml );
}
}
}
}
// Corpses
// -------
//
// Functions:
// item_pocket::can_contain
// item_pocket::insert_item
//
TEST_CASE( "corpse_can_contain_anything", "[pocket][corpse]" )
{
item rock( "test_rock" );
item glock( "test_glock" );
pocket_data data_corpse( pocket_type::CORPSE );
GIVEN( "a corpse" ) {
item_pocket pocket_corpse( &data_corpse );
// For corpses, can_contain is true to prevent unnecessary "spilling"
THEN( "it can contain items" ) {
expect_can_contain( pocket_corpse, rock );
expect_can_contain( pocket_corpse, glock );
}
THEN( "items can be added to the corpse" ) {
expect_can_insert( pocket_corpse, rock );
expect_can_insert( pocket_corpse, glock );
}
}
}
// Sealed pockets
// --------------
//
// Functions:
// item_pocket::seal
// item_pocket::unseal
// item_pocket::sealed
// item_pocket::sealable
//
TEST_CASE( "sealed_containers", "[pocket][seal]" )
{
item water( "water" );
GIVEN( "sealable can" ) {
item can( "test_can_drink" );
// Ensure it has exactly one contained pocket, and get that pocket for testing
std::vector<item_pocket *> can_pockets = can.get_all_contained_pockets();
REQUIRE( !can_pockets.empty() );
REQUIRE( can_pockets.size() == 1 );
item_pocket &pocket = *can_pockets.front();
// Must be sealable, but not sealed initially
REQUIRE( pocket.sealable() );
REQUIRE_FALSE( pocket.sealed() );
// Sealing does not work when empty
WHEN( "pocket is empty" ) {
REQUIRE( pocket.empty() );
THEN( "it cannot be sealed" ) {
CHECK_FALSE( pocket.seal() );
CHECK_FALSE( pocket.sealed() );
// Remains sealable
CHECK( pocket.sealable() );
}
}
// But after inserting something, sealing can succeed
WHEN( "pocket contains something" ) {
REQUIRE( pocket.insert_item( water ).success() );