From 9ead5d4cb0900395eaf9f5d26e11d05c50861c57 Mon Sep 17 00:00:00 2001 From: Chris White Date: Fri, 5 Apr 2019 08:03:18 -0400 Subject: [PATCH] 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, https://github.com/VBA-tools/vba-test/pull/33#issuecomment-479530864) - For folks who want to use the native `Scripting.Dictionary`, add a conditional-compilation parameter. --- README.md | 29 +++++++++++++++++++++++------ src/TestCase.cls | 9 +++++++++ tests/Tests_TestCase.bas | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d3ccd79..473fe47 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ End Function Public Function Add(ParamArray Values() As Variant) As Double Dim i As Integer Add = 0 - + For i = LBound(Values) To UBound(Values) Add = Add + Values(i) Next i @@ -57,10 +57,27 @@ For an advanced example of what is possible with vba-test, check out the [tests 1. Download the [latest release (v2.0.0-beta.3)](https://github.com/vba-tools/vba-test/releases) 2. Add `src/TestSuite.cls`, `src/TestCase.cls`, add `src/ImmediateReporter.cls` to your project -3. If you're starting from scratch with Excel, you can use `vba-test-blank.xlsm` + +If you're starting from scratch with Excel, you can use `vba-test-blank.xlsm` If you're updating from Excel-TDD v1, follow these [upgrade details](https://github.com/VBA-tools/vba-test/pull/23#issuecomment-416606307). +### Microsoft Word (how to fix `Dictionary` errors) + +If you are using this project on Word, or otherwise see error messages +related to `Dictionary`: + +- Easy option: add [@timhall's Dictionary class](https://github.com/VBA-tools/VBA-Dictionary/blob/master/Dictionary.cls) + to your project. + +- Slightly more complicated option for Windows: in the VBA Editor: + - select Tools | References and make sure `Microsoft Scripting Runtime` is checked + - select Tools | Project Properties. In the `Conditional Compilation + Arguments` box, add `VBA_Test_Scripting_Dictionary=1`. + +The more complicated option uses the native `Scripting.Dictionary`, so does +not require an additional VBA dependency. Take your pick! + ## TestSuite 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") .NotIncludes Array(1, 2, 3), 4 .IsApproximate 1.001, 1.002, 2 .NotApproximate 1.001, 1.009, 3 - + On Error Resume Next - + Err.Raise vbObjectError + 1, Description:="Uh oh." .IsError Description:="Uh oh." - + Err.Clear .NotError .Pass - .Fail "e.g. should not have gotten here" + .Fail "e.g. should not have gotten here" .Plan 4 ' Should only be 4 assertions, more or less fails .Skip ' skip this test End With diff --git a/src/TestCase.cls b/src/TestCase.cls index efd8e02..918f3bc 100644 --- a/src/TestCase.cls +++ b/src/TestCase.cls @@ -26,7 +26,11 @@ Private pFailures As VBA.Collection ' --------------------------------------------- ' Public Name As String +#If VBA_Test_Scripting_Dictionary=1 Then +Public Context As Scripting.Dictionary +#Else Public Context As Dictionary +#End If Public Planned As Long Public Successes As Long @@ -575,7 +579,12 @@ Private Function Indent(Optional Indentation As Long) End Function Private Sub Class_Initialize() +#If VBA_Test_Scripting_Dictionary=1 Then + Set Me.Context = New Scripting.Dictionary +#Else Set Me.Context = New Dictionary +#End If + Set pFailures = New VBA.Collection End Sub diff --git a/tests/Tests_TestCase.bas b/tests/Tests_TestCase.bas index 94d9ae4..dc6a22a 100644 --- a/tests/Tests_TestCase.bas +++ b/tests/Tests_TestCase.bas @@ -109,11 +109,19 @@ Sub PassingAssertions(Suite As TestSuite) .IsEqual A, B +#If VBA_Test_Scripting_Dictionary=1 Then + Set A = New Scripting.Dictionary +#Else Set A = New Dictionary +#End If A("a") = 1 A("b") = 2 +#If VBA_Test_Scripting_Dictionary=1 Then + Set B = New Scripting.Dictionary +#Else Set B = New Dictionary +#End If B("a") = 1 B("b") = 2 @@ -136,11 +144,19 @@ Sub PassingAssertions(Suite As TestSuite) .NotEqual A, B +#If VBA_Test_Scripting_Dictionary=1 Then + Set A = New Scripting.Dictionary +#Else Set A = New Dictionary +#End If A("a") = 1 A("b") = 2 +#If VBA_Test_Scripting_Dictionary=1 Then + Set B = New Scripting.Dictionary +#Else Set B = New Dictionary +#End If B("a") = 2 B("b") = 1