Files
Bubberstation/code/modules/unit_tests/say.dm
san7890 a21d68c824 Fixes Linters (Three Month Old PR Edition) (#71873)
<!-- Write **BELOW** The Headers and **ABOVE** The comments else it may
not be viewable. -->
<!-- You can view Contributing.MD for a detailed description of the pull
request process. -->

## About The Pull Request

On the tin, we required usage of `/mob/living/carbon/human/consistent`
sometime last month and linters were never reran on
48e36ef2c7 (#69799), causing it to error
out and skew.

Here's a link of a test run _on master_ that has this error:
https://github.com/tgstation/tgstation/actions/runs/3660211321/jobs/6187088218


![image](https://user-images.githubusercontent.com/34697715/206786650-bd5c29c2-ff22-40ea-b4f8-b769c0b44626.png)

<!-- Describe The Pull Request. Please be sure every change is
documented or this can delay review and even discourage maintainers from
merging your PR! -->

## Why It's Good For The Game

CI is BROKEN I am going to SOB.

<!-- Argue for the merits of your changes and how they benefit the game,
especially if they are controversial and/or far reaching. If you can't
actually explain WHY what you are doing will improve the game, then it
probably isn't good for the game in the first place. -->

## Changelog

<!-- If your PR modifies aspects of the game that can be concretely
observed by players or admins you should add a changelog. If your change
does NOT meet this description, remove this section. Be sure to properly
mark your PRs to prevent unnecessary GBP loss. You can read up on GBP
and it's effects on PRs in the tgstation guides for contributors. Please
note that maintainers freely reserve the right to remove and add tags
should they deem it appropriate. You can attempt to finagle the system
all you want, but it's best to shoot for clear communication right off
the bat. -->

Nothing to concern players.

<!-- Both 🆑's are required for the changelog to work! You can put
your name to the right of the first 🆑 if you want to overwrite your
GitHub username as author ingame. -->
<!-- You can use multiple of the same prefix (they're only used for the
icon ingame) and delete the unneeded ones. Despite some of the tags,
changelogs should generally represent how a player might be affected by
the changes rather than a summary of the PR's contents. -->
2022-12-09 21:17:11 +01:00

161 lines
7.9 KiB
Plaintext

/// Test to verify message mods are parsed correctly
/datum/unit_test/get_message_mods
var/mob/host_mob
/datum/unit_test/get_message_mods/Run()
host_mob = allocate(/mob/living/carbon/human/consistent)
test("Hello", "Hello", list())
test(";HELP", "HELP", list(MODE_HEADSET = TRUE))
test(";%Never gonna give you up", "Never gonna give you up", list(MODE_HEADSET = TRUE, MODE_SING = TRUE))
test(".s Gun plz", "Gun plz", list(RADIO_KEY = RADIO_KEY_SECURITY, RADIO_EXTENSION = RADIO_CHANNEL_SECURITY))
test("...What", "...What", list())
/datum/unit_test/get_message_mods/proc/test(message, expected_message, list/expected_mods)
var/list/mods = list()
TEST_ASSERT_EQUAL(host_mob.get_message_mods(message, mods), expected_message, "Chopped message was not what we expected. Message: [message]")
for (var/mod_key in mods)
TEST_ASSERT_EQUAL(mods[mod_key], expected_mods[mod_key], "The value for [mod_key] was not what we expected. Message: [message]")
expected_mods -= mod_key
TEST_ASSERT(!expected_mods.len,
"Some message mods were expected, but were not returned by get_message_mods: [json_encode(expected_mods)]. Message: [message]")
/// Test to verify COMSIG_MOB_SAY is sent the exact same list as the message args, as they're operated on
/datum/unit_test/say_signal
/datum/unit_test/say_signal/Run()
var/mob/living/dummy = allocate(/mob/living)
RegisterSignal(dummy, COMSIG_MOB_SAY, PROC_REF(check_say))
dummy.say("Make sure the say signal gets the arglist say is past, no copies!")
/datum/unit_test/say_signal/proc/check_say(mob/living/source, list/say_args)
SIGNAL_HANDLER
TEST_ASSERT_EQUAL(REF(say_args), source.last_say_args_ref, "Say signal didn't get the argslist of say as a reference. \
This is required for the signal to function in most places - do not create a new instance of a list when passing it in to the signal.")
// For the above test to track the last use of say's message args.
/mob/living
var/last_say_args_ref
/// This unit test translates a string from one language to another depending on if the person can understand the language
/datum/unit_test/translate_language
var/mob/host_mob
/datum/unit_test/translate_language/Run()
host_mob = allocate(/mob/living/carbon/human/consistent)
var/surfer_quote = "surfing in the USA"
host_mob.grant_language(/datum/language/beachbum, spoken=TRUE, understood=FALSE) // can speak but can't understand
host_mob.add_blocked_language(subtypesof(/datum/language) - /datum/language/beachbum, LANGUAGE_STONER)
TEST_ASSERT_NOTEQUAL(surfer_quote, host_mob.translate_language(host_mob, /datum/language/beachbum, surfer_quote), "Language test failed. Mob was supposed to understand: [surfer_quote]")
host_mob.grant_language(/datum/language/beachbum, spoken=TRUE, understood=TRUE) // can now understand
TEST_ASSERT_EQUAL(surfer_quote, host_mob.translate_language(host_mob, /datum/language/beachbum, surfer_quote), "Language test failed. Mob was supposed NOT to understand: [surfer_quote]")
/// This runs some simple speech tests on a speaker and listener and determines if a person can hear whispering or speaking as they are moved a distance away
/datum/unit_test/speech
var/list/handle_speech_result = null
var/list/handle_hearing_result = null
var/mob/living/carbon/human/speaker
var/mob/living/carbon/human/listener
/datum/unit_test/speech/proc/handle_speech(datum/source, list/speech_args)
SIGNAL_HANDLER
TEST_ASSERT(speech_args[SPEECH_MESSAGE], "Handle speech signal does not have a message arg")
TEST_ASSERT(speech_args[SPEECH_SPANS], "Handle speech signal does not have spans arg")
TEST_ASSERT(speech_args[SPEECH_LANGUAGE], "Handle speech signal does not have a language arg")
TEST_ASSERT(speech_args[SPEECH_RANGE], "Handle speech signal does not have a range arg")
// saving hearing_args directly via handle_speech_result = speech_args won't work since the arg list
// is a temporary variable that gets garbage collected after it's done being used by procs
// therefore we need to create a new list and transfer the args
handle_speech_result = list()
handle_speech_result += speech_args
/datum/unit_test/speech/proc/handle_hearing(datum/source, list/hearing_args)
SIGNAL_HANDLER
// So it turns out that the `message` arg for COMSIG_MOVABLE_HEAR is super redundant and should probably
// be gutted out of both the Hear() proc and signal since it's never used
//TEST_ASSERT(hearing_args[HEARING_MESSAGE], "Handle hearing signal does not have a message arg")
TEST_ASSERT(hearing_args[HEARING_SPEAKER], "Handle hearing signal does not have a speaker arg")
TEST_ASSERT(hearing_args[HEARING_LANGUAGE], "Handle hearing signal does not have a language arg")
TEST_ASSERT(hearing_args[HEARING_RAW_MESSAGE], "Handle hearing signal does not have a raw message arg")
// TODO radio unit tests
//TEST_ASSERT(hearing_args[HEARING_RADIO_FREQ], "Handle hearing signal does not have a radio freq arg")
TEST_ASSERT(hearing_args[HEARING_SPANS], "Handle hearing signal does not have a spans arg")
TEST_ASSERT(hearing_args[HEARING_MESSAGE_MODE], "Handle hearing signal does not have a message mode arg")
// saving hearing_args directly via handle_hearing_result = hearing_args won't work since the arg list
// is a temporary variable that gets garbage collected after it's done being used by procs
// therefore we need to create a new list and transfer the args
handle_hearing_result = list()
handle_hearing_result += hearing_args
/datum/unit_test/speech/Run()
speaker = allocate(/mob/living/carbon/human/consistent)
listener = allocate(/mob/living/carbon/human/consistent)
// Hear() requires a client otherwise it will early return
var/datum/client_interface/mock_client = new()
listener.mock_client = mock_client
RegisterSignal(speaker, COMSIG_MOB_SAY, PROC_REF(handle_speech))
RegisterSignal(listener, COMSIG_MOVABLE_HEAR, PROC_REF(handle_hearing))
// speaking and whispering should be hearable
conversation(distance = 1)
// speaking should be hearable but not whispering
conversation(distance = 5)
// neither speaking or whispering should be hearable
conversation(distance = 10)
// Language test
speaker.grant_language(/datum/language/beachbum)
speaker.language_holder.selected_language = /datum/language/beachbum
listener.add_blocked_language(/datum/language/beachbum)
// speaking and whispering should be hearable
conversation(distance = 1)
// speaking should be hearable but not whispering
conversation(distance = 5)
// neither speaking or whispering should be hearable
conversation(distance = 10)
#define NORMAL_HEARING_RANGE 7
#define WHISPER_HEARING_RANGE 1
/datum/unit_test/speech/proc/conversation(distance = 0)
speaker.forceMove(run_loc_floor_bottom_left)
listener.forceMove(locate((run_loc_floor_bottom_left.x + distance), run_loc_floor_bottom_left.y, run_loc_floor_bottom_left.z))
var/pangram_quote = "The quick brown fox jumps over the lazy dog"
// speaking
speaker.say(pangram_quote)
TEST_ASSERT(handle_speech_result, "Handle speech signal was not fired")
TEST_ASSERT_EQUAL(islist(handle_hearing_result), distance <= NORMAL_HEARING_RANGE, "Handle hearing signal was not fired")
if(handle_hearing_result)
if(listener.has_language(handle_speech_result[SPEECH_LANGUAGE]))
TEST_ASSERT_EQUAL(pangram_quote, handle_hearing_result[HEARING_RAW_MESSAGE], "Language test failed. Mob was supposed to understand: [pangram_quote] using language [handle_speech_result[SPEECH_LANGUAGE]]")
else
TEST_ASSERT_NOTEQUAL(pangram_quote, handle_hearing_result[HEARING_RAW_MESSAGE], "Language test failed. Mob was NOT supposed to understand: [pangram_quote] using language [handle_speech_result[SPEECH_LANGUAGE]]")
handle_speech_result = null
handle_hearing_result = null
// whispering
speaker.whisper(pangram_quote)
TEST_ASSERT(handle_speech_result, "Handle speech signal was not fired")
TEST_ASSERT_EQUAL(islist(handle_hearing_result), distance <= WHISPER_HEARING_RANGE, "Handle hearing signal was not fired")
handle_speech_result = null
handle_hearing_result = null
#undef NORMAL_HEARING_RANGE
#undef WHISPER_HEARING_RANGE