コード例 #1
0
        public void GivenDecimal_Set_SetsParsedValue()
        {
            var targetObject   = new MockClass();
            var uut            = new NullableDecimalSetter();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag1)), uut);
            var messageContext = new FixMessageContext();

            uut.Set(1M.ToString(), mappingDetails, messageContext, targetObject);
            Assert.True(targetObject.Tag1.HasValue);
        }
コード例 #2
0
        public void GivenFloat_SetTarget_SetsParsedValue()
        {
            var targetObject   = new MockStruct();
            var uut            = new FloatSetter();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag1)), uut);
            var messageContext = new FixMessageContext();

            uut.SetTarget <MockStruct>(1.5f.ToString(), mappingDetails, messageContext, ref targetObject);
            Assert.Equal(1.5, targetObject.Tag1);
        }
コード例 #3
0
        public void GivenDateTimeOffset_SetTarget_SetsParsedValue()
        {
            var targetObject   = new MockStruct();
            var uut            = new DateTimeOffsetSetter();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag1)), uut);
            var messageContext = new FixMessageContext();

            uut.SetTarget <MockStruct>("20190104-19:02:04", mappingDetails, messageContext, ref targetObject);
            Assert.Equal(new DateTimeOffset(2019, 1, 4, 19, 2, 4, TimeSpan.Zero), targetObject.Tag1);
        }
コード例 #4
0
        public void GivenDouble_Set_SetsParsedValue()
        {
            var targetObject   = new MockClass();
            var uut            = new DoubleSetter();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag1)), uut);
            var messageContext = new FixMessageContext();

            uut.Set(1.5d.ToString(), mappingDetails, messageContext, targetObject);
            Assert.Equal(1.5, targetObject.Tag1);
        }
コード例 #5
0
        public void GivenFalseValues_Set_SetsParsedValue(string value)
        {
            var targetObject   = new TestTypeParent();
            var uut            = new BooleanSetter();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag68)), uut);
            var messageContext = new FixMessageContext();

            uut.Set(value, mappingDetails, messageContext, targetObject);
            Assert.False(targetObject.Tag68);
        }
コード例 #6
0
        public void GivenBoolAsString_Set_SetsValueOnTargetObject()
        {
            var targetObject   = new TestTypeParent();
            var uut            = new CompositePropertySetter();
            var valueToSet     = "Y";
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag68)), new BooleanSetter());
            var messageContext = new FixMessageContext();

            uut.Set(valueToSet.AsSpan(), mappingDetails, messageContext, targetObject);
            Assert.True(targetObject.Tag68);
        }
コード例 #7
0
        public void GivenNullableDateTimeOffsetAsString_Set_SetsValueOnTargetObject()
        {
            var targetObject   = new TestTypeParent();
            var uut            = new CompositePropertySetter();
            var valueToSet     = "20181219-18:14:23";
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag71)), new NullableDateTimeOffsetSetter());
            var messageContext = new FixMessageContext();

            uut.Set(valueToSet.AsSpan(), mappingDetails, messageContext, targetObject);
            Assert.Equal(new DateTimeOffset(2018, 12, 19, 18, 14, 23, 0, TimeSpan.Zero), targetObject.Tag71.Value);
        }
コード例 #8
0
        public void GivenEncoding_Setup_ReturnsMessageContext()
        {
            var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build();
            var uut     = new FixMessageContext();
            var result  = uut.Setup(message.AsSpan(), MessageEncoding.JIS);

            Assert.NotEqual(-1, result.ChecksumTagStartIndex);
            Assert.NotEqual(-1, result.LengthTagStartIndex);
            Assert.NotEqual(-1, result.MessageTypeTagStartIndex);
            Assert.Equal(MessageEncoding.JIS, result.EncodedFields);
        }
コード例 #9
0
        public void GivenMessageContext_IsValid_ReturnsTrue()
        {
            var uut     = new ValidatorCollection(IntegerToFixConverter.Instance);
            var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build(out byte checksum, out var dummy);
            var msgCtx  = new FixMessageContext().Setup(message);

            msgCtx.ChecksumValue = checksum;

            Assert.True(uut.PreValidate(message, msgCtx));
            Assert.True(uut.PostValidate(message, msgCtx));
        }
コード例 #10
0
        public void GivenCorrectChecksum_Validate_ReturnsTrue(string input)
        {
            byte[] message    = new TestFixMessageBuilder(input).Build(out byte checksumValue, out int checksumStart);
            var    uut        = new ChecksumValidator(IntegerToFixConverter.Instance);
            var    msgContext = new FixMessageContext()
            {
                ChecksumValue = checksumValue, ChecksumTagStartIndex = checksumStart
            };
            var result = uut.IsValid(message.AsSpan(), msgContext);

            Assert.True(result);
        }
