forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
104 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
41 changes: 41 additions & 0 deletions
41
...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,41 @@ | ||
// 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] | ||
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] | ||
public sealed class SwitchExpressionException : InvalidOperationException//, ISerializable | ||
{ | ||
public SwitchExpressionException() | ||
: base(SR.Arg_SwitchExpressionException) { } | ||
|
||
public SwitchExpressionException(object unmatchedValue) | ||
: base() | ||
{ | ||
UnmatchedValue = unmatchedValue; | ||
} | ||
|
||
private SwitchExpressionException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object)); | ||
} | ||
|
||
public object UnmatchedValue { get; } | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object)); | ||
} | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...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,47 @@ | ||
// 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 static void DefaultConstructor() | ||
{ | ||
var ex = new SwitchExpressionException(); | ||
|
||
Assert.NotNull(ex.Message); | ||
} | ||
|
||
[Fact] | ||
public static void Constructor_StringAsObjectArg() | ||
{ | ||
string message = "stringInput"; | ||
var ex = new SwitchExpressionException(message); | ||
|
||
Assert.NotEqual(message, ex.Message); | ||
Assert.Equal(message, ex.UnmatchedValue); | ||
} | ||
|
||
[Fact] | ||
public void UnmatchedValue() | ||
{ | ||
var ex = new SwitchExpressionException(34); | ||
Assert.Equal(34, ex.UnmatchedValue); | ||
|
||
var data = new byte[] { 1, 2, 3 }; | ||
ex = new SwitchExpressionException(data); | ||
Assert.Equal(data, ex.UnmatchedValue); | ||
|
||
ex = new SwitchExpressionException(true); | ||
Assert.Equal(true, ex.UnmatchedValue); | ||
|
||
ex = new SwitchExpressionException("34"); | ||
Assert.Equal("34", ex.UnmatchedValue); | ||
} | ||
} | ||
} |