diff --git a/src/PVOutput.Net/Objects/Core/IArrayStringFactory.cs b/src/PVOutput.Net/Objects/Core/IArrayStringFactory.cs new file mode 100644 index 0000000..357ef7e --- /dev/null +++ b/src/PVOutput.Net/Objects/Core/IArrayStringFactory.cs @@ -0,0 +1,7 @@ +namespace PVOutput.Net.Objects.Core +{ + internal interface IArrayStringFactory : IObjectStringFactory + { + IArrayStringReader CreateArrayReader(); + } +} diff --git a/src/PVOutput.Net/Objects/Core/IStringFactory.cs b/src/PVOutput.Net/Objects/Core/IObjectStringFactory.cs similarity index 57% rename from src/PVOutput.Net/Objects/Core/IStringFactory.cs rename to src/PVOutput.Net/Objects/Core/IObjectStringFactory.cs index f0719e1..8671c86 100644 --- a/src/PVOutput.Net/Objects/Core/IStringFactory.cs +++ b/src/PVOutput.Net/Objects/Core/IObjectStringFactory.cs @@ -2,10 +2,8 @@ namespace PVOutput.Net.Objects.Core { - internal interface IStringFactory + internal interface IObjectStringFactory { IObjectStringReader CreateObjectReader(); - - IArrayStringReader CreateArrayReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/AggregatedOutputFactory.cs b/src/PVOutput.Net/Objects/Factories/AggregatedOutputFactory.cs index 9f3267c..29e6949 100644 --- a/src/PVOutput.Net/Objects/Factories/AggregatedOutputFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/AggregatedOutputFactory.cs @@ -1,10 +1,10 @@ -using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class AggregatedOutputFactory : IStringFactory + internal class AggregatedOutputFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/BatchStatusPostResultFactory.cs b/src/PVOutput.Net/Objects/Factories/BatchStatusPostResultFactory.cs index 29dbb7f..353ce41 100644 --- a/src/PVOutput.Net/Objects/Factories/BatchStatusPostResultFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/BatchStatusPostResultFactory.cs @@ -4,7 +4,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class BatchStatusPostResultFactory : IStringFactory + internal class BatchStatusPostResultFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/DayStatisticsFactory.cs b/src/PVOutput.Net/Objects/Factories/DayStatisticsFactory.cs index 8a693b4..b3ac28a 100644 --- a/src/PVOutput.Net/Objects/Factories/DayStatisticsFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/DayStatisticsFactory.cs @@ -1,12 +1,11 @@ -using PVOutput.Net.Objects.Core; +using System; +using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class DayStatisticsFactory : IStringFactory + internal class DayStatisticsFactory : IObjectStringFactory { - public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); - public IObjectStringReader CreateObjectReader() => new DayStatisticsObjectStringReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/ExtendedFactory.cs b/src/PVOutput.Net/Objects/Factories/ExtendedFactory.cs index 5eb1944..07b65b8 100644 --- a/src/PVOutput.Net/Objects/Factories/ExtendedFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/ExtendedFactory.cs @@ -5,7 +5,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class ExtendedFactory : IStringFactory + internal class ExtendedFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/FavouriteFactory.cs b/src/PVOutput.Net/Objects/Factories/FavouriteFactory.cs index fa22713..1c20a2a 100644 --- a/src/PVOutput.Net/Objects/Factories/FavouriteFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/FavouriteFactory.cs @@ -5,7 +5,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class FavouriteFactory : IStringFactory + internal class FavouriteFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader('\n'); diff --git a/src/PVOutput.Net/Objects/Factories/InsolationFactory.cs b/src/PVOutput.Net/Objects/Factories/InsolationFactory.cs index 082db5a..79a76dc 100644 --- a/src/PVOutput.Net/Objects/Factories/InsolationFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/InsolationFactory.cs @@ -5,7 +5,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class InsolationFactory : IStringFactory + internal class InsolationFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/MissingFactory.cs b/src/PVOutput.Net/Objects/Factories/MissingFactory.cs index db69016..83fe4ec 100644 --- a/src/PVOutput.Net/Objects/Factories/MissingFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/MissingFactory.cs @@ -1,14 +1,12 @@ -using System; +using System; using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class MissingFactory : IStringFactory + internal class MissingFactory : IObjectStringFactory { - public IArrayStringReader CreateArrayReader() => throw new NotImplementedException(); - public IObjectStringReader CreateObjectReader() => new MissingObjectStringReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/OutputFactory.cs b/src/PVOutput.Net/Objects/Factories/OutputFactory.cs index d47b406..71ae2d0 100644 --- a/src/PVOutput.Net/Objects/Factories/OutputFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/OutputFactory.cs @@ -1,10 +1,10 @@ -using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class OutputFactory : IStringFactory + internal class OutputFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/StatisticFactory.cs b/src/PVOutput.Net/Objects/Factories/StatisticFactory.cs index ef66859..b1ffd7e 100644 --- a/src/PVOutput.Net/Objects/Factories/StatisticFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/StatisticFactory.cs @@ -1,13 +1,11 @@ -using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class StatisticFactory : IStringFactory + internal class StatisticFactory : IObjectStringFactory { - public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); - public IObjectStringReader CreateObjectReader() => new StatisticObjectStringReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/StatusFactory.cs b/src/PVOutput.Net/Objects/Factories/StatusFactory.cs index 3cf72ed..8f707db 100644 --- a/src/PVOutput.Net/Objects/Factories/StatusFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/StatusFactory.cs @@ -4,10 +4,8 @@ namespace PVOutput.Net.Objects.Factories { - internal class StatusFactory : IStringFactory + internal class StatusFactory : IObjectStringFactory { - public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); - public IObjectStringReader CreateObjectReader() => new StatusObjectStringReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/StatusHistoryFactory.cs b/src/PVOutput.Net/Objects/Factories/StatusHistoryFactory.cs index d596dee..f8ab585 100644 --- a/src/PVOutput.Net/Objects/Factories/StatusHistoryFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/StatusHistoryFactory.cs @@ -4,7 +4,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class StatusHistoryFactory : IStringFactory + internal class StatusHistoryFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/StringFactoryContainer.cs b/src/PVOutput.Net/Objects/Factories/StringFactoryContainer.cs index b86b79b..bd2b64a 100644 --- a/src/PVOutput.Net/Objects/Factories/StringFactoryContainer.cs +++ b/src/PVOutput.Net/Objects/Factories/StringFactoryContainer.cs @@ -29,28 +29,32 @@ static StringFactoryContainer() _readerFactories.Add(typeof(ISystemSearchResult), new SystemSearchResultFactory()); } - public static IStringFactory GetStringFactory() + private static object GetObjectStringFactory() { - var type = typeof(TReturnType); - + Type type = typeof(TReturnType); if (!_readerFactories.ContainsKey(type)) { - throw new NotSupportedException($"Factory for {type} is not known"); + throw new InvalidOperationException($"Factory for {type} is not known"); } - return (IStringFactory)_readerFactories[type]; + return _readerFactories[type]; } public static IObjectStringReader CreateObjectReader() { - var factory = GetStringFactory(); + // Currently every factory is an ObjectStringFactory at minimum + var factory = GetObjectStringFactory() as IObjectStringFactory; return factory.CreateObjectReader(); } public static IArrayStringReader CreateArrayReader() { - var factory = GetStringFactory(); - return factory.CreateArrayReader(); + var factory = GetObjectStringFactory(); + if (factory is IArrayStringFactory arrayFactory) + { + return arrayFactory.CreateArrayReader(); + } + throw new InvalidOperationException($"Factory for {typeof(TReturnType)} is not an array factory"); } } } diff --git a/src/PVOutput.Net/Objects/Factories/SupplyFactory.cs b/src/PVOutput.Net/Objects/Factories/SupplyFactory.cs index f2d53ec..bfb5aea 100644 --- a/src/PVOutput.Net/Objects/Factories/SupplyFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/SupplyFactory.cs @@ -4,7 +4,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class SupplyFactory : IStringFactory + internal class SupplyFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/SystemFactory.cs b/src/PVOutput.Net/Objects/Factories/SystemFactory.cs index fbce73e..168a5cc 100644 --- a/src/PVOutput.Net/Objects/Factories/SystemFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/SystemFactory.cs @@ -1,13 +1,11 @@ -using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class SystemFactory : IStringFactory + internal class SystemFactory : IObjectStringFactory { - public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); - public IObjectStringReader CreateObjectReader() => new SystemObjectStringReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/SystemSearchResultFactory.cs b/src/PVOutput.Net/Objects/Factories/SystemSearchResultFactory.cs index 8934129..601bdd6 100644 --- a/src/PVOutput.Net/Objects/Factories/SystemSearchResultFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/SystemSearchResultFactory.cs @@ -5,7 +5,7 @@ namespace PVOutput.Net.Objects.Factories { - internal class SystemSearchResultFactory : IStringFactory + internal class SystemSearchResultFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new LineDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Objects/Factories/TeamFactory.cs b/src/PVOutput.Net/Objects/Factories/TeamFactory.cs index f1ed09f..a6063ac 100644 --- a/src/PVOutput.Net/Objects/Factories/TeamFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/TeamFactory.cs @@ -5,10 +5,8 @@ namespace PVOutput.Net.Objects.Factories { - internal class TeamFactory : IStringFactory + internal class TeamFactory : IObjectStringFactory { - public IArrayStringReader CreateArrayReader() => throw new NotImplementedException(); - public IObjectStringReader CreateObjectReader() => new TeamObjectStringReader(); } } diff --git a/src/PVOutput.Net/Objects/Factories/TeamOutputFactory.cs b/src/PVOutput.Net/Objects/Factories/TeamOutputFactory.cs index 0285965..7f0eadb 100644 --- a/src/PVOutput.Net/Objects/Factories/TeamOutputFactory.cs +++ b/src/PVOutput.Net/Objects/Factories/TeamOutputFactory.cs @@ -1,10 +1,10 @@ -using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Core; using PVOutput.Net.Objects.Modules; using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Objects.Factories { - internal class TeamOutputFactory : IStringFactory + internal class TeamOutputFactory : IArrayStringFactory { public IArrayStringReader CreateArrayReader() => new CharacterDelimitedArrayStringReader(); diff --git a/src/PVOutput.Net/Requests/Handler/RequestHandler.cs b/src/PVOutput.Net/Requests/Handler/RequestHandler.cs index 8882349..709de9f 100644 --- a/src/PVOutput.Net/Requests/Handler/RequestHandler.cs +++ b/src/PVOutput.Net/Requests/Handler/RequestHandler.cs @@ -60,7 +60,7 @@ internal async Task> ExecuteSingleItemReq } finally { - responseMessage?.Dispose(); + responseMessage.Dispose(); } } @@ -96,7 +96,7 @@ internal async Task> ExecuteArrayReq } finally { - responseMessage?.Dispose(); + responseMessage.Dispose(); } } @@ -129,7 +129,7 @@ internal async Task ExecutePostRequestAsync(IRequest requ } finally { - responseMessage?.Dispose(); + responseMessage.Dispose(); } } diff --git a/tests/PVOutput.Net.Tests/Handler/BaseRequestHandlingTests.cs b/tests/PVOutput.Net.Tests/Handler/BaseRequestHandlingTests.cs index dbeb308..4cbeb8a 100644 --- a/tests/PVOutput.Net.Tests/Handler/BaseRequestHandlingTests.cs +++ b/tests/PVOutput.Net.Tests/Handler/BaseRequestHandlingTests.cs @@ -6,6 +6,8 @@ using NUnit.Framework; using PVOutput.Net.Enums; using PVOutput.Net.Objects; +using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Factories; using PVOutput.Net.Objects.Modules.Implementations; using PVOutput.Net.Responses; using PVOutput.Net.Tests.Utils; @@ -225,5 +227,41 @@ public void BaseResponse_ImplicitBoolConversion_ReturnsSuccessState() Assert.That((bool)response3, Is.False); }); } + + [Test] + public async Task BaseObjectReader_WithNullStream_ReturnsDefaultForType() + { + IObjectStringReader reader = StringFactoryContainer.CreateObjectReader(); + IStatus content = await reader.ReadObjectAsync(stream: null, cancellationToken: default).ConfigureAwait(false); + + Assert.That(content, Is.Null); + } + + [Test] + public async Task BaseArrayReader_WithNullStream_ReturnsDefaultForType() + { + IArrayStringReader reader = StringFactoryContainer.CreateArrayReader(); + IEnumerable content = await reader.ReadArrayAsync(stream: null, cancellationToken: default).ConfigureAwait(false); + + Assert.That(content, Is.Null); + } + + [Test] + public async Task CharacterDelimitedArrayReader_WithNullStream_ReturnsDefaultForType() + { + IArrayStringReader reader = new CharacterDelimitedArrayStringReader(); + IEnumerable content = await reader.ReadArrayAsync(stream: null, cancellationToken: default).ConfigureAwait(false); + + Assert.That(content, Is.Null); + } + + [Test] + public async Task LineDelimitedArrayReader_WithNullStream_ReturnsDefaultForType() + { + IArrayStringReader reader = new LineDelimitedArrayStringReader(); + IEnumerable content = await reader.ReadArrayAsync(stream: null, cancellationToken: default).ConfigureAwait(false); + + Assert.That(content, Is.Null); + } } } diff --git a/tests/PVOutput.Net.Tests/Handler/OtherObjectTests.cs b/tests/PVOutput.Net.Tests/Handler/OtherObjectTests.cs index acac2f2..42ede4d 100644 --- a/tests/PVOutput.Net.Tests/Handler/OtherObjectTests.cs +++ b/tests/PVOutput.Net.Tests/Handler/OtherObjectTests.cs @@ -5,6 +5,9 @@ using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; using NUnit.Framework; using PVOutput.Net.Objects; +using PVOutput.Net.Objects.Core; +using PVOutput.Net.Objects.Factories; +using PVOutput.Net.Objects.Modules.Readers; namespace PVOutput.Net.Tests.Handler { @@ -23,7 +26,14 @@ public static IEnumerable PVCoordinateRegularEqualityTestCases [Test] [TestCaseSource(typeof(OtherObjectTests), nameof(PVCoordinateRegularEqualityTestCases))] - public bool PVCoordinate_Equals_ReturnsValueEquality(PVCoordinate coordinate1, PVCoordinate coordinate2) + public bool PVCoordinate_EqualsByType_ReturnsValueEquality(PVCoordinate coordinate1, PVCoordinate coordinate2) + { + return coordinate1.Equals(coordinate2); + } + + [Test] + [TestCaseSource(typeof(OtherObjectTests), nameof(PVCoordinateRegularEqualityTestCases))] + public bool PVCoordinate_EqualsByObject_ReturnsValueEquality(PVCoordinate coordinate1, object coordinate2) { return coordinate1.Equals(coordinate2); } @@ -71,7 +81,14 @@ public static IEnumerable ExtendedDataConfigurationRegularEqualityTestCases [Test] [TestCaseSource(typeof(OtherObjectTests), nameof(ExtendedDataConfigurationRegularEqualityTestCases))] - public bool ExtendedDataConfiguration_Equals_ReturnsValueEquality(ExtendedDataConfiguration configuration1, ExtendedDataConfiguration configuration2) + public bool ExtendedDataConfiguration_EqualsByType_ReturnsValueEquality(ExtendedDataConfiguration configuration1, ExtendedDataConfiguration configuration2) + { + return configuration1.Equals(configuration2); + } + + [Test] + [TestCaseSource(typeof(OtherObjectTests), nameof(ExtendedDataConfigurationRegularEqualityTestCases))] + public bool ExtendedDataConfiguration_EqualsByObject_ReturnsValueEquality(ExtendedDataConfiguration configuration1, object configuration2) { return configuration1.Equals(configuration2); } @@ -89,6 +106,7 @@ public bool ExtendedDataConfiguration_OperatorEquals_ReturnsValueEquality(Extend { return configuration1 == configuration2; } + public static IEnumerable ExtendedDataConfigurationInvertedEqualityTestCases { get @@ -105,5 +123,35 @@ public bool ExtendedDataConfiguration_OperatorNotEquals_ReturnsValueEquality(Ext { return configuration1 != configuration2; } + + [Test] + public void StringFactoryContainer_ForType_CreatesObjectReader() + { + IObjectStringReader reader = StringFactoryContainer.CreateObjectReader(); + Assert.That(reader, Is.TypeOf()); + } + + [Test] + public void StringFactoryContainer_ForType_CreatesArrayReader() + { + IArrayStringReader reader = StringFactoryContainer.CreateArrayReader(); + Assert.That(reader, Is.TypeOf>()); + } + + [Test] + public void StringFactoryContainer_ForUnknownType_Throws() + { + Assert.Throws(() => { + _ = StringFactoryContainer.CreateObjectReader(); + }); + } + + [Test] + public void StringFactoryContainer_ForTypeWithoutArrayReader_Throws() + { + Assert.Throws(() => { + _ = StringFactoryContainer.CreateArrayReader(); + }); + } } }