public void CastValueCustomResultTInToValueCustomResultTOutWithAs()
        {
            var testObject        = new TestClass();
            var testObjectLeaf    = new TestClassLeaf();
            var customErrorObject = new CustomErrorTest {
                ErrorCode = -4
            };

            // Result ok
            var ok     = Result.Ok <TestClass, CustomErrorTest>(testObjectLeaf);
            var result = ok.Cast <TestClassLeaf>();

            CheckResultOk(result, testObjectLeaf);

            ok     = Result.Ok <TestClass, CustomErrorTest>(testObject);
            result = ok.Cast <TestClassLeaf>();
            CheckResultOk(result, null);

            // Result warn
            var warning = Result.Warn <TestClass, CustomErrorTest>(testObjectLeaf, "My warning");

            result = warning.Cast <TestClassLeaf>();
            CheckResultWarn(result, testObjectLeaf, "My warning");

            warning = Result.Warn <TestClass, CustomErrorTest>(testObject, "My warning");
            result  = warning.Cast <TestClassLeaf>();
            CheckResultWarn(result, null, "My warning");

            // Result fail
            var failure = Result.Fail <TestClass, CustomErrorTest>("My failure", customErrorObject);

            result = failure.Cast <TestClassLeaf>();
            CheckResultFail(result, "My failure", customErrorObject);
        }
        public void CastValueResultTInToValueResultTOutWithAs()
        {
            var testObject     = new TestClass();
            var testObjectLeaf = new TestClassLeaf();

            // Result ok
            var ok          = Result.Ok <TestClass>(testObjectLeaf);
            var valueResult = ok.Cast <TestClassLeaf>();

            CheckResultOk(valueResult, testObjectLeaf);

            ok          = Result.Ok(testObject);
            valueResult = ok.Cast <TestClassLeaf>();
            CheckResultOk(valueResult, null);

            // Result warn
            var warning = Result.Warn <TestClass>(testObjectLeaf, "My warning");

            valueResult = warning.Cast <TestClassLeaf>();
            CheckResultWarn(valueResult, testObjectLeaf, "My warning");

            warning     = Result.Warn(testObject, "My warning");
            valueResult = warning.Cast <TestClassLeaf>();
            CheckResultWarn(valueResult, null, "My warning");

            // Result fail
            var failure = Result.Fail <int>("My failure");

            valueResult = failure.Cast <TestClassLeaf>();
            CheckResultFail(valueResult, "My failure");
        }
Пример #3
0
        public void OptionOfType()
        {
            // Cast succeed
            var testObjectLeaf  = new TestClassLeaf();
            var optionTestClass = Option <TestClass> .Some(testObjectLeaf);

            var optionTestClassLeaf = optionTestClass.OfType <TestClassLeaf>();

            CheckOptionSameValue(optionTestClassLeaf, testObjectLeaf);

            // Cast failed
            // From Value type
            var optionInt = Option <int> .Some(12);

            optionTestClass = optionInt.OfType <TestClass>();
            CheckEmptyOption(optionTestClass);

            // From Reference type
            var testObject = new TestClass();

            optionTestClass = Option <TestClass> .Some(testObject);

            optionTestClassLeaf = optionTestClass.OfType <TestClassLeaf>();
            CheckEmptyOption(optionTestClassLeaf);
        }
