Test for array segment and writers (#2157)

* tests for array and segment writers

* fix typo
This commit is contained in:
James Frowen 2020-08-14 23:29:07 +01:00 committed by GitHub
parent c1410b0924
commit 2fbd131f61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 156 additions and 7 deletions

View File

@ -122,10 +122,15 @@ public static bool CanBeResolved(this TypeReference parent)
}
// Given a method of a generic class such as ArraySegment<T>.get_Count,
// and a generic instance such as ArraySegment<int>
// Creates a reference to the specialized method ArraySegment<int>.get_Count
// Note that calling ArraySegment<T>.get_Count directly gives an invalid IL error
/// <summary>
/// Given a method of a generic class such as ArraySegment`T.get_Count,
/// and a generic instance such as ArraySegment`int
/// Creates a reference to the specialized method ArraySegment`int`.get_Count
/// <para> Note that calling ArraySegment`T.get_Count directly gives an invalid IL error </para>
/// </summary>
/// <param name="self"></param>
/// <param name="instanceType"></param>
/// <returns></returns>
public static MethodReference MakeHostInstanceGeneric(this MethodReference self, GenericInstanceType instanceType)
{
MethodReference reference = new MethodReference(self.Name, self.ReturnType, instanceType)

View File

@ -36,7 +36,10 @@ public static MethodReference GetReadFunc(TypeReference variable, int recursionC
if (variable.IsArray)
{
newReaderFunc = GenerateArrayReadFunc(variable, recursionCount);
RegisterReadFunc(variable.FullName, newReaderFunc);
if (newReaderFunc != null)
{
RegisterReadFunc(variable.FullName, newReaderFunc);
}
return newReaderFunc;
}
@ -121,6 +124,7 @@ static MethodDefinition GenerateArrayReadFunc(TypeReference variable, int recurs
MethodReference elementReadFunc = GetReadFunc(elementType, recursionCount + 1);
if (elementReadFunc == null)
{
Weaver.Error($"Cannot generate reader for Array because element {elementType.Name} does not have a reader. Use a supported type or provide a custom reader", variable);
return null;
}
@ -214,6 +218,7 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable, int
MethodReference elementReadFunc = GetReadFunc(elementType, recursionCount + 1);
if (elementReadFunc == null)
{
Weaver.Error($"Cannot generate reader for ArraySegment because element {elementType.Name} does not have a reader. Use a supported type or provide a custom reader", variable);
return null;
}

View File

@ -37,7 +37,10 @@ public static MethodReference GetWriteFunc(TypeReference variable, int recursion
if (variable.IsArray)
{
newWriterFunc = GenerateArrayWriteFunc(variable, recursionCount);
RegisterWriteFunc(variable.FullName, newWriterFunc);
if (newWriterFunc != null)
{
RegisterWriteFunc(variable.FullName, newWriterFunc);
}
return newWriterFunc;
}
@ -196,6 +199,7 @@ static MethodDefinition GenerateArrayWriteFunc(TypeReference variable, int recur
MethodReference elementWriteFunc = GetWriteFunc(elementType, recursionCount + 1);
if (elementWriteFunc == null)
{
Weaver.Error($"Cannot generate writer for Array because element {elementType.Name} does not have a writer. Use a supported type or provide a custom writer", variable);
return null;
}
@ -293,6 +297,7 @@ static MethodDefinition GenerateArraySegmentWriteFunc(TypeReference variable, in
if (elementWriteFunc == null)
{
Weaver.Error($"Cannot generate writer for ArraySegment because element {elementType.Name} does not have a writer. Use a supported type or provide a custom writer", variable);
return null;
}

View File

@ -1,4 +1,4 @@
<Project>
<Project>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>_WeaverTests2.csproj</RootNamespace>
@ -64,6 +64,7 @@
<ItemGroup>
<Compile Include="GeneratedReaderWriter~\CanUseCustomReadWriteForInterfaces.cs" />
<Compile Include="GeneratedReaderWriter~\CanUseCustomReadWriteForTypesFromDifferentAssemblies.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForArraySegment.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForClass.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForClassFromDifferentAssemblies.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForClassFromDifferentAssembliesWithValidConstructor.cs" />
@ -73,8 +74,13 @@
<Compile Include="GeneratedReaderWriter~\CreatesForInheritedFromScriptableObject.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForStructFromDifferentAssemblies.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForStructs.cs" />
<Compile Include="GeneratedReaderWriter~\CreatesForStuctArraySegment.cs" />
<Compile Include="GeneratedReaderWriter~\ExcludesNonSerializedFields.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorForClassWithNoValidConstructor.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorForInvalidArraySegmentType.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorForInvalidArrayType.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorForJaggedArray.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorForMultidimensionalArray.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorWhenUsingInterface.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorWhenUsingMonoBehaviour.cs" />
<Compile Include="GeneratedReaderWriter~\GivesErrorWhenUsingObject.cs" />

View File

@ -0,0 +1,14 @@
using System;
using Mirror;
namespace GeneratedReaderWriter.CreatesForArraySegment
{
public class CreatesForArraySegment : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(ArraySegment<int> data)
{
// empty
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using Mirror;
using UnityEngine;
namespace GeneratedReaderWriter.CreatesForStructArraySegment
{
public class CreatesForStructArraySegment : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(ArraySegment<MyStruct> data)
{
// empty
}
}
public struct MyStruct
{
public int someValue;
public Vector3 anotherValue;
}
}

View File

@ -0,0 +1,15 @@
using System;
using Mirror;
using UnityEngine;
namespace GeneratedReaderWriter.GivesErrorForInvalidArraySegmentType
{
public class GivesErrorForInvalidArraySegmentType : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(ArraySegment<MonoBehaviour> data)
{
// empty
}
}
}

View File

@ -0,0 +1,14 @@
using Mirror;
using UnityEngine;
namespace GeneratedReaderWriter.GivesErrorForInvalidArrayType
{
public class GivesErrorForInvalidArrayType : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(MonoBehaviour[] data)
{
// empty
}
}
}

View File

@ -0,0 +1,13 @@
using Mirror;
namespace GeneratedReaderWriter.GivesErrorForJaggedArray
{
public class GivesErrorForJaggedArray : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(int[][] data)
{
// empty
}
}
}

View File

@ -0,0 +1,13 @@
using Mirror;
namespace GeneratedReaderWriter.GivesErrorForMultidimensionalArray
{
public class GivesErrorForMultidimensionalArray : NetworkBehaviour
{
[ClientRpc]
public void RpcDoSomething(int[,] data)
{
// empty
}
}
}

View File

@ -141,5 +141,43 @@ public void CreatesForEnums()
{
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void CreatesForArraySegment()
{
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void CreatesForStructArraySegment()
{
Assert.That(weaverErrors, Is.Empty);
}
[Test]
public void GivesErrorForJaggedArray()
{
Assert.That(weaverErrors, Contains.Item($"Int32[][] is an unsupported type. Jagged and multidimensional arrays are not supported (at System.Int32[][])"));
}
[Test]
public void GivesErrorForMultidimensionalArray()
{
Assert.That(weaverErrors, Contains.Item($"Int32[0...,0...] is an unsupported type. Jagged and multidimensional arrays are not supported (at System.Int32[0...,0...])"));
}
[Test]
public void GivesErrorForInvalidArrayType()
{
Assert.That(weaverErrors, Contains.Item("Cannot generate writer for Array because element MonoBehaviour does not have a writer. Use a supported type or provide a custom writer (at UnityEngine.MonoBehaviour[])"));
Assert.That(weaverErrors, Contains.Item("Cannot generate reader for Array because element MonoBehaviour does not have a reader. Use a supported type or provide a custom reader (at UnityEngine.MonoBehaviour[])"));
}
[Test]
public void GivesErrorForInvalidArraySegmentType()
{
Assert.That(weaverErrors, Contains.Item("Cannot generate writer for ArraySegment because element MonoBehaviour does not have a writer. Use a supported type or provide a custom writer (at System.ArraySegment`1<UnityEngine.MonoBehaviour>)"));
Assert.That(weaverErrors, Contains.Item("Cannot generate reader for ArraySegment because element MonoBehaviour does not have a reader. Use a supported type or provide a custom reader (at System.ArraySegment`1<UnityEngine.MonoBehaviour>)"));
}
}
}