-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAssertLogger.cs
More file actions
163 lines (148 loc) · 6.74 KB
/
AssertLogger.cs
File metadata and controls
163 lines (148 loc) · 6.74 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Contentstack.Management.Core.Exceptions;
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);
}
/// <summary>
/// Asserts a Contentstack API error with an HTTP status in the allowed set.
/// </summary>
public static ContentstackErrorException ThrowsContentstackError(Action action, string name, params HttpStatusCode[] acceptableStatuses)
{
var ex = ThrowsException<ContentstackErrorException>(action, name);
IsTrue(
acceptableStatuses.Contains(ex.StatusCode),
$"Expected one of [{string.Join(", ", acceptableStatuses)}] but was {ex.StatusCode}",
"statusCode");
return ex;
}
/// <summary>
/// Async variant: runs the task and expects <see cref="ContentstackErrorException"/> with an allowed status.
/// </summary>
public static async Task<ContentstackErrorException> ThrowsContentstackErrorAsync(Func<Task> action, string name, params HttpStatusCode[] acceptableStatuses)
{
try
{
await action();
TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", "ContentstackErrorException", "NoException", false);
throw new AssertFailedException($"Expected exception ContentstackErrorException was not thrown.");
}
catch (ContentstackErrorException ex)
{
IsTrue(
acceptableStatuses.Contains(ex.StatusCode),
$"Expected one of [{string.Join(", ", acceptableStatuses)}] but was {ex.StatusCode}",
"statusCode");
TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", nameof(ContentstackErrorException), ex.StatusCode.ToString(), true);
return ex;
}
catch (AssertFailedException)
{
throw;
}
catch (Exception ex)
{
TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", nameof(ContentstackErrorException), ex.GetType().Name, false);
throw new AssertFailedException(
$"Expected exception ContentstackErrorException but got {ex.GetType().Name}: {ex.Message}", ex);
}
}
}
}