Skip to content

Instantly share code, notes, and snippets.

Created June 27, 2012 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3004007 to your computer and use it in GitHub Desktop.
Save anonymous/3004007 to your computer and use it in GitHub Desktop.
using System;
using NUnit.Framework;
namespace NDepend.Product.ErrorHandling {
[TestFixture]
public class Test_StackTraceHelper {
[Test]
public void Test_FormatUnlocalizedStackTraceWithILOffset_EMPTY_STACK_TRACE() {
var ex = new Exception();
var str = StackTraceHelper.FormatUnlocalizedStackTraceWithILOffset(ex);
Assert.IsTrue(str == StackTraceHelper.EMPTY_STACK_TRACE);
str = StackTraceHelper.FormatStackTrace(ex);
Assert.IsTrue(str == StackTraceHelper.EMPTY_STACK_TRACE);
}
[Test]
public void Test_FormatUnlocalizedStackTraceWithILOffset_RealEx() {
NullReferenceException nullReferenceException = null;
try {
string s = null;
var i = s.Length;
}
catch (NullReferenceException ex) {
nullReferenceException = ex;
}
Assert.IsNotNull(nullReferenceException);
var str = StackTraceHelper.FormatUnlocalizedStackTraceWithILOffset(nullReferenceException);
// Don't put the IL offset coz it can change once instrument with coverage tool!
Assert.IsTrue(str.StartsWith(@" NDepend.Product.ErrorHandling.Test_StackTraceHelper.Test_FormatUnlocalizedStackTraceWithILOffset_RealEx() L_00"));
var str1 = StackTraceHelper.FormatStackTrace(nullReferenceException);
Assert.IsTrue(str1 != str);
Assert.IsTrue(str1.StartsWith(" NDepend.Product.ErrorHandling.Test_StackTraceHelper.Test_FormatUnlocalizedStackTraceWithILOffset_RealEx()"));
}
[Test]
public void Test_FormatUnlocalizedStackTraceWithILOffset_RealExInAnonymouseMethod() {
NullReferenceException nullReferenceException = null;
try {
// Note that we test here with delegate with two parameters!
var action = new Action<int,string>(delegate(int i1, string s2) {
string s = null;
var i = s.Length;
});
action.Invoke(1,"");
}
catch (NullReferenceException ex) {
nullReferenceException = ex;
}
Assert.IsNotNull(nullReferenceException);
var str = StackTraceHelper.FormatUnlocalizedStackTraceWithILOffset(nullReferenceException);
// Don't put the IL offset coz it can change once instrument with coverage tool!
Assert.IsTrue(str.StartsWith(@" NDepend.Product.ErrorHandling.Test_StackTraceHelper.<Test_FormatUnlocalizedStackTraceWithILOffset_RealExInAnonymouseMethod>b__0(Int32 i1, String s2) L_00"));
Assert.IsTrue(str.Contains(@" NDepend.Product.ErrorHandling.Test_StackTraceHelper.Test_FormatUnlocalizedStackTraceWithILOffset_RealExInAnonymouseMethod() L_00"));
;
}
[Test]
public void Test1_FormatUnlocalizedStackTrace() {
const string strIn = @" 在 afs.h()
在 afs.c()}";
const string strOut = @" afs.h()
afs.c()}";
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(strIn) == strOut);
}
[Test]
public void Test2_FormatUnlocalizedStackTrace() {
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@" в qz.a(Window A_0, Window A_1)}") ==
StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@" in qz.a(Window A_0, Window A_1)}"));
}
[Test]
public void Test3_FormatUnlocalizedStackTrace() {
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@" в qz.a(Window A_0, Window A_1)}") ==
@" qz.a(Window A_0, Window A_1)}");
}
[Test]
public void Test4_FormatUnlocalizedStackTrace() {
try {
throw new Exception("hello");
} catch(Exception e) {
var unlocalizedStackTrace = StackTraceHelper.FormatUnlocalizedStackTraceFromString(e.StackTrace);
Assert.IsTrue(unlocalizedStackTrace.IndexOf(
" NDepend.Product.ErrorHandling.Test_StackTraceHelper.Test4_FormatUnlocalizedStackTrace()") == 0);
}
}
[Test]
public void Test5_Faultolerant() {
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@"xxx
xxx") ==
@"xxx
xxx");
}
[Test]
public void Test6_Faultolerant() {
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@"xxx") ==
@"xxx");
}
[Test]
public void Test7_Faultolerant() {
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@" xxx") ==
@" xxx");
}
[Test]
public void Test8_Faultolerant() {
Assert.IsTrue(StackTraceHelper.FormatUnlocalizedStackTraceFromString(
@" xxx
xxx") ==
@" xxx
xxx");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment