Skip to content

Commit 3a9b233

Browse files
authored
Merge pull request #6047 from BZngr/PRCExtensionsTests
Refactor ParserRuleContextExtensions Tests
2 parents 97b0b54 + 89b7c8b commit 3a9b233

File tree

2 files changed

+65
-44
lines changed

2 files changed

+65
-44
lines changed

RubberduckTests/Grammar/ParserRuleContextExtensionNullArgumentsTests.cs renamed to RubberduckTests/Grammar/ParserRuleContextExtensionsNullArgumentsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace RubberduckTests.Grammar
1616
{
1717
[TestFixture]
18-
public class ParserRuleContextExtensionNullArgumentsTests
18+
public class ParserRuleContextExtensionsNullArgumentsTests
1919
{
2020
[Test]
2121
[Category("Inspections")]

RubberduckTests/Grammar/ParserRuleContextExtensionTests.cs renamed to RubberduckTests/Grammar/ParserRuleContextExtensionsTests.cs

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using Antlr4.Runtime;
42
using NUnit.Framework;
53
using Rubberduck.Parsing;
64
using Rubberduck.Parsing.Grammar;
75
using Rubberduck.Parsing.Symbols;
86
using RubberduckTests.Mocks;
7+
using System.Collections.Generic;
8+
using System.Linq;
99

1010
namespace RubberduckTests.Grammar
1111
{
1212
[TestFixture]
13-
public class ParserRuleContextExtensionTests
13+
public class ParserRuleContextExtensionsTests
1414
{
1515
private List<Declaration> _allTestingDeclarations;
1616
private List<Declaration> AllTestingDeclarations
@@ -54,40 +54,45 @@ End Select
5454
}
5555
}
5656

57-
[TestCase("selectCase3Arg", ExpectedResult = true)]
58-
[TestCase("firstArg", ExpectedResult = false)]
57+
[TestCase("selectCase3Arg", true)]
58+
[TestCase("firstArg", false)]
5959
[Category("Inspections")]
60-
public bool ParserRuleCtxtExt_HasParentType(string identifer)
60+
[Category(nameof(ParserRuleContextExtensions))]
61+
public void ParserRuleCtxtExt_HasParentType(string identifer, bool expected)
6162
{
6263
var testArg = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(identifer)).First();
63-
var result = testArg.Context.IsDescendentOf<VBAParser.SelectCaseStmtContext>();
64-
return result;
64+
65+
var actual = testArg.Context.IsDescendentOf<VBAParser.SelectCaseStmtContext>();
66+
Assert.AreEqual(expected, actual);
6567
}
6668

67-
[TestCase("selectCase3", ExpectedResult = true)]
68-
[TestCase("selectCase1", ExpectedResult = false)]
69+
[TestCase("selectCase3", true)]
70+
[TestCase("selectCase1", false)]
6971
[Category("Inspections")]
70-
public bool ParserRuleCtxtExt_HasParentOfSameType(string contextID)
72+
[Category(nameof(ParserRuleContextExtensions))]
73+
public void ParserRuleCtxtExt_HasParentOfSameType(string contextID, bool expected)
7174
{
72-
bool result = false;
75+
var actual = false;
7376

7477
var testIdDecs = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(contextID));
7578
if (testIdDecs.Any())
7679
{
7780
var refs = testIdDecs.First().References;
7881
var testCtxt = (ParserRuleContext)refs.Where(rf => rf.Context.Parent.Parent.Parent is VBAParser.SelectCaseStmtContext).First().Context.Parent.Parent.Parent;
79-
result = testCtxt.IsDescendentOf<VBAParser.SelectCaseStmtContext>();
82+
actual = testCtxt.IsDescendentOf<VBAParser.SelectCaseStmtContext>();
8083
}
81-
return result;
84+
85+
Assert.AreEqual(expected, actual);
8286
}
8387

