示例#1
0
        protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, int valueToUse, byte[] expectedArray)
        {
            MessageSerializedPropertyInfo  propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerThreeByteNumeric typeSerializer = new TypeSerializerThreeByteNumeric(propertyInfo);

            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#2
0
        protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, TestSubClass valueToUse, byte[] expectedArray)
        {
            MessageSerializedPropertyInfo propertyInfo = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerSerializableClass <TestSubClass> typeSerializer = new TypeSerializerSerializableClass <TestSubClass>(propertyInfo);

            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#3
0
        protected int CalculateOutputLength(MessageSerializedPropertyInfo propertyInfo, byte[] value)
        {
            int outputLength = value.Length;

            if (propertyInfo.MessagePropertyAttribute.VariableLength)
            {
                int minLength = propertyInfo.MessagePropertyAttribute.MinLength;
                int maxLength = propertyInfo.MessagePropertyAttribute.MaxLength;
                if (minLength > 0 && minLength > outputLength)
                {
                    outputLength = minLength;
                }

                if (maxLength > 0 && maxLength < outputLength)
                {
                    outputLength = maxLength;
                }
            }
            else
            {
                outputLength = propertyInfo.MessagePropertyAttribute.Length;
            }

            return(outputLength);
        }
示例#4
0
 public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
 {
     if (!messageSerializedPropertyInfo.MessagePropertyAttribute.IsPrepadCharacterSpecified)
     {
         messageSerializedPropertyInfo.MessagePropertyAttribute.PrepadCharacter = '0';
     }
 }
示例#5
0
        protected virtual string GetExpectedListToString <TListType, TValueType>(MessageSerializedPropertyInfo propertyInfo, TListType listToUse, ToStringFormatProperties formatProperties)
            where TListType : IEnumerable <TValueType>, IList, new()
        {
            string expectedFormatString = GetExpectedToStringValueFormat(propertyInfo);

            string expectedToStringResult = string.Format("{0}{1}{2}", formatProperties.Fields.Prefix, propertyInfo.PropertyInfo.Name, formatProperties.Fields.NameValueSeparator);

            for (int index = 0; index < listToUse.Count; ++index)
            {
                expectedToStringResult += formatProperties.ListItems.GetSeparator(index == 0);

                int currentIndentLevel = 0;
                if (formatProperties.NumberListItems)
                {
                    expectedToStringResult += $"{formatProperties.ListItemHeaders.GetSeparator(index == 0)}{formatProperties.ListItemHeaders.GetIndentString(currentIndentLevel + 1, false)}{formatProperties.ListItemName}{index}{formatProperties.ListItemHeaders.NameValueSeparator}";
                }

                expectedToStringResult += formatProperties.ListItems.GetIndentString(currentIndentLevel + 2, false);
                expectedToStringResult += formatProperties.ListItems.Prefix;
                expectedToStringResult += string.Format(expectedFormatString, listToUse[index]);
                expectedToStringResult += formatProperties.ListItems.Suffix;
            }

            expectedToStringResult += formatProperties.Fields.Suffix;

            return(expectedToStringResult);
        }
示例#6
0
 public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
 {
     if (!messageSerializedPropertyInfo.ContainsLengthAttribute &&
         messageSerializedPropertyInfo.PropertyInfo.Name == "Length")
     {
         messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedLengthResultAttribute());
     }
 }
