mercredi 10 juin 2020

Why doesn't the Assert.Throws get into the method?

I have made a GroupBy method for Linq, and when trying to test the arguments the Assert.Throws won't even go into the method to check, and will say that it doesn't throw anything. Here is the code of what i wrote. The method:

if (source == null)
        {
            throw new ArgumentNullException("Source is null");
        }

        if (keySelector == null || elementSelector == null || resultSelector == null)
        {
            throw new ArgumentNullException("Selector is null");
        }

        Dictionary<TKey, List<TElement>> dictionary = new Dictionary<TKey, List<TElement>>(comparer);
        foreach (var obj in source)
        {
            if (dictionary.ContainsKey(keySelector(obj)))
            {
                dictionary[keySelector(obj)].Add(elementSelector(obj));
                continue;
            }

            dictionary.Add(keySelector(obj), new List<TElement> { elementSelector(obj) });
        }

        foreach (var obj in dictionary)
        {
            yield return resultSelector(obj.Key, obj.Value);
        }

and the test:

List<string> names = null;
        Func<string, char> firstLetterSelector = x => x[0];
        Func<string, string> valueSelector = x => x;
        Func<char, IEnumerable<string>, Grouping<char, string>> resultSelector = (x, y) => new Grouping<char, string>(x, y);
        CharEqualityComparer comparer = new CharEqualityComparer();
        const int z = 0;
        Assert.Throws<ArgumentNullException>(() => z.Equals(GroupBy(
            names, firstLetterSelector, valueSelector, resultSelector, comparer)));

Do you guys have any idea what might be wrong with it?

Aucun commentaire:

Enregistrer un commentaire