Skip to content

Commit 56d8523

Browse files
author
Andrin Meier
committed
add vbaparser tests based on real world code
1 parent a44c320 commit 56d8523

23 files changed

+5380
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Antlr4.Runtime;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Rubberduck.Parsing.Grammar;
4+
using Rubberduck.Parsing.Symbols;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
9+
namespace RubberduckTests.Grammar
10+
{
11+
[TestClass]
12+
public class VBAParserValidityTests
13+
{
14+
[TestMethod]
15+
[DeploymentItem(@"Testfiles\")]
16+
public void TestTest()
17+
{
18+
foreach (var testfile in GetTestFiles())
19+
{
20+
AssertParseResult(testfile, Parse(testfile).module().GetText());
21+
}
22+
}
23+
24+
private void AssertParseResult(string originalCode, string materializedParseTree)
25+
{
26+
Assert.AreEqual(originalCode, materializedParseTree);
27+
}
28+
29+
private IEnumerable<string> GetTestFiles()
30+
{
31+
return Directory.EnumerateFiles("Grammar").Select(file => File.ReadAllText(file)).ToList();
32+
}
33+
34+
private VBAParser.StartRuleContext Parse(string code)
35+
{
36+
var stream = new AntlrInputStream(code);
37+
var lexer = new VBALexer(stream);
38+
var tokens = new CommonTokenStream(lexer);
39+
var parser = new VBAParser(tokens);
40+
parser.AddErrorListener(new ExceptionErrorListener());
41+
return parser.startRule();
42+
}
43+
}
44+
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="ComponentTypeExtensionTests.cs" />
8181
<Compile Include="ConfigurationTests.cs" />
8282
<Compile Include="Grammar\VBAParserTests.cs" />
83+
<Compile Include="Grammar\VBAParserValidityTests.cs" />
8384
<Compile Include="Inspections\EncapsulatePublicFieldInspectionTests.cs" />
8485
<Compile Include="Inspections\FunctionReturnValueNotUsedInspectionTests.cs" />
8586
<Compile Include="Inspections\MoveFieldCloserToUsageInspectionTests.cs" />
@@ -169,6 +170,72 @@
169170
<Name>Rubberduck.VBEditor</Name>
170171
</ProjectReference>
171172
</ItemGroup>
173+
<ItemGroup />
174+
<ItemGroup>
175+
<Content Include="Testfiles\Grammar\Reference_Module_1.txt">
176+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
177+
</Content>
178+
<Content Include="Testfiles\Grammar\Reference_Module_10.txt">
179+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
180+
</Content>
181+
<Content Include="Testfiles\Grammar\Reference_Module_11.txt">
182+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
183+
</Content>
184+
<Content Include="Testfiles\Grammar\Reference_Module_12.txt">
185+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
186+
</Content>
187+
<Content Include="Testfiles\Grammar\Reference_Module_13.txt">
188+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
189+
</Content>
190+
<Content Include="Testfiles\Grammar\Reference_Module_14.txt">
191+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
192+
</Content>
193+
<Content Include="Testfiles\Grammar\Reference_Module_15.txt">
194+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
195+
</Content>
196+
<Content Include="Testfiles\Grammar\Reference_Module_16.txt">
197+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
198+
</Content>
199+
<Content Include="Testfiles\Grammar\Reference_Module_17.txt">
200+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
201+
</Content>
202+
<Content Include="Testfiles\Grammar\Reference_Module_18.txt">
203+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
204+
</Content>
205+
<Content Include="Testfiles\Grammar\Reference_Module_19.txt">
206+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
207+
</Content>
208+
<Content Include="Testfiles\Grammar\Reference_Module_2.txt">
209+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
210+
</Content>
211+
<Content Include="Testfiles\Grammar\Reference_Module_20.txt">
212+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
213+
</Content>
214+
<Content Include="Testfiles\Grammar\Reference_Module_21.txt">
215+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
216+
</Content>
217+
<Content Include="Testfiles\Grammar\Reference_Module_3.txt">
218+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
219+
</Content>
220+
<Content Include="Testfiles\Grammar\Reference_Module_4.txt">
221+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
222+
</Content>
223+
<Content Include="Testfiles\Grammar\Reference_Module_5.txt">
224+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
225+
</Content>
226+
<Content Include="Testfiles\Grammar\Reference_Module_6.txt">
227+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
228+
</Content>
229+
<Content Include="Testfiles\Grammar\Reference_Module_7.txt">
230+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
231+
</Content>
232+
<Content Include="Testfiles\Grammar\Reference_Module_8.txt">
233+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
234+
</Content>
235+
<Content Include="Testfiles\Grammar\Reference_Module_9.txt">
236+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
237+
</Content>
238+
</ItemGroup>
172239
<Choose>
173240
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
174241
<ItemGroup>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Attribute VB_Name = "fnCheckPri"
2+
Option Explicit
3+
Option Private Module
4+
5+
Private Const ThisModule As String = ThisProject & "|fnCheckPri"
6+
7+
Public Function GetLongOrStringValue(NameValue As tyNameValue) As Variant
8+
Const ThisProcedure As String = ThisModule & "|GetLongOrStringValue|Function"
9+
10+
With NameValue
11+
.Value = GetVariantValue(Value:=.Value)
12+
Select Case VBA.VarType(VarName:=.Value)
13+
Case VBA.VbVarType.vbString
14+
GetLongOrStringValue = .Value
15+
16+
Case VBA.VbVarType.vbDouble
17+
If VBA.Int(.Value) <> .Value Then
18+
RaiseParamIsNotInteger NameValue:=NameValue
19+
End If
20+
GetLongOrStringValue = VBA.CLng(.Value)
21+
22+
Case VBA.VbVarType.vbLong, VBA.VbVarType.vbInteger
23+
GetLongOrStringValue = VBA.CLng(.Value)
24+
25+
Case Else
26+
RaiseParamTypeInvalid NameValue:=NameValue
27+
28+
End Select
29+
End With
30+
End Function
31+
32+
'Public Sub CheckPivotFieldItemIndexPos(ByVal Name As String _
33+
' , ByVal Value As Long _
34+
' , ByVal PivotFields As Excel.PivotFields _
35+
' , PivotFieldOrientation As XlPivotFieldOrientation _
36+
' )
37+
' If Value < Lo1 Then
38+
' RaiseParamValueLt1 Name:=Name, Value:=Value
39+
' End If
40+
' If Value > PivotFields.Count Then
41+
' RaisePivotFieldsItemIndexGtCount Name:=Name _
42+
' , Value:=Value _
43+
' , PivotFieldOrientation:=PivotFieldOrientation _
44+
' , Count:=PivotFields.Count
45+
' End If
46+
'End Sub
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Attribute VB_Name = "vbFnCollectionPri"
2+
Option Explicit
3+
Option Private Module
4+
5+
Private Const ThisModule As String = ThisProject & "|vbFnCollectionPri"
6+
7+
Public Function GetCollectionItemIndex(NameValue As tyNameValue _
8+
, Collection As Object _
9+
, Count As Long _
10+
) As Long
11+
Const ThisProcedure As String = ThisModule & "|GetCollectionItemIndex|Function"
12+
13+
With NameValue
14+
Select Case VBA.Sgn(.Value)
15+
Case In1
16+
If .Value > Count Then
17+
RaiseParamIndexGtCollectionCount NameValue:=NameValue _
18+
, Collection:=Collection _
19+
, Count:=Count
20+
End If
21+
GetCollectionItemIndex = .Value
22+
23+
Case InM1
24+
If VBA.Abs(.Value) > Count Then
25+
RaiseParamIndexLtCollection1 NameValue:=NameValue _
26+
, Collection:=Collection _
27+
, Count:=Count
28+
End If
29+
GetCollectionItemIndex = Count + Lo1 + .Value
30+
31+
Case In0
32+
RaiseParamIndexEqCollection0 NameValue:=NameValue _
33+
, Collection:=Collection _
34+
, Count:=Count
35+
End Select
36+
End With
37+
End Function

0 commit comments

Comments
 (0)