Files
Aurora.3/code/unit_tests/font_awesome_icons.dm
Cody Brittain 364622508d Added a unit test to create defines for Font Awesome entries. (#21738)
Added a unit test that automatically generates byond defines for Font
Awesome entries. I need this for future usage, and it's difficult to get
a list of Font Awesome entries anyway. This solves both problems in an
atomized PR.
2026-01-27 19:02:09 +00:00

104 lines
3.9 KiB
Plaintext

// ****************************************************************************
// * Should the Font Awesome helper file be out of date, follow these instructions:
// * 1. Remove the "//" in [_compile_options.dm], line 36 // #define MANUAL_UNIT_TEST
// * 2. Run the game locally, let the unit test generate the updated file
// * 3. Copy the generated file from "data/font_awesome_icons.dm" to "code/__DEFINES/font_awesome_icons.dm"
// * 4. Re-comment the line in [_compile_options.dm]
// ****************************************************************************
GLOBAL_LIST_EMPTY(allowed_icons)
ABSTRACT_TYPE(/datum/unit_test/font_awesome_icons)
name = "FONT AWESOME: template"
groups = list("generic")
/datum/unit_test/font_awesome_icons/font_awesome_css
name = "FONT AWESOME: All CSS entries shall be present in code."
var/font_awesome_css
/datum/unit_test/font_awesome_icons/font_awesome_css/start_test()
var/font_awesome_file = file('html/font-awesome/css/all.min.css')
if(isnull(font_awesome_file))
return TEST_FAIL("Font Awesome CSS file could not be found!")
font_awesome_css = file2text(font_awesome_file)
if(isnull(font_awesome_css))
return TEST_FAIL("Font Awesome CSS file could not be loaded!")
. = load_parse_verify()
if(. != UNIT_TEST_PASSED)
return .
. = generate_helper_dm_file()
return .
/**
* Loads the Font Awesome CSS file, parses it into a list of icon names, and compares it to the list of icons in code.
* If there are any differences, note them.
*/
/datum/unit_test/font_awesome_icons/font_awesome_css/proc/load_parse_verify()
TEST_NOTICE("CSS Actual: [length(font_awesome_css)]")
GLOB.allowed_icons = parse_fa_css_into_icon_list(font_awesome_css)
if (!isnull(GLOB.allowed_icons))
TEST_PASS("Successfully parsed [length(GLOB.allowed_icons)] icons from Font Awesome CSS file.")
return UNIT_TEST_PASSED
else
TEST_FAIL("Failed to parse any icons from Font Awesome CSS file.")
return UNIT_TEST_FAILED
/// Parses the given Font Awesome CSS file into a list of icon names.
/datum/unit_test/font_awesome_icons/font_awesome_css/proc/parse_fa_css_into_icon_list(css)
css = replacetext(css, "\n", "")
var/list/css_entries = splittext(css, "}")
var/list/icons = list()
for(var/entry in css_entries)
entry = replacetext(entry, "\t", "")
if(!length(entry))
continue
var/entry_contents = splittext(entry, "{")
var/list/entry_names = splittext(entry_contents[1], ",")
for(var/entry_name in entry_names)
entry_names -= entry_name
if(!findtext(entry_name, ":"))
continue
entry_name = splittext(entry_name, ":")[1]
if(!findtext(entry_name, ".fa-"))
continue
entry_name = replacetext(entry_name, ".fa-", "fa-")
entry_names |= entry_name
icons |= entry_names
return sort_list(icons)
/datum/unit_test/font_awesome_icons/font_awesome_css/proc/generate_helper_dm_file()
var/list/output = list()
output += "/* This file is automatically generated by the unit test. Do not edit it manually."
output += " * Generating this file is done by running the unit test locally, see the fail message for more details."
output += " * All valid font awesome icons should be here."
output += " */"
output += ""
for(var/icon in GLOB.allowed_icons)
var/icon_name = replacetext(icon, "fa-", "")
output += "#define FA_ICON_[uppertext(replacetext(icon_name, "-", "_"))] \"[icon]\"" // #undef FA_ICON_ // we have this here to avoid define_sanity throwing a fit
var/output_file = "[output.Join("\n")]\n"
rustg_file_write(output_file, "data/font_awesome_icons.dm")
var/current = file2text('code/__DEFINES/font_awesome_icons.dm')
if(current == output_file)
TEST_PASS("Font Awesome helper file is up to date.")
return UNIT_TEST_PASSED
TEST_FAIL("Font Awesome helper file is out of date. Check instructions in code/unit_Tests/font_awesome_icons.dm to regenerate it.")
return UNIT_TEST_FAILED
// ============================================================================