Skip to content

Commit

Permalink
Made SetFileInfo absolutely bullet-proof (try..catch everything :|)
Browse files Browse the repository at this point in the history
git-svn-id: http://moq.googlecode.com/svn/trunk@609 b33fba48-7441-0410-8d5c-f397f7ceaa6c
  • Loading branch information
kzu.net committed Aug 8, 2009
1 parent e7fd267 commit b2f2cff
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions Source/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit b2f2cff

Please sign in to comment.