TIP: Click on subject to list as thread! ANSI
echo: visual_basic
to: All
from: Jeff
date: 2004-07-20 05:56:00
subject: Re: Getting a class insta

Hey Steve, my responses are weaved in to the message:

> I don't think that collection approach would work out well, since it
> would contain exactly one instance of each class. Every time you asked
> for a cat, you would get the same cat ...

In my case this is the desired outcome, since the collection contains
only testing classes, and the developer will only want to use each
testing class once (each testing class can contain several test
methods).  The framework follows the ideas of the XUnit testing
frameworks (as described in "JUnit: a cook's tour") -- I used an
implementation of the testing framework by Rob Harwood (see
http://c2.com/cgi/wiki?VbaUnit) as a starting point.


> If the classes were full blown ActiveX classes, duly registered in the
> registry with class IDs, you could use something like
>     Set NewObj = CreateObject("modulename.cat")
> I don't think that classes created in Access can be ActiveX classes.

Well that sucks.


> In fact, I'd be interested in how you are retrieving the list of classes
> at run time through VBA reflection. I have poked around just a little in
> VB extensibility, and it has a VBComponents collection available at
> design time which contains the defined classes, but I don't think it is
> available at run time.

Here's the idea:

I have a TestClassCatalog_CodeWriter that loops through all the code
modules.  If the module name ends in "_Tester", a line is added to the
module:

//////////////////////////////////////////

(a public method calls the getLoadCode() function and then rewrites
the TestClassCatalog function that creates the collection of Tester
objects) ...

Private Function getLoadCode() As String
    Dim ret As String
    
    ' Loop through all code modules, if it's a Tester object add a
line of code for it
    Dim c As VBIDE.VBComponent
    For Each c In Application.VBE.ActiveVBProject.VBComponents
        If isClassModule(c.Type) And isTestClassName(c.Name) Then
            ret = ret _
                & "  call " & ADD_TEST_ROUTINE &
" (new " & c.Name &
")" & vbCrLf
        End If
    Next

    getLoadCode = ret
End Function


Private Function isTestClassName(component_name As String) As Boolean
    Dim ret As Boolean
    ret = (Len(component_name) > 6) And (Right(component_name, 6) =
"Tester")
    isTestClassName = ret
End Function

Private Function isClassModule(component_type As
VBIDE.vbext_ComponentType) As Boolean
    isClassModule = (component_type = VBIDE.vbext_ct_ClassModule)
End Function


//////////////////////////////////////////////////


Later in the same project, all the public methods in a _Tester object
starting with "Public Sub Test" are pulled out into a collection,
which is then used to set up the final TestCases:


Private Function getTestMethods(class_name As String) As Collection
    Dim m As VBIDE.CodeModule
    Set m = Application.VBE.ActiveVBProject.VBComponents(class_name).CodeModule
    
    Dim ret As Collection
    Set ret = New Collection
    
    Dim line_num As Integer
    For line_num = 1 To m.CountOfLines
        If isTestMethodLine(m.Lines(line_num, 1)) Then
            ret.Add m.ProcOfLine(line_num, vbext_pk_Proc)
        End If
    Next
    
    If ret.count() = 0 Then
        Err.Raise vbObjectError, , "WARNING: no test methods in class
" & class_name & "!"
    End If
    
    Set getTestMethods = ret
    Set ret = Nothing
End Function


Private Function isTestMethodLine(Line As String) As Boolean
    isTestMethodLine = (UCase(Left(Trim(Line), 15)) = "PUBLIC SUB
TEST")
End Function


/////////////////////////////////////////////////////


As for it being available at design/runtime, I didn't think this was
an issue.  After all, the code is to help developers (and me) with
test-driven design, and so I'm constantly running the tests through
the debug window by typing:

vbaUnitMain.RunTests

(or, cuz I'm lazy):
rt


I'm going to contact Rob through e-mail right after I post this
message, and see if he can help me release this framework to a wider
audience.  I've found it very useful.

Jeff
---
þ RIMEGate(tm)/RGXPost V1.14 at BBSWORLD * Info{at}bbsworld.com

---
 * RIMEGate(tm)V10.2áÿ* RelayNet(tm) NNTP Gateway * MoonDog BBS
 * RgateImp.MoonDog.BBS at 7/20/04 5:56:30 AM
* Origin: MoonDog BBS, Brooklyn,NY, 718 692-2498, 1:278/230 (1:278/230)
SEEN-BY: 633/267 270
@PATH: 278/230 10/345 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.