Пример #1
0
        public void WhenValueIsSetAndNoErrorMatch_ElseResultIsReturned()
        {
            var valueOrError = ValueOrError.WithValue("1");
            var result       = valueOrError.Match <int>().Error().Do(x => 2).Else(x => 3).Result();

            AreEqual(3, result);
        }
Пример #2
0
        public void WhenValueIsSet_ValueSuppliedToFunction()
        {
            var valueOrError = ValueOrError.WithValue("1");
            var result       = valueOrError.Match <string>().Value().Do(s => "v" + s).Error().Do(s => "e" + s).Result();

            AreEqual("v1", result);
        }
Пример #3
0
        public void WhenSome_SomeOfDoWithExpressionSupported()
        {
            var valueOrError = ValueOrError.WithValue("1");
            var result       = valueOrError.Match <int>().Value().Of("1").Do(1).Value().Do(2).Error().Do(3).Result();

            AreEqual(1, result);
        }
        public void SameStringInOneValueOneError_AreNotEqual()
        {
            var a = ValueOrError.WithValue("1234");
            var b = ValueOrError.WithError("1234");

            IsFalse(a.Equals(b));
            IsTrue(a != b);
        }
        public void ComparingValueWithNull_ResultsInNotEqual()
        {
            var a = ValueOrError.WithValue("1234");

            IsFalse(a.Equals(null));
            IsTrue(a != null);
            IsTrue(null != a);
        }
Пример #6
0
        public void WhenSome_SomeWhereDoWithExpressionSupported()
        {
            var valueOrError = ValueOrError.WithValue("1");
            var result       = valueOrError.Match <int>()
                               .Value().Where(x => x == "1").Do(0).Value().Do(2).Error().Do(3).Result();

            AreEqual(0, result);
        }
        public void ValueOrErrorWithValueWithNoMatchAndIgnoreElse_DoesNothing()
        {
            var value  = ValueOrError.WithValue("test");
            var result = false;

            value.Match().Error().Do(x => result = true).IgnoreElse().Exec();
            Assert.False(result);
        }
Пример #8
0
        public void WhenValueIsSet_ValueSuppliedToAction()
        {
            var valueOrError = ValueOrError.WithValue("x");
            var value        = string.Empty;

            valueOrError.Match().Value().Do(s => value = s).Error().Do(_ => { }).Exec();
            AreEqual("x", value);
        }
Пример #9
0
        public void WhenValueIsSetAndNoValueMatchForExec_ElseResultIsReturned()
        {
            var valueOrError = ValueOrError.WithValue("2");
            var result       = "0";

            valueOrError.Match().Error().Do(x => result = x).Else(x => result = "1").Exec();
            AreEqual("1", result);
        }
        public void SameValues_AreEqualAndHaveSameHashCode()
        {
            var a = ValueOrError.WithValue("1234");
            var b = ValueOrError.WithValue("1234");

            IsTrue(a.Equals(b));
            IsTrue(a == b);
            AreEqual(a.GetHashCode(), b.GetHashCode());
        }
Пример #11
0
        public void WhenValueIsSet_OnlyValueActionOccurs()
        {
            var valueOrError = ValueOrError.WithValue("x");
            var valueSet     = false;
            var errorSet     = false;

            valueOrError.Match().Value().Do(_ => valueSet = true).Error().Do(_ => errorSet = true).Exec();
            AreEqual(new[] { true, false }, new[] { valueSet, errorSet });
        }
        public void DifferentValues_AreNotEqualAndHaveDifferentHashCodes()
        {
            var a = ValueOrError.WithValue("1234");
            var b = ValueOrError.WithValue("12345");

            IsFalse(a.Equals(b));
            IsTrue(a != b);
            AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
Пример #13
0
        public void ConvertingValueToJsonAndBack_PreservesOptionState()
        {
            var settings = new JsonSerializerSettings();

            settings.Converters.Add(new ValueOrErrorConverter());
            var value    = ValueOrError.WithValue("a");
            var json     = SerializeObject(value, settings);
            var newValue = DeserializeObject <ValueOrError>(json, settings);

            IsTrue(newValue.HasValue);
            AreEqual("a", newValue.Value);
        }
Пример #14
0
        public void ConvertingValueToJsonAndBack_PreservesOptionState()
        {
            var settings = new JsonSerializerSettings
            {
                ContractResolver = SuccinctContractResolver.Instance
            };

            var value    = ValueOrError.WithValue("a");
            var json     = SerializeObject(value, settings);
            var newValue = DeserializeObject <ValueOrError>(json, settings);

            IsTrue(newValue.HasValue);
            AreEqual("a", newValue.Value);
        }
Пример #15
0
        public override object ReadJson(JsonReader reader,
                                        Type objectType,
                                        object existingValue,
                                        JsonSerializer serializer)
        {
            var jsonObject    = JObject.Load(reader);
            var possibleValue = jsonObject.Properties().TryFirst(p => p.Name == "value");
            var possibleError = jsonObject.Properties().TryFirst(p => p.Name == "error");

            if (possibleValue.HasValue)
            {
                return(ValueOrError.WithValue(possibleValue.Value.ToObject <string>()));
            }

            if (possibleError.HasValue)
            {
                return(ValueOrError.WithError(possibleError.Value.ToObject <string>()));
            }

            throw new JsonSerializationException(
                      "Cannot deserialize a ValueOrError that contains neither a value or an error");
        }
Пример #16
0
        public void WhenValueIsSetAndNoValueMatchDefinedForExec_ExceptionThrown()
        {
            var valueOrError = ValueOrError.WithValue("1");

            Throws <NoMatchException>(() => valueOrError.Match().Error().Do(x => { }).Exec());
        }
Пример #17
0
        public void WhenValueIsSetAndNoValueMatchDefined_ExceptionThrown()
        {
            var valueOrError = ValueOrError.WithValue("1");

            Throws <NoMatchException>(() => valueOrError.Match <int>().Error().Do(x => 2).Result());
        }
Пример #18
0
        public void WhenValueIsSet_AccessingErrorCausesException()
        {
            var valueOrError = ValueOrError.WithValue("2");

            Throws <InvalidOperationException>(() => Ignore(valueOrError.Error));
        }
Пример #19
0
        public void WhenValueIsSet_ValueCanBeAccessed()
        {
            var valueOrError = ValueOrError.WithValue("1");

            AreEqual("1", valueOrError.Value);
        }
Пример #20
0
        public void WhenValueIsSet_HasValueIsTrue()
        {
            var valueOrError = ValueOrError.WithValue("1");

            IsTrue(valueOrError.HasValue);
        }
        public void IntPrefexReturnsNone_IfFollowedByJunk()
        {
            var result = ValueOrErrorExamples.IntParser(ValueOrError.WithValue("Int:no number here"));

            IsFalse(result.HasValue);
        }
        public void ValueOtherThanIntPrefex_ReturnsNone()
        {
            var result = ValueOrErrorExamples.IntParser(ValueOrError.WithValue("xxx"));

            IsFalse(result.HasValue);
        }
Пример #23
0
 public void CreatingWithNullValue_CausesNullException()
 {
     Throws <ArgumentNullException>(() => ValueOrError.WithValue(null));
 }
        public void IntPrefex_ReturnsInt()
        {
            var result = ValueOrErrorExamples.IntParser(ValueOrError.WithValue("Int:6"));

            AreEqual(6, result.Value);
        }
Пример #25
0
        public void WhenValueIsSet_PrintStringYieldsValue()
        {
            var valueOrError = ValueOrError.WithValue("42");

            AreEqual("Value of 42", valueOrError.ToString());
        }