Пример #4
0
        public void OptionCast()
        {
            // Value type
            // Option with value
            var optionInt = Option <int> .Some(12);

            var optionFloat = optionInt.Cast(intVal => intVal + 0.1f);

            CheckOptionValue(optionFloat, 12.1f);

            // Empty option
            Option <int> emptyOptionInt = Option.None;

            optionFloat = emptyOptionInt.Cast(intVal => intVal + 0.1f);
            CheckEmptyOption(optionFloat);


            // Reference type
            // Option with value
            var testObjectLeaf = new TestClassLeaf {
                TestInt = 12
            };
            var optionClassLeaf = Option <TestClassLeaf> .Some(testObjectLeaf);

            optionFloat = optionClassLeaf.Cast(obj => obj.TestInt + 0.2f);
            CheckOptionValue(optionFloat, 12.2f);

            var optionClass = optionClassLeaf.Cast <TestClass>();

            CheckOptionSameValue(optionClass, testObjectLeaf);

            // Invalid conversion
            var testObject = new TestClass {
                TestInt = 42
            };

            optionClass = Option <TestClass> .Some(testObject);

            optionClassLeaf = optionClass.Cast <TestClassLeaf>();
            CheckEmptyOption(optionClassLeaf);

            // Empty option
            Option <TestClassLeaf> emptyOptionClassLeaf = Option.None;

            optionFloat = emptyOptionClassLeaf.Cast(obj => obj.TestInt + 0.2f);
            CheckEmptyOption(optionFloat);

            optionClass = emptyOptionClassLeaf.Cast <TestClass>();
            CheckEmptyOption(optionClass);

            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <ArgumentNullException>(() => optionInt.Cast <float>(null));
        }
        public void CastValueResultToValueCustomResultWithAs()
        {
            var testObject     = new TestClass();
            var testObjectLeaf = new TestClassLeaf();

            var customErrorObjectFactory = new CustomErrorTest {
                ErrorCode = -8
            };
            int counterErrorFactory = 0;

            // Result ok
            var ok     = Result.Ok <TestClass>(testObjectLeaf);
            var result = ok.Cast <TestClassLeaf, CustomErrorTest>(customErrorObjectFactory);

            CheckResultOk(result, testObjectLeaf);

            result = ok.Cast <TestClassLeaf, CustomErrorTest>(
                () =>
            {
                ++counterErrorFactory;
                return(customErrorObjectFactory);
            });
            Assert.AreEqual(0, counterErrorFactory);
            CheckResultOk(result, testObjectLeaf);

            ok     = Result.Ok(testObject);
            result = ok.Cast <TestClassLeaf, CustomErrorTest>(customErrorObjectFactory);
            CheckResultOk(result, null);

            result = ok.Cast <TestClassLeaf, CustomErrorTest>(
                () =>
            {
                ++counterErrorFactory;
                return(customErrorObjectFactory);
            });
            Assert.AreEqual(0, counterErrorFactory);
            CheckResultOk(result, null);

            // Result warn
            var warning = Result.Warn <TestClass>(testObjectLeaf, "My warning");

            result = warning.Cast <TestClassLeaf, CustomErrorTest>(customErrorObjectFactory);
            CheckResultWarn(result, testObjectLeaf, "My warning");

            result = warning.Cast <TestClassLeaf, CustomErrorTest>(
                () =>
            {
                ++counterErrorFactory;
                return(customErrorObjectFactory);
            });
            Assert.AreEqual(0, counterErrorFactory);
            CheckResultWarn(result, testObjectLeaf, "My warning");


            warning = Result.Warn(testObject, "My warning");
            result  = warning.Cast <TestClassLeaf, CustomErrorTest>(customErrorObjectFactory);
            CheckResultWarn(result, null, "My warning");

            result = warning.Cast <TestClassLeaf, CustomErrorTest>(
                () =>
            {
                ++counterErrorFactory;
                return(customErrorObjectFactory);
            });
            Assert.AreEqual(0, counterErrorFactory);
            CheckResultWarn(result, null, "My warning");

            // Result fail
            var failure = Result.Fail <TestClass>("My failure");

            result = failure.Cast <TestClassLeaf, CustomErrorTest>(customErrorObjectFactory);
            CheckResultFail(result, "My failure", customErrorObjectFactory);

            result = failure.Cast <TestClassLeaf, CustomErrorTest>(
                () =>
            {
                ++counterErrorFactory;
                return(customErrorObjectFactory);
            });
            Assert.AreEqual(1, counterErrorFactory);
            CheckResultFail(result, "My failure", customErrorObjectFactory);

            Assert.Throws <ArgumentNullException>(() => ok.Cast <TestClass, CustomErrorTest>((CustomErrorTest)null));
            Assert.Throws <ArgumentNullException>(() => ok.Cast <TestClass, CustomErrorTest>((Func <CustomErrorTest>)null));
        }