2025-06-09 08:26:30 -07:00
|
|
|
#include "lib.arrays.h" // For arrays_equal, len_array, etc.
|
|
|
|
|
#include "test_utils.h" // For assertEquals, VALTYPE_STR, report_test_results
|
2026-07-10 15:10:05 -07:00
|
|
|
#include "sfall.h"
|
2025-06-09 08:26:30 -07:00
|
|
|
|
|
|
|
|
// To test: copy test.ini into the game folder before running
|
|
|
|
|
procedure ini_test_suite begin
|
|
|
|
|
variable result_array, expected_array, key_type, value_type, count, key_val_pair;
|
|
|
|
|
|
|
|
|
|
display_msg("--- Testing ini functions");
|
|
|
|
|
|
|
|
|
|
// Test Case 1: Valid file, valid section
|
|
|
|
|
result_array := get_ini_section("test.ini", "ValidSection");
|
2026-03-26 07:51:00 -07:00
|
|
|
call assertEquals("TC1 Size", len_array(result_array), 3);
|
|
|
|
|
call assertEquals("TC1 Key1", result_array["Key1"], "Value1");
|
|
|
|
|
call assertEquals("TC1 Key2", result_array["Key2"], "2");
|
|
|
|
|
call assertEquals("TC1 EmptyValue", result_array["EmptyValue"], "");
|
2025-06-09 08:26:30 -07:00
|
|
|
count := 0;
|
|
|
|
|
foreach key_val_pair in result_array begin
|
|
|
|
|
count += 1;
|
|
|
|
|
call assertEquals("TC1 Key Type " + count, typeof(key_val_pair[0]), VALTYPE_STR);
|
|
|
|
|
call assertEquals("TC1 Value Type " + count, typeof(key_val_pair[1]), VALTYPE_STR);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
// basic fetchers
|
|
|
|
|
call assertEquals("TC2 Specific Key1 Value", result_array["Key1"], "Value1");
|
|
|
|
|
call assertEquals("TC2 get_ini_setting string", get_ini_setting("test.ini|ValidSection|Key1"), 0);
|
|
|
|
|
call assertEquals("TC2 get_ini_string string", get_ini_string("test.ini|ValidSection|Key1"), "Value1");
|
|
|
|
|
call assertEquals("TC2 get_ini_setting int", get_ini_setting("test.ini|ValidSection|Key2"), 2);
|
|
|
|
|
call assertEquals("TC2 get_ini_string int", get_ini_string("test.ini|ValidSection|Key2"), "2");
|
2026-03-26 07:51:00 -07:00
|
|
|
call assertEquals("TC2 get_ini_setting empty int", get_ini_setting("test.ini|ValidSection|EmptyValue"), 0);
|
|
|
|
|
call assertEquals("TC2 get_ini_string empty string", get_ini_string("test.ini|ValidSection|EmptyValue"), "");
|
2025-06-09 08:26:30 -07:00
|
|
|
|
2026-07-10 15:10:05 -07:00
|
|
|
// Regression: sfall resolves leading-backslash paths relative to the game root.
|
|
|
|
|
result_array := get_ini_section("\\test.ini", "ValidSection");
|
|
|
|
|
call assertEquals("TC2B Size", len_array(result_array), 3);
|
|
|
|
|
call assertEquals("TC2B Key1", result_array["Key1"], "Value1");
|
|
|
|
|
call assertEquals("TC2B get_ini_setting int", get_ini_setting("\\test.ini|ValidSection|Key2"), 2);
|
|
|
|
|
call assertEquals("TC2B get_ini_string string", get_ini_string("\\test.ini|ValidSection|Key1"), "Value1");
|
|
|
|
|
result_array := get_ini_sections("\\test.ini");
|
|
|
|
|
call assertEquals("TC2B get_ini_sections size", len_array(result_array), 3);
|
|
|
|
|
|
2025-06-09 08:26:30 -07:00
|
|
|
// INI file not found
|
|
|
|
|
result_array := get_ini_section("nonexistent.ini", "AnySection");
|
|
|
|
|
call assertEquals("TC3 Size", len_array(result_array), 0);
|
|
|
|
|
call assertEquals("TC3 Is Map", array_key(result_array, -1), 1); // Should still be an associative array
|
2026-03-26 07:51:00 -07:00
|
|
|
call assertEquals("TC3 get_ini_setting missing file", get_ini_setting("notexist.ini|ValidSection|Key2"), -1);
|
|
|
|
|
call assertEquals("TC3 get_ini_setting missing key", get_ini_setting("test.ini|ValidSection|MissingKey"), -1);
|
2025-06-09 08:26:30 -07:00
|
|
|
call assertEquals("TC3 get_ini_string", get_ini_string("notexist.ini|ValidSection|Key2"), "");
|
|
|
|
|
|
|
|
|
|
// Valid file, section not found
|
|
|
|
|
result_array := get_ini_section("test.ini", "NonExistentSection");
|
|
|
|
|
call assertEquals("TC4 Size", len_array(result_array), 0);
|
|
|
|
|
call assertEquals("TC4 Is Map", array_key(result_array, -1), 1);
|
2026-03-26 07:51:00 -07:00
|
|
|
call assertEquals("TC4 get_ini_setting", get_ini_setting("test.ini|NonExistentSection|Key2"), -1);
|
2025-06-09 08:26:30 -07:00
|
|
|
call assertEquals("TC4 get_ini_string", get_ini_string("test.ini|NonExistentSection|Key2"), "");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Valid file, empty section
|
|
|
|
|
result_array := get_ini_section("test.ini", "EmptySection");
|
|
|
|
|
call assertEquals("TC5 Size", len_array(result_array), 0);
|
|
|
|
|
call assertEquals("TC5 Is Map", array_key(result_array, -1), 1);
|
2026-03-26 07:51:00 -07:00
|
|
|
call assertEquals("TC5 get_ini_setting", get_ini_setting("test.ini|EmptySection|Key2"), -1);
|
2025-06-09 08:26:30 -07:00
|
|
|
call assertEquals("TC5 get_ini_string", get_ini_string("test.ini|EmptySection|Key2"), "");
|
|
|
|
|
|
|
|
|
|
// set ini setting
|
|
|
|
|
set_ini_setting("test_set.ini|ValidSection|Key3", "OldValue");
|
|
|
|
|
call assertEquals("TC6 set_ini_setting 1", get_ini_string("test_set.ini|ValidSection|Key3"), "OldValue");
|
|
|
|
|
set_ini_setting("test_set.ini|ValidSection|Key3", "NewValue");
|
|
|
|
|
call assertEquals("TC6 set_ini_setting 2", get_ini_string("test_set.ini|ValidSection|Key3"), "NewValue");
|
|
|
|
|
set_ini_setting("test_set.ini|ValidSection|Key3", 2);
|
|
|
|
|
call assertEquals("TC6 set_ini_setting int", get_ini_setting("test_set.ini|ValidSection|Key3"), 2);
|
|
|
|
|
|
|
|
|
|
// get_init_sections
|
|
|
|
|
result_array := get_ini_sections("test.ini");
|
|
|
|
|
call assertEquals("TC7 get_ini_sections size", len_array(result_array), 3);
|
|
|
|
|
call assertTrue("TC7 get_ini_sections", scan_array(result_array, "TestMisc") >= 0);
|
|
|
|
|
|
2026-06-07 00:34:37 +02:00
|
|
|
// get_ini_config
|
|
|
|
|
result_array := get_ini_config_db("config\\config_from_db.cfg");
|
|
|
|
|
call assertEquals("TC8 get_ini_config_db size", len_array(result_array), 2);
|
|
|
|
|
call assertTrue("TC8 get_ini_config ValidSection", result_array.ValidSection != 0);
|
|
|
|
|
call assertEquals("TC8 get_ini_config_db ValidValue", result_array.ValidSection.ValidKey, "ValidValue");
|
|
|
|
|
|
2025-06-09 08:26:30 -07:00
|
|
|
call report_test_results("ini");
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
procedure start begin
|
|
|
|
|
call ini_test_suite();
|
|
|
|
|
end
|