Ok, so to use test driven development here, I will start writing tests to describe the behaviors of the control I want and then code to make the tests pass. I copied the code from the module from last time with the TestCustomTags Sub to a new Test module I created with RubberDuckVBA. here is the Test code, most of which is the boiler plate created when RubberDuck built the module:
'@TestMethod("GivenCustomTagsObject")
Private Sub WhenPassedControl_ThenReturnsName()
On Error GoTo TestFail
'Arrange:
Dim testCT As CustomTags
Set testCT = New CustomTags
Dim ctl As Access.Control
Set ctl = Form_TestForm.TestControl
Dim strResult As String
'Act:
strResult = testCT(ctl)
'Assert:
Assert.AreEqual "TestControl", strResult
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub
Now of course, I want to change this test right away because although it passes with my existing code, testCT(ctl) is REALLY going to return a singular CustomTag object.
Here is the latest syntax I was imagining:
Dim Tags As CustomTags
Private Sub Form_Load()
' Alternate, somewhat simple custom tags
Tags(Me.txtName)("OriginalTop") = Me.txtName.Top
End Sub
' CustomTag class module
Option Compare Database
Option Explicit
'@DefaultMember
Public Function Value(TagName As String) As String
Value = ""
End Function
So now I can change my test to this:
'@TestMethod("GivenCustomTagsObject")
Private Sub WhenPassedControlAndString_ThenReturnsEmptyString()
On Error GoTo TestFail
'Arrange:
Dim testCT As CustomTags
Set testCT = New CustomTags
Dim ctl As Access.Control
Set ctl = Form_TestForm.TestControl
Dim strResult As String
'Act:
strResult = testCT(ctl)("MyTag")
'Assert:
Assert.AreEqual "", strResult
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub
So now this updated test currently causes a compile error of a type mismatch because the default member of TagControls is returning a string. So I updated the code to return a new CustomTag object like so:
'Class module CustomTags
Option Compare Database
Option Explicit
'@DefaultMember
Public Function Controls(ctl As Access.Control) As CustomTag
Set Controls = New CustomTag
End Function
So now the code compiles AND my test passes. So far this is accurate behavior for how I’d like the object to behave.
Next time I will setup a second test to set a value and return the same value. This will get us into working with some kind of internal collection or dictionary or something to store the various custom tags and their tag names and values.