public async Task WithReferences()
        {
            int MatchSome(int some) => some + 1;
            int MatchNone(None none) => 0;

            var source = AsyncMaybe <int> .Some(1);

            var result = await source.Match(some : MatchSome, none : MatchNone);

            Assert.Equal(2, result);
        }
Beispiel #2
0
        public async Task OnSome_WithLambdaReturningAsyncNone_ReturnsSelf()
        {
            const int expected = 34;

            // ReSharper disable once ConvertClosureToMethodGroup
            var source = await AsyncMaybe <int> .Some(expected).OrAsync(() => Task.FromResult(AsyncMaybe <int> .None()));

            var result = source.MustBeSome();

            Assert.Equal(expected, result);
        }
        public async Task NoneValue_WithLambdas()
        {
            var some   = 0;
            var none   = 0;
            var source = AsyncMaybe <int> .None();

            await source.Match(_ => { some++; return(TaskCache.CompletedTask); }, _ => { none++; return(TaskCache.CompletedTask); });

            Assert.Equal(0, some);
            Assert.Equal(1, none);
        }
Beispiel #4
0
        public async Task WhenFirstIsNone_AndSecondIsSome_UsingLambda_ReturnsNone()
        {
            var first = AsyncMaybe <int> .None();

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            var result = await first.Zip(second, (f, s) => (first : f, second : s));

            result.MustBeNone();
        }
        public async Task WhenSome_ReturnsSingleItemEnumerable()
        {
            const int expectedValue = 230023;
            var       source        = AsyncMaybe <int> .Some(expectedValue);

            var result = await source.ToEnumerable();

            var value = Assert.Single(result);

            Assert.Equal(expectedValue, value);
        }
Beispiel #6
0
        public async Task WithNullZipper_ThrowsException()
        {
            var first = AsyncMaybe <int> .None();

            var second = AsyncMaybe <int> .None();

            var third = AsyncMaybe <int> .None();

            // ReSharper disable once AssignNullToNotNullAttribute
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await first.Zip <int, int, int, object>(second, third, null));
        }
        public async Task WithReferences()
        {
            int MatchSome(int some) => some + 1;
            Task <int> MatchNone() => Task.FromResult(0);

            var source = AsyncMaybe <int> .Some(1);

            var result = await source.Match(some : MatchSome, noneAsync : MatchNone);

            Assert.Equal(2, result);
        }
Beispiel #8
0
        public async Task WithLambdas()
        {
            var some   = 0;
            var none   = 0;
            var source = AsyncMaybe <int> .Some(1);

            await source.Match(_ => { some++; }, _ => { none++; });

            Assert.Equal(1, some);
            Assert.Equal(0, none);
        }
        public async Task WithReferences()
        {
            Task <int> MatchSome(int some) => Task.FromResult(some + 1);
            Task <int> MatchNone(None none) => Task.FromResult(0);

            var source = AsyncMaybe <int> .Some(1);

            var result = await source.MatchAsync(MatchSome, MatchNone);

            Assert.Equal(2, result);
        }
Beispiel #10
0
        public async Task OnNone_WithMethodReferenceReturningAsyncSome_ReturnsOther()
        {
            const int expected = 34;

            Task <AsyncMaybe <int> > Other() => Task.FromResult(AsyncMaybe <int> .Some(expected));

            var source = await AsyncMaybe <int> .None().OrAsync(otherAsync: Other);

            var result = source.MustBeSome();

            Assert.Equal(expected, result);
        }
