Skip to content

Commit

Permalink
Feature: GetItem, GetItemCount
Browse files Browse the repository at this point in the history
Functionality to enable easy work with separated values
  • Loading branch information
petrSchreiber committed Mar 12, 2016
1 parent f3a349c commit 37686f8
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 48 deletions.
18 changes: 18 additions & 0 deletions Module_Implementation.inc
Expand Up @@ -20,6 +20,24 @@ FUNCTION EnvironmentVariable_Get( sName AS STRING ) AS STRING

END FUNCTION

FUNCTION EnvironmentVariable_GetItem( sName AS STRING, itemSeparator AS STRING, itemIndex AS LONG ) AS STRING

LOCAL allItems AS STRING
allItems = EnvironmentVariable_Get(sName)

FUNCTION = PARSE$(allItems, itemSeparator, itemIndex)

END FUNCTION

FUNCTION EnvironmentVariable_GetItemCount( sName AS STRING, itemSeparator AS STRING ) AS LONG

LOCAL allItems AS STRING
allItems = EnvironmentVariable_Get(sName)

FUNCTION = PARSECOUNT(allItems, itemSeparator)

END FUNCTION

FUNCTION EnvironmentVariable_Exists( sName AS STRING ) AS LONG

FUNCTION = LEN(EnvironmentVariable_Get(sName)) > 0
Expand Down
60 changes: 55 additions & 5 deletions Module_thinBASICInterface.inc
Expand Up @@ -8,11 +8,13 @@

FUNCTION Module_LoadSymbols() AS LONG

