From b2f2cffee64d783f61321d193b13f9fb5f1511a5 Mon Sep 17 00:00:00 2001 From: "kzu.net" Date: Sat, 8 Aug 2009 19:50:58 +0000 Subject: [PATCH] Made SetFileInfo absolutely bullet-proof (try..catch everything :|) git-svn-id: http://moq.googlecode.com/svn/trunk@609 b33fba48-7441-0410-8d5c-f397f7ceaa6c --- Source/MethodCall.cs | 50 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/Source/MethodCall.cs b/Source/MethodCall.cs index a3dab60d5..f20c95a37 100644 --- a/Source/MethodCall.cs +++ b/Source/MethodCall.cs @@ -168,31 +168,39 @@ public MethodCall(Mock mock, Expression originalExpression, MethodInfo method, p private void SetFileInfo() { #if !SILVERLIGHT - var thisMethod = MethodBase.GetCurrentMethod(); - var stack = new StackTrace(true); - var index = 0; - - // Move 'till our own frame first - while (stack.GetFrame(index).GetMethod() != thisMethod && index <= stack.FrameCount) + try { - index++; - } + var thisMethod = MethodBase.GetCurrentMethod(); + var stack = new StackTrace(true); + var index = 0; - // Move 'till we're at the entry point - // into Moq API - var mockAssembly = Assembly.GetExecutingAssembly(); - while (stack.GetFrame(index).GetMethod().DeclaringType.Assembly == mockAssembly && - index <= stack.FrameCount) - { - index++; - } + // Move 'till our own frame first + while (stack.GetFrame(index).GetMethod() != thisMethod && index <= stack.FrameCount) + { + index++; + } - if (index < stack.FrameCount) + // Move 'till we're at the entry point + // into Moq API + var mockAssembly = Assembly.GetExecutingAssembly(); + while (stack.GetFrame(index).GetMethod().DeclaringType.Assembly == mockAssembly && + index <= stack.FrameCount) + { + index++; + } + + if (index < stack.FrameCount) + { + var frame = stack.GetFrame(index); + this.FileLine = frame.GetFileLineNumber(); + this.FileName = Path.GetFileName(frame.GetFileName()); + TestMethod = frame.GetMethod(); + } + + } + catch { } { - var frame = stack.GetFrame(index); - this.FileLine = frame.GetFileLineNumber(); - this.FileName = Path.GetFileName(frame.GetFileName()); - TestMethod = frame.GetMethod(); + // Must NEVER fail, as this is a nice-to-have feature only. } #endif }