示例#7
0
        protected void TestField <TNumericType>(MessageSerializedClassInfo classInfo, string propertyName, TNumericType valueToUse, byte[] expectedArray)
            where TNumericType : struct, IComparable <TNumericType>, IConvertible
        {
            MessageSerializedPropertyInfo    propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerBcd <TNumericType> typeSerializer = new TypeSerializerBcd <TNumericType>(propertyInfo);

            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#8
0
        protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, int valueToUse)
        {
            MessageSerializedPropertyInfo propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerJunk            typeSerializer = new TypeSerializerJunk(propertyInfo);

            byte[] expectedArray = new byte[] { 0x33, 0x44, 0x55 };
            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#9
0
        protected void TestListField <TNumericType>(MessageSerializedClassInfo classInfo, string propertyName, List <TNumericType> valueToUse) where TNumericType : struct
        {
            MessageSerializedPropertyInfo        propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerNumeric <TNumericType> typeSerializer = new TypeSerializerNumeric <TNumericType>(propertyInfo);

            byte[] expectedArray = GetExpectedByteArray(valueToUse);
            TestListField <TypeSerializerNumeric <TNumericType>, List <TNumericType>, TNumericType>(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#10
0
        protected void TestField <TEnumType>(MessageSerializedClassInfo classInfo, string propertyName, TEnumType valueToUse) where TEnumType : struct, IConvertible
        {
            MessageSerializedPropertyInfo  propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerEnum <TEnumType> typeSerializer = new TypeSerializerEnum <TEnumType>(propertyInfo);

            byte[] expectedArray = GetExpectedByteArray(valueToUse);

            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#11
0
            public Type CheckType(MessageSerializedPropertyInfo propertyInfo)
            {
                if (propertyInfo.PropertyInfo.PropertyType == typeof(int) &&
                    propertyInfo.PropertyInfo.Name.StartsWith("ThreeByte"))
                {
                    return(typeof(TypeSerializerThreeByteNumeric));
                }

                return(null);
            }
示例#12
0
        protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, byte[] valueToUse, int expectedOutputLength)
        {
            MessageSerializedPropertyInfo propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerByteArray       typeSerializer = new TypeSerializerByteArray(propertyInfo);

            //int outputLength = CalculateOutputLength(propertyInfo, valueToUse);
            byte[] expectedArray = (byte[])valueToUse.Clone();
            Array.Resize(ref expectedArray, expectedOutputLength);

            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#13
0
        protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, DateTime valueToUse)
        {
            MessageSerializedPropertyInfo propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerDateTime        typeSerializer = new TypeSerializerDateTime(propertyInfo);

            string format         = propertyInfo.MessagePropertyAttribute.Format;
            string formattedValue = valueToUse.ToString(format);

            // TODO: This uses the same method as the class which is not really what we want
            byte[] expectedArray = ArrayOps.GetBcdBytes(Convert.ToUInt64(formattedValue), GetOutputLength(format));

            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#14
0
            public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
            {
                if (!messageSerializedPropertyInfo.ContainsAuthenticationAttribute &&
                    messageSerializedPropertyInfo.PropertyInfo.Name.Equals("Hash", StringComparison.InvariantCultureIgnoreCase))
                {
                    messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationSha256)));

                    // Since we know the result of the hash is going to be 32 bytes if the Length property hasn't been set we'll set it.
                    if (!messageSerializedPropertyInfo.MessagePropertyAttribute.IsLengthSpecified)
                    {
                        messageSerializedPropertyInfo.MessagePropertyAttribute.Length = 32;
                    }
                }
            }
示例#15
0
        public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            if (!messageSerializedPropertyInfo.CalculatedFieldAttributes.Any(item => item is CalculatedChecksum5555ResultAttribute || item is CalculatedChecksum5555Attribute))
            {
                if (messageSerializedPropertyInfo.PropertyInfo.Name == "Checksum")
                {
                    messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedChecksum5555ResultAttribute());
                }

                if (messageSerializedPropertyInfo.PropertyInfo.Name == "NumberNotIncludedInChecksum")
                {
                    CalculatedChecksum5555Attribute attribute = new CalculatedChecksum5555Attribute();
                    attribute.Exclude = true;
                    messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(attribute);
                }
            }
        }
示例#16
0
        public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            var messagePropertyAttribute = messageSerializedPropertyInfo.MessagePropertyAttribute;

            if (!messagePropertyAttribute.IsEndiannessSpecified)
            {
                if (classAttribute.EndiannessExplicitlySpecified)
                {
                    messagePropertyAttribute.Endianness = classAttribute.Endianness;
                }
                else if (serializationDefaults != null)
                {
                    messagePropertyAttribute.Endianness = serializationDefaults.Endianness;
                }
                else
                {
                    messagePropertyAttribute.Endianness = Endiannesses.System;
                }
            }
        }
示例#17
0
        protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, string valueToUse)
        {
            MessageSerializedPropertyInfo propertyInfo   = GetPropertyInfo(classInfo, propertyName);
            TypeSerializerString          typeSerializer = new TypeSerializerString(propertyInfo);

            List <byte> list = new List <byte>();

            foreach (char character in valueToUse)
            {
                list.Add((byte)character);
            }

            int outputLength = CalculateOutputLength(propertyInfo, valueToUse);

            if (outputLength < list.Count)
            {
                list.RemoveRange(outputLength, list.Count - outputLength);
            }
            else if (outputLength > list.Count && propertyInfo.MessagePropertyAttribute.Prepad)
            {
                while (outputLength > list.Count)
                {
                    list.Insert(0, (byte)propertyInfo.MessagePropertyAttribute.PrepadCharacter);
                }
            }
            else if (outputLength > list.Count)
            {
                while (outputLength > list.Count)
                {
                    list.Add((byte)0);
                }
            }

            byte[] expectedArray = list.ToArray();
            TestField(typeSerializer, propertyInfo, valueToUse, expectedArray);
        }
示例#18
0
 protected override string GetToStringValue <TValueType>(MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray)
 {
     return(BitConverter.ToString(expectedArray));
 }
示例#19
0
 protected override void CheckToString <TTypeSerializer, TValueType>(TTypeSerializer typeSerializer, MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray)
 {
     base.CheckToString(typeSerializer, propertyInfo, (TValueType)(object)expectedArray, expectedArray);
 }
示例#20
0
        protected override void VerifyDeserialized <TValueType>(TValueType deserializedValue, TValueType expectedValue, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray)
        {
            byte[] deserializedValueArray = (byte[])(object)deserializedValue;

            // In our case the valueArray is what we actually expect things to be since it will already be resized to the proper value
            base.VerifyDeserialized(deserializedValueArray, valueArray, propertyInfo, valueArray);
        }
