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

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

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

            Assert.Equal(2, result);
        }
        public async Task WithReferences()
        {
            Task MatchSome(int some) => Task.CompletedTask;
            Task MatchNone() => Task.CompletedTask;

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

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

            Assert.Equal(Unit(), result);
        }
Esempio n. 3
0
        public async Task OnSome_WithLambdaReturningAsyncNone_ReturnsSelf()
        {
            const int expected = 34;

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

            var result = source.MustBeSome();

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

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

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

            Assert.Equal(2, result);
        }
Esempio n. 5
0
        public async Task WhenFirstIsSome_AndSecondIsNone_UsingLambda_ReturnsNone()
        {
            const int firstValue = 23;
            var       first      = AsyncMaybe <int> .Some(firstValue);

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

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

            result.MustBeNone();
        }
        public async Task WithReferences()
        {
            Task <int> MatchSome(int some) => Task.FromResult(some + 1);
            Task <int> MatchNone() => Task.FromResult(0);

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

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

            Assert.Equal(2, result);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        public async Task Some_Map_Action_T_MethodReference_ReturnsSomeUnit()
        {
            Task Foo(int it) => Task.CompletedTask;

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

            var result = await source.MapAsync(Foo);

            var unit = result.MustBeSome();

            Assert.IsType <Unit>(unit);
        }
Esempio n. 9
0
        public async Task OnNone_WithMethodReferenceReturningAsyncSome_ReturnsOther()
        {
            const int expected = 34;

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

            var source = await AsyncMaybe <int> .None().Or(Other);

            var result = source.MustBeSome();

            Assert.Equal(expected, result);
        }
Esempio n. 10
0
        public async Task OnSome_WithMethodReferenceReturningAsyncNone_ReturnsSelf()
        {
            const int expected = 34;

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

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

            var result = source.MustBeSome();

            Assert.Equal(expected, result);
        }
Esempio n. 11
0
        public async Task WithSomeLambda_AndNoneReference()
        {
            void MatchNone()
            {
            }

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

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

            Assert.Equal(Unit(), result);
        }
Esempio n. 12
0
        public async Task WithSomeReference_AndNoneLambda()
        {
            void MatchSome(int some)
            {
            }

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

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

            Assert.Equal(Unit(), result);
        }
Esempio n. 13
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();
        }
Esempio n. 14
0
        public async Task WithNullZipper_ThrowsException()
        {
            var first = AsyncMaybe <int> .None();

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

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

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

            // ReSharper disable once AssignNullToNotNullAttribute
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await first.Zip <int, int, int, int, object>(second, third, fourth, null));
        }
Esempio n. 15
0
        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);
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        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);
        }
Esempio n. 18
0
        public async Task Some_Map_ActionMethodReference_ReturnsSomeUnit()
        {
            void Foo(int it)
            {
            }

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

            var result = await source.Map(Foo);

            var unit = result.MustBeSome();

            Assert.IsType <Unit>(unit);
        }
Esempio n. 19
0
        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);
        }
Esempio n. 20
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);
        }
Esempio n. 21
0
        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);
        }
Esempio n. 22
0
        public async Task WithReferences()
        {
            void MatchSome(int some)
            {
            }

            void MatchNone()
            {
            }

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

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

            Assert.Equal(Unit(), result);
        }
Esempio n. 23
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);
        }
Esempio n. 24
0
        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);
        }
Esempio n. 25
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);
        }
Esempio n. 26
0
        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);
        }
Esempio n. 27
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);
        }
Esempio n. 28
0
        public async Task Sync_ReturningSome_WhenNone_ReturnsNone()
        {
            var isNone = await AsyncMaybe <int> .None().FlatMap(some => Some(some + 3)).IsNone;

            Assert.True(isNone);
        }
Esempio n. 29
0
        public async Task Async_ReturningAsyncSome_WhenNone_ReturnsNone()
        {
            var isNone = await AsyncMaybe <int> .None().FlatMapAsync(some => Task.FromResult(Some(some + 3).ToAsync())).IsNone;

            Assert.True(isNone);
        }
Esempio n. 30
0
        public async Task Sync_ReturningSome_UsingNoArgs_WhenNone_ReturnsNone()
        {
            var source = await AsyncMaybe <Unit> .None().FlatMapUnit(() => Maybe <int> .Some(3));

            source.MustBeNone();
        }