Beispiel #11
0
        public async Task WhenFirstIsSome_AndSecondIsNone_AndThirdIsSome_ReturnsNone()
        {
            var first = AsyncMaybe <int> .Some(451);

            var second = AsyncMaybe <int> .None();

            var third = AsyncMaybe <int> .Some(123);

            var result = await first.Zip(second, third, (f, s, t) => (f, s, t));

            result.MustBeNone();
        }
        public async Task OnNone_WithLambda_ReturnsOther()
        {
            const int expected = 79;
            var       source   = Maybe <int> .None();

            var other = AsyncMaybe <int> .Some(expected);

            var result = await source.OrAsync(() => other);

            var value = result.MustBeSome();

            Assert.Equal(expected, value);
        }
        public async Task OnSome_ReturnsSource()
        {
            const int expected = 5;
            var       source   = Maybe <int> .Some(expected);

            var other = AsyncMaybe <int> .Some(79);

            var result = await source.OrAsync(other);

            var value = result.MustBeSome();

            Assert.Equal(expected, value);
        }
        public async Task WithSomeLambda_AndNoneReference()
        {
            var some = 0;
            var none = 0;

            void MatchNone() => none++;

            var source = AsyncMaybe <int> .Some(1);

            await source.Match(value => { some++; }, none : MatchNone);

            Assert.Equal(1, some);
            Assert.Equal(0, none);
        }
        public async Task FromMaybeSome()
        {
            var expectedValue = new object();

            Maybe <object> source = Maybe <object> .Some(expectedValue);

            AsyncMaybe <object> implicitAsync = source;

            var isSome = await implicitAsync.IsSome;
            var value  = await implicitAsync.OrFail();

            Assert.True(isSome);
            Assert.Same(expectedValue, value);
        }
        public async Task WithSomeReference_AndNoneLambda()
        {
            var some = 0;
            var none = 0;

            void MatchSome(int value) => some++;

            var source = AsyncMaybe <int> .Some(1);

            await source.Match(some : MatchSome, none : () => { none++; });

            Assert.Equal(1, some);
            Assert.Equal(0, none);
        }
        public async Task OnNone_WithMethodReference_ReturnsOther()
        {
            const int expected = 79;

            AsyncMaybe <int> Other() => AsyncMaybe <int> .Some(expected);

            var source = Maybe <int> .None();

            var result = await source.OrAsync(Other);

            var value = result.MustBeSome();

            Assert.Equal(expected, value);
        }
        public async Task WhenAllAreSome_ReturnsSome()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            var result = await first.ZipAsync(second, (f, s) => Task.FromResult((first: f, second: s)));

            var value = result.MustBeSome();

            Assert.Equal(firstValue, value.first);
            Assert.Equal(secondValue, value.second);
        }
Beispiel #19
0
        public async Task WhenFirstIsSome_AndSecondIsSome_UsingLambda_ReturnsSome()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            var result = await first.Zip(second, (f, s) => (first : f, second : s));

            var value = result.MustBeSome();

            Assert.Equal(firstValue, value.first);
            Assert.Equal(secondValue, value.second);
        }
        public async Task WithReferences()
        {
            var some = 0;
            var none = 0;

            void MatchSome(int value) => some++;
            void MatchNone() => none++;

            var source = AsyncMaybe <int> .Some(1);

            await source.Match(some : MatchSome, none : MatchNone);

            Assert.Equal(1, some);
            Assert.Equal(0, none);
        }
Beispiel #21
0
        public async Task WhenFirstIsSome_AndSecondIsSome_UsingMethodReference_ReturnsSome()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            ValueTuple <int, int> Zipper(int f, int s) => (f, s);

            var result = await first.Zip(second, Zipper);

            var value = result.MustBeSome();

            Assert.Equal(firstValue, value.Item1);
            Assert.Equal(secondValue, value.Item2);
        }
        public async Task WhenAllAreSome_UsingMethodReference_ReturnsSome()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            Task <ValueTuple <int, int> > Zipper(int f, int s) => Task.FromResult((f, s));

            var result = await first.ZipAsync(second, Zipper);

            var value = result.MustBeSome();

            Assert.Equal(firstValue, value.Item1);
            Assert.Equal(secondValue, value.Item2);
        }
        public async Task WithSomeReference_AndNoneLambda()
        {
            var some = 0;
            var none = 0;

            Task MatchSome(int _)
            {
                some++; return(TaskCache.CompletedTask);
            }

            var source = AsyncMaybe <int> .Some(1);

            await source.Match(someAsync : MatchSome, noneAsync : _ => { none++; return(TaskCache.CompletedTask); });

            Assert.Equal(1, some);
            Assert.Equal(0, none);
        }
