Skip to content

Commit

Permalink
Add tests for union and match expressions codegen.
Browse files Browse the repository at this point in the history
  • Loading branch information
YairHalberstadt committed Feb 3, 2020
1 parent 87c90b9 commit c34a507
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions source/Compiler.Tests.Unit/Emit/UnionAndMatchExpressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using FluentLang.TestUtils;
using Xunit;
using Xunit.Abstractions;

namespace FluentLang.Compiler.Tests.Unit.Emit
{
public class UnionAndMatchExpressionTests : TestBase
{
public UnionAndMatchExpressionTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

[Fact]
public void MatchesCorrectly1()
{
CreateAssembly(@"
Main() : int {
let u : int | {} = 42;
return u match { x : int => x; {} => 41; };
}")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly2()
{
CreateAssembly(@"
Main() : int {
let u : int | {} = 42;
return u match { {} => 41; x : int => x; };
}")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly3()
{
CreateAssembly(@"
Main() : int {
let u : {} | int = 42;
return u match { x : int => x; {} => 41; };
}")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly4()
{
CreateAssembly(@"
Main() : int {
let u : int | {} = 42;
return u match { int => 42; {} => 41; };
}")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly5()
{
CreateAssembly(@"
Main() : int {
let u1 : int | {} = 42;
let u2 : int | {} | string = u1;
return u2 match { int => 42; {} => 41; string => 40; };
}")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly6()
{
CreateAssembly(@"
Main() : int {
let u : { M1(): int; } | { M2(): int; } = {} + M1, M2;
return u match { {} => 42; };
}
M1(a : {}) : int { return 42; }
M2(a : {}) : int { return 42; }")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly7()
{
CreateAssembly(@"
Main() : int {
let u : { M1(): int; } | { M2(): int; } = {} + M1, M2;
return u match { { M1(): int; } => 42; { M2(): int; } => 41; }
}
M1(a : {}) : int { return 42; }
M2(a : {}) : int { return 42; }")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly8()
{
CreateAssembly(@"
Main() : int {
let u : { M1(): int; } | { M2(): int; } = {} + M1, M2;
return u match { { M2(): int; } => 42; { M1(): int; } => 41; }
}
M1(a : {}) : int { return 42; }
M2(a : {}) : int { return 42; }")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly9()
{
CreateAssembly(@"
Main() : int {
let u1 : { M1(): int; } | { M2(): int; } = {} + M1, M2;
let u2 : { M1(): int; } | { M2(): int; } | int = u1;
return u2 match { { M2(): int; } => 42; { M1(): int; } => 41; int => 40; }
}
M1(a : {}) : int { return 42; }
M2(a : {}) : int { return 42; }")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}

[Fact]
public void MatchesCorrectly10()
{
CreateAssembly(@"
Main() : int {
let u : { M1(): int; } | { M2(): int; } = {} + M1, M2;
return u match { x : { M2(): int; } => x.M2(); x : { M1(): int; } => x.M1(); }
}
M1(a : {}) : int { return 42; }
M2(a : {}) : int { return 42; }")
.VerifyDiagnostics()
.VerifyEmit(expectedResult: 42);
}
}
}

0 comments on commit c34a507

Please sign in to comment.