This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SwitchExpressionException (#34954)
* Add SwitchExpressionException Fixes: #33284 * Apply PR feedbacks * Type just added to .net core 3.0 * quick cleanup * Add test in BinaryFormatterTestData * Updated the APIs for SwitchExpressionException * Remove IsSerializable condition
- Loading branch information
1 parent
719e29d
commit 3685052
Showing
7 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ystem.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.Serialization; | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
/// <summary> | ||
/// Indicates that a switch expression that was non-exhaustive failed to match its input | ||
/// at runtime, e.g. in the C# 8 expression <code>3 switch { 4 => 5 }</code>. | ||
/// The exception optionally contains an object representing the unmatched value. | ||
/// </summary> | ||
[Serializable] | ||
public sealed class SwitchExpressionException : InvalidOperationException | ||
{ | ||
public SwitchExpressionException() | ||
: base(SR.Arg_SwitchExpressionException) { } | ||
|
||
public SwitchExpressionException(Exception innerException) : | ||
base(SR.Arg_SwitchExpressionException, innerException) { } | ||
|
||
public SwitchExpressionException(object unmatchedValue) : this() | ||
{ | ||
UnmatchedValue = unmatchedValue; | ||
} | ||
|
||
private SwitchExpressionException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
UnmatchedValue = info.GetValue(nameof(UnmatchedValue), typeof(object)); | ||
} | ||
|
||
public SwitchExpressionException(string message) : base(message) { } | ||
|
||
public SwitchExpressionException(string message, Exception innerException) | ||
: base(message, innerException) { } | ||
|
||
public object UnmatchedValue { get; } | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object)); | ||
} | ||
|
||
public override string Message | ||
{ | ||
get | ||
{ | ||
if (UnmatchedValue is null) | ||
{ | ||
return base.Message; | ||
} | ||
string valueMessage = SR.Format(SR.SwitchExpressionException_UnmatchedValue, UnmatchedValue.ToString()); | ||
return base.Message + Environment.NewLine + valueMessage; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...untime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
using Xunit; | ||
|
||
namespace System.Runtime.CompilerServices.Tests | ||
{ | ||
public class SwitchExpressionExceptionTests | ||
{ | ||
[Fact] | ||
public void Constructors() | ||
{ | ||
string message = "exception message"; | ||
var e = new SwitchExpressionException(); | ||
Assert.NotEmpty(e.Message); | ||
Assert.Null(e.InnerException); | ||
|
||
e = new SwitchExpressionException(message); | ||
Assert.Equal(message, e.Message); | ||
Assert.Null(e.InnerException); | ||
|
||
var inner = new Exception(); | ||
e = new SwitchExpressionException(message, inner); | ||
Assert.Equal(message, e.Message); | ||
Assert.Same(inner, e.InnerException); | ||
} | ||
|
||
[Fact] | ||
public static void Constructor_StringVsObjectArg() | ||
{ | ||
object message = "exception message"; | ||
var ex = new SwitchExpressionException(message as object); | ||
|
||
Assert.NotEqual(message, ex.Message); | ||
Assert.Same(message, ex.UnmatchedValue); | ||
|
||
ex = new SwitchExpressionException(message as string); | ||
|
||
Assert.Same(message, ex.Message); | ||
Assert.Null(ex.UnmatchedValue); | ||
} | ||
|
||
[Fact] | ||
public void UnmatchedValue_Null() | ||
{ | ||
var ex = new SwitchExpressionException((object)null); | ||
Assert.Null(ex.UnmatchedValue); | ||
} | ||
|
||
[Theory] | ||
[InlineData(34)] | ||
[InlineData(new byte[] { 1, 2, 3 })] | ||
[InlineData(true)] | ||
[InlineData("34")] | ||
public void UnmatchedValue_NotNull(object unmatchedValue) | ||
{ | ||
var ex = new SwitchExpressionException(unmatchedValue); | ||
Assert.Equal(unmatchedValue, ex.UnmatchedValue); | ||
Assert.Contains(ex.UnmatchedValue.ToString(), ex.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters