Skip to content

Commit 77e8047

Browse files
authored
Merge pull request #2 from FlaxCommunityProjects/dev
Improved Exception Logging
2 parents fb5ede2 + b19a244 commit 77e8047

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

Source/Editor/Assert.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,34 @@ public SuccessException(string message, Exception innerException) : base(message
2121
}
2222
}
2323

24+
/// <summary>
25+
/// Exception thrown when an assertion fails
26+
/// </summary>
27+
/// <seealso cref="System.Exception" />
28+
public class AssertException : Exception
29+
{
30+
public AssertException()
31+
{
32+
}
33+
34+
public AssertException(string message) : base(message)
35+
{
36+
}
37+
38+
public AssertException(string message, Exception innerException) : base(message, innerException)
39+
{
40+
}
41+
}
42+
2443
public static class Assert
2544
{
2645
public static void Pass() => throw new SuccessException();
27-
public static void Fail() => throw new Exception();
46+
public static void Fail() => throw new AssertException("Fail");
2847

29-
public static void AreEqual(object a, object b) { if (!Equals(a, b)) throw new Exception(); }
30-
public static void AreNotEqual(object a, object b) { if (Equals(a, b)) throw new Exception(); }
48+
public static void AreEqual(object a, object b) { if (!Equals(a, b)) throw new AssertException($"{a} does not equal {b}"); }
49+
public static void AreNotEqual(object a, object b) { if (Equals(a, b)) throw new Exception($"{a} is equal to {b}"); }
3150

32-
public static void True(bool a) { if (!a) throw new Exception(); }
33-
public static void False(bool a) { if (a) throw new Exception(); }
51+
public static void True(bool a) { if (!a) throw new Exception($"{a} is not true"); }
52+
public static void False(bool a) { if (a) throw new Exception($"{a} is not false"); }
3453
}
3554
}

Source/Editor/TestRunner.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class TestRunner : EditorPlugin
2727
Version = new Version(1, 1),
2828
RepositoryUrl = "https://github.com/FlaxCommunityProjects/FlaxUnitTesting"
2929
};
30-
30+
3131
public override void InitializeEditor()
3232
{
3333
base.InitializeEditor();
@@ -94,16 +94,23 @@ public static void RunTests()
9494
catch (TargetInvocationException e)
9595
{
9696
if (!(e.InnerException is SuccessException))
97+
{
9798
failed = true;
98-
}
99-
catch (Exception e)
100-
{
101-
failed = true;
99+
Debug.LogException(e.InnerException);
100+
}
102101
}
103102
finally
104103
{
105104
afterEach?.Invoke(instance, null);
106-
Debug.Log($"Test '{suite.Name} {testMethod.Name}' finished with " + (failed ? "Error" : "Success"));
105+
string message = $"Test '{suite.Name} {testMethod.Name}' finished with " + (failed ? "Error" : "Success");
106+
if (failed)
107+
{
108+
Debug.LogError(message);
109+
}
110+
else
111+
{
112+
Debug.Log(message);
113+
}
107114
}
108115
}
109116
else
@@ -123,11 +130,9 @@ public static void RunTests()
123130
catch (TargetInvocationException e)
124131
{
125132
if (!(e.InnerException is SuccessException))
133+
{
126134
failed = true;
127-
}
128-
catch (Exception e)
129-
{
130-
failed = true;
135+
}
131136
}
132137
finally
133138
{
@@ -138,7 +143,16 @@ public static void RunTests()
138143
}
139144
}
140145

141-
Debug.Log($"Test '{suite.Name} {testMethod.Name}' finished with {successCount}/{testCases.Count()} successfull test cases.");
146+
int testCount = testCases.Count();
147+
string message = $"Test '{suite.Name} {testMethod.Name}' finished with {successCount}/{testCount} successfull test cases.";
148+
if (successCount < testCount)
149+
{
150+
Debug.LogError(message);
151+
}
152+
else
153+
{
154+
Debug.Log(message);
155+
}
142156
}
143157
}
144158

0 commit comments

Comments
 (0)