Created
July 18, 2018 22:46
-
-
Save danielfoxp2/0b01977507e97e4eb242f9b26c29717e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
namespace Flatten | |
{ | |
public class CollectionValidator<T> | |
{ | |
public static bool IsNotValidThis(T integerCollection) | |
{ | |
return IsNotAnIEnumerable(integerCollection) || IsAString(integerCollection); | |
} | |
private static bool IsNotAnIEnumerable(T integerCollection) | |
{ | |
return integerCollection == null || integerCollection is IEnumerable == false; | |
} | |
private static bool IsAString(T integerCollection) => integerCollection is string; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Flatten; | |
using System.Collections.Generic; | |
using NUnit.Framework; | |
namespace FlattenBehavior | |
{ | |
[TestFixture] | |
public class Describe_flatten | |
{ | |
public class When_receive_anything_but_collection | |
{ | |
[Test] | |
public void should_throw_an_argument_exception_if_param_is_string() | |
{ | |
string parameter = "Should not be accepted"; | |
Assert.That(() => new FlatIntegerCollection<string>().flatten(parameter), Throws.ArgumentException); | |
} | |
[Test] | |
public void should_throw_an_argument_exception_if_param_is_int() | |
{ | |
int parameter = 1; | |
Assert.That(() => new FlatIntegerCollection<int>().flatten(parameter), Throws.ArgumentException); | |
} | |
} | |
public class When_receive_an_empty_collection | |
{ | |
[Test] | |
public void should_throw_an_argument_exception() | |
{ | |
var parameter = new int[] { }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<int>>().flatten(parameter), Has.Count.EqualTo(0)); | |
} | |
} | |
public class When_receive_an_unnested_collection | |
{ | |
public class With_only_one_element | |
{ | |
[Test] | |
public void should_return_the_same_unnested_collection() | |
{ | |
var listToBeFlattened = new int[] { 1 }; | |
var expectedResult = new int[] { 1 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<int>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
} | |
public class With_many_elements | |
{ | |
[Test] | |
public void should_return_the_same_unnested_collection() | |
{ | |
var listToBeFlattened = new int[] { 1, 2, 3, 4, 5 }; | |
var expectedResult = new int[] { 1, 2, 3, 4, 5 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<int>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
} | |
} | |
public class When_receive_a_one_level_nested_collection | |
{ | |
public class With_only_one_element | |
{ | |
[Test] | |
public void should_return_a_flattened_collection() | |
{ | |
var listToBeFlattened = new int[][] { new int[] { 1 } }; | |
var expectedResult = new int[] { 1 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<IEnumerable<int>>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
} | |
public class With_many_elements | |
{ | |
[Test] | |
public void should_return_a_flattened_collection() | |
{ | |
var listToBeFlattened = new int[][] { new int[] { 1, 2 }, new int[] { 3 } }; | |
var expectedResult = new int[] { 1, 2, 3 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<IEnumerable<int>>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
} | |
} | |
public class When_receive_a_two_level_nested_collection | |
{ | |
public class With_only_one_element | |
{ | |
[Test] | |
public void should_return_a_flattened_collection() | |
{ | |
var listToBeFlattened = new int[][][] { new int[][] { new int[] { 1 } } }; | |
var expectedResult = new int[] { 1 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<IEnumerable<IEnumerable<int>>>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
} | |
public class With_many_elements | |
{ | |
[Test] | |
public void should_return_a_flattened_collection() | |
{ | |
var listToBeFlattened = new int[][][] { new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 } } }; | |
var expectedResult = new int[] { 1, 2, 3, 4 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<IEnumerable<IEnumerable<int>>>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
[Test] | |
public void should_execute_citrusbyte_similar_example() | |
{ | |
var listToBeFlattened = new int[][][] { new int[][] { new int[] { 1, 2 }, new int[] { 3 } } , new int[][] { new int[] { 4 } } }; | |
var expectedResult = new int[] { 1, 2, 3, 4 }; | |
Assert.That(new FlatIntegerCollection<IEnumerable<IEnumerable<IEnumerable<int>>>>().flatten(listToBeFlattened), Is.EquivalentTo(expectedResult)); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace Flatten | |
{ | |
public class FlatIntegerCollection<T> | |
{ | |
private List<int> flattenedCollection = new List<int>(); | |
public IEnumerable<int> flatten(T integerCollection) | |
{ | |
if (CollectionValidator<T>.IsNotValidThis(integerCollection)) throw new ArgumentException(); | |
flat(integerCollection); | |
return flattenedCollection; | |
} | |
private void flat(T integerCollection) | |
{ | |
flattenCollection((IEnumerable)integerCollection); | |
} | |
private void flattenCollection(IEnumerable collection) | |
{ | |
foreach (var element in collection) | |
{ | |
if (element is IEnumerable) flattenCollection((IEnumerable)element); | |
else flattenedCollection.Add((int)element); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment