Unit test Utils.Enumerables
continuous-integration/drone/push Build is passing Details

pull/191/head
Alexis Drai 3 years ago
parent 2ef6b304dc
commit 5694baf4cd

@ -3,10 +3,73 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
using Xunit;
namespace Tests.Utils_UTs
{
public class EnumerablesTest
{
[Fact]
public void TestFeedListsToDict()
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
string str3 = "azfyoaz";
int int1 = 5;
int int2 = 12;
int int3 = 3;
Dictionary<string, int> expected = new()
{
{ str1, int1 },
{ str2, int2 },
{ str3, int3 }
};
List<string> strings = new() { str2, str3 };
List<int> ints = new() { int2, int3 };
Dictionary<string, int> actual = new()
{
{str1, int1 }
}; // we will add on top of this
// Act
actual = Enumerables.FeedListsToDict(actual, strings, ints);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetDictFromLists()
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
int int1 = 5;
int int2 = 12;
Dictionary<string, int> expected = new()
{
{ str1, int1 },
{ str2, int2 }
};
List<string> strings = new() { str1, str2 };
List<int> ints = new() { int1, int2 };
// Act
Dictionary<string, int> actual = Enumerables.GetDictFromLists(strings, ints);
// Assert
Assert.Equal(expected, actual);
}
}
}

@ -3,11 +3,18 @@
public static class Enumerables
{
private static Dictionary<K, V> GetDictFromLists<K, V>(List<K> keys, List<V> values) => keys.Zip(values, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v);
public static Dictionary<K, V> GetDictFromLists<K, V>(List<K> keys, List<V> values)
{
return keys.Zip(values, (k, v) => new { k, v })
.ToDictionary(x => x.k, x => x.v);
}
public static Dictionary<K, V> FeedListsToDict<K, V>(Dictionary<K, V> kvps, List<K> keys, List<V> values)
{
foreach (var kv in GetDictFromLists(keys, values)) { kvps.Add(kv.Key, kv.Value); }
foreach (var kv in GetDictFromLists(keys, values))
{
kvps.Add(kv.Key, kv.Value);
}
return kvps;
}
}

Loading…
Cancel
Save