Unit tests are the best comments you can leave a developer or your future self.
Well, these aren’t unit test exactly, but they are tests. The question is, do they adequately document the code?
First, the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
Public Property Get GetStartDate(ByVal dtTomorrow As Date) As Date Dim clsTomorrow As CHoliday Dim dtReturn As Date Dim i As Long Set clsTomorrow = Me.HolidayByHolidayDate(dtTomorrow) If clsTomorrow Is Nothing Then dtReturn = dtTomorrow Else For i = 1 To 7 If Me.IsWorkDay(dtTomorrow + 1) Then dtReturn = dtTomorrow + 1 Exit For End If Next i End If GetStartDate = dtReturn End Property Public Property Get GetEndDate(ByVal dtStart As Date) As Date Dim i As Long Dim dtReturn As Date If Weekday(dtStart, vbMonday) < 6 Then dtReturn = dtStart Else For i = 1 To 7 If Me.IsWorkDay(dtStart + i) Then dtReturn = dtStart + i Exit For End If Next i End If GetEndDate = dtReturn End Property |
And, the tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
Sub TEST_Main() TEST_Initialize TEST_HolidayNormal TEST_HolidayFriday TEST_HolidayTomorrow TEST_HolidayMonday Debug.Print "Passed" End Sub Sub TEST_Initialize() Set gclsApp = New CApp gclsApp.Initialize End Sub Sub TEST_HolidayNormal() Dim dtDate As Date Dim dtStart As Date dtDate = #8/29/2013# 'typical weekday dtStart = gclsApp.Holidays.GetStartDate(dtDate + 1) Debug.Assert dtStart = #8/30/2013# 'starts tomorrow Debug.Assert gclsApp.Holidays.GetEndDate(dtStart) = #8/30/2013# 'end tomorrow End Sub Sub TEST_HolidayFriday() Dim dtDate As Date Dim dtStart As Date dtDate = #8/23/2013# 'typical friday dtStart = gclsApp.Holidays.GetStartDate(dtDate + 1) Debug.Assert dtStart = #8/24/2013# 'sat Debug.Assert gclsApp.Holidays.GetEndDate(dtStart) = #8/26/2013# 'mon End Sub Sub TEST_HolidayTomorrow() Dim dtDate As Date Dim dtStart As Date dtDate = #11/27/2013# 'day before thanksgiving dtStart = gclsApp.Holidays.GetStartDate(dtDate + 1) Debug.Assert dtStart = #11/29/2013# 'starts on Friday Debug.Assert gclsApp.Holidays.GetEndDate(dtStart) = #11/29/2013# 'ends on friday End Sub Sub TEST_HolidayMonday() Dim dtDate As Date Dim dtStart As Date dtDate = #8/30/2013# dtStart = gclsApp.Holidays.GetStartDate(dtDate + 1) Debug.Assert dtStart = #8/31/2013# 'starts saturday Debug.Assert gclsApp.Holidays.GetEndDate(dtStart) = #9/3/2013# 'ends tuesday End Sub |
No, not for me.
People are hero’s with comments, just write them, where and when you want and get on with it. :-)
The tests pretty accurately describe what the code is trying to accomplish (offset long weekends properly). And if your future self needs to change or extend that you make sure you didn’t break it. What more would you you say in comments that isn’t said in the test code.