-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAssertLogger.cs
More file actions
114 lines (101 loc) · 4.43 KB
/
AssertLogger.cs
File metadata and controls
114 lines (101 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Contentstack.Management.Core.Tests.Helpers
{
public static class AssertLogger
{
public static void IsNotNull(object value, string name = "")
{
bool passed = value != null;
TestOutputLogger.LogAssertion($"IsNotNull({name})", "NotNull", value?.ToString() ?? "null", passed);
Assert.IsNotNull(value);
}
public static void IsNull(object value, string name = "")
{
bool passed = value == null;
TestOutputLogger.LogAssertion($"IsNull({name})", "null", value?.ToString() ?? "null", passed);
Assert.IsNull(value);
}
public static void AreEqual<T>(T expected, T actual, string name = "")
{
bool passed = Equals(expected, actual);
TestOutputLogger.LogAssertion($"AreEqual({name})", expected?.ToString() ?? "null", actual?.ToString() ?? "null", passed);
Assert.AreEqual(expected, actual);
}
public static void AreEqual<T>(T expected, T actual, string message, string name)
{
bool passed = Equals(expected, actual);
TestOutputLogger.LogAssertion($"AreEqual({name})", expected?.ToString() ?? "null", actual?.ToString() ?? "null", passed);
Assert.AreEqual(expected, actual, message);
}
public static void IsTrue(bool condition, string name = "")
{
TestOutputLogger.LogAssertion($"IsTrue({name})", "True", condition.ToString(), condition);
Assert.IsTrue(condition);
}
public static void IsTrue(bool condition, string message, string name)
{
TestOutputLogger.LogAssertion($"IsTrue({name})", "True", condition.ToString(), condition);
Assert.IsTrue(condition, message);
}
public static void IsFalse(bool condition, string name = "")
{
TestOutputLogger.LogAssertion($"IsFalse({name})", "False", condition.ToString(), !condition);
Assert.IsFalse(condition);
}
public static void IsFalse(bool condition, string message, string name)
{
TestOutputLogger.LogAssertion($"IsFalse({name})", "False", condition.ToString(), !condition);
Assert.IsFalse(condition, message);
}
public static void IsInstanceOfType(object value, Type expectedType, string name = "")
{
bool passed = value != null && expectedType.IsInstanceOfType(value);
TestOutputLogger.LogAssertion(
$"IsInstanceOfType({name})",
expectedType?.Name ?? "null",
value?.GetType()?.Name ?? "null",
passed);
Assert.IsInstanceOfType(value, expectedType);
}
public static T ThrowsException<T>(Action action, string name = "") where T : Exception
{
try
{
action();
TestOutputLogger.LogAssertion($"ThrowsException<{typeof(T).Name}>({name})", typeof(T).Name, "NoException", false);
throw new AssertFailedException($"Expected exception {typeof(T).Name} was not thrown.");
}
catch (T ex)
{
TestOutputLogger.LogAssertion($"ThrowsException<{typeof(T).Name}>({name})", typeof(T).Name, typeof(T).Name, true);
return ex;
}
catch (AssertFailedException)
{
throw;
}
catch (Exception ex)
{
TestOutputLogger.LogAssertion($"ThrowsException<{typeof(T).Name}>({name})", typeof(T).Name, ex.GetType().Name, false);
throw new AssertFailedException(
$"Expected exception {typeof(T).Name} but got {ex.GetType().Name}: {ex.Message}", ex);
}
}
public static void Fail(string message)
{
TestOutputLogger.LogAssertion("Fail", "N/A", message ?? "", false);
Assert.Fail(message);
}
public static void Fail(string message, params object[] parameters)
{
TestOutputLogger.LogAssertion("Fail", "N/A", message ?? "", false);
Assert.Fail(message, parameters);
}
public static void Inconclusive(string message)
{
TestOutputLogger.LogAssertion("Inconclusive", "N/A", message ?? "", false);
Assert.Inconclusive(message);
}
}
}