コード例 #11
0
        public void GivenNullableIntegerAsString_Set_SetsValueOnTargetObject()
        {
            var targetObject = new TestTypeParent();
            var uut          = new CompositePropertySetter();
            ReadOnlySpan <char> valueToSet = "12357".AsSpan();
            var mappingDetails             = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag63)), new IntegerSetter());
            var messageContext             = new FixMessageContext();

            uut.Set(valueToSet, mappingDetails, messageContext, targetObject);

            Assert.Equal(12357, targetObject.Tag63);
        }
コード例 #12
0
 public static FixMessageContext Setup(this FixMessageContext messageContext, ReadOnlySpan <byte> data, MessageEncoding?encoding)
 {
     messageContext.FixVersion    = null;
     messageContext.EncodedFields = encoding ?? MessageEncoding.UTF8;
     messageContext.RepeatingGroupCounters.Clear();
     messageContext.CreatedParentTypes.Clear();
     messageContext.ChecksumValue            = 0;
     messageContext.LengthTagStartIndex      = data.IndexOf(KnownFixTags.Length);
     messageContext.MessageTypeTagStartIndex = data.IndexOf(KnownFixTags.MessageType);
     messageContext.ChecksumTagStartIndex    = data.IndexOf(KnownFixTags.Checksum);
     return(messageContext);
 }
コード例 #13
0
 public object Set(TagMapLeaf mappingDetails, TagMapParent parent, FixMessageContext fixMessageContext, object targetObject)
 {
     if (parent.IsEnumerable)
     {
         targetObject = SetRepeatingTypeParent(mappingDetails, parent, fixMessageContext, targetObject);
     }
     else
     {
         targetObject = SetSimpleTypeParent(parent, fixMessageContext, targetObject);
     }
     return(targetObject);
 }
コード例 #14
0
        public void GivenNullableBool_Set_SetsValueOnTargetObject()
        {
            var targetObject   = new TestTypeParent();
            var uut            = new CompositePropertySetter();
            var valueToSet     = Encoding.ASCII.GetBytes("Y").AsSpan();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag69)), new NullableBooleanSetter());
            var messageContext = new FixMessageContext();

            uut.Set(valueToSet, mappingDetails, messageContext, targetObject);
            Assert.True(targetObject.Tag69);
            Assert.False(targetObject.Tag68);
        }
コード例 #15
0
 /// <summary>
 /// Sets a typed value on an array property or a non-enumerable property based on the mapping details.
 /// </summary>
 protected void SetValue <TTarget, T>(TagMapLeaf mappingDetails, FixMessageContext fixMessageContext, ref TTarget targetObject, T parsedValue)
 {
     if (mappingDetails.IsEnumerable)
     {
         int index = GetAdvancedIndex(mappingDetails, fixMessageContext);
         GetTypedEnumeratedILSetterAction <TTarget, T>(mappingDetails.Current).Invoke(ref targetObject, parsedValue, index);
     }
     else
     {
         GetTypedILSetterAction <TTarget, T>(mappingDetails.Current).Invoke(ref targetObject, parsedValue);
     }
 }
コード例 #16
0
        public void GivenNullable_Set_SetsValueOnTargetObject()
        {
            var targetObject   = new TestTypeParent();
            var uut            = new CompositePropertySetter();
            var valueToSet     = Encoding.ASCII.GetBytes("12357").AsSpan();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag63)), new IntegerSetter());
            var messageContext = new FixMessageContext();

            uut.Set(valueToSet, mappingDetails, messageContext, targetObject);

            Assert.Equal(12357, targetObject.Tag63);
        }
コード例 #17
0
ファイル: MessageParser.cs プロジェクト: danubeDev/rapidefix
 public MessageParser(
     ITagToPropertyMapper tagToPropertyMapper,
     IPropertySetter compositeSetter,
     IValidator validators,
     MessageParserOptions options
     )
 {
     _propertyMapper  = tagToPropertyMapper ?? throw new ArgumentNullException(nameof(tagToPropertyMapper));
     _compositeSetter = compositeSetter ?? throw new ArgumentNullException(nameof(compositeSetter));
     _validators      = validators ?? throw new ArgumentNullException(nameof(validators));
     _options         = options ?? throw new ArgumentNullException(nameof(options));
     _messageContext  = new FixMessageContext();
 }