示例#21
0
        protected virtual void CheckToString <TTypeSerializer, TValueType>(TTypeSerializer typeSerializer, MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray)
            where TTypeSerializer : TypeSerializerBase <TValueType>
        {
            string expectedToStringResult = $"{propertyInfo.PropertyInfo.Name}: " + GetToStringValue(propertyInfo, valueToUse, expectedArray);
            string toStringResult         = typeSerializer.ToString(valueToUse, 0, ToStringFormatProperties.Default, true);

            Assert.That(toStringResult, Is.EqualTo(expectedToStringResult), "ToString");
        }
示例#22
0
        protected virtual string GetToStringValue <TValueType>(MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray)
        {
            string expectedFormatString = GetExpectedToStringValueFormat(propertyInfo);

            return(string.Format(expectedFormatString, valueToUse));
        }
示例#23
0
        protected virtual void TestListField <TTypeSerializer, TListType, TValueType>(TTypeSerializer typeSerializer, MessageSerializedPropertyInfo propertyInfo, TListType listToUse, byte[] expectedArray)
            where TTypeSerializer : TypeSerializerBase <TValueType>
            where TListType : IEnumerable <TValueType>, IList, new()
        {
            byte[] serializedArray = typeSerializer.Serialize(listToUse);
            Assert.That(serializedArray, Is.EqualTo(expectedArray), "Serialize");

            DeserializeStatus status    = new DeserializeStatus();
            int       currentArrayIndex = 0;
            TListType deserializedList  = typeSerializer.DeserializeList <TListType>(expectedArray, ref currentArrayIndex, expectedArray.Length, ref status);

            VerifyDeserializedList <TListType, TValueType>(deserializedList, listToUse, propertyInfo, expectedArray);

            ToStringFormatProperties formatProperties = ToStringFormatProperties.Default;
            string expectedToStringResult             = GetExpectedListToString <TListType, TValueType>(propertyInfo, listToUse, formatProperties);
            string toStringResult = typeSerializer.ToString(listToUse, 0, formatProperties, true);

            Assert.That(toStringResult, Is.EqualTo(expectedToStringResult), "ToString");
        }
示例#24
0
 protected virtual void VerifyDeserializedList <TListType, TValueType>(TListType deserializedList, TListType expectedList, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray)
     where TListType : IEnumerable <TValueType>, IList, new()
 {
     Assert.That(deserializedList.Count, Is.EqualTo(expectedList.Count), "ListCount");
     for (int index = 0; index < deserializedList.Count; ++index)
     {
         VerifyDeserialized(deserializedList[index], expectedList[index], propertyInfo, valueArray);
     }
 }
示例#25
0
 protected virtual void VerifyDeserialized <TValueType>(TValueType deserializedValue, TValueType expectedValue, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray)
 {
     Assert.That(deserializedValue, Is.EqualTo(expectedValue), "Deserialize");
 }
示例#26
0
        protected override void VerifyDeserialized <TValueType>(TValueType deserializedValue, TValueType expectedValue, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray)
        {
            // If fixed length but our original value is longer than the fixed length then we need to cut the expected value down
            string deserializedValueString = $"{deserializedValue}";
            string expectedValueString     = $"{deserializedValue}";

            int outputLength = CalculateOutputLength(propertyInfo, expectedValueString);

            if (outputLength < expectedValueString.Length)
            {
                expectedValueString = expectedValueString.Substring(0, outputLength);
            }
            else if (outputLength > expectedValueString.Length && propertyInfo.MessagePropertyAttribute.Prepad)
            {
                expectedValueString = new string(propertyInfo.MessagePropertyAttribute.PrepadCharacter, outputLength - expectedValueString.Length) + expectedValueString;
            }

            base.VerifyDeserialized(deserializedValueString, expectedValueString, propertyInfo, valueArray);
        }
示例#27
0
 protected override string GetExpectedToStringValueFormat(MessageSerializedPropertyInfo propertyInfo)
 {
     return($"{{0:yyyy-MM-dd HH:mm:ss}}");
 }
示例#28
0
        protected override void VerifyDeserialized <TValueType>(TValueType deserializedValue, TValueType expectedValue, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray)
        {
            // The "Kind" property after deserialization is "Unspecified" so we need to ignore that part
            DateTime expectedDateTime     = (DateTime)(object)expectedValue;
            DateTime deserializedDateTime = DateTime.SpecifyKind((DateTime)(object)deserializedValue, expectedDateTime.Kind);

            base.VerifyDeserialized(deserializedDateTime, expectedDateTime, propertyInfo, valueArray);
        }
示例#29
0
 public TypeSerializerThreeByteNumeric(MessageSerializedPropertyInfo propertyInfo)
     : base(propertyInfo)
 {
 }
示例#30
0
 protected virtual string GetExpectedToStringValueFormat(MessageSerializedPropertyInfo propertyInfo)
 {
     return("{0}");
 }