예제 #1
0
        public void TestSetInnerValueNonNullableNull()
        {
            var maybe      = new Maybe <int>();
            var boxedMaybe = (object)maybe;

            Assert.Throws <InvalidOperationException>(() => MaybeUtils.SetInnerValue(boxedMaybe, null));
        }
예제 #2
0
        public void TestSetInnerValueNullValue()
        {
            var maybe      = new Maybe <object>();
            var boxedMaybe = (object)maybe;

            MaybeUtils.SetInnerValue(boxedMaybe, null);

            maybe = (Maybe <object>)boxedMaybe;
            Assert.IsNull(maybe.Value);
        }
예제 #3
0
        public void TestSetInnerValue()
        {
            var maybe      = new Maybe <int>();
            var boxedMaybe = (object)maybe;

            MaybeUtils.SetInnerValue(boxedMaybe, 10);

            maybe = (Maybe <int>)boxedMaybe;
            Assert.AreEqual(10, maybe.Value);
        }
예제 #4
0
        public void TestSetInnerValueDerived()
        {
            var maybe      = new Maybe <IList <int> >();
            var boxedMaybe = (object)maybe;
            var innerValue = new List <int>();

            MaybeUtils.SetInnerValue(boxedMaybe, innerValue);

            maybe = (Maybe <IList <int> >)boxedMaybe;
            Assert.AreSame(innerValue, maybe.Value);
        }
예제 #5
0
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Undefined)
            {
                // undefined 表示空的 Maybe<T> 实例。直接使用 Activator 实例化即可。
                existingValue = Activator.CreateInstance(objectType);
                return(existingValue);
            }

            var valueType = MaybeUtils.GetInnerType(objectType);
            var value     = serializer.Deserialize(reader, valueType);

            if (existingValue != null && MaybeUtils.IsMaybe(existingValue))
            {
                MaybeUtils.SetInnerValue(existingValue, value);
            }
            else
            {
                existingValue = MaybeUtils.Create(valueType, value);
            }

            return(existingValue);
        }
예제 #6
0
 public void TestSetInnerValueTypeMismatch()
 {
     Assert.Throws <InvalidOperationException>(() => MaybeUtils.SetInnerValue(new Maybe <int>(), new object()));
 }
예제 #7
0
 public void TestSetInnerValueNonMaybe()
 {
     Assert.Throws <ArgumentException>(() => MaybeUtils.SetInnerValue(new object(), new object()));
 }
예제 #8
0
 public void TestSetInnerValueNull()
 {
     Assert.Throws <ArgumentNullException>(() => MaybeUtils.SetInnerValue(null, 10));
     Assert.DoesNotThrow(() => MaybeUtils.SetInnerValue(new Maybe <object>(), null));
 }