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

Add unit tests #24

Merged
merged 2 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions build-and-test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.\build.ps1
.\test.ps1
45 changes: 45 additions & 0 deletions src/Classes/Assert.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Assert"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
' Indicates whether the assertion was successful
Public AssertSuccessful As Boolean
' The message of this assertion
Public AssertMessage As String


' Asserts that both values are equal. If not, this will return a failed assertion.
' Arguments:
' - expected The value the caller expects to have
' - actual The actual value
' - message A message to show in case the assertion failed
Public Function AreEqual(ByRef expected, ByRef actual, ByRef message As String) As Assert
Set AreEqual = IsTrue(Not expected <> actual, message)

If Not AreEqual.AssertSuccessful Then
AreEqual.AssertMessage = AreEqual.AssertMessage & vbCrLf & " Expected: " & expected & vbCrLf & " Actual: " & actual
End If
End Function


' Asserts that the given value is true.
' Arguments:
' - truthy A value that can be seen as truthy. Very likely should be boolean, but maybe you like to live dangerously
' - message A message to show in case the assertion failed
Public Function IsTrue(ByRef truthy, ByRef message As String) As Assert
Set IsTrue = New Assert
IsTrue.AssertSuccessful = True
IsTrue.AssertMessage = message

If Not truthy Then
IsTrue.AssertSuccessful = False
IsTrue.AssertMessage = " Assert failed: " & message
End If
End Function


18 changes: 18 additions & 0 deletions src/Modules/Marshal.bas
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ Private Const integer0xFF As Integer = 256
Private Const long0xFF As Long = 256


' Converts the given int8 to a big endian byte string.
' Arguments:
' - value The byte to convert
Public Function Int8ToBytes(ByVal value As Byte) As String
Int8ToBytes = Chr(value)
End Function


' Converts the given big endian byte string to an int8
' Arguments:
' - bytes The bytes to convert
Public Function BytesToInt8(ByVal bytes As String) As Byte
BytesToInt8 = Asc(CharAt(bytes, 1))
End Function


' Converts the given int16 to a big endian byte string.
' Arguments:
' - value The byte to convert
Public Function Int16ToBytes(ByVal value As Integer) As String
Dim bytes As String * 2
Dim rest As Integer
Expand All @@ -28,11 +37,17 @@ Public Function Int16ToBytes(ByVal value As Integer) As String
End Function


' Converts the given big endian byte string to an int16
' Arguments:
' - bytes The bytes to convert
Public Function BytesToInt16(ByVal bytes As String) As Long
BytesToInt16 = Asc(CharAt(bytes, 1)) * long0xFF + Asc(CharAt(bytes, 2))
End Function


' Converts the given int32 to a big endian byte string.
' Arguments:
' - value The byte to convert
Public Function Int32ToBytes(ByVal value As Long) As String
Dim bytes As String * 4
Dim rest As Long
Expand All @@ -56,6 +71,9 @@ Public Function Int32ToBytes(ByVal value As Long) As String
End Function


' Converts the given big endian byte string to an int32
' Arguments:
' - bytes The bytes to convert
Public Function BytesToInt32(ByVal bytes As String) As Long
BytesToInt32 = Asc(CharAt(bytes, 1)) * long0xFF * long0xFF * long0xFF + Asc(CharAt(bytes, 2)) * long0xFF * long0xFF + Asc(CharAt(bytes, 3)) * long0xFF + Asc(CharAt(bytes, 4))
End Function
49 changes: 45 additions & 4 deletions src/Modules/StringExtensions.bas
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Attribute VB_Name = "StringExtensions"
Public Function TrimLeft(text As String, c As String) As String
' Trims the given character from the given text starting from the left.
' Arguments:
' - text The text to trim
' - c The character to trim
Public Function TrimLeft(ByVal text As String, c As String) As String
Dim textLength As Long
textLength = Len(text)

Expand All @@ -20,7 +24,11 @@ Public Function TrimLeft(text As String, c As String) As String
End Function


Public Function TrimRight(text As String, c As String) As String
' Trims the given character from the given text starting from the right.
' Arguments:
' - text The text to trim
' - c The character to trim
Public Function TrimRight(ByVal text As String, c As String) As String
Dim textLength As Long
textLength = Len(text)

Expand All @@ -41,34 +49,59 @@ Public Function TrimRight(text As String, c As String) As String
End Function


Public Function Substring(ByVal text As String, startIndex As Integer, Optional length As Variant) As String
' Gets the substring from the given text.
' Arguments:
' - text The text to get the substring from
' - startIndex The index of the first character of the substring
' - [length] The amount of characters to take from the original string
Public Function Substring(ByVal text As String, ByVal startIndex As Integer, Optional ByVal length As Variant) As String
If startIndex > Len(text) Then
startIndex = Len(text)
End If

