示例#1
0
        public void Either_TypeChecking()
        {
            var helloWorld = "Hello World!";
            Either <int, string> either = helloWorld;

            either.Is <int>().Should().BeFalse();
            either.Is <string>().Should().BeTrue();

            // The methods Is<T> & As<T> do not look at subtyping
            // they merely check against the type args used in Either<T1, T2>
            Action checkInvalidCase = () => either.Is <object>();

            checkInvalidCase.Should().Throw <Exception>();

            Action castToWrongCase = () => either.CastTo <int>();

            castToWrongCase.Should().Throw <Exception>();
            either.As <string>().Should().Be(helloWorld);

            Action invalidCast = () => either.As <object>();

            invalidCast.Should().Throw <Exception>();
        }