16. メニュー→ 挿入→ 標準モジュール
Option Explicit
Sub いちに2を加えると3になるべき()
Dim Calc As New Calc
Debug.Assert Calc.Add(1, 2) = 3
End Sub
サブプロシージャ名は数 字から始められないの で平仮名の「いち」
20. ちょっとリファクタ。イミディェイトに表示
Option Explicit
Sub いちに2を加えると3になるべき()
Dim CalcAs New Calc
Dim actual As Integer
actual = Calc.add(1, 2)
Debug.Assertactual = 3
Debug.Printactual
End Sub
24. Option Explicit
''usage
''Sub allTest()
'' Dim TestSuite As New TestSuite
''
'' With TestSuite
'' .testAll
'' '.testAll ("test") 'procedurePrefix
'' '.testAll (procedurePrefix =:"test", modeulePrefix :="テスト")
'' End With
''
''End Sub
Private Const VBEXT_CT_STDMODULE As Integer = 1
Public Sub testAll(Optional procedurePrefix As String = "", Optional modeulePrefix As String = "test")
Dim subProcedureNames As Collection 'Collection<String>
Set subProcedureNames = getSubProcedureNames(modeulePrefix)
Dim subProcedureName As String
Dim v
For Each v In subProcedureNames
subProcedureName = v
If Not isEmpty(procedurePrefix) Then
If Not startWith(subProcedureName, procedurePrefix) Then
GoTo continue
End If
End If
Application.Run ThisWorkbook.Name & "!" & subProcedureName
continue:
Next
End Sub
25. Private Function getSubProcedureNames(modeulePrefix As String) As Collection 'Collection<String>
Dim modules As Collection 'Collection<VBComponent>
Dim subProcedureNames As New Collection 'Collection<String>
Dim VBComponent As Object
Set modules = getModules(modeulePrefix)
Dim v
Dim i
Dim subProcedureName As String
For Each v In modules
Set VBComponent = v
With VBComponent.CodeModule
For i = 1 To .CountOfLines
subProcedureName = .ProcOfLine(i, 0)
If isEmpty(subProcedureName) Then
GoTo continue
End If
If contains(subProcedureNames, subProcedureName) Then
GoTo continue
End If
subProcedureNames.add subProcedureName
continue:
Next i
End With
Next
Set getSubProcedureNames = subProcedureNames
End Function
26. Private Function getModules(modeulePrefix As String) As Collection 'Collection<VBComponent>
Dim col As New Collection
Dim VBComponent As Object
Dim v
For Each v In ThisWorkbook.VBProject.VBComponents
Set VBComponent = v
With VBComponent
If .Type <> VBEXT_CT_STDMODULE Then
GoTo continue:
End If
If Not startWith(.Name, modeulePrefix) Then
GoTo continue:
End If
col.add VBComponent
End With
continue:
Next
Set getModules = col
End Function
Private Function contains(col As Collection, value) As Boolean
Dim v
For Each v In col
If v = value Then
contains = True
Exit Function
End If
Next
End Function
Private Function isEmpty(s As String) As Boolean
If s = "" Then
isEmpty = True
End If
End Function
Private Function startWith(s As String, prefix As String) As Boolean
If InStr(1, s, prefix) = 1 Then
startWith = True
End If
End Function