/*
This is part of the ini.ahk.
Copyright (c) 2009, 20010 Tuncay D.
See the file COPYING.txt and COPYING.LESSER.txt for license and copying conditions.
*/

#NoEnv 
SetWorkingDir %A_ScriptDir% 
#Include ini.ahk

; --- BEGIN SECTION Configuration ---

DEBUG = 1 ; 0=show all steps | 1=show only on errors | 2=show only end result

; --- END OF SECTION Configuration ---

Main:
SupportPage = http://www.autohotkey.com/forum/viewtopic.php?p=280883
GoSub, setIni
TestNo = 0
ErrorCount = 0

/*
i1 =
(
[favorites]

)
i2 =
(
[favorites]
)
i3 =
(
[favorites]
test=xxx
)
;~ Loop, 3
;~ msgbox % ini_getSection(i%A_Index%, "favorites")
;~ Exit
Loop, 3
ini_insertKey(i%A_Index%, "favorites", "test=xxx")
msgbox "%i1%"`n--------`n"%i2%"`n--------`n"%i3%"
Exit
*/

;~ /*
;~ path := "E:\Tuncay\Projects\ahk\ini\test.ini"
;~ path := "..\test.ini"
;~ path := ini_load(ini, "test1.ini", 1)
;~ msgbox %errorlevel%`n%path%`n%ini%
;~ path := ini_save(ini, "test2.ini", 1)
;~ ;msgbox %errorlevel%`n%path%
;~ msgbox % ini_getAllValues(ini, "", c, 0)
;~ msgbox %c%
;Exit
;~ */

; --------+--------+--------+--------

TestTypesNo++
; Read value, equivalent command in Ahk:
;   IniRead, Result, file.ini, Tip, TimeStamp
Explan := "Get a value from a key. (1)"
ErrLineNum := A_LineNumber
Result := ini_getValue(ini, "", "TimeStamp")
isError := ErrorLevel ? 1 : 0
Expect := "Tue Mar 10 18:34:51 2009"
GoSub, ShowResult

Explan := "Get a value from a key. (2)"
ErrLineNum := A_LineNumber
Result := ini_getValue(ini, "Section1", "key2")
isError := ErrorLevel ? 1 : 0
Expect := "value3"
GoSub, ShowResult

Explan := "Get a value from a key. (3)"
ErrLineNum := A_LineNumber
Result := ini_getValue(ini, "WinLogIn", "Hide")
isError := ErrorLevel ? 1 : 0
Expect := "0"
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Read complete key, no command equivalent in Ahk.
Explan := "Get a complete key with name and value without loosing the`nspaces around the value. (1)"
ErrLineNum := A_LineNumber
Result := ini_getKey(ini, "", "TimeStamp") 
isError := ErrorLevel ? 1 : 0
Expect := "TimeStamp = Tue Mar 10 18:34:51 2009 "
GoSub, ShowResult

Explan := "Get a complete key with name and value without loosing the`nspaces around the value. (2)"
ErrLineNum := A_LineNumber
Result := ini_getKey(ini, "Section2", "key2") 
isError := ErrorLevel ? 1 : 0
Expect := "key2=value2"
GoSub, ShowResult

Explan := "Get a complete key with name and value without loosing the`nspaces around the value. (3)"
ErrLineNum := A_LineNumber
Result := ini_getKey(ini, "WinLogIn", "Domain") 
isError := ErrorLevel ? 1 : 0
Expect := "Domain="
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Read complete section, no equivalent command in Ahk.
Explan := "Get a complete section with section name and all keys. (1)"
ErrLineNum := A_LineNumber
Result := ini_getSection(ini, "Tip") 
isError := ErrorLevel ? 1 : 0
Expect =
(RTrim0 Join`n
[Tip]
TimeStamp = Tue Mar 10 18:34:51 2009 
StartUp=0
FilePos=3546
)
GoSub, ShowResult

Explan := "Get a complete section with section name and all keys. (2)"
ErrLineNum := A_LineNumber
Result := ini_getSection(ini, "Recent File List") 
isError := ErrorLevel ? 1 : 0
Expect =
(RTrim0 Join`n
[Recent File List]
File1=F:\Tuncay\NEW_10-03-2009_17-55-56.VIP
)
GoSub, ShowResult

