Skip to content

Commit

Permalink
Merge pull request #27 from Szpi/FixIEnumerableOfNullableType
Browse files Browse the repository at this point in the history
Support DateTime, TimeSpan, DateTimeOffset, Guid, Uri
  • Loading branch information
Szpi authored Feb 8, 2019
2 parents fdbebc2 + bccab8b commit f380cf0
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 2 deletions.
3 changes: 1 addition & 2 deletions INIWrapper/IniWrapper/IniWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Authors>Piotr Szkudlarski</Authors>
<Copyright>2019 Piotr Szkudlarski</Copyright>
<PackageReleaseNotes>Breaking change:
- Fixed Key and Section evaluation in Enumerable of complex type</PackageReleaseNotes>
<PackageReleaseNotes>- Support DateTime, TimeSpan, DateTimeOffset, Guid, Uri</PackageReleaseNotes>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<Configurations>Debug;Release;ReleaseBenchmark</Configurations>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace IniWrapper.ModuleTests.Main.Configuration.Properties
{
public class GuidConfiguration
{
public Guid Guid { get; set; }
public Uri Uri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace IniWrapper.ModuleTests.Main.Configuration.Properties
{
public class TimeConfiguration
{
public DateTime DateTime { get; set; }

public TimeSpan TimeSpan { get; set; }

public DateTimeOffset DateTimeOffset { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using FluentAssertions;
using IniWrapper.ModuleTests.Main.Configuration.Properties;
using IniWrapper.ModuleTests.MockParser;
using IniWrapper.ParserWrapper;
using IniWrapper.Wrapper;
using NSubstitute;
using NUnit.Framework;

namespace IniWrapper.ModuleTests.Main.Read.Properties
{
[TestFixture]
public class IniWrapperReadGuidConfigurationTests
{
private IIniWrapper _iniWrapper;

private IIniParser _iniParser;

[SetUp]
public void SetUp()
{
_iniParser = Substitute.For<IIniParser>();
_iniWrapper = MockWrapperFactory.CreateWithFileSystem(_iniParser);
}

[Test]
public void LoadConfiguration_ShouldLoadGuid()
{
var configuration = new GuidConfiguration()
{
Guid = Guid.NewGuid(),
Uri = new Uri("http://testttt.com/")
};

_iniParser.Read(nameof(GuidConfiguration), nameof(GuidConfiguration.Guid)).Returns(configuration.Guid.ToString());
_iniParser.Read(nameof(GuidConfiguration), nameof(GuidConfiguration.Uri)).Returns(configuration.Uri.ToString());

var result = _iniWrapper.LoadConfiguration<GuidConfiguration>();

result.Guid.Should().Be(configuration.Guid);
result.Uri.Should().Be(configuration.Uri);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using FluentAssertions;
using IniWrapper.ModuleTests.Main.Configuration.Properties;
using IniWrapper.ModuleTests.MockParser;
using IniWrapper.ParserWrapper;
using IniWrapper.Wrapper;
using NSubstitute;
using NUnit.Framework;

namespace IniWrapper.ModuleTests.Main.Read.Properties
{
[TestFixture]
public class IniWrapperReadTimeConfigurationTests
{
private IIniWrapper _iniWrapper;

private IIniParser _iniParser;

[SetUp]
public void SetUp()
{
_iniParser = Substitute.For<IIniParser>();
_iniWrapper = MockWrapperFactory.CreateWithFileSystem(_iniParser);
}

[Test]
public void LoadConfiguration_ShouldLoadDateTimeAndTimeSpan()
{
var dateTime = new DateTime(2019, 02, 07, 18, 58, 58);
var timeSpan = new TimeSpan(10, 10, 10);
var datetimeOffset = new DateTimeOffset(2019, 02, 07, 18, 58,10,TimeSpan.FromMinutes(20));

_iniParser.Read(nameof(TimeConfiguration), nameof(TimeConfiguration.DateTime)).Returns(dateTime.ToString("O"));
_iniParser.Read(nameof(TimeConfiguration), nameof(TimeConfiguration.TimeSpan)).Returns(timeSpan.ToString());
_iniParser.Read(nameof(TimeConfiguration), nameof(TimeConfiguration.DateTimeOffset)).Returns(datetimeOffset.ToString());

var result = _iniWrapper.LoadConfiguration<TimeConfiguration>();

result.DateTime.Should().Be(dateTime);
result.TimeSpan.Should().Be(timeSpan);
result.DateTimeOffset.Should().Be(datetimeOffset);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using IniWrapper.ModuleTests.Main.Configuration.Properties;
using IniWrapper.ModuleTests.MockParser;
using IniWrapper.ParserWrapper;
using IniWrapper.Wrapper;
using NSubstitute;
using NUnit.Framework;

namespace IniWrapper.ModuleTests.Main.Save.Properties
{
[TestFixture]
public class IniWrapperSaveGuidConfigurationTests
{
private IIniWrapper _iniWrapper;

private IIniParser _iniParser;

[SetUp]
public void SetUp()
{
_iniParser = Substitute.For<IIniParser>();
_iniWrapper = MockWrapperFactory.CreateWithFileSystem(_iniParser);
}

[Test]
public void SaveConfiguration_ShouldSaveCorrectWriteDateTime_And_TimeSpan()
{
var config = new GuidConfiguration()
{
Guid = Guid.NewGuid(),
Uri = new Uri("http://testttt.com/")
};

_iniWrapper.SaveConfiguration(config);
_iniParser.Received(1).Write(nameof(GuidConfiguration), nameof(GuidConfiguration.Guid), config.Guid.ToString());
_iniParser.Received(1).Write(nameof(GuidConfiguration), nameof(GuidConfiguration.Uri), config.Uri.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using IniWrapper.ModuleTests.Main.Configuration.Properties;
using IniWrapper.ModuleTests.MockParser;
using IniWrapper.ParserWrapper;
using IniWrapper.Wrapper;
using NSubstitute;
using NUnit.Framework;
using System;

namespace IniWrapper.ModuleTests.Main.Save.Properties
{
[TestFixture]
public class IniWrapperSaveTimeConfigurationTests
{
private IIniWrapper _iniWrapper;

private IIniParser _iniParser;

[SetUp]
public void SetUp()
{
_iniParser = Substitute.For<IIniParser>();
_iniWrapper = MockWrapperFactory.CreateWithFileSystem(_iniParser);
}

[Test]
public void SaveConfiguration_ShouldSaveCorrectWriteDateTime_And_TimeSpan()
{
var config = new TimeConfiguration()
{
DateTime = new DateTime(2019, 02, 07, 18, 58, 58),
TimeSpan = new TimeSpan(10, 10, 10),
DateTimeOffset = new DateTimeOffset(2019, 02, 07, 18, 58, 10, TimeSpan.FromMinutes(20))
};

_iniWrapper.SaveConfiguration(config);

_iniParser.Received(1).Write(nameof(TimeConfiguration), nameof(TimeConfiguration.DateTime), config.DateTime.ToString("O"));
_iniParser.Received(1).Write(nameof(TimeConfiguration), nameof(TimeConfiguration.TimeSpan), config.TimeSpan.ToString());
_iniParser.Received(1).Write(nameof(TimeConfiguration), nameof(TimeConfiguration.DateTimeOffset), config.DateTimeOffset.ToString());
}
}
}
28 changes: 28 additions & 0 deletions IniWrapper/IniWrapper/ConverterFactory/IniConverterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using IniWrapper.Wrapper;
using IniWrapper.Wrapper.CustomMemberFactory;
using System;
using IniWrapper.Converters.Guid;
using IniWrapper.Converters.Time;
using IniWrapper.Converters.Uri;
using TypeCode = IniWrapper.Utils.TypeCode;

namespace IniWrapper.ConverterFactory
Expand Down Expand Up @@ -131,6 +134,31 @@ private IIniConverter GetBaseHandler(TypeCode typeCode, bool? isEnum)
return new EnumConverter(typeCode);
}

if (typeCode == TypeCode.DateTime)
{
return new DateTimeConverter();
}

if (typeCode == TypeCode.TimeSpan)
{
return new TimeSpanConverter();
}

if (typeCode == TypeCode.Guid)
{
return new GuidConverter();
}

if (typeCode == TypeCode.DateTimeOffset)
{
return new DateTimeOffsetConverter();
}

if (typeCode == TypeCode.Uri)
{
return new UriConverter();
}

return new PrimitivesConverter();
}
}
Expand Down
19 changes: 19 additions & 0 deletions IniWrapper/IniWrapper/Converters/Guid/GuidConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using IniWrapper.Manager;

namespace IniWrapper.Converters.Guid
{
public class GuidConverter : IIniConverter
{
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext)
{
return System.Guid.Parse(readValue);
}

public IniValue FormatToWrite(object objectToFormat, IniContext iniContext)
{
iniContext.IniValue.Value = objectToFormat.ToString();
return iniContext.IniValue;
}
}
}
20 changes: 20 additions & 0 deletions IniWrapper/IniWrapper/Converters/Time/DateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using IniWrapper.Manager;

namespace IniWrapper.Converters.Time
{
public class DateTimeConverter : IIniConverter
{
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext)
{
return DateTime.Parse(readValue);
}

public IniValue FormatToWrite(object objectToFormat, IniContext iniContext)
{
var dateTime = (DateTime) objectToFormat;
iniContext.IniValue.Value = dateTime.ToString("O");
return iniContext.IniValue;
}
}
}
19 changes: 19 additions & 0 deletions IniWrapper/IniWrapper/Converters/Time/DateTimeOffsetConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using IniWrapper.Manager;

namespace IniWrapper.Converters.Time
{
public class DateTimeOffsetConverter : IIniConverter
{
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext)
{
return DateTimeOffset.Parse(readValue);
}

public IniValue FormatToWrite(object objectToFormat, IniContext iniContext)
{
iniContext.IniValue.Value = objectToFormat.ToString();
return iniContext.IniValue;
}
}
}
19 changes: 19 additions & 0 deletions IniWrapper/IniWrapper/Converters/Time/TimeSpanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using IniWrapper.Manager;

namespace IniWrapper.Converters.Time
{
public class TimeSpanConverter : IIniConverter
{
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext)
{
return TimeSpan.Parse(readValue);
}

public IniValue FormatToWrite(object objectToFormat, IniContext iniContext)
{
iniContext.IniValue.Value = objectToFormat.ToString();
return iniContext.IniValue;
}
}
}
19 changes: 19 additions & 0 deletions IniWrapper/IniWrapper/Converters/Uri/UriConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using IniWrapper.Manager;

namespace IniWrapper.Converters.Uri
{
public class UriConverter : IIniConverter
{
public object ParseReadValue(string readValue, Type destinationType, IniContext iniContext)
{
return new System.Uri(readValue);
}

public IniValue FormatToWrite(object objectToFormat, IniContext iniContext)
{
iniContext.IniValue.Value = objectToFormat.ToString();
return iniContext.IniValue;
}
}
}

0 comments on commit f380cf0

Please sign in to comment.