コード例 #18
0
        public void GivenIEnumerablePrimitive_SetAsString_CreatesArryOfPrimitive()
        {
            var          targetObject   = new TestTypeParent();
            var          uut            = new RepeatingGroupTagSetter();
            var          valueToSet     = "3".AsSpan();
            PropertyInfo propertyInfo   = targetObject.GetType().GetProperty(nameof(targetObject.Tag57s));
            var          mappingDetails = TagMapLeaf.CreateRepeatingTag(propertyInfo, propertyInfo.PropertyType.GetGenericArguments()[0], uut);
            var          messageContext = new FixMessageContext();
            var          result         = uut.Set(valueToSet, mappingDetails, messageContext, targetObject);

            Assert.NotNull(targetObject.Tag57s);
            Assert.Equal(3, targetObject.Tag57s.Count());
        }
コード例 #19
0
        public object Set(ReadOnlySpan <char> value, TagMapLeaf mappingDetails, FixMessageContext fixMessageContext, object targetObject)
        {
            if (mappingDetails.Parents != null)
            {
                foreach (var parent in mappingDetails.Parents)
                {
                    targetObject = parent.ParentSetter.Set(mappingDetails, parent, fixMessageContext, targetObject);
                }
            }

            targetObject = mappingDetails.Setter.Set(value, mappingDetails, fixMessageContext, targetObject);
            return(targetObject);
        }
コード例 #20
0
 public TypedStringMessageParser(
     ITagToPropertyMapper tagToPropertyMapper,
     ITypedPropertySetter typedPropertySetter,
     IValidator validators,
     MessageParserOptions options
     )
 {
     _propertyMapper      = tagToPropertyMapper ?? throw new ArgumentNullException(nameof(tagToPropertyMapper));
     _typedPropertySetter = typedPropertySetter ?? throw new ArgumentNullException(nameof(typedPropertySetter));
     _options             = options ?? throw new ArgumentNullException(nameof(options));
     _messageContext      = new FixMessageContext();
     _propertyMapper.Map <TTarget>();
     _isValueType = typeof(TTarget).IsValueType;
 }
コード例 #21
0
        public void GivenIEnumerableT_Set_CreatesArryOfT()
        {
            var targetObject = new TestTypeParent();
            var uut          = new RepeatingGroupTagSetter();
            var valueToSet   = new byte[2];

            IntegerToFixConverter.Instance.Convert(15, valueToSet, 2);
            PropertyInfo propertyInfo   = targetObject.GetType().GetProperty(nameof(targetObject.Tag59s));
            var          mappingDetails = TagMapLeaf.CreateRepeatingTag(propertyInfo, propertyInfo.PropertyType.GetGenericArguments()[0], uut);
            var          messageContext = new FixMessageContext();
            var          result         = uut.Set(valueToSet, mappingDetails, messageContext, targetObject);

            Assert.NotNull(targetObject.Tag59s);
            Assert.Equal(15, targetObject.Tag59s.Count());
        }
コード例 #22
0
        public void GivenMessageContext_SetupTwice_ResetsProperties()
        {
            var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build();
            var uut     = new FixMessageContext();

            uut.CreatedParentTypes.Add(1);
            uut.RepeatingGroupCounters.Add(1, new FixMessageContext.RepeatingCounter(1));
            var result = uut.Setup(message.AsSpan(), MessageEncoding.JIS).Setup(message.AsSpan(), MessageEncoding.EUC);

            Assert.NotEqual(-1, result.ChecksumTagStartIndex);
            Assert.NotEqual(-1, result.LengthTagStartIndex);
            Assert.NotEqual(-1, result.MessageTypeTagStartIndex);
            Assert.Equal(MessageEncoding.EUC, result.EncodedFields);
            Assert.Empty(result.RepeatingGroupCounters);
            Assert.Empty(result.CreatedParentTypes);
        }
コード例 #23
0
        public object Set(ReadOnlySpan <byte> value, TagMapLeaf mappingDetails, FixMessageContext fixMessageContext, object targetObject)
        {
            int         valueLength = value.Length;
            Span <char> valueChars  = stackalloc char[valueLength];

            if (mappingDetails.IsEncoded)
            {
                valueLength = fixMessageContext.EncodedFields.GetEncoder().GetChars(value, valueChars);
                valueChars  = valueChars.Slice(0, valueLength);
            }
            else
            {
                valueLength = Encoding.ASCII.GetChars(value, valueChars);
            }
            return(Set(valueChars, mappingDetails, fixMessageContext, targetObject));
        }
コード例 #24
0
 public bool PreValidate(ReadOnlySpan <byte> message, FixMessageContext messageContext)
 {
     if (messageContext is null)
     {
         throw new ArgumentNullException(nameof(messageContext));
     }
     if (!_versionValidator.IsValid(message, messageContext))
     {
         return(false);
     }
     if (!_lengthValidator.IsValid(message, messageContext))
     {
         return(false);
     }
     return(true);
 }
