|
1 | 1 | using System;
|
| 2 | +using System.Diagnostics; |
2 | 3 | using System.Linq;
|
| 4 | +using System.Reflection; |
3 | 5 |
|
4 | 6 | namespace PowerAssert.MultipleAssertions
|
5 | 7 | {
|
6 | 8 | public class Error
|
7 | 9 | {
|
| 10 | + static Assembly MyAssembly = typeof(Error).Assembly; |
| 11 | + |
8 | 12 | internal static readonly string Crlf = Environment.NewLine;
|
| 13 | + |
9 | 14 | static readonly string Seperator = new string('=', 60);
|
10 | 15 |
|
11 |
| - public string Message { get; set; } |
12 |
| - public Exception Exception { get; set; } |
13 |
| - public string Path { get; set; } |
14 |
| - public int Line { get; set; } |
15 | 16 |
|
16 |
| - public bool CausesFail { get; set; } |
17 |
| - |
18 |
| - public Error(string message, string path, int line) |
| 17 | + public Error(string message) |
19 | 18 | {
|
20 | 19 | Message = message;
|
21 |
| - Path = path; |
22 |
| - Line = line; |
| 20 | + var stackFrames = from f in new StackTrace(1, true).GetFrames() |
| 21 | + let m = f.GetMethod() |
| 22 | + where m != null |
| 23 | + let t = m.DeclaringType |
| 24 | + where t.Assembly != MyAssembly |
| 25 | + select f; |
| 26 | + var frame = stackFrames.FirstOrDefault(); |
| 27 | + if (frame != null) |
| 28 | + { |
| 29 | + var method = frame.GetMethod(); |
| 30 | + var typeName = method.DeclaringType == null ? "" : method.DeclaringType.Name; |
| 31 | + Location = string.Format("in {0}.{1} at {2}:{3}", typeName, method.Name, frame.GetFileName(), frame.GetFileLineNumber()); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + Location = "(Unknown location)"; |
| 36 | + } |
| 37 | + |
23 | 38 | }
|
24 | 39 |
|
25 |
| - public Error(Exception exception, string path, int line):this(exception.Message, path, line) |
| 40 | + |
| 41 | + public Error(Exception exception):this(exception.Message) |
26 | 42 | {
|
27 | 43 | Exception = exception;
|
28 | 44 | CausesFail = true;
|
29 | 45 | }
|
30 | 46 |
|
| 47 | + |
| 48 | + public string Message { get; set; } |
| 49 | + public Exception Exception { get; set; } |
| 50 | + public string Location { get; set; } |
| 51 | + |
| 52 | + public bool CausesFail { get; set; } |
| 53 | + |
31 | 54 | public override string ToString()
|
32 | 55 | {
|
33 | 56 | if(!CausesFail)
|
34 | 57 | {
|
35 |
| - return string.Concat("> ", Path, ":", Line, "> ", Message, Crlf); |
| 58 | + return string.Concat("> ", Message, Crlf); |
36 | 59 | }
|
37 |
| - return string.Concat(Seperator, Crlf, "ERROR ", Path, ":", Line, ":", Crlf, Seperator, Crlf, Message, Crlf, Seperator, Crlf, Crlf); |
| 60 | + return string.Concat(Seperator, Crlf, "ERROR ", Location, ":", Crlf, Seperator, Crlf, Message, Crlf, Seperator, Crlf, Crlf); |
38 | 61 |
|
39 | 62 | }
|
40 | 63 | }
|
|
0 commit comments