84-
[TestCase("selectCase3", "selectCase1", ExpectedResult = true)]
85-
[TestCase("selectCase1", "selectCase3", ExpectedResult = false)]
86-
[TestCase("selectCase3", "selectCase3", ExpectedResult = false)]
88+
[TestCase("selectCase3", "selectCase1", true)]
89+
[TestCase("selectCase1", "selectCase3", false)]
90+
[TestCase("selectCase3", "selectCase3", false)]
8791
[Category("Inspections")]
88-
public bool ParserRuleCtxtExt_IsDescendentOf_ByContext(string contextID, string parentContextID)
92+
[Category(nameof(ParserRuleContextExtensions))]
93+
public void ParserRuleCtxtExt_IsDescendentOf_ByContext(string contextID, string parentContextID, bool expected)
8994
{
90-
bool result = false;
95+
var actual = false;
9196
var parentIdDec = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(parentContextID)).First();
9297
var parentCtxt = (VBAParser.SelectCaseStmtContext)parentIdDec.References.Where(rf => rf.Context.Parent.Parent.Parent is VBAParser.SelectCaseStmtContext).First().Context.Parent.Parent.Parent;
9398

@@ -96,25 +101,28 @@ public bool ParserRuleCtxtExt_IsDescendentOf_ByContext(string contextID, string
96101
{
97102
var refs = testIdDecs.First().References;
98103
var testCtxt = (ParserRuleContext)refs.Where(rf => rf.Context.Parent.Parent.Parent is VBAParser.SelectCaseStmtContext).First().Context.Parent.Parent.Parent;
99-
result = testCtxt.IsDescendentOf(parentCtxt);
104+
actual = testCtxt.IsDescendentOf(parentCtxt);
100105
}
101-
return result;
102-
}
103106

107+
Assert.AreEqual(expected, actual);
108+
}
104109

105110
[Test]
106111
[Category("Inspections")]
112+
[Category(nameof(ParserRuleContextExtensions))]
107113
public void ParserRuleCtxtExt_IsDescendentOf_ByType_False()
108114
{
109115
var selectCase3Arg = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals("selectCase3Arg")).First();
116+
110117
var result = selectCase3Arg.Context.IsDescendentOf<VBAParser.SubStmtContext>();
111118
Assert.AreEqual(false, result);
112119
}
113120

114-
[TestCase("Foo", PRCExtensionTestContextTypes.SelectStmtCtxt, ExpectedResult = 3)]
115-
[TestCase("Foo", PRCExtensionTestContextTypes.PowOpCtxt, ExpectedResult = 0)]
121+
[TestCase("Foo", PRCExtensionTestContextTypes.SelectStmtCtxt, 3)]
122+
[TestCase("Foo", PRCExtensionTestContextTypes.PowOpCtxt, 0)]
116123
[Category("Inspections")]
117-
public int ParserRuleCtxtExt_GetDescendents(string parentName, PRCExtensionTestContextTypes descendentType)
124+
[Category(nameof(ParserRuleContextExtensions))]
125+
public void ParserRuleCtxtExt_GetDescendents(string parentName, PRCExtensionTestContextTypes descendentType, long expected)
118126
{
119127
var parentContext = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals("Foo")).First().Context;
120128
var descendents = new List<ParserRuleContext>();
@@ -126,13 +134,16 @@ public int ParserRuleCtxtExt_GetDescendents(string parentName, PRCExtensionTestC
126134
{
127135
descendents = parentContext.GetDescendents<VBAParser.PowOpContext>().Select(desc => (ParserRuleContext)desc).ToList();
128136
}
129-
return descendents.Count();
137+
138+
var actual = descendents.Count();
139+
Assert.AreEqual(expected, actual);
130140
}
131141

132-
[TestCase("Foo", PRCExtensionTestContextTypes.SelectStmtCtxt, ExpectedResult = true)]
133-
[TestCase("Foo", PRCExtensionTestContextTypes.PowOpCtxt, ExpectedResult = false)]
142+
[TestCase("Foo", PRCExtensionTestContextTypes.SelectStmtCtxt, true)]
143+
[TestCase("Foo", PRCExtensionTestContextTypes.PowOpCtxt, false)]
134144
[Category("Inspections")]
135-
public bool ParserRuleCtxtExt_GetDescendent(string parentName, PRCExtensionTestContextTypes descendentType)
145+
[Category(nameof(ParserRuleContextExtensions))]
146+
public void ParserRuleCtxtExt_GetDescendent(string parentName, PRCExtensionTestContextTypes descendentType, bool expected)
136147
{
137148
var parentContext = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(parentName)).First().Context;
138149
ParserRuleContext descendent = null;
@@ -144,13 +155,16 @@ public bool ParserRuleCtxtExt_GetDescendent(string parentName, PRCExtensionTestC
144155
{
145156
descendent = parentContext.GetDescendent<VBAParser.PowOpContext>();
146157
}
147-
return descendent != null;
158+
159+
var actual = descendent != null;
160+
Assert.AreEqual(expected, actual);
148161
}
149162

