-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement Unwrap method for variants.
- Loading branch information
Showing
5 changed files
with
184 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Reflection; | ||
|
||
namespace Dunet.Test.UnionGeneration; | ||
|
||
public sealed class UnwrapTests | ||
{ | ||
[Fact] | ||
public void CanUseUnwrapMethodToUnsafelyGetVariantValue() | ||
{ | ||
// Arrange. | ||
var programCs = """ | ||
using Dunet; | ||
var value = GetValue(); | ||
static int GetValue() | ||
{ | ||
var option = new Option.Some(1); | ||
return option.UnwrapSome().Value; | ||
} | ||
[Union] | ||
public partial record Option | ||
{ | ||
public partial record Some(int Value); | ||
public partial record None; | ||
} | ||
"""; | ||
|
||
// Act. | ||
var compilation = Compiler.Compile(programCs); | ||
var value = compilation.Assembly?.ExecuteStaticMethod<int>("GetValue"); | ||
|
||
// Assert. | ||
using var scope = new AssertionScope(); | ||
compilation.CompilationErrors.Should().BeEmpty(); | ||
compilation.GenerationErrors.Should().BeEmpty(); | ||
value.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void CanUseUnwrapMethodToUnsafelyGetGenericVariantValue() | ||
{ | ||
// Arrange. | ||
var programCs = """ | ||
using Dunet; | ||
var value = GetValue(); | ||
static int GetValue() | ||
{ | ||
var option = new Option<int>.Some(1); | ||
return option.UnwrapSome().Value; | ||
} | ||
[Union] | ||
public partial record Option<T> | ||
{ | ||
public partial record Some(T Value); | ||
public partial record None; | ||
} | ||
"""; | ||
|
||
// Act. | ||
var compilation = Compiler.Compile(programCs); | ||
var value = compilation.Assembly?.ExecuteStaticMethod<int>("GetValue"); | ||
|
||
// Assert. | ||
using var scope = new AssertionScope(); | ||
compilation.CompilationErrors.Should().BeEmpty(); | ||
compilation.GenerationErrors.Should().BeEmpty(); | ||
value.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void UnwrapMethodThrowsWhenCalledWithWrongUnderlyingValue() | ||
{ | ||
// Arrange. | ||
var programCs = """ | ||
using Dunet; | ||
var value = GetValue(); | ||
static int GetValue() | ||
{ | ||
var option = new Option.None(); | ||
return option.UnwrapSome().Value; | ||
} | ||
[Union] | ||
public partial record Option | ||
{ | ||
public partial record Some(int Value); | ||
public partial record None; | ||
} | ||
"""; | ||
|
||
// Act. | ||
var compilation = Compiler.Compile(programCs); | ||
var action = () => compilation.Assembly?.ExecuteStaticMethod<int>("GetValue"); | ||
|
||
// Assert. | ||
using var scope = new AssertionScope(); | ||
compilation.CompilationErrors.Should().BeEmpty(); | ||
compilation.GenerationErrors.Should().BeEmpty(); | ||
action | ||
.Should() | ||
.Throw<TargetInvocationException>() | ||
.WithInnerExceptionExactly<InvalidOperationException>() | ||
.WithMessage("Called `Option.UnwrapSome()` on `None` value."); | ||
} | ||
} |