Explan := "Get a complete section with section name and all keys. (3)"
ErrLineNum := A_LineNumber
Result := ini_getSection(ini, "WinLogIn")
isError := ErrorLevel ? 1 : 0
Expect =
(RTrim0 Join`n
[WinLogIn]

Hide=0
Domain=
UseDomain=0
)
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Update value, equivalent command in Ahk:
;   IniWrite, 128, file.ini, Tip, FilePos
Explan := "Updates the value of a key. (1)"
ErrLineNum := A_LineNumber
ini_replaceValue(ini, "", "FilePos", "128")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getValue(ini, "Tip", "FilePos")
Expect := "128"
GoSub, ShowResult

Explan := "Updates the value of a key. (2)"
ErrLineNum := A_LineNumber
ini_replaceValue(ini, "Colors_P0", "OsciWaveColor", "")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getValue(ini, "Colors_P0", "OsciWaveColor")
Expect := ""
GoSub, ShowResult

Explan := "Updates the value of a key. (3)"
ErrLineNum := A_LineNumber
ini_replaceValue(ini, "WinLogIn", "Domain", "autohotkey.com")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getSection(ini, "WinLogIn")
Expect = 
(RTrim0 Join`n
[WinLogIn]

Hide=0
Domain=autohotkey.com
UseDomain=0
)
GoSub, ShowResult

Explan := "Updates the value of a key. (4)"
ErrLineNum := A_LineNumber
ini_replaceValue(ini_2, "Tip", "TimeStamp", 2009)
isError := ErrorLevel ? 1 : 0
Result := ini_getValue(ini_2, "Tip", "TimeStamp")
Expect := 2009
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Replace existing key, equivalent command in Ahk:
;   IniDelete, file.ini, Tip, TimeStamp
;   IniWrite, 20090712093219, file.ini, Tip, Now
Explan := "Replace a key completly, with its name and value. (1)"
ErrLineNum := A_LineNumber
ini_replaceKey(ini, "", "TimeStamp", "Now=20090712093219")
isError := ErrorLevel ? 1 : 0
Result := ini_getSection(ini, "Tip")
Expect = 
(RTrim0 Join`n
[Tip]
Now=20090712093219
StartUp=0
FilePos=128
)
GoSub, ShowResult

Explan := "Replace a key completly, with its name and value. (2)"
ErrLineNum := A_LineNumber
ini_replaceKey(ini, "Factors", "Resampling_HighQualityFlags", "Samplerate=44100")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getSection(ini, "Factors")
Expect = 
(RTrim0 Join`n
[Factors]
Samplerate=44100

CDMarkerManager_FilterFlags=0 ; this is a comment
)
GoSub, ShowResult

