Skip to content

Commit

Permalink
Version 1.2: Added Enumerable.GroupAdjacent(), Enumerable.OnBeforeFir…
Browse files Browse the repository at this point in the history
…st(), Enumerable.OnBeforeEach(), Enumerable.OnAfterEach() and Enumerable.OnAfterLast().
  • Loading branch information
ashmind committed Apr 12, 2014
1 parent 37b2566 commit 60bbc02
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 22 deletions.
59 changes: 59 additions & 0 deletions AshMind.Extensions.Tests/EnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Xunit;
using Xunit.Extensions;

// ReSharper disable PossibleNullReferenceException

namespace AshMind.Extensions.Tests {
public class EnumerableExtensionsTests {
[Fact]
Expand Down Expand Up @@ -112,6 +114,63 @@ public void HavingMin_ReturnsAllMinValues() {
);
}

[Theory]
[InlineData("A", "A:0")]
[InlineData("A,A,A", "A:0,1,2")]
[InlineData("A,A,A,B,C,C,A,A,B", "A:0,1,2; B:3; C:4,5; A:6,7; B:8")]
public void GroupAdjacentBy_GroupsByKeyCorrectly(string itemsString, string expected) {
var items = itemsString.Split(',').Select((key, index) => new { key, index });
var grouped = items.GroupAdjacentBy(x => x.key, x => x.index);
var groupedString = string.Join("; ", grouped.Select(g => g.Key + ":" + string.Join(",", g)));

Assert.Equal(expected, groupedString);
}

[Fact]
public void GroupAdjacentBy_ReturnsEmptySequence_WhenPassedEmpty() {
var grouped = (new string[0]).GroupAdjacentBy(x => x);
Assert.Empty(grouped);
}

[Fact]
public void GroupAdjacentBy_DoesNotCallKeySelectorImmediately() {
var items = new[] { new { key = 1 } };
var selectorCalled = false;

items.GroupAdjacentBy(x => {
selectorCalled = true;
return x.key;
});

Assert.False(selectorCalled);
}

[Fact]
public void GroupAdjacentBy_DoesNotCallElementSelectorImmediately() {
var items = new[] { new { key = 1 } };
var selectorCalled = false;

items.GroupAdjacentBy(x => x.key, x => {
selectorCalled = true;
return x;
});

Assert.False(selectorCalled);
}

[Fact]
public void GroupAdjacentBy_DoesNotCallResultSelectorImmediately() {
var items = new[] { new { key = 1 } };
var selectorCalled = false;

items.GroupAdjacentBy(x => x.key, x => x, (_, xs) => {
selectorCalled = true;
return xs;
});

Assert.False(selectorCalled);
}

//[StaticTestFactory]
//public static IEnumerable<Test> GetCommonTests() {
// var methods = typeof(EnumerableExtensions).GetMethods(BindingFlags.Static | BindingFlags.Public);
Expand Down
1 change: 1 addition & 0 deletions AshMind.Extensions/AshMind.Extensions.Net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</Compile>
<Compile Include="FormattableExtensions.cs" />
<Compile Include="Int32Extensions.cs" />
<Compile Include="Internal\Grouping.cs" />
<Compile Include="ListExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionExtensions.cs" />
Expand Down
1 change: 1 addition & 0 deletions AshMind.Extensions/AshMind.Extensions.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
</Compile>
<Compile Include="FormattableExtensions.cs" />
<Compile Include="Int32Extensions.cs" />
<Compile Include="Internal\Grouping.cs" />
<Compile Include="ListExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionExtensions.cs" />
Expand Down
1 change: 1 addition & 0 deletions AshMind.Extensions/AshMind.Extensions.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
</Compile>
<Compile Include="FormattableExtensions.cs" />
<Compile Include="Int32Extensions.cs" />
<Compile Include="Internal\Grouping.cs" />
<Compile Include="ListExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionExtensions.cs" />
Expand Down
6 changes: 3 additions & 3 deletions AshMind.Extensions/AshMind.Extensions.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>AshMind.Extensions</id>
<version>1.1.5</version> <!-- Note: Please keep in sync with AssemblyInfo.cs version -->
<version>1.2</version> <!-- Note: Please keep in sync with AssemblyInfo.cs version -->
<authors>Andrey Shchekin</authors>
<owners>Andrey Shchekin</owners>
<projectUrl>https://github.com/ashmind/ashmind-extensions</projectUrl>
Expand All @@ -11,8 +11,8 @@
Extension methods that could have been written by BCL authors
(with one or two Ruby-like things thrown in the mix).
</description>
<releaseNotes>Added Dictionary.GetOrAdd(), Enumerable.EmptyIfNull(), Type.HasInterface(Type) and String.NullIfEmpty().</releaseNotes>
<copyright>Copyright (c) 2013 Andrey Shchekin</copyright>
<releaseNotes>Added Enumerable.GroupAdjacent(), Enumerable.OnBeforeFirst(), Enumerable.OnBeforeEach(), Enumerable.OnAfterEach() and Enumerable.OnAfterLast().</releaseNotes>
<copyright>Copyright (c) 2013–2014 Andrey Shchekin</copyright>
<tags>extensions</tags>

<references>
Expand Down
Loading

0 comments on commit 60bbc02

Please sign in to comment.