Skip to content
Open
Changes from all commits
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
31 changes: 31 additions & 0 deletions Other/Citra_per_game_config/v2/CitraConfigHelpers_Test.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ AssertEqual(actual, expected, testName) {
}
}

TestSetKey() {
global stdout
stdout.WriteLine("Running TestSetKey...")

; Test 1: Add new key
content := "key1=value1"
result := SetKey(content, "key2", "value2")
AssertEqual(result, "key1=value1`nkey2=value2", "SetKey appends new key")

; Test 2: Update existing key
content := "key1=value1`nkey2=old_value"
result := SetKey(content, "key2", "new_value")
AssertEqual(result, "key1=value1`nkey2=new_value", "SetKey updates existing key")

; Test 3: Update key with special regex characters
content := "my.key[1]=old"
result := SetKey(content, "my.key[1]", "new")
AssertEqual(result, "my.key[1]=new", "SetKey escapes special regex characters in key")

; Test 4: Add to empty config
content := ""
result := SetKey(content, "key", "value")
AssertEqual(result, "`nkey=value", "SetKey handles empty config")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The expectation of a leading newline (\nkey=value) when the input configuration is empty appears to be codifying a bug in the SetKey function. Ideally, setting a key in an empty string should result in key=value without a preceding newline, as the current behavior creates an unnecessary blank line at the start of the configuration file.


; Test 5: Handle spaces before equals sign
content := "key = old"
result := SetKey(content, "key", "new")
AssertEqual(result, "key=new", "SetKey handles spaces before equals sign")
Comment on lines +44 to +47
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Consider adding a test case for values containing the $ character. Since SetKey uses RegExReplace with the value directly in the replacement string, a $ in the value (e.g., $1) will be interpreted as a backreference to a regex capture group, leading to incorrect configuration values (e.g., the key name being repeated in the value).

    ; Test 5: Handle spaces before equals sign
    content := "key    = old"
    result := SetKey(content, "key", "new")
    AssertEqual(result, "key=new", "SetKey handles spaces before equals sign")

    ; Test 6: Handle dollar signs in values (potential RegExReplace backreference issue)
    content := "key=old"
    result := SetKey(content, "key", "$1")
    AssertEqual(result, "key=$1", "SetKey handles dollar signs in values")

}

TestReplaceInFile() {
global stdout
stdout.WriteLine("Running TestReplaceInFile...")
Expand Down Expand Up @@ -64,6 +94,7 @@ TestReplaceInFile() {
}

TestReplaceInFile()
TestSetKey()

stdout.WriteLine("Tests Passed: " . testsPassed)
stdout.WriteLine("Tests Failed: " . testsFailed)
Expand Down
Loading