thinBasic_LoadSymbol "EnVar_List" , %thinBasic_ReturnString , CODEPTR( Exec_EnVar_List ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Set" , %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_Set ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Get" , %thinBasic_ReturnString , CODEPTR( Exec_EnVar_Get ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Remove", %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_Remove ), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Exists", %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_Exists ), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_List" , %thinBasic_ReturnString , CODEPTR( Exec_EnVar_List ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Set" , %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_Set ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Get" , %thinBasic_ReturnString , CODEPTR( Exec_EnVar_Get ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_GetItem" , %thinBasic_ReturnString , CODEPTR( Exec_EnVar_GetItem ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_GetItemCount", %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_GetItemCount ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Remove" , %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_Remove ) , %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "EnVar_Exists" , %thinBasic_ReturnCodeLong, CODEPTR( Exec_EnVar_Exists ) , %thinBasic_ForceOverWrite

END FUNCTION

Expand Down Expand Up @@ -69,6 +71,54 @@ FUNCTION Exec_EnVar_Get() AS STRING

END FUNCTION

FUNCTION Exec_EnVar_GetItem() AS STRING

REGISTER parensSpecified AS LONG

LOCAL varName AS STRING
LOCAL itemIndex AS LONG
LOCAL itemSeparator AS STRING

parensSpecified = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString(varName)
IF thinBasic_CheckComma_Mandatory AND thinBasic_ErrorFree THEN
thinBasic_ParseString(itemSeparator)
IF thinBasic_CheckComma_Mandatory AND thinBasic_ErrorFree THEN
itemIndex = thinBasic_Parse1Number

IF parensSpecified THEN
thinBasic_CheckCloseParens_Mandatory
END IF

FUNCTION = EnvironmentVariable_GetItem(varName, itemSeparator, itemIndex)

END IF

END IF

END FUNCTION

FUNCTION Exec_EnVar_GetItemCount() AS LONG

REGISTER parensSpecified AS LONG

LOCAL varName AS STRING
LOCAL itemSeparator AS STRING

parensSpecified = thinBasic_CheckOpenParens_Optional
thinBasic_ParseString(varName)
IF thinBasic_CheckComma_Optional AND thinBasic_ErrorFree THEN
thinBasic_ParseString(itemSeparator)
END IF

IF parensSpecified THEN
thinBasic_CheckCloseParens_Mandatory
END IF

FUNCTION = EnvironmentVariable_GetItemCount(varName, itemSeparator)

END FUNCTION

FUNCTION Exec_EnVar_Remove() AS LONG

LOCAL varName AS STRING
Expand Down
124 changes: 92 additions & 32 deletions UnitTests/UnitTest_EnVar.tbasic
Expand Up @@ -5,43 +5,54 @@ Uses "Console", "File"

$FILE_INI = APP_SourcePath + "test.ini"

' -- Main body of our testing engine
Function TBMain()

' -- Initialize testing engine
Print "Testing thinBasic_EnvVar feature set..."
ut_Initialize()

' -- Automatically launch all possible tests
ut_LaunchTests()

' -- Report failures found in report, if any
Long nFailures = ut_GetFailureCount()
Long i

If nFailures Then
PrintL

For i = 1 To nFailures
PrintL "#"+i
PrintL "Test: " + ut_GetFailureTestName(i)
PrintL "Assert: " + ut_GetFailureAssertType(i)
PrintL "Description: " + ut_GetFailureDescription(i)
PrintL "Comment: " + ut_GetFailureComment(i)
PrintL
Next
Else
PrintL "PASSED"
End If

' -- Save results to default file
ut_SaveLog()

' -- Release engine

ut_LaunchTests("")

ut_Release()


If ut_FailCount Then
ut_SaveLog()
PrintL
PrintL
PrintL "See the following file for all the details:" In 15
PrintL ut_logFileName In 14
PrintL
PrintL "Brief overview of " + ut_FailCount + " failures:" In 11
PrintUnitTestIssues(ut_logFileName)

Else
PrintL "PASSED" In 10
End If

WaitKey

End Function

End Function

Function PrintUnitTestIssues(sourceFile As String)

String content = FILE_Load(ut_logFileName)
String unitTest, failure

Long issueCount = Grab$(content, "<failCount>", "</failCount>")

Long i
For i = 1 To issueCount
unitTest = Grab$(content, "<testName>", "</testName>", i)
failure = Grab$(content, "<description>", "</description>", i)

PrintL unitTest In 15
PrintL failure In 12
PrintL

Next

End Function

' -----------------------------------------------------------------------------

Function test_Teardown()
enVar_Remove("enVarUnitTest")
Expand Down Expand Up @@ -75,6 +86,55 @@ Function test_Get_PairPassed_PairRead()

End Function

Function test_GetItem_3ValuesPassed_AllRetrieveable()

enVar_Set ("enVarUnitTest", "One,Two,Three")
ut_assertEqualText("One" , enVar_GetItem("enVarUnitTest", ",", 1))
ut_assertEqualText("Two" , enVar_GetItem("enVarUnitTest", ",", 2))
ut_assertEqualText("Three", enVar_GetItem("enVarUnitTest", ",", 3))

End Function

Function test_GetItem_3ValuesPassed_AllRetrieveableWithNegativeIndex()

enVar_Set ("enVarUnitTest", "One,Two,Three")
ut_assertEqualText("One" , enVar_GetItem("enVarUnitTest", ",", -3))
ut_assertEqualText("Two" , enVar_GetItem("enVarUnitTest", ",", -2))
ut_assertEqualText("Three", enVar_GetItem("enVarUnitTest", ",", -1))

End Function

Function test_GetItem_3ValuesPassedEmptySeparatorSpecified_AllRetrievable()

enVar_Set ("enVarUnitTest", "One,Two,Three")
ut_assertEqualText("One" , enVar_GetItem("enVarUnitTest", "", 1))
ut_assertEqualText("Two" , enVar_GetItem("enVarUnitTest", "", 2))
ut_assertEqualText("Three", enVar_GetItem("enVarUnitTest", "", 3))

End Function

Function test_GetItem_3ValuesPassedRetrieving4th_GotEmptyString()

enVar_Set ("enVarUnitTest", "One,Two,Three")
ut_assertEqualText("", enVar_GetItem("enVarUnitTest", ",", 4))

End Function

Function test_GetItemCount_3ValuesPassed_CountOf3Detected()

enVar_Set ("enVarUnitTest", "One,Two,Three")
ut_assertEqual(3, enVar_GetItemCount("enVarUnitTest", ","))

End Function

Function test_GetItemCount_3ValuesPassedNoSeparator_CountOf3Detected()

enVar_Set ("enVarUnitTest", "One,Two,Three")
ut_assertEqual(3, enVar_GetItemCount("enVarUnitTest"))

End Function


Function test_Remove_FirstPassed_NoLongerPresent()

String myVar = Parse$(enVar_List, ",", 1)
Expand Down
Binary file modified UnitTests/thinBasic_EnVar.dll
Binary file not shown.
16 changes: 10 additions & 6 deletions UnitTests/unitTesting.tbasicU
@@ -1,7 +1,7 @@
'
' Concept library for unit testing
' V1.5
' Petr Schreiber, 2014
' V1.6
' Petr Schreiber, 2015
'

' -- Modules
Expand Down Expand Up @@ -37,7 +37,11 @@ Function ut_Initialize()
"</header>" + $CRLF +
"<body>" + $CRLF

ut_logFileName = APP_SourcePath + "unitTest.txt"
ut_logFileName = APP_SourcePath + "unitTest.txt"

If FILE_Exists(ut_logFileName) Then
FILE_Kill(ut_logFileName)
End If

End Function

Expand Down Expand Up @@ -262,7 +266,7 @@ Function ut_assertIsTrue(ByVal aValue As Number, Optional aComment As String ) A

Else

internal_ut_RaiseFail("Expected " + aValue + " = " + Format$(TRUE) + "(= TRUE)" + IIf$(len(aComment), "[" + aComment + "]", ""))
internal_ut_RaiseFail("Expected " + Format$(TRUE) + "(= TRUE), got " + Format$(aValue) + IIf$(Len(aComment), "[" + aComment + "]", ""))
Return FALSE

End If
Expand All @@ -278,7 +282,7 @@ Function ut_assertIsFalse(ByVal aValue As Number, Optional aComment As String )

Else

internal_ut_RaiseFail("Expected " + aValue + " = " + Format$(FALSE) + "(= FALSE)" + IIf$(len(aComment), "[" + aComment + "]", ""))
internal_ut_RaiseFail("Expected " + Format$(FALSE) + "(= FALSE), got " + Format$(aValue) + IIf$(Len(aComment), "[" + aComment + "]", ""))
Return FALSE

End If
Expand Down Expand Up @@ -363,7 +367,7 @@ Function ut_assertError(ByVal aValue As Number, Optional aComment As String ) As

End If

End Function
End Function

'[] Test information
Function ut_GetFailureCount() As Long
Expand Down
10 changes: 5 additions & 5 deletions thinBasic_EnVar.bas
Expand Up @@ -10,19 +10,19 @@

' -- The version itself should reflect the targetted thinBasic version
#RESOURCE VERSIONINFO
#RESOURCE FILEVERSION 1, 9, 16, 0
#RESOURCE PRODUCTVERSION 1, 9, 16, 0
#RESOURCE FILEVERSION 1, 9, 16, 1
#RESOURCE PRODUCTVERSION 1, 9, 16, 1

#RESOURCE STRINGINFO "0409", "04B0"

#RESOURCE VERSION$ "CompanyName", "Petr Schreiber"
#RESOURCE VERSION$ "FileDescription", "Module for Environment Variables handling"
#RESOURCE VERSION$ "FileVersion", "1.9.16.0"
#RESOURCE VERSION$ "FileVersion", "1.9.16.1"
#RESOURCE VERSION$ "InternalName", "EnVars"
#RESOURCE VERSION$ "OriginalFilename", "ThinBASIC_EnVar.dll"
#RESOURCE VERSION$ "LegalCopyright", "Copyright � Petr Schreiber 2015"
#RESOURCE VERSION$ "LegalCopyright", "Copyright � Petr Schreiber 2016"
#RESOURCE VERSION$ "ProductName", "thinBasic_EnVar"
#RESOURCE VERSION$ "ProductVersion", "1.9.16.0"
#RESOURCE VERSION$ "ProductVersion", "1.9.16.1"
#RESOURCE VERSION$ "Comments", "Support site: http://www.thinbasic.com/"

' -----------------------------------------------------------------------------
Expand Down

0 comments on commit 37686f8

Please sign in to comment.