1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

added DynamicEnum tests and switched to using Activator for instance creation

This commit is contained in:
Ell 2021-10-28 22:14:43 +02:00
parent 6ff2c1c1cc
commit d05262e8a6
4 changed files with 41 additions and 5 deletions

View file

@ -100,8 +100,7 @@ namespace MLEM.Data.Content {
continue; continue;
if (!type.IsSubclassOf(typeof(RawContentReader))) if (!type.IsSubclassOf(typeof(RawContentReader)))
continue; continue;
var inst = type.GetConstructor(Type.EmptyTypes).Invoke(null); ret.Add((RawContentReader) Activator.CreateInstance(type));
ret.Add((RawContentReader) inst);
} catch (Exception e) { } catch (Exception e) {
throw new NotSupportedException($"The type {type} cannot be constructed by a RawContentManager. Does it have a visible parameterless constructor?", e); throw new NotSupportedException($"The type {type} cannot be constructed by a RawContentManager. Does it have a visible parameterless constructor?", e);
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Reflection; using System.Reflection;
@ -322,8 +323,7 @@ namespace MLEM.Data {
} }
private static DynamicEnum Construct(Type type, string name, BigInteger value) { private static DynamicEnum Construct(Type type, string name, BigInteger value) {
var constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new[] {typeof(string), typeof(BigInteger)}, null); return (DynamicEnum) Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] {name, value}, CultureInfo.InvariantCulture);
return (DynamicEnum) constructor.Invoke(new object[] {name, value});
} }
private class Storage { private class Storage {

View file

@ -13,7 +13,7 @@ namespace MLEM.Data.Json {
/// </summary> /// </summary>
public static readonly JsonConverter[] Converters = typeof(JsonConverters).Assembly.GetExportedTypes() public static readonly JsonConverter[] Converters = typeof(JsonConverters).Assembly.GetExportedTypes()
.Where(t => t.IsSubclassOf(typeof(JsonConverter)) && !t.IsGenericType) .Where(t => t.IsSubclassOf(typeof(JsonConverter)) && !t.IsGenericType)
.Select(t => t.GetConstructor(Type.EmptyTypes).Invoke(null)).Cast<JsonConverter>().ToArray(); .Select(Activator.CreateInstance).Cast<JsonConverter>().ToArray();
/// <summary> /// <summary>
/// Adds all of the <see cref="JsonConverter"/> objects that are part of MLEM.Data to the given <see cref="JsonSerializer"/> /// Adds all of the <see cref="JsonConverter"/> objects that are part of MLEM.Data to the given <see cref="JsonSerializer"/>

View file

@ -1,12 +1,15 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Numerics;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Data; using MLEM.Data;
using MLEM.Data.Json; using MLEM.Data.Json;
using MLEM.Misc; using MLEM.Misc;
using Newtonsoft.Json; using Newtonsoft.Json;
using NUnit.Framework; using NUnit.Framework;
using static MLEM.Data.DynamicEnum;
using Vector2 = Microsoft.Xna.Framework.Vector2;
namespace Tests { namespace Tests {
public class DataTests { public class DataTests {
@ -62,6 +65,33 @@ namespace Tests {
TestContext.WriteLine($"DeepCopy took {stopwatch.Elapsed.TotalMilliseconds / count * 1000000}ns on average"); TestContext.WriteLine($"DeepCopy took {stopwatch.Elapsed.TotalMilliseconds / count * 1000000}ns on average");
} }
[Test]
public void TestDynamicEnum() {
var flags = new TestEnum[100];
for (var i = 0; i < flags.Length; i++)
flags[i] = AddFlag<TestEnum>("Flag" + i);
Assert.AreEqual(GetValue(flags[7]), BigInteger.One << 7);
Assert.AreEqual(GetEnumValue<TestEnum>(BigInteger.One << 75), flags[75]);
Assert.AreEqual(GetValue(Or(flags[2], flags[17])), (BigInteger.One << 2) | (BigInteger.One << 17));
Assert.AreEqual(GetValue(And(flags[2], flags[3])), BigInteger.Zero);
Assert.AreEqual(And(Or(flags[24], flags[52]), Or(flags[52], flags[75])), flags[52]);
Assert.AreEqual(Xor(Or(flags[85], flags[73]), flags[73]), flags[85]);
Assert.AreEqual(Xor(Or(flags[85], Or(flags[73], flags[12])), flags[73]), Or(flags[85], flags[12]));
Assert.AreEqual(GetValue(Neg(flags[74])), ~(BigInteger.One << 74));
Assert.AreEqual(Or(flags[24], flags[52]).HasFlag(flags[24]), true);
Assert.AreEqual(Or(flags[24], flags[52]).HasAnyFlag(flags[24]), true);
Assert.AreEqual(Or(flags[24], flags[52]).HasFlag(Or(flags[24], flags[26])), false);
Assert.AreEqual(Or(flags[24], flags[52]).HasAnyFlag(Or(flags[24], flags[26])), true);
Assert.AreEqual(Parse<TestEnum>("Flag24"), flags[24]);
Assert.AreEqual(Parse<TestEnum>("Flag24 | Flag43"), Or(flags[24], flags[43]));
Assert.AreEqual(flags[24].ToString(), "Flag24");
Assert.AreEqual(Or(flags[24], flags[43]).ToString(), "Flag24 | Flag43");
}
private class TestObject { private class TestObject {
public Vector2 Vec; public Vector2 Vec;
@ -86,5 +116,12 @@ namespace Tests {
} }
private class TestEnum : DynamicEnum {
public TestEnum(string name, BigInteger value) : base(name, value) {
}
}
} }
} }