150-
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.SelectStmtCtxt, ExpectedResult = true)]
151-
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.PowOpCtxt, ExpectedResult = false)]
163+
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.SelectStmtCtxt, true)]
164+
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.PowOpCtxt, false)]
152165
[Category("Inspections")]
153-
public bool ParserRuleCtxtExt_GetAncestor(string name, PRCExtensionTestContextTypes ancestorType)
166+
[Category(nameof(ParserRuleContextExtensions))]
167+
public void ParserRuleCtxtExt_GetAncestor(string name, PRCExtensionTestContextTypes ancestorType, bool expected)
154168
{
155169
var context = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(name)).First().Context;
156170
ParserRuleContext ancestor = null;
@@ -162,26 +176,31 @@ public bool ParserRuleCtxtExt_GetAncestor(string name, PRCExtensionTestContextTy
162176
{
163177
ancestor = context.GetAncestor<VBAParser.PowOpContext>();
164178
}
165-
return ancestor != null;
179+
180+
var actual = ancestor != null;
181+
Assert.AreEqual(expected, actual);
166182
}
167183

168-
[TestCase("selectCase3Arg", "Foo", ExpectedResult = true)]
169-
[TestCase("selectCase3Arg", "selectCase1", ExpectedResult = false)]
184+
[TestCase("selectCase3Arg", "Foo", true)]
185+
[TestCase("selectCase3Arg", "selectCase1", false)]
170186
[Category("Inspections")]
171-
public bool ParserRuleCtxtExt_IsDescendentOf_ByContext2(string contextName, string parentName)
187+
[Category(nameof(ParserRuleContextExtensions))]
188+
public void ParserRuleCtxtExt_IsDescendentOf_ByContext2(string contextName, string parentName, bool expected)
172189
{
173190
var descendentCandidate = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(contextName)).First().Context;
174191
var parentCandidate = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(parentName)).First().Context;
175-
var result = descendentCandidate.IsDescendentOf(parentCandidate);
176-
return result;
192+
193+
var actual = descendentCandidate.IsDescendentOf(parentCandidate);
194+
Assert.AreEqual(expected, actual);
177195
}
178196

179197
public enum PRCExtensionTestContextTypes {SelectStmtCtxt, AsTypeCtxt, PowOpCtxt };
180198

181-
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.SelectStmtCtxt, ExpectedResult = false)]
182-
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.AsTypeCtxt, ExpectedResult = true)]
199+
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.SelectStmtCtxt, false)]
200+
[TestCase("selectCase3Arg", PRCExtensionTestContextTypes.AsTypeCtxt, true)]
183201
[Category("Inspections")]
184-
public bool ParserRuleCtxtExt_GetChild(string parentContextName, PRCExtensionTestContextTypes ctxtType)
202+
[Category(nameof(ParserRuleContextExtensions))]
203+
public void ParserRuleCtxtExt_GetChild(string parentContextName, PRCExtensionTestContextTypes ctxtType, bool expected)
185204
{
186205
ParserRuleContext result = null;
187206
var parentContext = AllTestingDeclarations.Where(dc => dc.IdentifierName.Equals(parentContextName)).First().Context;
@@ -193,7 +212,9 @@ public bool ParserRuleCtxtExt_GetChild(string parentContextName, PRCExtensionTes
193212
{
194213
result = parentContext.GetChild<VBAParser.AsTypeClauseContext>();
195214
}
196-
return result != null;
215+
216+
var actual = result != null;
217+
Assert.AreEqual(expected, actual);
197218
}
198219

199220
private IEnumerable<Declaration> GetAllUserDeclarations(string inputCode)

0 commit comments

Comments
 (0)