Tests for sync dictionary and sync set (#1753)

* sync dictionary tests

* rename

* changing error message

* sync set tests
This commit is contained in:
James Frowen 2020-04-22 17:54:09 +01:00 committed by GitHub
parent cf6823acb5
commit 1614c68629
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 756 additions and 4 deletions

View File

@ -346,7 +346,7 @@ public static void ProcessSyncVars(TypeDefinition td, List<FieldDefinition> sync
if (fd.FieldType.Resolve().HasGenericParameters)
{
Weaver.Error($"{fd} Can not use generic SyncObjects directly in NetworkBehaviour. Create a class and inherit from the generic syncList instead.");
Weaver.Error($"{fd} Can not use generic SyncObjects directly in NetworkBehaviour. Create a class and inherit from the generic SyncObject instead.");
return;
}

View File

@ -0,0 +1,177 @@
using NUnit.Framework;
namespace Mirror.Weaver.Tests
{
// Some tests for SyncObjects are in WeaverSyncListTests and apply to SyncDictionary too
public class WeaverSyncDictionaryTests : WeaverTestsBuildFromTestName
{
[Test]
public void SyncDictionary()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryGenericAbstractInheritance()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryGenericInheritance()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryInheritance()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructKey()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructItem()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructKeyWithCustomDeserializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructItemWithCustomDeserializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructKeyWithCustomMethods()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructItemWithCustomMethods()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructKeyWithCustomSerializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryStructItemWithCustomSerializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryErrorForGenericStructKey()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.MyGenericStructDictionary";
string errorMessage = @"Can not create Serialize or Deserialize for generic element\. Override virtual methods with custom Serialize and Deserialize to use MirrorTest.MyGenericStruct`1<System.Single> in SyncList";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
[Test]
public void SyncDictionaryErrorForGenericStructItem()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.MyGenericStructDictionary";
string errorMessage = @"Can not create Serialize or Deserialize for generic element\. Override virtual methods with custom Serialize and Deserialize to use MirrorTest.MyGenericStruct`1<System.Single> in SyncList";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
[Test]
public void SyncDictionaryErrorForGenericStructKeyWithCustomDeserializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.MyGenericStructDictionary";
string errorMessage = @"Can not create Serialize or Deserialize for generic element\. Override virtual methods with custom Serialize and Deserialize to use MirrorTest.MyGenericStruct`1<System.Single> in SyncList";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
[Test]
public void SyncDictionaryErrorForGenericStructItemWithCustomDeserializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.MyGenericStructDictionary";
string errorMessage = @"Can not create Serialize or Deserialize for generic element\. Override virtual methods with custom Serialize and Deserialize to use MirrorTest.MyGenericStruct`1<System.Single> in SyncList";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
[Test]
public void SyncDictionaryErrorForGenericStructKeyWithCustomSerializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.MyGenericStructDictionary";
string errorMessage = @"Can not create Serialize or Deserialize for generic element\. Override virtual methods with custom Serialize and Deserialize to use MirrorTest.MyGenericStruct`1<System.Single> in SyncList";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
[Test]
public void SyncDictionaryErrorForGenericStructItemWithCustomSerializeOnly()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.MyGenericStructDictionary";
string errorMessage = @"Can not create Serialize or Deserialize for generic element\. Override virtual methods with custom Serialize and Deserialize to use MirrorTest.MyGenericStruct`1<System.Single> in SyncList";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
[Test]
public void SyncDictionaryGenericStructKeyWithCustomMethods()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryGenericStructItemWithCustomMethods()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncDictionaryErrorWhenUsingGenericInNetworkBehaviour()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.SomeSyncDictionary`2<System\.Int32,System\.String> MirrorTest\.SyncDictionaryErrorWhenUsingGenericInNetworkBehaviour::someDictionary";
string errorMessage = @"Can not use generic SyncObjects directly in NetworkBehaviour\. Create a class and inherit from the generic SyncObject instead\.";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: de75571c46d27c34bb2c6a67d5eac558
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,12 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryValid : NetworkBehaviour
{
public SyncDictionaryIntString Foo;
}
public class SyncDictionaryIntString : SyncDictionary<int, string> { }
}

View File

@ -0,0 +1,16 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorForGenericStructItem : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<int, MyGenericStruct<float>> { };
}

View File

@ -0,0 +1,22 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorForGenericStructItemWithCustomDeserializeOnly : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
public T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<int, MyGenericStruct<float>>
{
protected override MyGenericStruct<float> DeserializeItem(NetworkReader reader)
{
return new MyGenericStruct<float>() { genericpotato = reader.ReadSingle() };
}
};
}

View File

@ -0,0 +1,22 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorForGenericStructItemWithCustomSerializeOnly : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
public T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<int, MyGenericStruct<float>>
{
protected override void SerializeItem(NetworkWriter writer, MyGenericStruct<float> item)
{
writer.WriteSingle(item.genericpotato);
}
};
}

View File

@ -0,0 +1,16 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorForGenericStructKey : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<MyGenericStruct<float>, int> { };
}

View File

@ -0,0 +1,22 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorForGenericStructKeyWithCustomDeserializeOnly : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
public T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<MyGenericStruct<float>, int>
{
protected override MyGenericStruct<float> DeserializeKey(NetworkReader reader)
{
return new MyGenericStruct<float>() { genericpotato = reader.ReadSingle() };
}
};
}

View File

@ -0,0 +1,22 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorForGenericStructKeyWithCustomSerializeOnly : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
public T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<MyGenericStruct<float>, int>
{
protected override void SerializeKey(NetworkWriter writer, MyGenericStruct<float> item)
{
writer.WriteSingle(item.genericpotato);
}
};
}

View File

@ -0,0 +1,11 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryErrorWhenUsingGenericInNetworkBehaviour : NetworkBehaviour
{
readonly SomeSyncDictionary<int, string> someDictionary;
}
public class SomeSyncDictionary<TKey, TItem> : SyncDictionary<TKey, TItem> { }
}

View File

@ -0,0 +1,13 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryGenericAbstractInheritance : NetworkBehaviour
{
readonly SomeDictionaryIntString dictionary = new SomeDictionaryIntString();
}
public abstract class SomeDictionary<TKey, TItem> : SyncDictionary<TKey, TItem> { }
public class SomeDictionaryIntString : SomeDictionary<int, string> { }
}

View File

@ -0,0 +1,13 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryGenericInheritance : NetworkBehaviour
{
readonly SomeDictionaryIntString dictionary = new SomeDictionaryIntString();
}
public class SomeDictionary<TKey, TItem> : SyncDictionary<TKey, TItem> { }
public class SomeDictionaryIntString : SomeDictionary<int, string> { }
}

View File

@ -0,0 +1,27 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryGenericStructItemWithCustomMethods : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
public T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<int, MyGenericStruct<float>>
{
protected override void SerializeItem(NetworkWriter writer, MyGenericStruct<float> item)
{
writer.WriteSingle(item.genericpotato);
}
protected override MyGenericStruct<float> DeserializeItem(NetworkReader reader)
{
return new MyGenericStruct<float>() { genericpotato = reader.ReadSingle() };
}
};
}

View File

@ -0,0 +1,27 @@
using Mirror;
namespace MirrorTest
{
class SyncDictionaryGenericStructKeyWithCustomMethods : NetworkBehaviour
{
MyGenericStructDictionary harpseals;
}
struct MyGenericStruct<T>
{
public T genericpotato;
}
class MyGenericStructDictionary : SyncDictionary<MyGenericStruct<float>, int>
{
protected override void SerializeKey(NetworkWriter writer, MyGenericStruct<float> item)
{
writer.WriteSingle(item.genericpotato);
}
protected override MyGenericStruct<float> DeserializeKey(NetworkReader reader)
{
return new MyGenericStruct<float>() { genericpotato = reader.ReadSingle() };
}
};
}

View File

@ -0,0 +1,19 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryInheritance : NetworkBehaviour
{
readonly SuperDictionary dictionary = new SuperDictionary();
}
public class SomeDictionary<TKey, TItem> : SyncDictionary<TKey, TItem> { }
public class SomeDictionaryIntString : SomeDictionary<int, string> { }
public class SuperDictionary : SomeDictionaryIntString
{
}
}

View File

@ -0,0 +1,17 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryStructItem : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<int, MyStruct> { }
}

View File

@ -0,0 +1,23 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryStructItemWithCustomDeserializeOnly : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<int, MyStruct>
{
protected override MyStruct DeserializeItem(NetworkReader reader)
{
return new MyStruct() { /* read some stuff here */ };
}
}
}

View File

@ -0,0 +1,28 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryItemStructWithCustomMethods : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<int, MyStruct>
{
protected override void SerializeItem(NetworkWriter writer, MyStruct item)
{
// write some stuff here
}
protected override MyStruct DeserializeItem(NetworkReader reader)
{
return new MyStruct() { /* read some stuff here */ };
}
}
}

View File

@ -0,0 +1,23 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryStructItemWithCustomSerializeOnly : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<int, MyStruct>
{
protected override void SerializeItem(NetworkWriter writer, MyStruct item)
{
// write some stuff here
}
}
}

View File

@ -0,0 +1,17 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryStructKey : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<MyStruct, string> { }
}

View File

@ -0,0 +1,23 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryStructKeyWithCustomDeserializeOnly : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<MyStruct, int>
{
protected override MyStruct DeserializeKey(NetworkReader reader)
{
return new MyStruct() { /* read some stuff here */ };
}
}
}

View File

@ -0,0 +1,28 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryKeyStructWithCustomMethods : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<MyStruct, int>
{
protected override void SerializeKey(NetworkWriter writer, MyStruct item)
{
// write some stuff here
}
protected override MyStruct DeserializeKey(NetworkReader reader)
{
return new MyStruct() { /* read some stuff here */ };
}
}
}

View File

@ -0,0 +1,23 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncDictionaryStructKeyWithCustomSerializeOnly : NetworkBehaviour
{
MyStructDictionary Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructDictionary : SyncDictionary<MyStruct, int>
{
protected override void SerializeKey(NetworkWriter writer, MyStruct item)
{
// write some stuff here
}
}
}

View File

@ -171,8 +171,8 @@ public void SyncListErrorWhenUsingGenericListInNetworkBehaviour()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.True);
string weaverError = @"Mirror\.Weaver error:";
string type = @"MirrorTest\.SomeList`1<System\.Int32> MirrorTest.SyncListErrorWhenUsingGenericListInNetworkBehaviour::someList";
string errorMessage = @"Can not use generic SyncObjects directly in NetworkBehaviour\. Create a class and inherit from the generic syncList instead\.";
string type = @"MirrorTest\.SomeList`1<System\.Int32> MirrorTest\.SyncListErrorWhenUsingGenericListInNetworkBehaviour::someList";
string errorMessage = @"Can not use generic SyncObjects directly in NetworkBehaviour\. Create a class and inherit from the generic SyncObject instead\.";
Assert.That(weaverErrors, Has.Some.Match($"{weaverError} {type} {errorMessage}"));
}
}

View File

@ -3,7 +3,7 @@
namespace MirrorTest
{
class SyncListValid : NetworkBehaviour
class SyncList : NetworkBehaviour
{
public SyncListInt Foo;
}

View File

@ -0,0 +1,50 @@
using NUnit.Framework;
namespace Mirror.Weaver.Tests
{
// Some tests for SyncObjects are in WeaverSyncListTests and apply to SyncDictionary too
public class WeaverSyncSetTests : WeaverTestsBuildFromTestName
{
[Test]
public void SyncSet()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncSetByteValid()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncSetGenericAbstractInheritance()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncSetGenericInheritance()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncSetInheritance()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void SyncSetStruct()
{
Assert.That(CompilationFinishedHook.WeaveFailed, Is.False);
Assert.That(weaverErrors, Is.Empty);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6aeadedebed6110459fac293b9435e88
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncSet : NetworkBehaviour
{
public SyncListInt Foo;
}
}

View File

@ -0,0 +1,12 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncSetByteValid : NetworkBehaviour
{
class MyByteClass : SyncHashSet<byte> {};
MyByteClass Foo;
}
}

View File

@ -0,0 +1,13 @@
using Mirror;
namespace MirrorTest
{
class SyncSetGenericAbstractInheritance : NetworkBehaviour
{
readonly SomeSetInt superSyncSetString = new SomeSetInt();
}
public abstract class SomeSet<T> : SyncHashSet<T> { }
public class SomeSetInt : SomeSet<int> { }
}

View File

@ -0,0 +1,13 @@
using Mirror;
namespace MirrorTest
{
class SyncSetGenericInheritance : NetworkBehaviour
{
readonly SomeSetInt someSet = new SomeSetInt();
}
public class SomeSet<T> : SyncHashSet<T> { }
public class SomeSetInt : SomeSet<int> { }
}

View File

@ -0,0 +1,14 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncSetInheritance : NetworkBehaviour
{
readonly SuperSet superSet = new SuperSet();
}
public class SomeSet : SyncHashSet<string> { }
public class SuperSet : SomeSet { }
}

View File

@ -0,0 +1,17 @@
using UnityEngine;
using Mirror;
namespace MirrorTest
{
class SyncSetStruct : NetworkBehaviour
{
MyStructSet Foo;
}
struct MyStruct
{
int potato;
float floatingpotato;
double givemetwopotatoes;
}
class MyStructSet : SyncHashSet<MyStruct> { }
}