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); }
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); }
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); }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { if (!messageSerializedPropertyInfo.MessagePropertyAttribute.IsPrepadCharacterSpecified) { messageSerializedPropertyInfo.MessagePropertyAttribute.PrepadCharacter = '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); }
public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute) { if (!messageSerializedPropertyInfo.ContainsLengthAttribute && messageSerializedPropertyInfo.PropertyInfo.Name == "Length") { messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedLengthResultAttribute()); } }
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); }
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); }
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); }
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); }
public Type CheckType(MessageSerializedPropertyInfo propertyInfo) { if (propertyInfo.PropertyInfo.PropertyType == typeof(int) && propertyInfo.PropertyInfo.Name.StartsWith("ThreeByte")) { return(typeof(TypeSerializerThreeByteNumeric)); } return(null); }
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); }
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); }
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; } } }
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); } } }
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; } } }
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); }
protected override string GetToStringValue <TValueType>(MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray) { return(BitConverter.ToString(expectedArray)); }
protected override void CheckToString <TTypeSerializer, TValueType>(TTypeSerializer typeSerializer, MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray) { base.CheckToString(typeSerializer, propertyInfo, (TValueType)(object)expectedArray, expectedArray); }
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); }
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"); }
protected virtual string GetToStringValue <TValueType>(MessageSerializedPropertyInfo propertyInfo, TValueType valueToUse, byte[] expectedArray) { string expectedFormatString = GetExpectedToStringValueFormat(propertyInfo); return(string.Format(expectedFormatString, valueToUse)); }
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"); }
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); } }
protected virtual void VerifyDeserialized <TValueType>(TValueType deserializedValue, TValueType expectedValue, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray) { Assert.That(deserializedValue, Is.EqualTo(expectedValue), "Deserialize"); }
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); }
protected override string GetExpectedToStringValueFormat(MessageSerializedPropertyInfo propertyInfo) { return($"{{0:yyyy-MM-dd HH:mm:ss}}"); }
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); }
public TypeSerializerThreeByteNumeric(MessageSerializedPropertyInfo propertyInfo) : base(propertyInfo) { }
protected virtual string GetExpectedToStringValueFormat(MessageSerializedPropertyInfo propertyInfo) { return("{0}"); }