Files
Bubberstation/code/modules/unit_tests/json_savefile_importing.dm
Zephyr 47ba13033f JSON Savefiles | Player Saves use JSON (#70492)
<!-- 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. -->
TODO:

- [x] DOCUMENT SHIT
- [x] UPDATE DOCUMENTATION

## About The Pull Request

Adds a new datum, which is intended to be a replacement for the stock
savefile type, json_savefile
As you can imagine, this is essentially just a wrapper around a json
file for reading/writing/manipulation that is intended to be a dropin
replacement for savefiles
It also have the ability to import stock savefiles and parse them into a
json tree

<!-- 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

Permission obtained from MSO and Mothblocks.

<!-- 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. -->
Not player facing, tested locally exhaustively to ensure it doesnt break
shit
🆑
/🆑

<!-- 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. -->

Co-authored-by: Kyle Spier-Swenson <kyleshome@gmail.com>
Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: san7890 <the@san7890.com>
2022-11-17 21:45:18 -08:00

84 lines
3.4 KiB
Plaintext

/**
* The job of this unit test is to ensure that save files are correctly imported from BYOND to JSON.
* It's a rather convoluted process and so this test ensures that something didn't fuck up somewhere.
*/
/datum/unit_test/json_savefiles
var/savefile/test_savefile
var/datum/json_savefile/json_savefile
var/list/basic_list
var/list/assoc_list
var/var_string
/datum/unit_test/json_savefiles/proc/setup()
var/path_byond_file = "data/json_savefile_test.sav"
var/path_json_file = "data/json_savefile_test.json"
if(fexists(path_byond_file))
fdel(path_byond_file)
if(fexists(path_json_file))
fdel(path_json_file)
test_savefile = new /savefile(path_byond_file)
json_savefile = new /datum/json_savefile(path_json_file)
var_string = random_nukecode()
basic_list = list(rand(), rand(), rand(), "3", "6", "null", "\proper house")
assoc_list = list("2" = rand(), "4" = "3", "341" = "15134123", "\[22\]\[\]\[\[")
test_savefile["basic_list"] << basic_list
test_savefile["assoc_list"] << assoc_list
test_savefile["null_value"] << null
test_savefile["empty_list"] << list()
test_savefile.cd = "/v1/v2"
test_savefile["var_string"] << var_string
/datum/unit_test/json_savefiles/Run()
setup()
// first, we import the file to json
json_savefile.import_byond_savefile(test_savefile)
// now we seperate out the different values
var/byond_basic_list = json_encode(basic_list)
var/json_basic_list = json_encode(json_savefile.get_entry("basic_list"))
TEST_ASSERT_EQUAL(byond_basic_list, json_basic_list, "didn't convert basic list correctly")
var/byond_assoc_list = json_encode(assoc_list)
var/json_assoc_list = json_encode(json_savefile.get_entry("assoc_list"))
TEST_ASSERT_EQUAL(byond_assoc_list, json_assoc_list, "didn't convert associative list correctly")
var/null_value = json_savefile.get_entry("null_value")
var/default_value = json_savefile.get_entry("this_key_doesnt_exist", "defval")
TEST_ASSERT_NULL(null_value, "read an invalid value for what should be null")
TEST_ASSERT_EQUAL(default_value, "defval", "didn't grab the default value for a non existant key")
var/empty_list = json_savefile.get_entry("empty_list")
if(!istype(empty_list, /list))
TEST_FAIL("empty_list was not a list")
else
if(length(empty_list))
TEST_FAIL("empty_list was not empty")
var/empty_list_check_default = json_savefile.get_entry("empty_list", "123")
TEST_ASSERT_NOTEQUAL(empty_list_check_default, "123", "grabbed the default value for a key when key exists in tree")
// Now we check to ensure dir traversal is working as intended
// we are expecting v1 -> v2 -> var_string
var/dir_v1 = json_savefile.get_entry("v1")
var/dir_v2 = dir_v1?["v2"]
var/dir_string = dir_v2?["var_string"]
TEST_ASSERT_EQUAL(dir_string, var_string, "didn't traverse dirs correctly")
var/runtime_check_string = random_nukecode()
json_savefile.auto_save = TRUE
json_savefile.set_entry("runtime_saving", runtime_check_string)
var/runtime_read = json_savefile.get_entry("runtime_saving")
TEST_ASSERT_EQUAL(runtime_check_string, runtime_read, "wrote and read the same key but got different values")
json_savefile.wipe()
runtime_read = json_savefile.get_entry("runtime_saving")
TEST_ASSERT_NULL(runtime_read, "wiped the tree but data remained")
json_savefile.load()
runtime_read = json_savefile.get_entry("runtime_saving")
TEST_ASSERT_EQUAL(runtime_check_string, runtime_read, "saved and read the same key but got different values, auto save didn't work as expected")