-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implicit operator to Union types (#438)
- Loading branch information
Showing
4 changed files
with
96 additions
and
1 deletion.
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
50 changes: 50 additions & 0 deletions
50
src/Tests/FlatSharpEndToEndTests/UnionImplicitOperator/UnionImplicitOperator.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,50 @@ | ||
namespace FlatSharpEndToEndTests.UnionImplicitOperator; | ||
|
||
[TestClass] | ||
public class UnionImplicitOperatorTests | ||
{ | ||
[TestMethod] | ||
public void ImplicitOperator_Struct() | ||
{ | ||
A a = new A { V = 1 }; | ||
|
||
StructUnion u = a; | ||
|
||
Assert.AreEqual(u.GetType(), typeof(StructUnion)); | ||
Assert.AreEqual(a.V, u.A.V); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitOperator_ValueStruct() | ||
{ | ||
Vec3 v = new Vec3 { X = 1, Y = 2, Z = 3 }; | ||
|
||
StructUnion u = v; | ||
|
||
Assert.AreEqual(u.GetType(), typeof(StructUnion)); | ||
Assert.AreEqual(v.X, u.Vec3.X); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitOperator_Table() | ||
{ | ||
MyTable t = new MyTable { Field = "hello", Value = 42 }; | ||
|
||
TableUnion u = t; | ||
|
||
Assert.AreEqual(u.GetType(), typeof(TableUnion)); | ||
Assert.AreEqual(t.Field, u.MyTable.Field); | ||
Assert.AreEqual(t.Value, u.MyTable.Value); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitOperator_Vector() | ||
{ | ||
VectorTable vt = new VectorTable { InnerVector = new[] { "A", "B", "C" } }; | ||
|
||
TableUnion u = vt; | ||
|
||
Assert.AreEqual(u.GetType(), typeof(TableUnion)); | ||
Assert.AreEqual(vt.InnerVector, u.VectorTable.InnerVector); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Tests/FlatSharpEndToEndTests/UnionImplicitOperator/UnionImplicitOperator.fbs
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,25 @@ | ||
attribute "fs_serializer"; | ||
attribute "fs_valueStruct"; | ||
|
||
namespace FlatSharpEndToEndTests.UnionImplicitOperator; | ||
|
||
struct A { V : uint; } | ||
|
||
table MyTable { | ||
Field : string; | ||
Value : uint; | ||
} | ||
|
||
struct Vec3 (fs_valueStruct) { | ||
X : uint; | ||
Y : uint; | ||
Z : uint; | ||
} | ||
|
||
table VectorTable { | ||
InnerVector : [ string ]; | ||
} | ||
|
||
union StructUnion { A, Vec3 } | ||
|
||
union TableUnion { MyTable, VectorTable } |