Skip to content

Commit

Permalink
Fixed extension ambivalence of IDictionary, IReadyOnlyDictionary and …
Browse files Browse the repository at this point in the history
…its implementations

ignoreCase option added to Enums.ValueOf methods
Removed Struct and IConvertible type requirement for Enums utilities
Fixed typo in InstanceCreator.CreateInstance method
Extensions for CountDownEvent class
DateTime extensions: ToDate, Plus and MinusDays
  • Loading branch information
Jakub Malek authored and Jakub Malek committed Jun 13, 2016
1 parent f2ffeca commit 324081c
Show file tree
Hide file tree
Showing 14 changed files with 620 additions and 19 deletions.
39 changes: 39 additions & 0 deletions src/Sharpility.Tests/Extensions/CountdownEventExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Threading;
using NFluent;
using NUnit.Framework;
using Sharpility.Extensions;

namespace Sharpility.Tests.Extensions
{
[TestFixture]
public class CountdownEventExtensionsTests
{
[Test]
public void ShouldSignalCountDownEvent()
{
// given
var countDownEvent = new CountdownEvent(1);

// when
var result = countDownEvent.TrySignal();

// then
Check.That(result).IsTrue();
Check.That(countDownEvent.CurrentCount).IsEqualTo(0);
}

[Test]
public void ShouldIgnoreSignalingCountDownWhenCurrentCountIsZero()
{
// given
var countDownEvent = new CountdownEvent(0);

// when
var result = countDownEvent.TrySignal();

// then
Check.That(result).IsFalse();
Check.That(countDownEvent.CurrentCount).IsEqualTo(0);
}
}
}
38 changes: 38 additions & 0 deletions src/Sharpility.Tests/Extensions/DateTimeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,43 @@ public void ShouldExtendDateTimeByToMillisecondsMethod()
Check.That(milliseconds).IsEqualTo(1433762100000L);
}

[Test]
public void ShouldConvertDateTimeToDate()
{
// given
var dateTime = new DateTime(2015, 12, 30, 11, 30, 15, DateTimeKind.Utc);

// when
var date = dateTime.ToDate();

// then
Check.That(date).IsEqualTo(new DateTime(2015, 12, 30, 0, 0, 0, DateTimeKind.Utc));
}

[Test]
public void ShouldSubstractDaysFromDateTime()
{
// given
var dateTime = new DateTime(2015, 12, 30, 11, 30, 15, DateTimeKind.Utc);

// when
var results = dateTime.MinusDays(7);

// then
Check.That(results).IsEqualTo(new DateTime(2015, 12, 23, 11, 30, 15, DateTimeKind.Utc));
}

[Test]
public void ShouldAddDaysToDateTime()
{
// given
var dateTime = new DateTime(2015, 12, 30, 11, 30, 15, DateTimeKind.Utc);

// when
var results = dateTime.PlusDays(7);

// then
Check.That(results).IsEqualTo(new DateTime(2016, 1, 6, 11, 30, 15, DateTimeKind.Utc));
}
}
}
1 change: 1 addition & 0 deletions src/Sharpility.Tests/Sharpility.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Collections\OrderedImmutableDictionaryTests.cs" />
<Compile Include="Collections\SortedMappedQueueTests.cs" />
<Compile Include="Extensions\ComparerExtensionsTests.cs" />
<Compile Include="Extensions\CountdownEventExtensionsTests.cs" />
<Compile Include="Extensions\DateTimeExtensionsTests.cs" />
<Compile Include="Extensions\DbCommandExtensionsTests.cs" />
<Compile Include="Extensions\MultiDictionaryExtensionsTests.cs" />
Expand Down
94 changes: 94 additions & 0 deletions src/Sharpility.Tests/Util/EnumsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ public void ShouldReturnEnumByName()
Check.That(result).IsEqualTo(TestEnum.TestValue1);
}

[Test]
public void ShouldReturnEnumByNameCaseInsensitive()
{
// given
const string name = "testvalue2";

// when
var result = Enums.ValueOf<TestEnum>(name, ignoreCase: true);

// when
Check.That(result).IsEqualTo(TestEnum.TestValue2);
}

[Test]
public void ShouldThrowExceptionWhenEnumNotFoundByName()
{
Expand Down Expand Up @@ -66,6 +79,87 @@ public void ShouldReturnEnumValuesNames()
// then
Check.That(names).Contains("TestValue1", "TestValue2");
}

[Test]
public void ShouldReturnEnumByNameOnTry()
{
// given
const string name = "TestValue2";

// var
var value = Enums.TryValueOf<TestEnum>(name);

// then
Check.That(value).IsEqualTo(TestEnum.TestValue2);
}

[Test]
public void ShouldReturnEnumByNameCaseInsensitiveOnTry()
{
// given
const string name = "TESTVALUE2";

// var
var value = Enums.TryValueOf<TestEnum>(name, ignoreCase: true);

// then
Check.That(value).IsEqualTo(TestEnum.TestValue2);
}

