Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings - Fixes for whitespaces and comments, added unit tests #1622

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions addons/settings/fnc_parse.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Description:
Parameters:
_info - Content of file or clipboard to parse. <STRING>
_validate - Check if settings are valid. (optional, default: false) <BOOL>
_source - Can be "client", "mission" or "server" (optional, default: "") <STRING>

Returns:
Settings with values and priority states. <ARRAY>
Expand All @@ -22,14 +23,16 @@ params [["_info", "", [""]], ["_validate", false, [false]], ["_source", "", [""]
private _fnc_parseAny = {
params ["_string"];

// Remove whitespace so parseSimpleArray can handle arrays.
// Means that strings inside arrays don't support white space chars, but w/e.
// No such setting exists atm anyway.
_string = _string splitString _whitespace joinString "";

parseSimpleArray (["[", _string, "]"] joinString "") select 0
};

// If string comes from the "import" button, it is not preprocessed
// Therefore, remove single line comments (//)
_info = _info regexReplace ["/{2}[^\n\r]*((\n|\r)?)", ""];

// Remove multiline comments too (/* */)
_info = _info regexReplace ["/\*[^(\*/)]*((\*/)?)", ""];

// Remove whitespaces at the start and end of each statement, a statement being defined by the ";" at its end
private _parsed = [];

Expand Down
19 changes: 19 additions & 0 deletions addons/settings/test.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// -----------------------------------------------------------------------------
// Automatically generated by 'functions_config.rb'
// DO NOT MANUALLY EDIT THIS FILE!
// -----------------------------------------------------------------------------
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["parse"]

SCRIPT(test-settings);

// ----------------------------------------------------------------------------

LOG("=== Testing Settings ===");

{
private _test = execVM format ["\x\cba\addons\settings\test_%1.sqf", _x];
waitUntil { scriptDone _test };
} forEach TESTS;
94 changes: 94 additions & 0 deletions addons/settings/test_parse.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ----------------------------------------------------------------------------
#define DEBUG_MODE_FULL
#include "script_component.hpp"

SCRIPT(test_settings);

// ----------------------------------------------------------------------------

private ["_funcName", "_settings", "_result"];

LOG('Testing Settings');

// UNIT TESTS (parse)
_funcName = QFUNC(parse);
TEST_DEFINED(QFUNC(parse),"");

// Purposely weird formatting, must remain this way because newlines remain part of result
_settings = (preprocessFile "x\cba\addons\settings\test_settings_regular.sqf") call FUNC(parse);
_result = _settings isEqualTo [
["ace_advanced_ballistics_ammoTemperatureEnabled", true, 0],
["ace_advanced_ballistics_barrelLengthInfluenceEnabled", false, 2],
["ace_advanced_ballistics_bulletTraceEnabled", true, 1],
["ace_advanced_fatigue_enabled", false, 0],
["ace_advanced_fatigue_enableStaminaBar", true, 0],
["ace_advanced_fatigue_performanceFactor", 1, 1],
["ace_advanced_fatigue_terrainGradientFactor", 1, 2]
];
TEST_TRUE(_result,_funcName);

_settings = (preprocessFile "x\cba\addons\settings\test_settings_multiline.sqf") call FUNC(parse);
_result = _settings isEqualTo [
["test1", "[
"" item_1 "",
"" item_2 ""
]", 0],
["test2", "[
' item_1 ',
' item_2 '
]", 1],
["test3", "[
' item_1 ' , "" item_2 ""
]", 2],
["test4", "
[
'"" item_1 ""',
' item_2 '
]
", 0]
];
TEST_TRUE(_result,_funcName);

_settings = (preprocessFile "x\cba\addons\settings\test_settings_unicode.sqf") call FUNC(parse);
_result = _settings isEqualTo [["test1", "[Āā, Ăă, Ҙ, привет]", 1]];
TEST_TRUE(_result,_funcName);

_settings = (preprocessFile "x\cba\addons\settings\test_settings_strings.sqf") call FUNC(parse);
_result = _settings isEqualTo [
["test1", "", 0],
["test2", "", 0],
["test3", " T E S T " , 0],
["test4", " T E S T ", 0],
["test5", "[ ' t e s t ' , "" T E S T "" ]", 0],
["test6", "[ "" t e s t "" ,
"""" T E S T """" ]", 0],
["test7", "[ true, false ]", 0],
["test8", "[ "" item_1 "" , "" item_2 "" ]", 0],
["test9", "[ ' item_1 ' , ' item_2 ' ]", 0],
["test10", "[ ' item_1 ' , "" item_2 "" ]", 0],
["test11", "[ '"" item_1 ""' , ' item_2 ' ]", 0],
["test12", "[ ' item_1 ' , ""' item_2 '"" ]", 0]
Comment on lines +60 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tabs!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tabs!

Tabs inside or outside of the strings? I saw the github action complained about tabs, but I want the tests to run with tabs (to make sure they work - unless that's unnecessary?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I see now. @PabstMirror do we have a way to ignore validating parts of code?

I don't think we need to test for tabs though, as whitespace is whitespace to Arma, but I may be wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing CI now

];
TEST_TRUE(_result,_funcName);

// Don't preprocess for testing comments
_settings = (loadFile "x\cba\addons\settings\test_settings_comments.sqf") call FUNC(parse);
_result = _settings isEqualTo [
["test2", "[true,false]", 1],
["test4", "[ ' t e s t ' , "" T E S T "" ]", 0],
["ace_advanced_ballistics_ammoTemperatureEnabled", true, 0],
["ace_advanced_ballistics_barrelLengthInfluenceEnabled", true, 2],
["ace_advanced_ballistics_bulletTraceEnabled", true, 1]
];
TEST_TRUE(_result,_funcName);

_settings = (loadFile "x\cba\addons\settings\test_settings_comments_eof.sqf") call FUNC(parse);
_result = _settings isEqualTo [
["test1", "[""item_1"",""item_2""]", 1]
];
TEST_TRUE(_result,_funcName);

nil
35 changes: 35 additions & 0 deletions addons/settings/test_settings_comments.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
force test1 = "[""item_1"",""item_2""]";
*/

force test2 = "[true,false]";
/*
//forceforceforce = true;//"[""ACE_DAGR"",""ACE_microDAGR""]";
*/

//test3 = "[Āā,Ăă,Ҙ,привет]";
test4 = "[ ' t e s t ' , "" T E S T "" ]";
/*
test5 = '[ " t e s t ",

"" T E S T "" ]';
test6 = ''; // "settings,";
test7 = " T S T ";
*/


// ACE Advanced Ballistics
ace_advanced_ballistics_ammoTemperatureEnabled = true;
force force ace_advanced_ballistics_barrelLengthInfluenceEnabled = true;
force ace_advanced_ballistics_bulletTraceEnabled = true;

// ACE Advanced Fatigue
/*/*
ace_advanced_fatigue_enabled = true;
ace_advanced_fatigue_enableStaminaBar = true;
/*
force ace_advanced_fatigue_performanceFactor = 1;
force force ace_advanced_fatigue_terrainGradientFactor = 1;
*/

// End of file
5 changes: 5 additions & 0 deletions addons/settings/test_settings_comments_eof.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
force test1 = "[""item_1"",""item_2""]";

// ACE Advanced Fatigue
/*
force test2 = "[true,false]";
28 changes: 28 additions & 0 deletions addons/settings/test_settings_multiline.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test1 = "[
"" item_1 "",
"" item_2 ""
]";

force test2 = "[
' item_1 ',
' item_2 '
]";

force force test3 =

"[
' item_1 ' , "" item_2 ""
]"

;

test4 = "

[
'"" item_1 ""',
' item_2 '
]

"

;
10 changes: 10 additions & 0 deletions addons/settings/test_settings_regular.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ACE Advanced Ballistics
ace_advanced_ballistics_ammoTemperatureEnabled = true;
force force ace_advanced_ballistics_barrelLengthInfluenceEnabled = false;
force ace_advanced_ballistics_bulletTraceEnabled = true;

// ACE Advanced Fatigue
ace_advanced_fatigue_enabled = false;
ace_advanced_fatigue_enableStaminaBar = true;
force ace_advanced_fatigue_performanceFactor = 1;
force force ace_advanced_fatigue_terrainGradientFactor = 1;
25 changes: 25 additions & 0 deletions addons/settings/test_settings_strings.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test1 = "";

test2 = '';

test3 = ' T E S T ';

test4 = " T E S T ";

test5 = "[ ' t e s t ' , "" T E S T "" ]";

test6 = '[ " t e s t " ,

"" T E S T "" ]';

test7 = "[ true, false ]";

test8 = "[ "" item_1 "" , "" item_2 "" ]";

test9 = "[ ' item_1 ' , ' item_2 ' ]";

test10 = "[ ' item_1 ' , "" item_2 "" ]";

test11 = "[ '"" item_1 ""' , ' item_2 ' ]";

test12 = "[ ' item_1 ' , ""' item_2 '"" ]";
1 change: 1 addition & 0 deletions addons/settings/test_settings_unicode.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force test1 = "[Āā, Ăă, Ҙ, привет]";
Loading