Skip to content

Commit 5c7a8ed

Browse files
committed
Remove of .First() and .Last() method extensions for collections: LINQ FirstOrDefault and LastOrDefault should be used instead
Utilities for TimeSpan struct added Extensions for TimeSpan stuct added Utilities for DateTime struct added Dictionary for http header values added: Sharpility.Net.HttpHeaders Dictionary for content types values added: Sharpility.Net.MediaTypes Enum for http methods values added: Sharpility.Net.HttpMethod Extension method ToComparable added to IDictionary, IList, ISet, ICollection and IEnumerable that wraps object with equals/hashCode/toString implementations InstanceCreator utility added for creating instance of type without default constructor requirement System.Collections.Immutable version update: 1.1.36 -> 1.1.37
1 parent e744dfb commit 5c7a8ed

54 files changed

Lines changed: 2962 additions & 207 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using NFluent;
5+
using NUnit.Framework;
6+
using Sharpility.Collections.Concurrent;
7+
8+
namespace Sharpility.Tests.Collections.Concurrent
9+
{
10+
[TestFixture]
11+
public class DefaultBlockingQueueTests
12+
{
13+
[Test]
14+
public void PeekShouldReturnNull()
15+
{
16+
// given
17+
var queue = new DefaultBlockingQueue<string>();
18+
19+
// when
20+
var item = queue.Peek();
21+
22+
// then
23+
Check.That(item).IsNull();
24+
}
25+
26+
[Test]
27+
public void PeekShouldReturnValueWithoutDequeue()
28+
{
29+
// given
30+
var queue = new DefaultBlockingQueue<string>();
31+
queue.Offer("test");
32+
33+
// when
34+
var item = queue.Peek();
35+
36+
// then
37+
Check.That(item).IsEqualTo("test");
38+
Check.That(queue).HasSize(1);
39+
}
40+
41+
[Test]
42+
public void ShouldPollValue()
43+
{
44+
// given
45+
var queue = new DefaultBlockingQueue<string>();
46+
queue.Offer("test");
47+
48+
// when
49+
var item = queue.Poll();
50+
51+
// then
52+
Check.That(item).IsEqualTo("test");
53+
Check.That(queue).IsEmpty();
54+
}
55+
56+
[Test]
57+
public void PollShouldReturnNull()
58+
{
59+
// given
60+
var queue = new DefaultBlockingQueue<string>();
61+
62+
// when
63+
var item = queue.Poll();
64+
65+
// then
66+
Check.That(item).IsNull();
67+
}
68+
69+
[Test, Timeout(2000)]
70+
public void TakeShouldWaitForElementAvailability()
71+
{
72+
// given
73+
var queue = new DefaultBlockingQueue<string>();
74+
const string element = "test";
75+
Task.Factory.StartNew(() =>
76+
{
77+
Thread.Sleep(TimeSpan.FromSeconds(1));
78+
queue.Offer(element);
79+
});
80+
81+
// when
82+
var item = queue.Take();
83+
84+
// then
85+
Check.That(item).IsEqualTo(element);
86+
}
87+
88+
89+
[Test]
90+
public void ShouldWaitForValueTake()
91+
{
92+
// given
93+
var queue = new DefaultBlockingQueue<string>();
94+
var timeout = TimeSpan.FromSeconds(10);
95+
const string itemToOffer = "item";
96+
97+
// when
98+
Task.Run(() =>
99+
{
100+
Thread.Sleep(100);
101+
queue.Offer(itemToOffer);
102+
});
103+
var item = queue.Poll(timeout);
104+
105+
// then
106+
Check.That(item).IsEqualTo(itemToOffer);
107+
}
108+
}
109+
}

