* Dynamic 2021 (#56221) About The Pull Request Dynamic 2021, among much other things, splits the threat level into two separate budgets, one for round start and one for midround/latejoin. You can read the design doc, plus methodology and charts here: https://hackmd.io/@ tgstation/S1C4dYJkO. To server owners: this is incompatible with current dynamic.json configurations. This is not just because some fields have different values now, but because the older values are not balanced towards this system. Inidividual list of changes (as I remember them): Threat level is now split into two separate budgets, one for round start and one for midround/latejoin. In TESTING, you can now run dynamic simulations to see the roundstart picks. antag_cap is now based on an equation rather than a fixed list of antags per indice--the old system is not scalable. You can now pass it either a number for a fixed constant, or a list with "denominator" and an optional "offset" to fit the equation ceil(x / denominator) + offset, with x being population. Midround/latejoin timers are now vars on /datum/game_mode/dynamic itself, and thus can be configured by head admins per server. Removes some older, as far as I can tell completely unused admin tools relating to dynamic. autotraitor_cooldown is now properly respected from config, and is based on an exact time rather than on ticks (450 used to be 15 minutes, now it's just 15 MINUTES). The rulesets dynamic chooses are now in a dynamic.json file in round logs. Relies on tgstation/tgstation13.org#64 to be public. Adds more logging to the autotraitor ruleset for admins. Midround/latejoin chance modifiers are now configurable. Labelled "Do Not Merge", as the design doc outlines the specific playtesting schedule I would like to perform. Note to self: Remove the custom MOTD before merge. Why It's Good For The Game (Copied from design doc) Dynamic currently dumps as much threat as it can into round start threats. This means that rounds at ~50 threat can have an absurd amount of antagonists round start, while leaving very little threat for midround/latejoin antagonists. What inevitably happens is just one of those picked decides to murderbone, leading to the inevitable 20 minute shuttle call. Furthermore, once those antagonists are dead, Dynamic doesn’t have any threat left to spend on new antagonists that keep the round flowing, promoting the mentality of “antags dead, I’m bored, let’s leave”. The proposed solution of Dynamic 2021 is to split threat level into two separate budgets, one for round start and one for midround/latejoin. This split, like threat level, will also be done on a lorentz curve–meaning that while most rounds will have them evenly split, there is still the possibility of chaotic high antagonist round start rounds, or the possibility of a midround onslaught. * Dynamic 2021 Co-authored-by: Mothblocks <35135081+Jared-Fogle@users.noreply.github.com>
Unit Tests
What is unit testing?
Unit tests are automated code to verify that parts of the game work exactly as they should. For example, a test to make sure that the amputation surgery actually amputates the limb. These are ran every time a PR is made, and thus are very helpful for preventing bugs from cropping up in your code that would've otherwise gone unnoticed. For example, would you have thought to check that beach boys would still work the same after editing pizza? If you value your time, probably not.
On their most basic level, when UNIT_TESTS is defined, all subtypes of /datum/unit_test will have their Run proc executed. From here, if Fail is called at any point, then the tests will report as failed.
How do I write one?
- Find a relevant file.
All unit test related code is in code/modules/unit_tests. If you are adding a new test for a surgery, for example, then you'd open surgeries.dm. If a relevant file does not exist, simply create one in this folder, then #include it in _unit_tests.dm.
- Create the unit test.
To make a new unit test, you simply need to define a /datum/unit_test.
For example, let's suppose that we are creating a test to make sure a proc square correctly raises inputs to the power of two. We'd start with first:
/datum/unit_test/square/Run()
This defines our new unit test, /datum/unit_test/square. Inside this function, we're then going to run through whatever we want to check. Tests provide a few assertion functions to make this easy. For now, we're going to use TEST_ASSERT_EQUAL.
/datum/unit_test/square/Run()
TEST_ASSERT_EQUAL(square(3), 9, "square(3) did not return 9")
TEST_ASSERT_EQUAL(square(4), 16, "square(4) did not return 16")
As you can hopefully tell, we're simply checking if the output of square matches the output we are expecting. If the test fails, it'll report the error message given as well as whatever the actual output was.
- Run the unit test
Open code/_compile_options.dm and uncomment the following line.
//#define UNIT_TESTS //If this is uncommented, we do a single run though of the game setup and tear down process with unit tests in between
Then, run tgstation.dmb in Dream Daemon. Don't bother trying to connect, you won't need to. You'll be able to see the outputs of all the tests. You'll get to see which tests failed and for what reason. If they all pass, you're set!
How to think about tests
Unit tests exist to prevent bugs that would happen in a real game. Thus, they should attempt to emulate the game world wherever possible. For example, the quick swap sanity test emulates a real scenario of the bug it fixed occurring by creating a character and giving it real items. The unrecommended alternative would be to create special test-only items. This isn't a hard rule, the reagent method exposure tests create a test-only reagent for example, but do keep it in mind.
Unit tests should also be just that--testing units of code. For example, instead of having one massive test for reagents, there are instead several smaller tests for testing exposure, metabolization, etc.
The unit testing API
You can find more information about all of these from their respective doc comments, but for a brief overview:
/datum/unit_test - The base for all tests to be ran. Subtypes must override Run(). New() and Destroy() can be used for setup and teardown. To fail, use Fail(reason).
/datum/unit_test/proc/allocate(type, ...) - Allocates an instance of the provided type with the given arguments. Is automatically destroyed when the test is over. Commonly seen in the form of var/mob/living/carbon/human/human = allocate(/mob/living/carbon/human).
TEST_ASSERT(assertion, reason) - Stops the unit test and fails if the assertion is not met. For example: TEST_ASSERT(powered(), "Machine is not powered").
TEST_ASSERT_EQUAL(a, b, message) - Same as TEST_ASSERT, but checks if a == b. If not, gives a helpful message showing what both a and b were. For example: TEST_ASSERT_EQUAL(2 + 2, 4, "The universe is falling apart before our eyes!").
TEST_ASSERT_NOTEQUAL(a, b, message) - Same as TEST_ASSERT_EQUAL, but reversed.
TEST_FOCUS(test_path) - Only run the test provided within the parameters. Useful for reducing noise. For example, if we only want to run our example square test, we can add TEST_FOCUS(/datum/unit_test/square). Should never be pushed in a pull request--you will be laughed at.
Final Notes
- Writing tests before you attempt to fix the bug can actually speed up development a lot! It means you don't have to go in game and folllow the same exact steps manually every time. This process is known as "TDD" (test driven development). Write the test first, make sure it fails, then start work on the fix/feature, and you'll know you're done when your tests pass. If you do try this, do make sure to confirm in a non-testing environment just to double check.
- Make sure that your tests don't accidentally call RNG functions like
prob. Since RNG is seeded during tests, you may not realize you have until someone else makes a PR and the tests fail! - Do your best not to change the behavior of non-testing code during tests. While it may sometimes be necessary in the case of situations such as the above, it is still a slippery slope that can lead to the code you're testing being too different from the production environment to be useful.