Explan := "Replace a key completly, with its name and value. (3)"
ErrLineNum := A_LineNumber
ini_replaceKey(ini, "WinLogIn", "Domain")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getSection(ini, "WinLogIn")
Expect = 
(RTrim0 Join`n
[WinLogIn]

Hide=0
UseDomain=0
)
GoSub, ShowResult

Explan := "Replace a key completly, with its name and value. (4)"
ErrLineNum := A_LineNumber
ini_replaceKey(ini_2, "Tip", "TimeStamp", "TimeStamp= 1980")
isError := ErrorLevel ? 1 : 0
Result := ini_getValue(ini_2, "Tip", "TimeStamp")
Expect := " 1980"
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Replace section, equivalent command in Ahk:
;   IniDelete, ini.ini, Tip
;   IniWrite, Tuncay`, Fry, file.ini, Special Thanks, Testers
;   IniWrite, The community, file.ini, Special Thanks, Others
Explan := "Replaces a complete section with its name and all keys. (1)"
ErrLineNum := A_LineNumber
ini_replaceSection(ini, "Tip", "[ Special Thanks ]`nTesters = Tuncay, Fry`nOthers = The community")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getKey(ini, "Special Thanks", "Testers")
Expect := "Testers = Tuncay, Fry"
GoSub, ShowResult

Explan := "Replaces a complete section with its name and all keys. (2)"
ErrLineNum := A_LineNumber
ini_replaceSection(ini, "Recent File List", "[Recent File List]`nFile1=F:\testfile.ahk`nFile2=Z:\tempfile.tmp")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getKey(ini, "Recent File List", "File2")
Expect := "File2=Z:\tempfile.tmp"
GoSub, ShowResult

Explan := "Replaces a complete section with its name and all keys. (3)"
ErrLineNum := A_LineNumber
ini_replaceSection(ini, "WinLogIn", "[WinLogIn]`n`nHide=0`nUseDomain=0")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getSection(ini, "WinLogIn")
Expect = 
(RTrim0 Join`n
[WinLogIn]

Hide=0
UseDomain=0
)
GoSub, ShowResult

Explan := "Replaces a complete section with its name and all keys. (4)"
ErrLineNum := A_LineNumber
ini_replaceKey(ini_3, "Tip", "TimeStamp", "[Section1]`nKey1=Hello`nKey2=You!")
isError := ErrorLevel ? 1 : 0
Result := ini_getSection(ini_3, "Section1")
Expect =
(
[Section1]
Key1=Hello
Key2=You!
)
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Add key if not exist, equivalent command in Ahk:
;   IniWrite, Z:\, file.ini, Paths, temp
Explan := "Inserts a key with its name and value, if not exist. (1)"
ErrLineNum := A_LineNumber
ini_insertKey(ini, "Special Thanks", "AutoHotkey = Chris, Lexikos and others ...")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getKey(ini, "Special Thanks", "AutoHotkey")
Expect := "AutoHotkey = Chris, Lexikos and others ..."
GoSub, ShowResult

Explan := "Inserts a key with its name and value, if not exist. (2)"
ErrLineNum := A_LineNumber
ini_insertKey(ini, "Paths", "temp=Z:\")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getValue(ini, "Paths", "temp")
Expect := "Z:\"
GoSub, ShowResult

; Should not change.
Explan := "Inserts a key with its name and value, if not exist. (3)"
ErrLineNum := A_LineNumber
ini_insertKey(ini, "WinLogIn", "UseDomain=no")
isError := ErrorLevel ? 0 : 1
If Not isError
    Result := ini_getSection(ini, "WinLogIn")
Else
    Result := ""
Expect =
(RTrim0 Join`n
[WinLogIn]

Hide=0
UseDomain=0
)
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Add value to existing value, equivalent command in Ahk:
;   IniRead, Result, file.ini, Paths, temp
;   IniWrite, %Result%`, Z:\, file.ini, Paths, temp
Explan := "Inserts a value to an existing value. (1)"
ErrLineNum := A_LineNumber
ini_insertValue(ini, "", "AutoHotkey", " (Community)", true)
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getKey(ini, "Special Thanks", "AutoHotkey")
Expect := "AutoHotkey = Chris, Lexikos and others ... (Community)"
GoSub, ShowResult

Explan := "Inserts a value to an existing value. (2)"
ErrLineNum := A_LineNumber
ini_insertValue(ini, "Paths", "temp", "; C:\Windows\Temp")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getValue(ini, "Paths", "temp")
Expect := "Z:\; C:\Windows\Temp"
GoSub, ShowResult

; Should not change.
Explan := "Inserts a value to an existing value. (3)"
ErrLineNum := A_LineNumber
ini_insertValue(ini, "WinLogIn", "NotExistingKey", 1)
isError := ErrorLevel ? 0 : 1
If Not isError
    Result := ini_getSection(ini, "WinLogIn")
Expect =
(RTrim0 Join`n
[WinLogIn]

Hide=0
UseDomain=0
)
GoSub, ShowResult

; --------+--------+--------+--------

; Add section if not exist, equivalent command in Ahk:
;   IniWrite, TEST, file.ini, Test, test
TestTypesNo++
Explan := "Inserts a Section, if not exist. (1)"
ErrLineNum := A_LineNumber
ini_insertSection(ini, "Special Thanks", "this should not be added")
isError := ErrorLevel ? 0 : 1
If Not isError
    Result := ini_getSection(ini, "Special Thanks")
Expect =
(RTrim0 Join`n
[ Special Thanks ]
Testers = Tuncay, Fry
Others = The community
AutoHotkey = Chris, Lexikos and others ... (Community)
)
GoSub, ShowResult

Explan := "Inserts a Section, if not exist. (2)"
ErrLineNum := A_LineNumber
ini_insertSection(ini, "Test", "test=TEST")
isError := ErrorLevel ? 1 : 0
If Not isError
    Result := ini_getSection(ini, "Test")
Expect =
(RTrim0 Join`n
[Test]
test=TEST
)
GoSub, ShowResult

