示例#1
0
        public void DelegateConverter_Ctr2Test()
        {
            ValueConversion convert = (v, t, p, c) => v.ToString();
            ValueConversion convertBack = (v, t, p, c) => int.Parse(v.ToString());

            var converter = new DelegateConverter(convert, convertBack);
            var result = converter.Convert(5, null, null, null);
            Assert.AreEqual("5", result);

            var result2 = converter.ConvertBack("5", null, null, null);
            Assert.AreEqual(5, result2);
        }
示例#2
0
 public void DelegateConverter_Ctr1Test()
 {
     ValueConversion convert = (v, t, p, c) => v.ToString();
     var converter = new DelegateConverter(convert);
     var result = converter.Convert(5, null, null, null);
     Assert.AreEqual("5", result);
     try
     {
         converter.ConvertBack(5, null, null, null);
         Assert.Fail("Expected an exception");
     }
     catch (NotImplementedException)
     {
     }
 }
示例#3
0
 protected object ConvertValue(object value, Type propertyType)
 {
     if (ValueConversion != null)
     {
         var delegates = ValueConversion.GetInvocationList();
         foreach (Delegate del in delegates)
         {
             ParameterInfo[] parainfos = del.Method.GetParameters();
             if (parainfos[0].ParameterType == propertyType)
             {
             }
         }
     }
     return(DefaultConvertValue(value, propertyType));
 }
示例#4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DelegateConverter"/> class.
        /// </summary>
        /// <param name="convert">The convert.</param>
        /// <param name="convertBack">The convert back.</param>
        /// <exception cref="System.ArgumentNullException">convert</exception>

        public DelegateConverter(ValueConversion convert, ValueConversion?convertBack)
        {
            if (convert == null)
            {
                throw new ArgumentNullException(nameof(convert), $"{nameof(convert)} is null.");
            }
            m_Convert = convert;

            if (convertBack != null)
            {
                m_ConvertBack = convertBack;
            }
            else
            {
                m_ConvertBack = (v, t, p, c) => { throw new NotImplementedException(); }
            };
        }
示例#5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DelegateConverter"/> class.
        /// </summary>
        /// <param name="convert">The convert.</param>
        /// <param name="convertBack">The convert back.</param>
        /// <exception cref="System.ArgumentNullException">convert</exception>

        public DelegateConverter(Func <object, object> convert, Func <object, object>?convertBack)
        {
            if (convert == null)
            {
                throw new ArgumentNullException(nameof(convert), $"{nameof(convert)} is null.");
            }

            m_Convert = (v, t, p, c) => convert(v);

            if (convertBack != null)
            {
                m_ConvertBack = (v, t, p, c) => convertBack(v);
            }
            else
            {
                m_ConvertBack = (v, t, p, c) => { throw new NotImplementedException(); }
            };
        }
 static InlineGenerators()
 {
     _alphaCharGenerator   = new AlphaCharGenerator();
     _digitCharGenerator   = new DigitCharGenerator();
     _lowerCaseAlphaChar   = _alphaCharGenerator.AsLowerCase();
     _upperCaseAlphaChar   = _alphaCharGenerator.AsUpperCase();
     _simpleValueGenerator = new SimpleValueGenerator <char>();
     _stringGenerator      = new SimpleValueGenerator <string>();
     _lowercaseString      = new ValueConversion <string, string>(_stringGenerator, s => s.ToLowerInvariant());
     _uppercaseString      = new ValueConversion <string, string>(_stringGenerator, s => s.ToUpperInvariant());
     _lowercaseAlphaString = new ValueConversion <string, string>(
         AlphaString(System.Guid.NewGuid().ToString().Length), s => s.ToLowerInvariant());
     _uppercaseAlphaString = new ValueConversion <string, string>(
         AlphaString(System.Guid.NewGuid().ToString().Length), s => s.ToUpperInvariant());
     _identifierStringGenerator = new IdentifierStringGenerator(_digitCharGenerator, _alphaCharGenerator);
     _uriGenerator       = new SimpleValueGenerator <Uri>();
     _uriStringGenerator = new ValueConversion <Uri, string>(_uriGenerator, u => u.ToString());
     _guid                    = new SimpleValueGenerator <Guid>();
     _intGenerator            = new SimpleValueGenerator <int>();
     _doubleGenerator         = new SimpleValueGenerator <double>();
     _longGenerator           = new SimpleValueGenerator <long>();
     _unsignedLongGenerator   = new SimpleValueGenerator <ulong>();
     _byteGenerator           = new SimpleValueGenerator <byte>();
     _decimalGenerator        = new SimpleValueGenerator <decimal>();
     _uintGenerator           = new SimpleValueGenerator <uint>();
     _ushortGenerator         = new SimpleValueGenerator <ushort>();
     _shortGenerator          = new SimpleValueGenerator <short>();
     _digitGenerator          = new DigitGenerator();
     _positiveDigitGenerator  = new PositiveDigitGenerator(_digitGenerator);
     _ipStringGenerator       = new IpStringGenerator();
     _portNumberGenerator     = new PortNumberGenerator();
     _actionGenerator         = new SimpleInstanceGenerator <Action>();
     _notStartedTaskGenerator = new NotStartedTaskGenerator();
     _ipAddressGenerator      = new SimpleValueGenerator <IPAddress>();
     _dateTimeGenerator       = new SimpleValueGenerator <DateTime>();
     _timeSpanGenerator       = new SimpleValueGenerator <TimeSpan>();
     _boolGenerator           = new SimpleValueGenerator <bool>();
     _objectGenerator         = new SimpleValueGenerator <object>();
     _methodInfoGenerator     = new SimpleValueGenerator <MethodInfo>();
     _typeGenerator           = new SimpleValueGenerator <Type>();
     _exceptionGenerator      = new SimpleValueGenerator <Exception>();
 }
 public void TestSerializeToValue(
     CultureInfo culture,
     object clrValue,
     SpannerDbType spannerDbType,
     string expectedJsonValue,
     TestType testType = TestType.Both)
 {
     if (testType == TestType.ValueToClr)
     {
         return;
     }
     WithCulture(culture, () =>
     {
         string infoAddendum = $", type:{clrValue?.GetType().Name}, spannerType:{spannerDbType} ";
         try
         {
             string expected = expectedJsonValue;
             var jsonValue   = ValueConversion.ToValue(clrValue, spannerDbType);
             string actual   = jsonValue.ToString();
             if (expected != actual)
             {
                 if (jsonValue.KindCase == Value.KindOneofCase.StructValue)
                 {
                     AssertJsonEqual <Struct>(expected, actual);
                 }
                 else
                 {
                     //our error message contains an informational addendum
                     //which tells us which theory test case failed.
                     Assert.Equal(expected + infoAddendum, actual + infoAddendum);
                 }
             }
         }
         catch (Exception e)
         {
             Assert.True(false, infoAddendum + e.Message);
             throw;
         }
     });
 }
示例#8
0
        public void TestInvalidSerializeToValue(
            object value,
            SpannerDbType type,
            TestType testType = TestType.Both)
        {
            if (testType == TestType.ValueToClr)
            {
                return;
            }
            string infoAddendum = $"type:{value?.GetType().Name}, spannerType:{type}";

            var exceptionCaught = false;

            try
            {
                ValueConversion.ToValue(value, type);
            }
            catch (Exception e) when(e is OverflowException || e is InvalidCastException || e is FormatException)
            {
                exceptionCaught = true;
            }
            Assert.True(exceptionCaught, infoAddendum);
        }
示例#9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DelegateConverter"/> class.
        /// </summary>
        /// <param name="convert">The convert.</param>

        public DelegateConverter(ValueConversion convert)
            : this(convert, null)
        {
        }