Skip to content

Commit 238a8a5

Browse files
committed
Using a stacktrace instead of CallerMemberName so we can continue to support .NET 4.0
1 parent 8744f33 commit 238a8a5

File tree

7 files changed

+68
-34
lines changed

7 files changed

+68
-34
lines changed

PowerAssert/MultipleAssertions/Error.cs

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,63 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Linq;
4+
using System.Reflection;
35

46
namespace PowerAssert.MultipleAssertions
57
{
68
public class Error
79
{
10+
static Assembly MyAssembly = typeof(Error).Assembly;
11+
812
internal static readonly string Crlf = Environment.NewLine;
13+
914
static readonly string Seperator = new string('=', 60);
1015

11-
public string Message { get; set; }
12-
public Exception Exception { get; set; }
13-
public string Path { get; set; }
14-
public int Line { get; set; }
1516

16-
public bool CausesFail { get; set; }
17-
18-
public Error(string message, string path, int line)
17+
public Error(string message)
1918
{
2019
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+
2338
}
2439

25-
public Error(Exception exception, string path, int line):this(exception.Message, path, line)
40+
41+
public Error(Exception exception):this(exception.Message)
2642
{
2743
Exception = exception;
2844
CausesFail = true;
2945
}
3046

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+
3154
public override string ToString()
3255
{
3356
if(!CausesFail)
3457
{
35-
return string.Concat("> ", Path, ":", Line, "> ", Message, Crlf);
58+
return string.Concat("> ", Message, Crlf);
3659
}
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);
3861

3962
}
4063
}

