Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell committed Jul 27, 2023
1 parent 43322c3 commit 8f28c97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Serializers/SerializerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public static void Register<T, TSerializer>(DataDirection direction = DataDirect
/// <typeparam name="TSerializer">The serializer to apply</typeparam>
public static void Register<TSerializer>(Type targetType, DataDirection direction = DataDirection.All) where TSerializer : ApiSerializer, new()
{
if (!targetType.IsClass)
{
throw new ArgumentException($"{targetType.Name} is not a class", nameof(targetType));
}

if (direction.HasFlag(DataDirection.In))
{
DeserializerMap[targetType] = typeof(TSerializer);
Expand Down Expand Up @@ -79,7 +84,7 @@ public static void Unregister<T>(DataDirection direction = DataDirection.All) wh
/// <param name="direction">Whether this serializer should be removed from incoming/outgoing data</param>
public static void Unregister(Type targetType, DataDirection direction = DataDirection.All)
{
if (targetType.IsClass)
if (!targetType.IsClass)
{
throw new ArgumentException($"{targetType.Name} is not a class", nameof(targetType));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Serializers/SerializerResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ public void Setup()

// anothertestobject will use the dummyserializer
SerializerResolver.Register<AnotherTestObject, DummySerializer>();

SerializerResolver.Register<DummySerializer>(typeof(TestContainer<>));
}

[Test]
public void TestResolution()
{
Assert.AreEqual(typeof(ApiXmlSerializer), Client.Serializer.Resolve<TestObject>(DataDirection.In).GetType());
Assert.AreEqual(typeof(ApiJsonSerializer), Client.Serializer.Resolve<YetAnotherTestObject>(DataDirection.Out).GetType());

Assert.AreEqual(typeof(DummySerializer), Client.Serializer.Resolve<TestContainer<TestObject>>(DataDirection.In).GetType());
}

[Test]
Expand Down Expand Up @@ -66,6 +70,11 @@ public class YetAnotherTestObject
{
}

public class TestContainer<T>
{
public T Item { get; set; }
}

public class DummySerializer : ApiSerializer
{
public override string ContentType => "nothing";
Expand Down

0 comments on commit 8f28c97

Please sign in to comment.