-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding attribute that instructs Weaver to write a type as generic
Adding [WeaverWriteAsGeneric] that will tell weaver to write a type using the generic write functions instead of creating functions for it.
- Loading branch information
1 parent
7eab498
commit 1b1e4e6
Showing
5 changed files
with
97 additions
and
8 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
61 changes: 61 additions & 0 deletions
61
Assets/Tests/Runtime/Serialization/WeaverWriteAsGenericTest.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,61 @@ | ||
using Mirage.Serialization; | ||
using NUnit.Framework; | ||
|
||
namespace Mirage.Tests.Runtime.Serialization | ||
{ | ||
public class WeaverWriteAsGenericTest | ||
{ | ||
[Test] | ||
public void WeaverShouldWriteUsingGenericButNotCreateThem() | ||
{ | ||
Assert.That(Writer<IMyInterface>.Write, Is.Null, "Weaver should not have set writer"); | ||
Assert.That(Reader<IMyInterface>.Read, Is.Null, "Weaver should not have set read"); | ||
|
||
var writeCalled = 0; | ||
var readCalled = 0; | ||
|
||
Writer<IMyInterface>.Write = (writer, value) => writeCalled++; | ||
Reader<IMyInterface>.Read = (reader) => { readCalled++; return null; }; | ||
|
||
try | ||
{ | ||
Assert.That(writeCalled, Is.EqualTo(0)); | ||
Assert.That(readCalled, Is.EqualTo(0)); | ||
|
||
var writer = new NetworkWriter(1300); | ||
writer.Write(new MessageWithInterface()); | ||
|
||
Assert.That(writeCalled, Is.EqualTo(1)); | ||
Assert.That(readCalled, Is.EqualTo(0)); | ||
|
||
var reader = new NetworkReader(); | ||
reader.Reset(writer.ToArraySegment()); | ||
var copy = reader.Read<MessageWithInterface>(); | ||
|
||
Assert.That(writeCalled, Is.EqualTo(1)); | ||
Assert.That(readCalled, Is.EqualTo(1)); | ||
} | ||
finally | ||
{ | ||
// they started null, so we need to reset them to null | ||
// if they dont start null, then the assert checks that the start will throw and stop us from setting them in test | ||
Writer<IMyInterface>.Write = null; | ||
Reader<IMyInterface>.Read = null; | ||
} | ||
} | ||
|
||
// a type that weaver can not create a writer for, it should instead write it as a generic | ||
[WeaverWriteAsGeneric] | ||
public interface IMyInterface | ||
{ | ||
int Id { get; set; } | ||
} | ||
|
||
// use the interface in a message so we can check what methods it calls when writing | ||
[NetworkMessage] | ||
public struct MessageWithInterface | ||
{ | ||
public IMyInterface data; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Tests/Runtime/Serialization/WeaverWriteAsGenericTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.