-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AkkaEqualException.cs
94 lines (84 loc) · 3.93 KB
/
AkkaEqualException.cs
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
//-----------------------------------------------------------------------
// <copyright file="AkkaEqualException.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Runtime.Serialization;
using Xunit.Sdk;
#nullable enable
namespace Akka.TestKit.Xunit2.Internals
{
/// <summary>
/// TBD
/// </summary>
public class AkkaEqualException : XunitException
{
// Length of "Expected: " and "Actual: "
private static readonly string NewLineAndIndent = Environment.NewLine + new string(' ', 10);
private readonly string? _format;
private readonly object[] _args = Array.Empty<object>();
public static AkkaEqualException ForMismatchedValues(
object? expected,
object? actual,
string? format = null,
params object[] args)
{
// Strings normally come through ForMismatchedStrings, so we want to make sure any
// string value that comes through here isn't re-formatted/truncated. This is for
// two reasons: (a) to support Assert.Equal<object>(string1, string2) to get a full
// printout of the raw string values, which is useful when debugging; and (b) to
// allow the assertion functions to pre-format the value themselves, perhaps with
// additional information (like DateTime/DateTimeOffset when providing the precision
// of the comparison).
var expectedText = expected as string ?? ArgumentFormatter.Format(expected);
var actualText = actual as string ?? ArgumentFormatter.Format(actual);
return new AkkaEqualException(
"Assert.Equal() Failure: " + (format ?? "Values differ") + Environment.NewLine +
"Expected: " + expectedText.Replace(Environment.NewLine, NewLineAndIndent) + Environment.NewLine +
"Actual: " + actualText.Replace(Environment.NewLine, NewLineAndIndent),
args
);
}
/// <summary>
/// Initializes a new instance of the <see cref="AkkaEqualException"/> class.
/// </summary>
/// <param name="format">A template string that describes the error.</param>
/// <param name="args">An optional object array that contains zero or more objects to format.</param>
public AkkaEqualException(string format = "", params object[] args): base(null)
{
_format = format;
_args = args;
}
/// <summary>
/// Initializes a new instance of the <see cref="AkkaEqualException"/> class.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected AkkaEqualException(SerializationInfo info, StreamingContext context): base(null)
{
}
/// <summary>
/// The message that describes the error.
/// </summary>
public override string Message
{
get
{
if(string.IsNullOrEmpty(_format))
return base.Message;
string message;
try
{
message = string.Format(_format!, _args);
}
catch(Exception)
{
message = $@"[Could not string.Format(""{_format}"", {string.Join(", ", _args)})]";
}
return base.Message is not null ? $"{base.Message} {message}" : message;
}
}
}
}