コード例 #1
0
        public void ResolvedWithBoolErr_UnwrapValueThrowsException()
        {
            Resolved <int, bool> resolved = Resolved.Err(true);

            Assert.Throws(typeof(InvalidOperationException),
                          () => resolved.Unwrap());
        }
コード例 #2
0
        public void ResolvedWithDefaultErr_IsErrEqualsTrue()
        {
            IEnumerable <Exception> errors = new List <Exception>();
            Resolved resolved = Resolved.Err(errors);

            Assert.IsTrue(resolved.IsErr);
        }
コード例 #3
0
        public void ResolvedWithDefaultErr_IsOkEqualsFalse()
        {
            IEnumerable <Exception> err = new List <Exception>();
            Resolved resolved           = Resolved.Err(err);

            Assert.IsFalse(resolved.IsOk);
        }
コード例 #4
0
        public void DifferentTypeChainedResolvedErr_ResolvedValueIsString()
        {
            IEnumerable <Exception> errors = new Exception[0];
            Resolved resolved = Resolved.Err(errors);

            var r = resolved
                    .Match <Resolved <bool> >(
                Ok: () => Resolved.Ok(true),
                Err: (err) => Resolved.Err(err)
                )
                    .Match <Resolved <int, string> >(
                Ok: (b) => Resolved.Ok(1),
                Err: (err) => Resolved.Err("Erro")
                );

            Assert.IsTrue(r.IsErr);

            r.Match(
                Ok: (n) => { Assert.Fail("Unexpected error occurred"); },
                Err: (err) =>
            {
                Assert.AreEqual(typeof(string), err.GetType());
                Assert.AreEqual("Erro", err);
            }
                );
        }
コード例 #5
0
        public void ResolvedWithStringErr_ForwardErrThroughTheChain()
        {
            string errorMessage = "Forwarded Err";
            Resolved <string, string> resolved = Resolved.Err(errorMessage);

            var result = resolved
                         .Match <int>(
                Ok: (innerValue) => Resolved.Ok(1),
                Err: Behavior.Forward
                )
                         .Match <bool>(
                Ok: (innerValue) => Resolved.Ok(true),
                Err: Behavior.Forward
                )
                         .Match <char>(
                Ok: (innerValue) => Resolved.Ok('\a'),
                Err: Behavior.Forward
                )
                         .Match(
                Ok: (innerValue) => Resolved.Ok('\b'),
                Err: Behavior.Forward
                );

            Assert.AreEqual(typeof(Err <string>), result.Value.GetType());

            var err = (Err <string>)result.Value;

            Assert.AreEqual(err.Value, errorMessage);
        }
コード例 #6
0
        public void DifferentTypeChainedResolvedOk_ResolvedValueIsTrue()
        {
            Resolved resolved = Resolved.Ok();

            var r = resolved
                    .Match <Resolved <int> >(
                Ok: () => Resolved.Ok(1),
                Err: (err) => Resolved.Err(err)
                )
                    .Match <Resolved>(
                Ok: (n) => Resolved.Ok(),
                Err: (err) => Resolved.Err(err)
                )
                    .Match <Resolved <bool> >(
                Ok: () => Resolved.Ok(true),
                Err: (err) => Resolved.Err(err)
                );

            Assert.IsTrue(r.IsOk);

            r.Match(
                Ok: (value) => { Assert.IsTrue(value); },
                Err: (err) => { Assert.Fail("Unexpected error occurred"); }
                );
        }
