예제 #1
0
        public static async Task MultipleValues_Nested_HappyPath()
        {
            async Eff <(bool, int, string)> Test()
            {
                async Eff <(bool, int)> Nested()
                {
                    var x = await NonDetEffect.Choose(false, true);

                    var y = await NonDetEffect.Choose(1, 2, 3);

                    return(!x, y + 1);
                }

                var(x, y) = await Nested();

                var z = await NonDetEffect.Choose("foo", "bar");

                return(x, y, z);
            }

            var expected =
                from x in new[] { false, true }
            from y in new[] { 1, 2, 3 }
            from z in new[] { "foo", "bar" }
            select(!x, y + 1, z);

            var results = await NonDetEffectHandler.Run(Test());

            Assert.Equal(expected, results);
        }
예제 #2
0
        public static async Task NestedExceptionHandler_ShouldExecuteAsExpected()
        {
            async Eff <int> Test()
            {
                async Eff <int> Divide(int y)
                {
                    try
                    {
                        var x = await NonDetEffect.Choose(1, 2, 3);

                        return(x / y);
                    }
                    catch (DivideByZeroException)
                    {
                        return(-1);
                    }
                }

                var y = await NonDetEffect.Choose(1, 2, 0);

                return(await Divide(y));
            }

            var expected =
                from y in new[] { 1, 2, 0 }
            from x in new[] { 1, 2, 3 }
            select(y == 0) ? -1 : x / y;

            var results = await NonDetEffectHandler.Run(Test());

            Assert.Equal(expected, results);
        }
예제 #3
0
        public static async Task SimpleEffect_HappyPath()
        {
            async Eff <int> Test() => await NonDetEffect.Choose(1, 2, 3, 4);

            var expected = new int[] { 1, 2, 3, 4 };
            var results  = await NonDetEffectHandler.Run(Test());

            Assert.Equal(expected, results);
        }
예제 #4
0
        public static async Task NestedException_ShouldPropagate()
        {
            async Eff <int> Test()
            {
                async Eff <int> Divide(int y)
                {
                    var x = await NonDetEffect.Choose(1, 2, 3);

                    return(x / y);
                }

                var y = await NonDetEffect.Choose(1, 2, 0);

                return(await Divide(y));
            }

            await Assert.ThrowsAsync <DivideByZeroException>(() => NonDetEffectHandler.Run(Test()));
        }