Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vbaでもtdd

 Vbaでもtdd

2013/7/27 TDD on ExcelVBA DEMOs

terahide

July 27, 2013

Transcript

33 slides
  1. メニュー → 挿入→ 標準モジュール Option Explicit Sub いちに2を加えると3になるべき() Dim Calc

    As New Calc Debug.Assert Calc.Add(1, 2) = 3 End Sub サブプロシージャ名は数 字から始められないの で平仮名の「いち」
  2. クラスモジュールを修正(明白な実装) Option Explicit Function add( i1 As Integer, i2 As

    Integer) As Integer add = i1 + i2 End Function Calc 仮実装は省略!
  3. ちょっとリファクタ。イミディェイトに表示 Option Explicit Sub いちに2を加えると3になるべき() Dim Calc As New Calc

    Dim actual As Integer actual = Calc.add(1, 2) Debug.Assert actual = 3 Debug.Print actual End Sub
  4. 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
  5. 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
  6. 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
  7. 標準モジュールのテストにカーソルをあてて F5 Sub allTest() Dim TestSuite As New TestSuite With

    TestSuite .testAll ("test") End With End Sub クリアしておきましょう

More Decks by terahide

Other Decks in Programming