コード例 #7
0
        public void SameTypeChainedResolvedOk_ReturnsLastNumberInTheChain()
        {
            Resolved <int> resolved = Resolved.Ok(0);

            var r = resolved
                    .Match <Resolved <int> >(
                Ok: (n) => Resolved.Ok(1),
                Err: (err) => Resolved.Err(err)
                )
                    .Match <Resolved <int> >(
                Ok: (n) => Resolved.Ok(2),
                Err: (err) => Resolved.Err(err)
                )
                    .Match <Resolved <int> >(
                Ok: (n) => Resolved.Ok(3),
                Err: (err) => Resolved.Err(err)
                );

            Assert.IsTrue(r.IsOk);

            r.Match(
                Ok: (n) => { Assert.AreEqual(3, n); },
                Err: (err) => { Assert.Fail("Unexpected error occurred"); }
                );
        }
コード例 #8
0
        public void NestedResolved_ReturnsResolvedOkWithIntValue()
        {
            Resolved <int>             innerResolved = Resolved.Ok(1);
            Resolved <Resolved <int> > resolved      = Resolved.Ok(innerResolved);

            var result = resolved.Match <Resolved <int> >(
                Ok: (r) =>
            {
                var number = r.Match(
                    Ok: (n) => 1,
                    Err: (err) => 0
                    );

                return(Resolved.Ok(number));
            },
                Err: (err) => Resolved.Err(err)
                );

            Assert.IsTrue(result.IsOk);

            result.Match(
                Ok: (n) => Assert.AreEqual(1, n),
                Err: (err) => Assert.Fail("Unexpected error occurred")
                );
        }
コード例 #9
0
        public void ResolvedErrWithNullValue_ArgumentNullException()
        {
            Resolved resolved = Resolved.Err(null);

            Assert.Throws(
                typeof(ArgumentNullException),
                () => resolved.Match(
                    Ok: () => true,
                    Err: (err) => true
                    ));
        }
コード例 #10
0
        public void ResolvedWithEmptyOk_MatchOkAndReturnsEmptyOk()
        {
            Resolved resolved = Resolved.Ok();

            var r = resolved.Match <Resolved>(
                Ok: () => Resolved.Ok(),
                Err: (err) => Resolved.Err(err)
                );

            Assert.IsTrue(r.IsOk);
            Assert.AreEqual(r.Value.GetType(), typeof(Ok));
        }
コード例 #11
0
        public void ResolvedWithEmptyOk_MatchOkAndReturnsOkWithIntegerValue()
        {
            Resolved resolved = Resolved.Ok();

            var r = resolved.Match <Resolved <int> >(
                Ok: () => Resolved.Ok(10),
                Err: (err) => Resolved.Err(err)
                );

            Assert.IsTrue(r.IsOk);
            Assert.AreEqual(r.Value.GetType(), typeof(Ok <int>));
            Assert.AreEqual(10, ((Ok <int>)r.Value).Value);
        }
コード例 #12
0
        public void SameTypeChainedResolvedErr_ReturnsLastNumberInTheChain()
        {
            IEnumerable <Exception> errors = new[] { new Exception(), new Exception() };
            Resolved resolved = Resolved.Err(errors);

            var r = resolved
                    .Match <Resolved>(
                Ok: () => Resolved.Ok(),
                Err: (err) => Resolved.Err(new Exception[] { new Exception(), new Exception() } as IEnumerable <Exception>)
                )
                    .Match <Resolved>(
                Ok: () => Resolved.Ok(),
                Err: (err) => Resolved.Err(new Exception[] { new Exception() } as IEnumerable <Exception>)
                );

            Assert.IsTrue(r.IsErr);

            r.Match(
                Ok: () => { Assert.Fail("Unexpected error occurred"); },
                Err: (err) => { Assert.AreEqual(1, err.Count()); }
                );
        }
コード例 #13
0
        public void ResolvedWithIntegerErr_IsOkEqualsFalse()
        {
            Resolved <object, int> resolved = Resolved.Err(10);

            Assert.IsFalse(resolved.IsOk);
        }
コード例 #14
0
        public void ResolvedWithIntegerErr_IsErrEqualsTrue()
        {
            Resolved <object, int> resolved = Resolved.Err(10);

            Assert.IsTrue(resolved.IsErr);
        }