Skip to content

Commit 9ead5d4

Browse files
author
Chris White
committed
Use Scripting.Dictionary to support Word
On Word, `Dictionary` is ambiguous, since it could be `Word.Dictionary` or `Scripting.Dictionary`. Therefore: - In README, explain how to use VBA-Dictionary (thanks to @retailcoder, #33 (comment)) - For folks who want to use the native `Scripting.Dictionary`, add a conditional-compilation parameter.
1 parent e7af4a0 commit 9ead5d4

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ End Function
3333
Public Function Add(ParamArray Values() As Variant) As Double
3434
Dim i As Integer
3535
Add = 0
36-
36+
3737
For i = LBound(Values) To UBound(Values)
3838
Add = Add + Values(i)
3939
Next i
@@ -57,10 +57,27 @@ For an advanced example of what is possible with vba-test, check out the [tests
5757

5858
1. Download the [latest release (v2.0.0-beta.3)](https://github.com/vba-tools/vba-test/releases)
5959
2. Add `src/TestSuite.cls`, `src/TestCase.cls`, add `src/ImmediateReporter.cls` to your project
60-
3. If you're starting from scratch with Excel, you can use `vba-test-blank.xlsm`
60+
61+
If you're starting from scratch with Excel, you can use `vba-test-blank.xlsm`
6162

6263
If you're updating from Excel-TDD v1, follow these [upgrade details](https://github.com/VBA-tools/vba-test/pull/23#issuecomment-416606307).
6364

65+
### Microsoft Word (how to fix `Dictionary` errors)
66+
67+
If you are using this project on Word, or otherwise see error messages
68+
related to `Dictionary`:
69+
70+
- Easy option: add [@timhall's Dictionary class](https://github.com/VBA-tools/VBA-Dictionary/blob/master/Dictionary.cls)
71+
to your project.
72+
73+
- Slightly more complicated option for Windows: in the VBA Editor:
74+
- select Tools | References and make sure `Microsoft Scripting Runtime` is checked
75+
- select Tools | Project Properties. In the `Conditional Compilation
76+
Arguments` box, add `VBA_Test_Scripting_Dictionary=1`.
77+
78+
The more complicated option uses the native `Scripting.Dictionary`, so does
79+
not require an additional VBA dependency. Take your pick!
80+
6481
## TestSuite
6582

6683
A test suite groups tests together, runs test hooks for actions that should be run before and after tests, and is responsible for passing test results to reporters.
@@ -108,17 +125,17 @@ With Suite.Test("specific part of your application")
108125
.NotIncludes Array(1, 2, 3), 4
109126
.IsApproximate 1.001, 1.002, 2
110127
.NotApproximate 1.001, 1.009, 3
111-
128+
112129
On Error Resume Next
113-
130+
114131
Err.Raise vbObjectError + 1, Description:="Uh oh."
115132
.IsError Description:="Uh oh."
116-
133+
117134
Err.Clear
118135
.NotError
119136

120137
.Pass
121-
.Fail "e.g. should not have gotten here"
138+
.Fail "e.g. should not have gotten here"
122139
.Plan 4 ' Should only be 4 assertions, more or less fails
123140
.Skip ' skip this test
124141
End With

src/TestCase.cls

+9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ Private pFailures As VBA.Collection
2626
' --------------------------------------------- '
2727

2828
Public Name As String
29+
#If VBA_Test_Scripting_Dictionary=1 Then
30+
Public Context As Scripting.Dictionary
31+
#Else
2932
Public Context As Dictionary
33+
#End If
3034

3135
Public Planned As Long
3236
Public Successes As Long
@@ -575,7 +579,12 @@ Private Function Indent(Optional Indentation As Long)
575579
End Function
576580

577581
Private Sub Class_Initialize()
582+
#If VBA_Test_Scripting_Dictionary=1 Then
583+
Set Me.Context = New Scripting.Dictionary
584+
#Else
578585
Set Me.Context = New Dictionary
586+
#End If
587+
579588
Set pFailures = New VBA.Collection
580589
End Sub
581590

tests/Tests_TestCase.bas

+16
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,19 @@ Sub PassingAssertions(Suite As TestSuite)
109109

110110
.IsEqual A, B
111111

112+
#If VBA_Test_Scripting_Dictionary=1 Then
113+
Set A = New Scripting.Dictionary
114+
#Else
112115
Set A = New Dictionary
116+
#End If
113117
A("a") = 1
114118
A("b") = 2
115119

120+
#If VBA_Test_Scripting_Dictionary=1 Then
121+
Set B = New Scripting.Dictionary
122+
#Else
116123
Set B = New Dictionary
124+
#End If
117125
B("a") = 1
118126
B("b") = 2
119127

@@ -136,11 +144,19 @@ Sub PassingAssertions(Suite As TestSuite)
136144

137145
.NotEqual A, B
138146

147+
#If VBA_Test_Scripting_Dictionary=1 Then
148+
Set A = New Scripting.Dictionary
149+
#Else
139150
Set A = New Dictionary
151+
#End If
140152
A("a") = 1
141153
A("b") = 2
142154

155+
#If VBA_Test_Scripting_Dictionary=1 Then
156+
Set B = New Scripting.Dictionary
157+
#Else
143158
Set B = New Dictionary
159+
#End If
144160
B("a") = 2
145161
B("b") = 1
146162

0 commit comments

Comments
 (0)