src/Sharpility.Tests/Collections/OrderedImmutableDictionaryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class OrderedImmutableDictionaryTests
1212
public void ShouldReturnKeysInPutOrder()
1313
{
1414
// given
15-
var dictionary = OrderedHashImmutableDictionary<string, int>.CreateBuilder()
15+
var dictionary = OrderedHashImmutableDictionary<string, int>.Builder()
1616
.Put("Z", 1)
1717
.Put("Y", 2)
1818
.Put("X", 3)
@@ -29,7 +29,7 @@ public void ShouldReturnKeysInPutOrder()
2929
public void ShouldReturnEntriesInPutOrder()
3030
{
3131
// given
32-
var dictionary = OrderedHashImmutableDictionary<string, int>.CreateBuilder()
32+
var dictionary = OrderedHashImmutableDictionary<string, int>.Builder()
3333
.Put("Z", 1)
3434
.Put("Y", 2)
3535
.Put("X", 3)

src/Sharpility.Tests/Extensions/CollectionExtensionsTests.cs

Lines changed: 217 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections;
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
6+
using System.Linq;
67
using NFluent;
78
using NiceTry;
89
using NUnit.Framework;
@@ -471,19 +472,6 @@ public void ShouldExtenedListByFirstMethod()
471472
Check.That(first).IsEqualTo("A");
472473
}
473474

474-
[Test]
475-
public void ShouldReturnNullFirstElementOfEmptyCollection()
476-
{
477-
// given
478-
ICollection<int?> collection = Lists.EmptyList<int?>();
479-
480-
// when
481-
var first = collection.First();
482-
483-
// then
484-
Check.That(first).IsNull();
485-
}
486-
487475
[Test]
488476
public void ShouldExtenedLinkedListByLastMethod()
489477
{
@@ -512,19 +500,6 @@ public void ShouldExtenedListByLastMethod()
512500
Check.That(first).IsEqualTo("C");
513501
}
514502

515-
[Test]
516-
public void ShouldReturnNullLastElementOfEmptyCollection()
517-
{
518-
// given
519-
ICollection<int?> collection = Lists.EmptyList<int?>();
520-
521-
// when
522-
var first = collection.Last();
523-
524-
// then
525-
Check.That(first).IsNull();
526-
}
527-
528503
[Test]
529504
public void ShouldExtendLinkedListByRemoveFirstMethod()
530505
{
@@ -727,5 +702,221 @@ public void ShouldExtenedEnumerableByDistinctElementCountMethod()
727702
Check.That(setDistinctElementsCount).IsEqualTo(Dictionaries.Create("A", 1, "B", 1, "C", 1));
728703
Check.That(listDistinctElementsCount).IsEqualTo(Dictionaries.Create("A", 1, "B", 3, "C", 2, "D", 4));
729704
}
705+
706+
[Test]
707+
[TestCaseSource("TopSliceCases")]
708+
public IList<string> ShouldReturnTopSliceOfList(IList<string> list, int size)
709+
{
710+
// when
711+
var result = list.TopSlice(size);
712+
713+
// then
714+
return result;
715+
}
716+
717+
private static IEnumerable<ITestCaseData> TopSliceCases()
718+
{
719+
var givenList = Lists.AsList("A", "B", "C");
720+
var givenSize = 3;
721+
var expectedSlice = Lists.AsList("A", "B", "C");
722+
yield return new TestCaseData(givenList, givenSize)
723+
.SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
724+
.Returns(expectedSlice);
725+
726+
givenList = Lists.AsList("A", "B", "C");
727+
givenSize = 2;
728+
expectedSlice = Lists.AsList("B", "C");
729+
yield return new TestCaseData(givenList, givenSize)
730+
.SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
731+
.Returns(expectedSlice);
732+
733+
givenList = Lists.AsList("A", "B", "C");
734+
givenSize = 1;
735+
expectedSlice = Lists.AsList("C");
736+
yield return new TestCaseData(givenList, givenSize)
737+
.SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
738+
.Returns(expectedSlice);
739+
740+
givenList = Lists.AsList("A", "B", "C");
741+
givenSize = 5;
742+
expectedSlice = Lists.AsList("A", "B", "C");
743+
yield return new TestCaseData(givenList, givenSize)
744+
.SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
745+
.Returns(expectedSlice);
746+
747+
givenList = Lists.AsList("A", "B", "C");
748+
givenSize = 0;
749+
expectedSlice = Lists.EmptyList<string>();
750+
yield return new TestCaseData(givenList, givenSize)
751+
.SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
752+
.Returns(expectedSlice);
753+
}
754+
755+
[Test]
756+
[TestCaseSource("BottomSliceCases")]
757+
public IList<string> ShouldTrimCollectionRight(IList<string> list, int size)
758+
{
759+
// when
760+
var result = list.BottomSlice(size);
761+
762+
// then
763+
return result;
764+
}
765+
766+
private static IEnumerable<ITestCaseData> BottomSliceCases()
767+
{
768+
var givenList = Lists.AsList("A", "B", "C");
769+
var givenSize = 3;
770+
var expectedSlice = Lists.AsList("A", "B", "C");
771+
yield return new TestCaseData(givenList, givenSize)
772+
.SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
773+
.Returns(expectedSlice);
774+
775+
givenList = Lists.AsList("A", "B", "C");
776+
givenSize = 2;
777+
expectedSlice = Lists.AsList("A", "B");
778+
yield return new TestCaseData(givenList, givenSize)
779+
.SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
780+
.Returns(expectedSlice);
781+
782+
givenList = Lists.AsList("A", "B", "C");
783+
givenSize = 1;
784+
expectedSlice = Lists.AsList("A");
785+
yield return new TestCaseData(givenList, givenSize)
786+
.SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
787+
.Returns(expectedSlice);
788+
789+
givenList = Lists.AsList("A", "B", "C");
790+
givenSize = 5;
791+
expectedSlice = Lists.AsList("A", "B", "C");
792+
yield return new TestCaseData(givenList, givenSize)
793+
.SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
794+
.Returns(expectedSlice);
795+
796+
givenList = Lists.AsList("A", "B", "C");
797+
givenSize = 0;
798+
expectedSlice = Lists.EmptyList<string>();
799+
yield return new TestCaseData(givenList, givenSize)
800+
.SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice))
801+
.Returns(expectedSlice);
802+
}
803+
804+
[Test]
805+
[TestCaseSource("SetLastListElementCases")]
806+
public IList<string> ShouldReplaceLastElementList(IList<string> elements, string element)
807+
{
808+
// given
809+
var list = new List<string>(elements);
810+
811+
// when
812+
list.SetLast(element);
813+
814+
// then
815+
return list;
816+
}
817+
818+
private static IEnumerable<ITestCaseData> SetLastListElementCases()
819+
{
820+
const string element = "X";
821+
var givenList = Lists.AsList("A", "B", "C");
822+
var expected = Lists.AsList("A", "B", element);
823+
yield return new TestCaseData(givenList, element)
824+
.SetName(Strings.Format("Replace last item of {1} by {0} results {2}", element, givenList, expected))
825+
.Returns(expected);
826+
827+
givenList = Lists.EmptyList<string>();
828+
expected = Lists.AsList(element);
829+
yield return new TestCaseData(givenList, element)
830+
.SetName(Strings.Format("Replace last item of {1} by {0} results {2}", element, givenList, expected))
831+
.Returns(expected);
832+
}
833+
834+
[Test]
835+
[TestCaseSource("SetFirstListElementCases")]
836+
public IList<string> ShouldReplaceFirstElementList(IList<string> elements, string element)
837+
{
838+
// given
839+
var list = new List<string>(elements);
840+
841+
// when
842+
list.SetFirst(element);
843+
844+
// then
845+
return list;
846+
}
847+
848+
private static IEnumerable<ITestCaseData> SetFirstListElementCases()
849+
{
850+
const string element = "X";
851+
var givenList = Lists.AsList("A", "B", "C");
852+
var expected = Lists.AsList(element, "B", "C");
853+
yield return new TestCaseData(givenList, element)
854+
.SetName(Strings.Format("Replace first item of {1} by {0} results {2}", element, givenList, expected))
855+
.Returns(expected);
856+
857+
givenList = Lists.EmptyList<string>();
858+
expected = Lists.AsList(element);
859+
yield return new TestCaseData(givenList, element)
860+
.SetName(Strings.Format("Replace first item of {1} by {0} results {2}", element, givenList, expected))
861+
.Returns(expected);
862+
}
863+
864+
[Test]
865+
public void ShouldConvertEnumerableToComparable()
866+
{
867+
// given
868+
IEnumerable<string> enumerable = new[] {"A", "B", "C"};
869+
870+
// when
871+
var comparable = enumerable.ToComparable();
872+
873+
// then
874+
Check.That(comparable).IsEqualTo(Lists.AsList("A", "B", "C"));
875+
Check.That(comparable.GetHashCode()).IsEqualTo(enumerable.ToComparable().GetHashCode());
876+
Check.That(comparable.ToString()).IsEqualTo("[A, B, C]");
877+
}
878+
879+
[Test]
880+
public void ShouldConvertListToComparable()
881+
{
882+
// given
883+
IList<string> enumerable = new[] { "A", "B", "C" };
884+
885+
// when
886+
var comparable = enumerable.ToComparable();
887+
888+
// then
889+
Check.That(comparable).IsEqualTo(Lists.AsList("A", "B", "C"));
890+
Check.That(comparable.GetHashCode()).IsEqualTo(enumerable.ToComparable().GetHashCode());
891+
Check.That(comparable.ToString()).IsEqualTo("[A, B, C]");
892+
}
893+
894+
[Test]
895+
public void ShouldConvertCollectionToComparable()
896+
{
897+
// given
898+
ICollection<string> enumerable = new[] { "A", "B", "C" };
899+
900+
// when
901+
var comparable = enumerable.ToComparable();
902+
903+
// then
904+
Check.That(comparable).IsEqualTo(Lists.AsList("A", "B", "C"));
905+
Check.That(comparable.GetHashCode()).IsEqualTo(enumerable.ToComparable().GetHashCode());
906+
Check.That(comparable.ToString()).IsEqualTo("[A, B, C]");
907+
}
908+
909+
[Test]
910+
public void ShouldConvertSetToComparable()
911+
{
912+
// given
913+
ISet<string> enumerable = new HashSet<string>(new [] {"A", "B", "C"});
914+
915+
// when
916+
var comparable = enumerable.ToComparable();
917+
918+
// then
919+
Check.That(comparable).IsEqualTo(Lists.AsList("C", "B", "A"));
920+
}
730921
}
731922
}

0 commit comments

Comments
 (0)