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); }
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); }
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); }
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); }
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); }
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()); }
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()); }
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); }
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); }
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"); }
public void WhenValueIsSetAndNoValueMatchDefinedForExec_ExceptionThrown() { var valueOrError = ValueOrError.WithValue("1"); Throws <NoMatchException>(() => valueOrError.Match().Error().Do(x => { }).Exec()); }
public void WhenValueIsSetAndNoValueMatchDefined_ExceptionThrown() { var valueOrError = ValueOrError.WithValue("1"); Throws <NoMatchException>(() => valueOrError.Match <int>().Error().Do(x => 2).Result()); }
public void WhenValueIsSet_AccessingErrorCausesException() { var valueOrError = ValueOrError.WithValue("2"); Throws <InvalidOperationException>(() => Ignore(valueOrError.Error)); }
public void WhenValueIsSet_ValueCanBeAccessed() { var valueOrError = ValueOrError.WithValue("1"); AreEqual("1", valueOrError.Value); }
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); }
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); }
public void WhenValueIsSet_PrintStringYieldsValue() { var valueOrError = ValueOrError.WithValue("42"); AreEqual("Value of 42", valueOrError.ToString()); }