Skip to content

Commit 33eaf8a

Browse files
Create interaction test harness. (#27643)
* Create interaction test harness. * retrofit tests, add janicart attack chain tests * extract puppeteer to separate file * move puppeteer creation to constructor
1 parent 5c549ad commit 33eaf8a

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

code/tests/_game_test_puppeteer.dm

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* A testing object used to control mobs in game tests.
3+
*
4+
* Puppeteers provide an easy way to create mobs and objects,
5+
* perform interactions in the same way that a player would,
6+
* and check the state of the mob during tests.
7+
*/
8+
/datum/test_puppeteer
9+
var/mob/living/carbon/puppet
10+
var/datum/game_test/origin_test
11+
12+
/datum/test_puppeteer/New(datum/game_test/origin_test_, carbon_type = /mob/living/carbon/human, turf/initial_location)
13+
if(!ispath(carbon_type, /mob/living/carbon/human))
14+
origin_test.Fail("unexpected puppeteer carbon type [carbon_type]", __FILE__, __LINE__)
15+
16+
if(!initial_location)
17+
initial_location = locate(179, 136, 1) // Center of admin testing area
18+
origin_test = origin_test_
19+
puppet = origin_test.allocate(carbon_type, initial_location)
20+
var/datum/mind/new_mind = new("interaction_test_[puppet.UID()]")
21+
new_mind.transfer_to(puppet)
22+
23+
/datum/test_puppeteer/proc/spawn_puppet_nearby(carbon_type = /mob/living/carbon/human)
24+
for(var/turf/T in RANGE_TURFS(1, puppet.loc))
25+
if(!is_blocked_turf(T, exclude_mobs = FALSE))
26+
return new/datum/test_puppeteer(origin_test, carbon_type, T)
27+
28+
origin_test.Fail("could not spawn puppeteer near [src]")
29+
30+
/datum/test_puppeteer/proc/spawn_obj_in_hand(obj_type)
31+
var/obj/new_obj = origin_test.allocate(obj_type, null)
32+
if(puppet.put_in_hands(new_obj))
33+
return new_obj
34+
35+
origin_test.Fail("could not spawn obj [obj_type] in hand of [puppet]")
36+
37+
/datum/test_puppeteer/proc/spawn_obj_nearby(obj_type)
38+
for(var/turf/T in RANGE_TURFS(1, puppet.loc))
39+
if(!is_blocked_turf(T, exclude_mobs = FALSE))
40+
return origin_test.allocate(obj_type, T)
41+
42+
origin_test.Fail("could not spawn obj [obj_type] near [src]")
43+
44+
/datum/test_puppeteer/proc/click_on(target, params)
45+
var/datum/test_puppeteer/puppet_target = target
46+
sleep(max(puppet.next_click, puppet.next_move) - world.time + 1)
47+
if(istype(puppet_target))
48+
puppet.ClickOn(puppet_target.puppet, params)
49+
return
50+
51+
puppet.ClickOn(target, params)
52+
53+
/datum/test_puppeteer/proc/spawn_mob_nearby(mob_type)
54+
for(var/turf/T in RANGE_TURFS(1, puppet))
55+
if(!is_blocked_turf(T, exclude_mobs = FALSE))
56+
var/mob/new_mob = origin_test.allocate(mob_type, T)
57+
return new_mob
58+
59+
/datum/test_puppeteer/proc/check_attack_log(snippet)
60+
for(var/log_text in puppet.attack_log_old)
61+
if(findtextEx(log_text, snippet))
62+
return TRUE
63+
64+
/datum/test_puppeteer/proc/set_intent(new_intent)
65+
puppet.a_intent_change(new_intent)
66+
67+
/datum/test_puppeteer/proc/rejuvenate()
68+
puppet.rejuvenate()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/datum/game_test/attack_chain_cult_dagger/Run()
2+
var/datum/test_puppeteer/cultist = new(src)
3+
var/datum/test_puppeteer/target = cultist.spawn_puppet_nearby()
4+
5+
cultist.puppet.mind.add_antag_datum(/datum/antagonist/cultist)
6+
cultist.spawn_obj_in_hand(/obj/item/melee/cultblade/dagger)
7+
cultist.set_intent("harm")
8+
cultist.click_on(target)
9+
10+
TEST_ASSERT(target.check_attack_log("Attacked with ritual dagger"), "non-cultist missing dagger attack log")
11+
TEST_ASSERT_NOTEQUAL(target.puppet.health, target.puppet.getMaxHealth(), "cultist attacking non-cultist with dagger caused no damage")
12+
13+
target.rejuvenate()
14+
target.puppet.mind.add_antag_datum(/datum/antagonist/cultist)
15+
16+
cultist.click_on(target)
17+
TEST_ASSERT_EQUAL(target.puppet.health, target.puppet.getMaxHealth(), "cultist attacking cultist with dagger caused damage")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/datum/game_test/attack_chain_vehicles/Run()
2+
var/datum/test_puppeteer/player = new(src)
3+
var/obj/item/key/janitor/janicart_key = player.spawn_obj_in_hand(/obj/item/key/janitor)
4+
var/obj/vehicle/janicart/janicart = player.spawn_obj_nearby(/obj/vehicle/janicart)
5+
6+
player.click_on(janicart)
7+
TEST_ASSERT_EQUAL(janicart.inserted_key, janicart_key, "did not find janicart key in vehicle")
8+
9+
var/move_delay = janicart.vehicle_move_delay
10+
player.spawn_obj_in_hand(/obj/item/borg/upgrade/vtec)
11+
player.click_on(janicart)
12+
TEST_ASSERT(janicart.vehicle_move_delay < move_delay, "VTEC upgrade not applied properly")
13+
14+
TEST_ASSERT_NULL(janicart.mybag, "unexpected trash bag on janicart")
15+
var/obj/item/storage/bag/trash/bag = player.spawn_obj_in_hand(/obj/item/storage/bag/trash)
16+
player.click_on(janicart)
17+
TEST_ASSERT_EQUAL(janicart.mybag, bag, "trash bag not attached to janicart")
18+
19+
var/obj/item/kitchen/knife/knife = player.spawn_obj_in_hand(/obj/item/kitchen/knife)
20+
player.set_intent("harm")
21+
player.click_on(janicart)
22+
TEST_ASSERT(janicart.obj_integrity < janicart.max_integrity, "knife attack not performed")
23+
24+
player.set_intent("help")
25+
player.click_on(janicart)
26+
TEST_ASSERT(knife in bag, "knife not placed in trash bag")

code/tests/game_tests.dm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
//Keep this sorted alphabetically
33

44
#ifdef GAME_TESTS
5+
#include "_game_test_puppeteer.dm"
56
#include "_game_test.dm"
67
#include "atmos\test_ventcrawl.dm"
8+
#include "attack_chain\test_attack_chain_cult_dagger.dm"
9+
#include "attack_chain\test_attack_chain_vehicles.dm"
710
#include "games\test_cards.dm"
811
#include "jobs\test_job_globals.dm"
912
#include "test_aicard_icons.dm"

code/tests/test_runner.dm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@
7070

7171
if(!test.succeeded)
7272
failed_any_test = TRUE
73-
test_logs[I] += test.fail_reasons
73+
for(var/fail_reason in test.fail_reasons)
74+
if(islist(fail_reason))
75+
var/text = fail_reason[1]
76+
var/file = fail_reason[2]
77+
var/line = fail_reason[3]
78+
79+
test_logs[I] += "[file]:[line]: [text]"
80+
else
81+
test_logs[I] += fail_reason
7482

7583
qdel(test)
7684

0 commit comments

Comments
 (0)