public void TestEnumerable() { Option <int> none = Option.None; Assert.AreEqual(0, none.Count()); Assert.AreEqual(0, none.Aggregate(0, (sum, x) => sum + x)); var some = Option.Some <int>(3); Assert.AreEqual(1, some.Count()); Assert.AreEqual(3, some.Aggregate(0, (sum, x) => sum + x)); }
public void OptionAggregate() { // Option with value var optionInt = Option <int> .Some(12); Assert.AreEqual(15, optionInt.Aggregate(3, (intValue, initValue) => intValue + initValue)); // Empty option Option <int> emptyOptionInt = Option.None; Assert.AreEqual(3, emptyOptionInt.Aggregate(3, (intValue, initValue) => intValue + initValue)); // ReSharper disable ReturnValueOfPureMethodIsNotUsed Assert.Throws <ArgumentNullException>(() => optionInt.Aggregate(1, null)); Assert.Throws <ArgumentNullException>(() => optionInt.Aggregate((Person)null, (person, int32) => null)); Assert.Throws <ArgumentNullException>(() => optionInt.Aggregate((Person)null, null)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
private static void TestIEnumerableHelper(Option<int> o) { var select = o.Select(x => x); Assert.AreEqual(o.HasValue ? 1 : 0, select.Count()); select = o.Select((x, index) => x); Assert.AreEqual(o.HasValue ? 1 : 0, select.Count()); var where = o.Where(x => true); Assert.AreEqual(o.HasValue ? 1 : 0, where.Count()); where = o.Where((x, index) => true); Assert.AreEqual(o.HasValue ? 1 : 0, where.Count()); var toArr = o.ToArray(); Assert.AreEqual(o.HasValue ? 1 : 0, toArr.Length); if (o.HasValue) { var aggregate = o.Aggregate((acc, val) => acc + val); Assert.AreEqual(o.ValueOrDefault, aggregate); aggregate = o.Aggregate(0, (acc, val) => acc + val); Assert.AreEqual(o.ValueOrDefault, aggregate); } bool run = false; foreach (var obj in ((System.Collections.IEnumerable)o)) { run = true; Assert.AreEqual(1, (int)obj); } Assert.AreEqual(run, o.HasValue); run = false; o.ForEach((val) => run = true); Assert.AreEqual(run, o.HasValue); }