[Test]
public void ShouldReturnNullEnumValueWhenNotFound()
{
// given
const string name = "not existing";

// var
var value = Enums.TryValueOf<TestEnum>(name);

// then
Check.That(value).IsNull();
}

[Test]
public void ShouldReturEnumValueByNameIgnoringDefault()
{
// given
const string name = "TestValue1";
const TestEnum defaultValue = TestEnum.TestValue2;

// when
var value = Enums.ValueOf(name, defaultValue);

// then
Check.That(value).IsEqualTo(TestEnum.TestValue1);
}

[Test]
public void ShouldReturEnumValueByNameCaseInsensivieIgnoringDefault()
{
// given
const string name = "Testvalue1";
const TestEnum defaultValue = TestEnum.TestValue2;

// when
var value = Enums.ValueOf(name, defaultValue, ignoreCase: true);

// then
Check.That(value).IsEqualTo(TestEnum.TestValue1);
}

[Test]
public void ShouldReturnDefaultEnumValueWhenNotFoundByName()
{
// given
const string name = "not existing";
const TestEnum defaultValue = TestEnum.TestValue2;

// when
var value = Enums.ValueOf(name, defaultValue);

// then
Check.That(value).IsEqualTo(defaultValue);
}
}

internal enum TestEnum
Expand Down
6 changes: 3 additions & 3 deletions src/Sharpility.Tests/Util/InstanceCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class InstanceCreatorTests
public void ShouldCreateInstanceOfSimpleObject()
{
// when
var instance = InstanceCreator.CreateIntance<SimpleObject>();
var instance = InstanceCreator.CreateInstance<SimpleObject>();

// then
Check.That(instance).IsNotNull();
Expand All @@ -24,7 +24,7 @@ public void ShouldCreateInstanceOfSimpleObject()
public void ShouldCreateInstanceOfObjectWithPrivateConstructor()
{
// when
var instance = InstanceCreator.CreateIntance<PrivateConstructor>();
var instance = InstanceCreator.CreateInstance<PrivateConstructor>();

// then
Check.That(instance).IsNotNull();
Expand All @@ -34,7 +34,7 @@ public void ShouldCreateInstanceOfObjectWithPrivateConstructor()
public void ShouldCreateInstanceOfObjectWithoutDefaultConstrutor()
{
// when
var instance = InstanceCreator.CreateIntance<NoDefaultConstructor>();
var instance = InstanceCreator.CreateInstance<NoDefaultConstructor>();

// then
Check.That(instance).IsNotNull();
Expand Down
2 changes: 1 addition & 1 deletion src/Sharpility.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharpility", "Sharpility\Sharpility.csproj", "{F0922164-3E0B-4B8A-BBD1-730CB96BB704}"
EndProject
Expand Down
34 changes: 34 additions & 0 deletions src/Sharpility/Extensions/CountdownEventExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading;
using Sharpility.Base;

namespace Sharpility.Extensions
{
/// <summary>
/// Extensions for CountdownEvent
/// </summary>
public static class CountdownEventExtensions
{
/// <summary>
/// Signals if current count is greater than zero.
/// </summary>
/// <param name="source">source</param>
public static bool TrySignal(this CountdownEvent source)
{
Preconditions.IsNotNull(source, () => new NullReferenceException("source"));
if (source.CurrentCount > 0)
{
try
{
source.Signal();
}
catch (ObjectDisposedException)
{
return false;
}
return true;
}
return false;
}
}
}
35 changes: 35 additions & 0 deletions src/Sharpility/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,41 @@ public static bool IsEqualsTo(this DateTime source, DateTime dateTime)
return source == dateTime;
}

/// <summary>
/// Converts DateTime to Date with 00:00:00 time.
/// </summary>
/// <param name="source">this</param>
/// <returns>Date</returns>
public static DateTime ToDate(this DateTime source)
{
Preconditions.IsNotNull(source, () => new NullReferenceException("source"));
return new DateTime(source.Year, source.Month, source.Day, 0, 0, 0, source.Kind);
}

/// <summary>
/// Returns DateTime instance with days instance addition to it.
/// </summary>
/// <param name="source">this</param>
/// <param name="days">Number of days added</param>
/// <returns>DateTime with days added</returns>
public static DateTime PlusDays(this DateTime source, uint days)
{
Preconditions.IsNotNull(source, () => new NullReferenceException("source"));
return source.AddDays(days);
}

/// <summary>
/// Returns DateTime instance with days instance substracted from it.
/// </summary>
/// <param name="source">this</param>
/// <param name="days">Number of days substracted</param>
/// <returns>DateTime with substracted days</returns>
public static DateTime MinusDays(this DateTime source, uint days)
{
Preconditions.IsNotNull(source, () => new NullReferenceException("source"));
return source.AddDays(-(int)days);
}

private static bool IsUtcConvertable(DateTime dateTime)
{
switch (dateTime.Kind)
Expand Down
Loading

0 comments on commit 324081c

Please sign in to comment.