Skip to content

Commit 0f11f29

Browse files
committed
Fix crash in NewTestMethodCommand
1 parent 27874fe commit 0f11f29

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

RetailCoder.VBE/UnitTesting/NewTestMethodCommand.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.InteropServices;
33
using Microsoft.Vbe.Interop;
44
using Rubberduck.Parsing.Annotations;
5+
using Rubberduck.Parsing.Symbols;
56
using Rubberduck.Parsing.VBA;
67
using Rubberduck.UI;
78

@@ -67,11 +68,12 @@ public void NewTestMethod()
6768

6869
try
6970
{
70-
var declaration = _state.AllUserDeclarations.First(f =>
71-
f.DeclarationType == Parsing.Symbols.DeclarationType.ProceduralModule &&
71+
var declaration = _state.AllUserDeclarations.FirstOrDefault(f =>
7272
f.QualifiedName.QualifiedModuleName.Component.CodeModule == _vbe.ActiveCodePane.CodeModule);
7373

74-
if (declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.TestModule))
74+
if (declaration != null &&
75+
declaration.DeclarationType == DeclarationType.ProceduralModule &&
76+
declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.TestModule))
7577
{
7678
var module = _vbe.ActiveCodePane.CodeModule;
7779
var name = GetNextTestMethodName(module.Parent);
@@ -95,11 +97,12 @@ public void NewExpectedErrorTestMethod()
9597

9698
try
9799
{
98-
var declaration = _state.AllUserDeclarations.First(f =>
99-
f.DeclarationType == Parsing.Symbols.DeclarationType.ProceduralModule &&
100+
var declaration = _state.AllUserDeclarations.FirstOrDefault(f =>
100101
f.QualifiedName.QualifiedModuleName.Component.CodeModule == _vbe.ActiveCodePane.CodeModule);
101102

102-
if (declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.TestModule))
103+
if (declaration != null &&
104+
declaration.DeclarationType == DeclarationType.ProceduralModule &&
105+
declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.TestModule))
103106
{
104107
var module = _vbe.ActiveCodePane.CodeModule;
105108
var name = GetNextTestMethodName(module.Parent);

0 commit comments

Comments
 (0)