Here is another simple code kata produced for me by CoPilot. Thanks CoPilot!
Task:
We need a function that can reverse a given string.
Examples (input → output):
"hello"
→"olleh"
"VBA"
→"ABV"
"12345"
→"54321"
Ok, so first I will create the test cases and then create a generalized function to take a string as an argument and provide the reversed string as the returned value.
Here are my tests, very similar to the module I created for the first kata:
'@TestModule '@Folder("Tests") Option Compare Database Option Explicit Option Private Module #Const LateBind = LateBindTests #If LateBind Then Private Assert As Object Private Fakes As Object #Else Private Assert As Rubberduck.AssertClass Private Fakes As Rubberduck.FakesProvider #End If '@ModuleInitialize Private Sub ModuleInitialize() 'this method runs once per module. #If LateBind Then Set Assert = CreateObject( "Rubberduck.AssertClass") Set Fakes = CreateObject( "Rubberduck.FakesProvider") #Else Set Assert = New Rubberduck.AssertClass Set Fakes = New Rubberduck.FakesProvider #End If End Sub '@ModuleCleanup Private Sub ModuleCleanup() 'this method runs once per module. Set Assert = Nothing Set Fakes = Nothing End Sub '@TestMethod("ReverseString") Private Sub Test_hello() On Error GoTo TestFail 'Arrange: Dim paramIn As String Dim funcOut As String paramIn = "hello" 'Act: funcOut = ReverseString(paramIn) 'Assert: Assert.AreEqual "olleh",funcOut TestExit: '@Ignore UnhandledOnErrorResumeNext On Error Resume Next Exit Sub TestFail: Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description Resume TestExit End Sub '@TestMethod("ReverseString") Private Sub Test_VBA() On Error GoTo TestFail 'Arrange: Dim paramIn As String Dim funcOut As String paramIn = "VBA" 'Act: funcOut = ReverseString(paramIn) 'Assert: Assert.AreEqual "ABV",funcOut TestExit: '@Ignore UnhandledOnErrorResumeNext On Error Resume Next Exit Sub TestFail: Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description Resume TestExit End Sub '@TestMethod("ReverseString") Private Sub Test_12345() On Error GoTo TestFail 'Arrange: Dim paramIn As String Dim funcOut As String paramIn = "12345" 'Act: funcOut = ReverseString(paramIn) 'Assert: Assert.AreEqual "54321",funcOut TestExit: '@Ignore UnhandledOnErrorResumeNext On Error Resume Next Exit Sub TestFail: Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description Resume TestExit End Sub
And here is the CodeKata module with both the prior function and my new ReverseString function:
Option Compare Database Option Explicit Public Function IntegerToString(intVal As Integer) As String IntegerToString = intVal End Function Public Function ReverseString(forward As String) As String Dim i As Integer Dim reverse As String For i = Len(forward) To 1 Step -1 reverse = reverse & Mid(forward,i,1) Next i ReverseString = reverse End Functio
In this case I chose to do a loop to count down backwards from the length of the original string and recreate it using the mid function one character at a time. how many ways can you find to reverse a string?