Beispiel #24
0
        public async Task WhenAnyIsNone_ReturnsNone()
        {
            AsyncMaybe <int> CreateMaybe(int flag) => flag == 0 ? AsyncMaybe <int> .None() : AsyncMaybe <int> .Some(flag);

            const int numberOfSources         = 4;
            const int numberOfStates          = 2;
            const int expectedSomeInvocations = 1;
            var       someInvocations         = 0;

            var expectedNoneInvocations = (int)Math.Pow(numberOfStates, numberOfSources) - expectedSomeInvocations;
            var noneInvocations         = 0;

            foreach (var a in Enumerable.Range(0, 2))
            {
                foreach (var b in Enumerable.Range(0, 2))
                {
                    foreach (var c in Enumerable.Range(0, 2))
                    {
                        foreach (var d in Enumerable.Range(0, 2))
                        {
                            var first  = CreateMaybe(a);
                            var second = CreateMaybe(b);
                            var third  = CreateMaybe(c);
                            var fourth = CreateMaybe(d);

                            if (await first.IsSome && await second.IsSome && await third.IsSome && await fourth.IsSome)
                            {
                                someInvocations++;
                                continue;
                            }

                            var result = await first.ZipAsync(second, third, fourth, (v1, v2, v3, v4) => Task.FromResult((v1, v2, v3, v4)));

                            result.MustBeNone();

                            noneInvocations++;
                        }
                    }
                }
            }

            Assert.Equal(expectedNoneInvocations, noneInvocations);
            Assert.Equal(expectedSomeInvocations, someInvocations);
        }
        public async Task WhenAllAreSome_ReturnsSome()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            const int thirdValue = 236589;
            var       third      = AsyncMaybe <int> .Some(thirdValue);

            var result = await first.ZipAsync(second, third, (v1, v2, v3) => Task.FromResult((v1, v2, v3)));

            var value = result.MustBeSome();

            Assert.Equal(firstValue, value.Item1);
            Assert.Equal(secondValue, value.Item2);
            Assert.Equal(thirdValue, value.Item3);
        }
Beispiel #26
0
        public async Task WhenFirstIsSome_AndSecondIsSome_AndThirdIsSome_ReturnsSome()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

            const int secondValue = 6589;
            var       second      = AsyncMaybe <int> .Some(secondValue);

            const int thirdValue = 2136589;
            var       third      = AsyncMaybe <int> .Some(thirdValue);

            var result = await first.Zip(second, third, (f, s, t) => (first : f, second : s, third : t));

            var value = result.MustBeSome();

            Assert.Equal(firstValue, value.first);
            Assert.Equal(secondValue, value.second);
            Assert.Equal(thirdValue, value.third);
        }
Beispiel #27
0
        public async Task WithAsyncNoneNone_ReturnsNone()
        {
            var source = await AsyncMaybe <int> .None().Map(it => AsyncMaybe <int> .None()).Flatten();

            source.MustBeNone();
        }
Beispiel #28
0
        public async Task WithNoneSome_ReturnsNone()
        {
            var source = await AsyncMaybe <int> .None().Map(it => Maybe <int> .Some(1)).Flatten();

            source.MustBeNone();
        }
 public static async Task <T> OrFail <T>(this AsyncMaybe <T> source)
 {
     return((await source.ToTask()).OrFail());
 }
Beispiel #30
0
        public async Task Async_OnNone_WithFunction_ThrowsException()
        {
            var source = AsyncMaybe <int> .None();

            await Assert.ThrowsAsync <ExpectedException>(() => source.OrThrowAsync(() => Task.FromResult <Exception>(new ExpectedException())));
        }