PowerAssert/MultipleAssertions/PolyAssert.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Runtime.CompilerServices;
@@ -14,52 +15,52 @@ public class PolyAssert : IDisposable
1415
/// <summary>
1516
/// Write a log message to be printed IF the PolyAssert has any errors
1617
/// </summary>
17-
public void Log(string s, [CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
18+
public void Log(string s)
1819
{
19-
_errors.Add(new Error(s, path, line));
20+
_errors.Add(new Error(s));
2021
}
2122

2223
/// <summary>
2324
/// Calls PAssert.IsTrue and stores the exception, if one occurs
2425
/// </summary>
2526
[MethodImpl(MethodImplOptions.NoInlining)]
26-
public void IsTrue(Expression<Func<bool>> expression, [CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
27+
public void IsTrue(Expression<Func<bool>> expression)
2728
{
28-
Try(() => PAssert.IsTrue(expression), path, line);
29+
Try(() => PAssert.IsTrue(expression));
2930
}
3031

3132
/// <summary>
3233
/// Calls PAssert.IsTrue and stores the exception, if one occurs
3334
/// </summary>
3435
[MethodImpl(MethodImplOptions.NoInlining)]
35-
public void IsTrue<TTarget>(TTarget target, Expression<Func<TTarget, bool>> expression, [CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
36+
public void IsTrue<TTarget>(TTarget target, Expression<Func<TTarget, bool>> expression)
3637
{
37-
Try(() => PAssert.IsTrue(target, expression), path, line);
38+
Try(() => PAssert.IsTrue(target, expression));
3839
}
3940

4041
/// <summary>
4142
/// Runs any action and stores the exception, if one occurs
4243
/// </summary>
43-
public void Try(Action action, [CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
44+
public void Try(Action action)
4445
{
4546
try
4647
{
4748
action();
4849
}
4950
catch (Exception e)
5051
{
51-
_errors.Add(new Error(e, path, line));
52+
_errors.Add(new Error(e));
5253
}
5354
}
5455

5556
/// <summary>
5657
/// Stores a failure message, if shouldFail is true
5758
/// </summary>
58-
public void FailIf(bool shouldFail, string message, [CallerFilePath] string path = null, [CallerLineNumber] int line = 0)
59+
public void FailIf(bool shouldFail, string message)
5960
{
6061
if (shouldFail)
6162
{
62-
_errors.Add(new Error(message, path, line) { CausesFail = true});
63+
_errors.Add(new Error(message) { CausesFail = true});
6364
}
6465
}
6566

PowerAssert/MultipleAssertions/PolyAssertException.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ namespace PowerAssert.MultipleAssertions
66
{
77
public class PolyAssertException : Exception
88
{
9-
public IReadOnlyList<Error> Errors { get; set; }
9+
public List<Error> Errors { get; set; }
1010

11-
public PolyAssertException(IReadOnlyList<Error> errors) : base(BuildMessage(errors))
11+
public PolyAssertException(List<Error> errors) : base(BuildMessage(errors))
1212
{
1313
Errors = errors;
1414
}
1515

16-
static string BuildMessage(IReadOnlyList<Error> errors)
16+
static string BuildMessage(List<Error> errors)
1717
{
1818
int fails = errors.Count(x => x.CausesFail);
1919

PowerAssert/PowerAssert.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<AppDesignerFolder>Properties</AppDesignerFolder>
1111
<RootNamespace>PowerAssert</RootNamespace>
1212
<AssemblyName>PowerAssert</AssemblyName>
13-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1414
<FileAlignment>512</FileAlignment>
1515
<TargetFrameworkProfile />
1616
</PropertyGroup>

PowerAssertTests/Approvals/EndToEndTest.PolyAssert.approved.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PowerAssert.MultipleAssertions.PolyAssertException: PolyAssert encountered 3 failures:
2-
> ...\EndToEndTest.cs:483> I will report all the errors i saw when i get disposed
2+
> I will report all the errors i saw when i get disposed
33
============================================================
4-
ERROR ...\EndToEndTest.cs:484:
4+
ERROR in EndToEndTest.PolyAssert at ...\EndToEndTest.cs:484:
55
============================================================
66
IsTrue failed, expression was:
77

@@ -14,7 +14,7 @@ x == 5
1414
============================================================
1515

1616
============================================================
17-
ERROR ...\EndToEndTest.cs:486:
17+
ERROR in EndToEndTest.PolyAssert at ...\EndToEndTest.cs:486:
1818
============================================================
1919
IsTrue failed, expression was:
2020

@@ -27,12 +27,12 @@ x == 7
2727
============================================================
2828

2929
============================================================
30-
ERROR ...\EndToEndTest.cs:487:
30+
ERROR in EndToEndTest.PolyAssert at ...\EndToEndTest.cs:487:
3131
============================================================
3232
Wah wah
3333
============================================================
3434

35-
> ...\EndToEndTest.cs:488> PolyAssert.Log messages are only printed if the test fails
35+
> PolyAssert.Log messages are only printed if the test fails
3636

3737
at PowerAssert.MultipleAssertions.PolyAssert.StopIfErrorsHaveOccurred() in ...\PolyAssert.cs
3838
at PowerAssert.MultipleAssertions.PolyAssert.Dispose() in ...\PolyAssert.cs

PowerAssertTests/Approvals/EndToEndTest.PolyAssertCanFinishEarly.approved.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
PowerAssert.MultipleAssertions.PolyAssertException: PolyAssert encountered 1 failures:
2-
> ...\EndToEndTest.cs:506> Sometimes you do want to end the test early after all
3-
> ...\EndToEndTest.cs:508> So just call StopIfErrorsHaveOccurred (behaves the same as disposing the PolyAssert)
2+
> Sometimes you do want to end the test early after all
3+
> So just call StopIfErrorsHaveOccurred (behaves the same as disposing the PolyAssert)
44
============================================================
5-
ERROR ...\EndToEndTest.cs:509:
5+
ERROR in EndToEndTest.PolyAssertCanFinishEarly at ...\EndToEndTest.cs:509:
66
============================================================
77
Wah wah
88
============================================================

PowerAssertTests/Approvals/EndToEndTest.cs

+10
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,16 @@ public void PolyAssertCanFinishEarly()
518518
}
519519
}
520520

521+
[Test]
522+
public void PolyAssertCanPassATest()
523+
{
524+
using (var poly = PAssert.Poly())
525+
{
526+
poly.Log("PolyAssert.Log messages are only printed if the test fails");
527+
poly.Try(() => Assert.True(true));
528+
}
529+
}
530+
521531
void ApproveException(Expression<Func<bool>> func)
522532
{
523533
try

0 commit comments

Comments
 (0)