Example #1
0
        public void TryGetKind(object value, ScalarValueKind expectedKind)
        {
            // arrange
            // act
            bool isScalar = Scalars.TryGetKind(value, out ScalarValueKind kind);

            // assert
            Assert.True(isScalar);
            Assert.Equal(expectedKind, kind);
        }
Example #2
0
        protected static bool TryConvertSerialized <T>(
            object serialized,
            ScalarValueKind expectedKind,
            out T value)
        {
            if (Scalars.TryGetKind(serialized, out ScalarValueKind kind) &&
                kind == expectedKind &&
                _converter.TryConvert <object, T>(serialized, out T c))
            {
                value = c;
                return(true);
            }

            value = default;
            return(false);
        }
Example #3
0
        public static bool TryGetKind(object value, out ScalarValueKind kind)
        {
            if (value is null)
            {
                kind = ScalarValueKind.Null;
                return(true);
            }

            Type valueType = value.GetType();

            if (valueType.IsEnum)
            {
                kind = ScalarValueKind.Enum;
                return(true);
            }

            return(_scalarKinds.TryGetValue(valueType, out kind));
        }
Example #4
0
        public void TryGetKind_From_Nullable(
            object value,
            ScalarValueKind expectedKind)
        {
            // arrange
            Type            type        = typeof(Nullable <>).MakeGenericType(value.GetType());
            ConstructorInfo constructor =
                type.GetConstructor(new[] { value.GetType() });
            object nullableValue = constructor.Invoke(new[] { value });

            // act
            bool isScalar = Scalars.TryGetKind(
                nullableValue, out ScalarValueKind kind);

            // assert
            Assert.True(isScalar);
            Assert.Equal(expectedKind, kind);
        }