StackTrace

Jan 13, 2011 at 1:31 AM

I have modified the code to remove stack traces lines from SoftwareApproach.TestingExtensions. What do you think?

        internal static void HandleFail(string assertionName, string message, params object[] parameters)
        {
            var exp = new AssertFailedMinialStackTraceException(string.Format("{0} failed. {1}", assertionName, message));
            exp.StackTraceFilter = a => !a.Contains("SoftwareApproach.TestingExtensions.");
            throw exp;
        }

And the Exception 

public class AssertFailedMinialStackTraceException : AssertFailedException
    {
        public AssertFailedMinialStackTraceException(string msg, Exception ex)
            : base(msg, ex)
        {
        }

        public AssertFailedMinialStackTraceException(string msg)
            : base(msg)
        {
        }

        public AssertFailedMinialStackTraceException()
        {
        }

        protected AssertFailedMinialStackTraceException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        public Func<String, bool> StackTraceFilter { get; set; }
        private string filteredStackTrace;
        public override string StackTrace
        {
            get
            {
                if (filteredStackTrace != null)
                    return filteredStackTrace;
                var before = base.StackTrace;
                var parts = before.Split('\n');
                var filtered = parts
                    .Where(a => !string.IsNullOrEmpty(a))
                    .Where(StackTraceFilter);
                var after = String.Join("\r\n", filtered.ToArray());
                filteredStackTrace = after;
                return filteredStackTrace;
            }
        }
    }