コード例 #25
0
        public bool IsValid(ReadOnlySpan <byte> data, FixMessageContext msgContext)
        {
            var endingTagPos = msgContext.ChecksumTagStartIndex;

            if (endingTagPos < 0 || (endingTagPos + KnownFixTags.Checksum.Length + ChecksumLength + 1) != data.Length)
            {
                return(false);
            }

            Span <byte> expectedDigits = stackalloc byte[ChecksumLength];

            _converter.Convert(number: msgContext.ChecksumValue, into: expectedDigits, count: ChecksumLength);

            var receivedChecksum = data.Slice(endingTagPos + KnownFixTags.Checksum.Length, ChecksumLength);

            return(receivedChecksum.SequenceEqual(expectedDigits));
        }
コード例 #26
0
        public void GivenString_Set_SetsValueOnTargetObject()
        {
            var targetObject   = new TestTypeParent();
            var uut            = new TypeConvertedSetter();
            var valueToSet     = "12358";
            var property       = targetObject.GetType().GetProperty(nameof(targetObject.Tag61));
            var typeConverter  = property.GetCustomAttribute <TypeConverterAttribute>();
            var mappingDetails = new TagMapLeaf(property, uut)
            {
                TypeConverterName = typeConverter.ConverterTypeName
            };
            var messageContext = new FixMessageContext();

            uut.Set(valueToSet.AsSpan(), mappingDetails, messageContext, targetObject);

            Assert.Equal(12358, targetObject.Tag61.Value);
        }
コード例 #27
0
        public void GivenParentsAsString_Set_CreatesParentTypes()
        {
            var targetObject   = new TestTypeParent();
            var uut            = new CompositePropertySetter();
            var valueToSet     = "test";
            var mappingDetails = new TagMapLeaf(MockPropertyInfo.Get, new MockPropertySetter())
            {
                Parents = new List <TagMapParent>
                {
                    new TagMapParent(targetObject.GetType().GetProperty(nameof(targetObject.CustomType)), new ParentPropertySetter())
                }
            };
            var messageContext = new FixMessageContext();
            var result         = uut.Set(valueToSet.AsSpan(), mappingDetails, messageContext, targetObject);

            Assert.NotNull(targetObject.CustomType);
            Assert.Equal(result, targetObject.CustomType);
        }
コード例 #28
0
        public void GivenCorrectLength_Validate_Performance()
        {
            byte[] message = TestFixMessageBuilder.CreateDefaultMessage();

            var       uut        = new LengthValidator(IntegerToFixConverter.Instance);
            var       msgContext = new FixMessageContext().Setup(message);
            var       result     = uut.IsValid(message.AsSpan(), msgContext);
            Stopwatch sw         = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 100; i++)
            {
                result = uut.IsValid(message.AsSpan(), msgContext);
            }
            sw.Stop();

            _output.WriteLine($"==================={sw.ElapsedMilliseconds}======================");
        }
コード例 #29
0
        public void GivenEnumerableValue_Set_SetsValueOnTargetObjectsProperIndex()
        {
            var targetObject = new TestTypeParent()
            {
                Tag57s = new string[3]
            };
            var uut            = new CompositePropertySetter();
            var valueToSet     = Encoding.ASCII.GetBytes("12357").AsSpan();
            var mappingDetails = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag57s)), new StringSetter())
            {
                IsEnumerable       = true,
                RepeatingTagNumber = 56,
                InnerType          = typeof(string),
            };
            var messageContext = new FixMessageContext();

            uut.Set(valueToSet, mappingDetails, messageContext, targetObject);

            Assert.Equal("12357", targetObject.Tag57s.First());
        }
コード例 #30
0
        public void GivenStructAsSting_Set_SetsValueOnTargetObject()
        {
            var targetObject         = new TestTypeStruct();
            var uut                  = new CompositePropertySetter();
            var integerToSet         = "12357".AsSpan();
            var stringToSet          = "message".AsSpan();
            var doubleToSet          = "123.456".AsSpan();
            var mappingDetailsInt    = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag101)), new IntegerSetter());
            var mappingDetailsString = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag100)), new StringSetter());
            var mappingDetailsDouble = new TagMapLeaf(targetObject.GetType().GetProperty(nameof(targetObject.Tag102)), new DoubleSetter());
            var messageContext       = new FixMessageContext();

            uut.SetTarget(integerToSet, mappingDetailsInt, messageContext, ref targetObject);
            uut.SetTarget(stringToSet, mappingDetailsString, messageContext, ref targetObject);
            uut.SetTarget(doubleToSet, mappingDetailsDouble, messageContext, ref targetObject);

            Assert.Equal(12357, targetObject.Tag101);
            Assert.Equal("message", targetObject.Tag100);
            Assert.Equal(123.456, targetObject.Tag102);
        }