Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Examine.Core/ValueSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -147,12 +148,27 @@ public IEnumerable<object> GetValues(string key)

/// <summary>
/// Helper method to return IEnumerable from a single
/// If object passed in is also enumerable
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
private static IEnumerable<object> Yield(object i)
{
yield return i;
if (i is string)
{
yield return i;
}
else if (i is IEnumerable enumerable)
{
foreach (var element in enumerable)
{
yield return element;
}
}
else
{
yield return i;
}
}

/// <summary>
Expand Down
129 changes: 129 additions & 0 deletions src/Examine.Test/Examine.Core/ValueSetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using NUnit.Framework;

namespace Examine.Test.Examine.Core
{
[TestFixture]
public class ValueSetTests
{

[Test]
public void Given_SingleValues_When_Yielding_ThenConvertsToEnumerableOfSingleValues()
{
/* Includes implicit test that String (which is technically IEnumerable<char>)
* gets passed through as a single object - NOT as multiple chars
*/

IDictionary<string, object> input = new Dictionary<string, object>()
{
{"boolean", false},
{"int", 1},
{"float", 1.2f},
{"double", 1.2d},
{"long", 45678923456345L},
{"datetime", DateTime.Today},
{"string", "A test sentence"}
};

var sut = new ValueSet("id", "category", "", input);

foreach (var key in input.Keys)
{
object[] value = sut.Values[key].ToArray();
Assert.IsTrue(value.Length == 1);
Assert.AreEqual(value[0], input[key]);
}
}

[Test]
public void Given_Enumerable_When_Yielding_ThenConvertsToEnumerable()
{

IDictionary<string, object> input = new Dictionary<string, object>()
{
{"booleanArray", new bool[] { false, true, true }},
{"intArray", new int[] {1, 2, 3}},
{"floatArray", new float[] {1.0f, 1.1f, 1.2f}},
{"doubleArray", new double[] { 123456789.123456789d, 7654321.7654321d}},
{"longArray", new long[] { 123456789012345678L, 876543210987654321L}},
{"datetimeArray", new DateTime[] { DateTime.Today, DateTime.MinValue }},
{"objectArray", new object[] { false, 123, 1.2f, 123456789.123456789d, 123456789012345678L, DateTime.Today, "a test string" }},
{"stringArray", new string[] { "first, second, third"}},
{ "objectEnumerable", (IEnumerable<object>) (new object[] { "a", 123, DateTime.Today}).ToList()},
{ "objectList", new List<object> { "a", 123, DateTime.MinValue}},
{ "dictionary", new Dictionary<string, object>(){ { "a", 1}, {"b", 2}}}
};

var sut = new ValueSet("id", "category", "", input);


foreach (var key in input.Keys)
{
object[] expected = null;
// ArrayEnumerator does not inherit IEnumerable, so we have to
// test both options.
if (input[key] is IEnumerable enumerable)
expected = enumerable.Cast<object>().ToArray();
else if (input[key] is Array array)
expected = array.Cast<object>().ToArray();

object[] output = sut.Values[key].ToArray();

CollectionAssert.AreEqual(expected, output);
}
}


[Test]
public void Given_SingleAndEnumerableValues_When_Yeilding_ThenConvertsToEnumerable()
{

IDictionary<string, object> input = new Dictionary<string, object>()
{
{"boolean", false},
{"int", 1},
{"float", 1.2f},
{"double", 1.2d},
{"long", 45678923456345L},
{"datetime", DateTime.Today},
{"string", "A test sentence"},
{"intArray", new int[] {1, 2, 3}},
{"objectArray", new object[] { false, 123, 1.2f, 123456789.123456789d, 123456789012345678L, DateTime.Today, "a test string" }},
{"stringArray", new string[] { "first, second, third"}},
{ "objectList", new List<object> { "a", 123, DateTime.MinValue}},
};

var sut = new ValueSet("id", "category", "", input);


foreach (var key in input.Keys)
{
object[] expected = null;
// ArrayEnumerator does not inherit IEnumerable, so we have to
// test both options.
if (input[key] is string s)
expected = new object[] { s };
else if (input[key] is IEnumerable enumerable)
expected = enumerable.Cast<object>().ToArray();
else if (input[key] is Array array)
expected = array.Cast<object>().ToArray();
else
expected = new object[] { input[key] };

object[] output = sut.Values[key].ToArray();

CollectionAssert.AreEqual(expected, output);

Assert.IsNotNull(sut.Values[key] as IEnumerable<object>);
}
}


}
}