Faction Test 100.13.0.0
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Barotrauma;
|
||||
using Barotrauma.Extensions;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Xunit;
|
||||
|
||||
@@ -11,6 +13,61 @@ namespace TestProject;
|
||||
public class INetSerializableStructImplementationChecks
|
||||
{
|
||||
private delegate bool TryFindBehaviorDelegate(Type type, out NetSerializableProperties.IReadWriteBehavior behavior);
|
||||
|
||||
private Type FillGenericParameters(Type type)
|
||||
{
|
||||
// Plug in some known good parameters to evaluate
|
||||
// a concrete instance of this generic type
|
||||
|
||||
var paramsConstraints = type.GetGenericArguments()
|
||||
.Select(p => p.GetGenericParameterConstraints())
|
||||
.ToImmutableArray();
|
||||
|
||||
var chosenArgs = new Type[paramsConstraints.Length];
|
||||
|
||||
for (int i = 0; i < paramsConstraints.Length; i++)
|
||||
{
|
||||
var constraints = paramsConstraints[i];
|
||||
var baseTypeConstraints = constraints.Where(c => !c.IsGenericParameter);
|
||||
|
||||
bool hasGenericConstraint(GenericParameterAttributes flag)
|
||||
=> constraints.Any(c
|
||||
=> c.IsGenericParameter && c.GenericParameterAttributes.HasFlag(flag));
|
||||
|
||||
bool refTypeConstraint = hasGenericConstraint(GenericParameterAttributes.ReferenceTypeConstraint);
|
||||
bool valueTypeConstraint = baseTypeConstraints.Contains(typeof(ValueType));
|
||||
|
||||
if (refTypeConstraint && valueTypeConstraint)
|
||||
{
|
||||
throw new Exception($"Type \"{type.Name}\" has invalid generic constraints");
|
||||
}
|
||||
|
||||
var viableArguments = new List<Type>();
|
||||
if (!refTypeConstraint)
|
||||
{
|
||||
// Value types are viable
|
||||
viableArguments.AddRange(new[]
|
||||
{
|
||||
typeof(Vector2),
|
||||
typeof(Point),
|
||||
typeof(int)
|
||||
});
|
||||
}
|
||||
if (!valueTypeConstraint)
|
||||
{
|
||||
// Reference types are viable
|
||||
viableArguments.AddRange(new[]
|
||||
{
|
||||
typeof(string),
|
||||
typeof(float[]),
|
||||
typeof(int[])
|
||||
});
|
||||
}
|
||||
|
||||
chosenArgs[i] = viableArguments.GetRandomUnsynced();
|
||||
}
|
||||
return type.MakeGenericType(chosenArgs);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckStructMemberTypes()
|
||||
@@ -29,50 +86,10 @@ public class INetSerializableStructImplementationChecks
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var concreteType = type;
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
// Plug in some known good parameters to evaluate
|
||||
// a concrete instance of this generic type
|
||||
|
||||
var paramsConstraints = type.GetGenericArguments()
|
||||
.Select(p => p.GetGenericParameterConstraints())
|
||||
.ToImmutableArray();
|
||||
var concreteType = type.IsGenericType
|
||||
? FillGenericParameters(type)
|
||||
: type;
|
||||
|
||||
var chosenArgs = new Type[paramsConstraints.Length];
|
||||
|
||||
for (int i = 0; i < paramsConstraints.Length; i++)
|
||||
{
|
||||
var constraints = paramsConstraints[i];
|
||||
bool refTypeConstraint = constraints.Any(c
|
||||
=> c.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint));
|
||||
bool valueTypeConstraint = constraints.Any(c
|
||||
=> c.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint));
|
||||
if (refTypeConstraint && valueTypeConstraint)
|
||||
{
|
||||
throw new Exception($"Type \"{type.Name}\" has invalid generic constraints");
|
||||
}
|
||||
|
||||
int rngMin = refTypeConstraint ? 3 : 0;
|
||||
int rngMax = valueTypeConstraint ? 3 : 6;
|
||||
|
||||
chosenArgs[i] = Rand.Range(rngMin, rngMax) switch
|
||||
{
|
||||
0 => typeof(Vector2),
|
||||
1 => typeof(Point),
|
||||
2 => typeof(int),
|
||||
|
||||
3 => typeof(string),
|
||||
4 => typeof(float[]),
|
||||
5 => typeof(int[]),
|
||||
|
||||
var invalid => throw new Exception($"Broken RNG ranges in test, got {invalid}")
|
||||
};
|
||||
}
|
||||
|
||||
concreteType = type.MakeGenericType(chosenArgs);
|
||||
}
|
||||
|
||||
var members = NetSerializableProperties.GetPropertiesAndFields(concreteType);
|
||||
foreach (var member in members)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user