If IsMissing(length) Or TypeName(length) <> "Integer" Or length > Len(text) Then
If IsMissing(length) Then
length = Len(text) - startIndex
End If

If length > Len(text) Then
length = Len(text) - startIndex
End If

Substring = Left(Right(text, Len(text) - startIndex), length)
End Function


' Checks whether the given text starts with the given sequence.
' Arguments:
' - text The text to check for the sequence
' - startText The text to be located at the start
Public Function StartsWith(ByVal text As String, ByVal startText As String) As Boolean
StartsWith = InStr(text, startText) = 1
End Function


' Checks whether the given text ends with the given sequence.
' Arguments:
' - text The text to check for the sequence
' - endText The text to be located at the end
Public Function EndsWith(ByVal text As String, ByVal endText As String) As Boolean
EndsWith = Right(text, Len(endText)) = endText
End Function


' Gets the character at the given index from the given string.
' Arguments:
' - text The text to get the character from
' - index The index of the character to get
Public Function CharAt(ByVal text As String, ByVal index As Integer) As String
CharAt = Mid(text, index, 1)
End Function


' Repeats the given string the given amount of times.
' Arguments:
' - text The text to repeat
' - count The amount of times to repeat the given string
Public Function Repeat(ByVal text As String, ByVal count As Long) As String
Repeat = ""

Expand All @@ -77,3 +110,11 @@ Public Function Repeat(ByVal text As String, ByVal count As Long) As String
Repeat = Repeat & text
Next
End Function


' Converts a regular string "foo" to a L"foo" string.
' Arguments:
' - text The string to convert
Public Function StringToWideString(ByVal text As String) As String
StringToWideString = StrConv(text, vbUnicode)
End Function
21 changes: 21 additions & 0 deletions src/Modules/TestMarshal.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Attribute VB_Name = "TestMarshal"
Public Function TestMarshalInt8() As Assert
Dim value As Byte
value = 10

Set TestMarshalInt8 = Assert.AreEqual(value, Marshal.BytesToInt8(Marshal.Int8ToBytes(value)), "marshals int8")
End Function

Public Function TestMarshalInt16() As Assert
Dim value As Long
value = 10

Set TestMarshalInt16 = Assert.AreEqual(value, Marshal.BytesToInt16(Marshal.Int16ToBytes(value)), "marshals int16")
End Function

Public Function TestMarshalInt32() As Assert
Dim value As Long
value = 10

Set TestMarshalInt32 = Assert.AreEqual(value, Marshal.BytesToInt32(Marshal.Int32ToBytes(value)), "marshals int32")
End Function
35 changes: 35 additions & 0 deletions src/Modules/TestStringExtensions.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Attribute VB_Name = "TestStringExtensions"
Public Function TestTrimLeft() As Assert
Set TestTrimLeft = Assert.AreEqual("bar", StringExtensions.TrimLeft("oobar", "o"), "left-trims strings")
End Function


Public Function TestTrimRight() As Assert
Set TestTrimRight = Assert.AreEqual("f", StringExtensions.TrimRight("foo", "o"), "right-trims strings")
End Function


Public Function TestStartsWith() As Assert
Set TestStartsWith = Assert.IsTrue(StringExtensions.StartsWith("foobar", "foo"), "detects string starts")
End Function


Public Function TestEndsWith() As Assert
Set TestEndsWith = Assert.IsTrue(StringExtensions.EndsWith("foobar", "bar"), "detects string ends")
End Function


Public Function TestCharAt() As Assert
Set TestCharAt = Assert.AreEqual("a", StringExtensions.CharAt("foobar", 5), "gets chars from strings")
End Function


Public Function TestSubstring() As Assert
Set TestSubstring = Assert.AreEqual("oo", StringExtensions.Substring("foobar", 1, 2), "gets parts from strings")
End Function


Public Function TestRepeat() As Assert
Set TestRepeat = Assert.AreEqual("aaa", StringExtensions.Repeat("a", 3), "repeats strings")
End Function

25 changes: 25 additions & 0 deletions src/Modules/TestUtils.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Attribute VB_Name = "TestUtils"
Public Type TestFileHandle
TestFilename As String
TestFilestream As Variant
End Type

' Creates a temporary test file.
Public Function CreateTestFile() As TestFileHandle
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

CreateTestFile.TestFilename = fso.BuildPath(ActiveDocument.Path, fso.GetTempName())
Set CreateTestFile.TestFilestream = fso.CreateTextFile(CreateTestFile.TestFilename, True)
End Function


' Deletes the given test file.
' Arguments:
' - fileHandle The file handle to delete
Public Sub DeleteTestFile(ByRef fileHandle As TestFileHandle)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

fso.DeleteFile fileHandle.TestFilename
End Sub
Loading