; Does not insert.
Explan := "Inserts a Section, if not exist. (3)"
ErrLineNum := A_LineNumber
ini_insertSection(ini, "Test", "hello=world")
isError := ErrorLevel ? 0 : 1
Result := ini_getSection(ini, "Test")
Expect =
(RTrim0 Join`n
[Test]
test=TEST
)
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Read all key names as a comma separated list. No command equivalent in Ahk.
Explan := "Get key names as a comma separated list. (1)"
ErrLineNum := A_LineNumber
Result := ini_getAllKeyNames(ini)
isError := ErrorLevel ? 1 : 0
Expect := "Testers,Others,AutoHotkey,Samplerate,CDMarkerManager_FilterFlags,OsciGridColor,OsciWaveColor,NewVIPBaseDirectory,TempFilePath,ProjectFilesDirectory,ImportDirectory,FormatFileDialog_RecentDir0,temp,key1,key2,key2,File1,File2,Hide,UseDomain,test"
GoSub, ShowResult

Explan := "Get count of keys. (2)"
ErrLineNum := A_LineNumber
ini_getAllKeyNames(ini, "Factors", Result)
isError := ErrorLevel ? 1 : 0
Expect := 2
GoSub, ShowResult

Explan := "Get key names as a comma separated list. (3)"
ErrLineNum := A_LineNumber
Result := ini_getAllKeyNames(ini, "Test")
isError := ErrorLevel ? 1 : 0
Expect := "test"
GoSub, ShowResult

Explan := "Get key names as a comma separated list. (5)"
ErrLineNum := A_LineNumber
Result := ini_getAllKeyNames(ini_2)
isError := ErrorLevel ? 1 : 0
Expect := "TimeStamp,File1,File2,k1,k2,k2,k1,this is a key,1"
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; Read all section names as a comma separated list. No command equivalent in Ahk.
Explan := "Get section names as a comma separated list. (1)"
ErrLineNum := A_LineNumber
Result := ini_getAllSectionNames(ini)
isError := ErrorLevel ? 1 : 0
Expect := "Special Thanks,Factors,Colors_P0,Paths,Section1,Section2,Recent File List,WinLogIn,Test"
GoSub, ShowResult

Explan := "Get section names as a comma separated list. (2)"
ErrLineNum := A_LineNumber
Result := ini_getAllSectionNames(ini_2)
isError := ErrorLevel ? 1 : 0
Expect := "Tip,Recent File List,s,nokey,t,g"
GoSub, ShowResult

Explan := "Get count of sections. (3)"
ErrLineNum := A_LineNumber
ini_getAllSectionNames(ini, Result)
isError := ErrorLevel ? 1 : 0
Expect := 9
GoSub, ShowResult

; --------+--------+--------+--------

TestTypesNo++
; All comments are lost and it is standardized. Unneeded lines and whitespaces are lost. No command equivalent in Ahk.
Explan := "Repair and build an ini from scratch. (1)"
ErrLineNum := A_LineNumber
ini_2 := ini_repair(ini_2, true, ";#", "`n")
isError := ErrorLevel ? 1 : 0
Result := ini_getSection(ini_2, "g")
Expect = 
(
[g]
this is a key=
1=
)
GoSub, ShowResult


; --------+--------+--------+--------

; Final Test.
TestNo++
TestTypesNo++
Explan := "All tests are succsessful, but something goes wrong.`nThe end result ini seems not to be what it should be!`nThere is a hidden error. May be a new line or space conflict."
ErrLineNum := A_LineNumber
If (ErrorCount = 0 AND (ini != ini_after) OR (ini_2 != ini_2_after))
{
    If (ini = ini_after)
    {
        ini := ini_2
    }
    MsgBox, 17, Unsuccsessful
            , Test Case %TestNo% (final):`n%Explan%`n`nFollowing content is the current ini content and will be`ncopied to clipboard, if clicked to ok:`n`n"%ini%"
    IfMsgBox Ok
        Clipboard := ini
}

; Result Info.
If ErrorCount
    MsgBox, 308, Test Results for ini functions
            , %A_ScriptFullPath%`n`n%ErrorCount% of %TestNo% test results are unsuccsessful. (%TestTypesNo% different types)`n`nDo you want to open the support page in your browser?`n"%SupportPage%"`n`n2009 (c) Tuncay
Else
    MsgBox, 324, Test Results for ini functions
            , %A_ScriptFullPath%`n`nAll %TestNo% tests are success. (%TestTypesNo% different types)`n`nDo you want to open the support page in your browser?`n"%SupportPage%"`n`n2009 (c) Tuncay
IfMsgBox Yes
    Run, %SupportPage%
    

Return ; END OF Main.


showResult:
TestNo++
If (Not isError 
    AND Expect == Result)
{
    If DEBUG = 0
        MsgBox, 65, Success, %A_ScriptFullPath%`n`nTest Case %TestNo%`nLine number: %ErrLineNum%`nErrorLevel: %ErrorLevel%`n`n%Explan%`n`nExpected:`n'%Expect%'`n`nResult:`n'%Result%'	
}
Else
{
    ErrorCount++
    If DEBUG <= 1
        MsgBox, 17, Unsuccsessful, %A_ScriptFullPath%`n`nTest Case %TestNo%`nLine number: %ErrLineNum%`nErrorLevel: %ErrorLevel%`n`n%Explan%`n`nExpected:`n'%Expect%'`n`nResult:`n'%Result%'
}
IfMsgBox Cancel
    DEBUG = 2
Return


setIni:
ini =
(% ` RTrim0 Join`n
[Tip]
TimeStamp = Tue Mar 10 18:34:51 2009 
StartUp=0
FilePos=3546
[Factors]
Resampling_HighQualityFlags=15

CDMarkerManager_FilterFlags=0 ; this is a comment
[Colors_P0]
OsciGridColor=12632256
 OsciWaveColor = 49152 ; Space is alt + 0160
[Paths]
NewVIPBaseDirectory=F:\Tuncay\Samplitude\
TempFilePath=  "G:\Tuncay\Temp\"
ProjectFilesDirectory=' F:\Tuncay\Samplitude\'
ImportDirectory=F:\Tuncay\

FormatFileDialog_RecentDir0=F:\Tuncay\
[Section1]
key1=value1
key2=value3
[Section2]
key2=value2
[Recent File List]
File1=F:\Tuncay\NEW_10-03-2009_17-55-56.VIP

    [WinLogIn]

Hide=0
Domain=
UseDomain=0
)


ini_after =
(% ` RTrim0 Join`n
[ Special Thanks ]
Testers = Tuncay, Fry
Others = The community
AutoHotkey = Chris, Lexikos and others ... (Community)
[Factors]
Samplerate=44100

CDMarkerManager_FilterFlags=0 ; this is a comment
[Colors_P0]
OsciGridColor=12632256
 OsciWaveColor =
[Paths]
NewVIPBaseDirectory=F:\Tuncay\Samplitude\
TempFilePath=  "G:\Tuncay\Temp\"
ProjectFilesDirectory=' F:\Tuncay\Samplitude\'
ImportDirectory=F:\Tuncay\

FormatFileDialog_RecentDir0=F:\Tuncay\
temp=Z:\; C:\Windows\Temp
[Section1]
key1=value1
key2=value3
[Section2]
key2=value2
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp

    [WinLogIn]

Hide=0
UseDomain=0
[Test]
test=TEST
)

ini_2 =
(% ` RTrim0 Join`n
    [Tip]
    TimeStamp = 20090716194758	
    [Recent File List]		
    File1=F:\testfile.ahk
    File2=Z:\tempfile.tmp	
    
[s]
k1 =|test1|test2|test3|test4|test5|test6|test7|test8|test9|test10
k2= this should never be accessible, but the second k2
k2= t    test its a string   #test   

[nokey]

[   t ]

k1  =           key
[g ]
this is a key   =
1=
        

)

ini_2_after =
(% ` RTrim0 Join`n
[Tip]
TimeStamp= 1980
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp	
[s]
k1=|test1|test2|test3|test4|test5|test6|test7|test8|test9|test10
k2= t    test its a string   
[nokey]
[t]
k1=           key
[g]
this is a key=
1=
)
ini_3 := ini_2
Return
