static IXunitSerializable DeserializeSerializable(Type type, string serializedValue) { var serializationInfo = new XunitSerializationInfo(); // Will end up with an empty string if the serialization type did not serialize any data if (serializedValue != string.Empty) { var elements = FromBase64(serializedValue).Split('\n'); foreach (var element in elements) { var triple = DeserializeTriple(element); serializationInfo.data[triple.Key] = triple; } } try { var value = (IXunitSerializable)Activator.CreateInstance(type); value.Deserialize(serializationInfo); return(value); } catch (MissingMemberException) { throw new InvalidOperationException($"Could not de-serialize type '{type.FullName}' because it lacks a parameterless constructor."); } }
/// <inheritdoc/> public void SetData(XunitSerializationInfo info) { TestClass = info.GetValue<ITestClass>("TestClass"); var methodName = info.GetString("MethodName"); Method = TestClass.Class.GetMethod(methodName, includePrivateMethod: false); }
/// <inheritdoc/> public void SetData(XunitSerializationInfo info) { TestCollection = info.GetValue<ITestCollection>("TestCollection"); var assemblyName = info.GetString("ClassAssemblyName"); var typeName = info.GetString("ClassTypeName"); Class = Reflector.Wrap(Reflector.GetType(assemblyName, typeName)); }
/// <inheritdoc/> public void SetData(XunitSerializationInfo info) { var assemblyPath = info.GetString("AssemblyPath"); var assembly = System.Reflection.Assembly.Load(new AssemblyName { Name = Path.GetFileNameWithoutExtension(assemblyPath) }); ConfigFileName = info.GetString("ConfigFileName"); Assembly = Reflector.Wrap(assembly); }
public void SetData(XunitSerializationInfo info) { var len = info.GetValue <int>("Length"); var arrTypeStr = info.GetString("ElementType"); var arrType = arrTypeStr != null?Type.GetType(arrTypeStr) : typeof(object); array = (object[])Array.CreateInstance(arrType, len); for (var i = 0; i < array.Length; i++) { array[i] = info.GetValue("Item" + i, arrType); } }
public void GetData(XunitSerializationInfo info) { info.AddValue("Length", array.Length); if (array.Length > 0) { info.AddValue("ElementType", array[0].GetType().AssemblyQualifiedName); } for (var i = 0; i < array.Length; i++) { info.AddValue("Item" + i, array[i]); } }
static IXunitSerializable DeserializeSerializable(Type type, string serializedValue) { var serializationInfo = new XunitSerializationInfo(); var elements = FromBase64(serializedValue).Split('\n'); foreach (var element in elements) { var triple = DeserializeTriple(element); serializationInfo.data[triple.Key] = triple; } var value = (IXunitSerializable)Activator.CreateInstance(type); value.Deserialize(serializationInfo); return(value); }
/// <inheritdoc/> public void GetData(XunitSerializationInfo info) { info.AddValue("DisplayName", DisplayName); info.AddValue("TestAssembly", TestAssembly); info.AddValue("UniqueID", UniqueID.ToString()); if (CollectionDefinition != null) { info.AddValue("DeclarationAssemblyName", CollectionDefinition.Assembly.Name); info.AddValue("DeclarationTypeName", CollectionDefinition.Name); } else { info.AddValue("DeclarationAssemblyName", null); info.AddValue("DeclarationTypeName", null); } }
/// <summary> /// Serializes an object. /// </summary> /// <param name="value">The value to be serialized</param> /// <returns>The serialized object</returns> public static string Serialize(object value) { if (value == null) { return(""); } var getTypeData = value as IGetTypeData; if (getTypeData != null) { var info = new XunitSerializationInfo(); getTypeData.GetData(info); return(info.ToSerializedString()); } var stringData = value as string; if (stringData != null) { return(ToBase64(stringData)); } var nullableIntData = value as int?; if (nullableIntData != null) { return(nullableIntData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var array = value as object[]; if (array != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(array); arraySer.GetData(info); return(info.ToSerializedString()); } throw new ArgumentException("We don't know how to serialize type " + value.GetType().FullName, "value"); }
static IXunitSerializable DeserializeSerializable(Type type, string serializedValue) { var serializationInfo = new XunitSerializationInfo(); var elements = FromBase64(serializedValue).Split('\n'); foreach (var element in elements) { var pieces = element.Split(new[] { ':' }, 3); if (pieces.Length != 3) { throw new ArgumentException("Could not split element into 3 pieces: " + element); } var pieceType = Type.GetType(pieces[1]); serializationInfo.data[pieces[0]] = new SerializationData(Deserialize(pieceType, pieces[2]), pieceType); } var value = (IXunitSerializable)Activator.CreateInstance(type); value.Deserialize(serializationInfo); return(value); }
/// <inheritdoc/> public void GetData(XunitSerializationInfo info) { info.AddValue("FileName", FileName); info.AddValue("LineNumber", LineNumber, typeof(int?)); }
/// <inheritdoc/> public void SetData(XunitSerializationInfo info) { DisplayName = info.GetString("DisplayName"); TestAssembly = info.GetValue<ITestAssembly>("TestAssembly"); UniqueID = Guid.Parse(info.GetString("UniqueID")); var assemblyName = info.GetString("DeclarationAssemblyName"); var typeName = info.GetString("DeclarationTypeName"); if (!String.IsNullOrWhiteSpace(assemblyName) && String.IsNullOrWhiteSpace(typeName)) CollectionDefinition = Reflector.Wrap(Reflector.GetType(assemblyName, typeName)); }
/// <summary> /// Serializes an object. /// </summary> /// <param name="value">The value to be serialized</param> /// <returns>The serialized object</returns> public static string Serialize(object value) { if (value == null) { return(null); } var serializable = value as IXunitSerializable; if (serializable != null) { var info = new XunitSerializationInfo(); serializable.Serialize(info); return(info.ToSerializedString()); } var stringData = value as string; if (stringData != null) { return(ToBase64(stringData)); } var intData = value as int?; if (intData != null) { return(intData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var longData = value as long?; if (longData != null) { return(longData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var floatData = value as float?; if (floatData != null) { return(floatData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var doubleData = value as double?; if (doubleData != null) { return(doubleData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var decimalData = value as decimal?; if (decimalData != null) { return(decimalData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var booleanData = value as bool?; if (booleanData != null) { return(booleanData.GetValueOrDefault().ToString()); } var datetimeData = value as DateTime?; if (datetimeData != null) { return(datetimeData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture)); // Round-trippable format } var datetimeoffsetData = value as DateTimeOffset?; if (datetimeoffsetData != null) { return(datetimeoffsetData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture)); // Round-trippable format } var typeData = value as Type; if (typeData != null) { return(SerializationHelper.GetTypeNameForSerialization(typeData)); } if (value.GetType().IsEnum()) { return(value.ToString()); } var array = value as object[]; if (array != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(array); arraySer.Serialize(info); return(info.ToSerializedString()); } throw new ArgumentException("We don't know how to serialize type " + value.GetType().FullName, "value"); }
static IGetTypeData DeserializeGetTypeData(Type type, string serializedValue) { var serializationInfo = new XunitSerializationInfo(); var elements = FromBase64(serializedValue).Split('\n'); foreach (var element in elements) { var pieces = element.Split(new[] { ':' }, 3); if (pieces.Length != 3) throw new ArgumentException("Could not split element into 3 pieces: " + element); var pieceType = Type.GetType(pieces[1]); serializationInfo.data[pieces[0]] = Tuple.Create(Deserialize(pieceType, pieces[2]), pieceType); } var value = (IGetTypeData)Activator.CreateInstance(type); value.SetData(serializationInfo); return value; }
/// <summary> /// Serializes an object. /// </summary> /// <param name="value">The value to be serialized</param> /// <returns>The serialized object</returns> public static string Serialize(object value) { if (value == null) { return(""); } var serializable = value as IXunitSerializable; if (serializable != null) { var info = new XunitSerializationInfo(); serializable.Serialize(info); return(info.ToSerializedString()); } var stringData = value as string; if (stringData != null) { return(ToBase64(stringData)); } var intData = value as int?; if (intData != null) { return(intData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var longData = value as long?; if (longData != null) { return(longData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var floatData = value as float?; if (floatData != null) { return(floatData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var doubleData = value as double?; if (doubleData != null) { return(doubleData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var decimalData = value as decimal?; if (decimalData != null) { return(decimalData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var array = value as object[]; if (array != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(array); arraySer.Serialize(info); return(info.ToSerializedString()); } throw new ArgumentException("We don't know how to serialize type " + value.GetType().FullName, "value"); }
/// <inheritdoc/> public void GetData(XunitSerializationInfo info) { info.AddValue("AssemblyPath", Assembly.AssemblyPath); info.AddValue("ConfigFileName", ConfigFileName); }
/// <summary> /// Serializes an object. /// </summary> /// <param name="value">The value to be serialized</param> /// <returns>The serialized object</returns> public static string Serialize(object value) { if (value == null) return ""; var getTypeData = value as IGetTypeData; if (getTypeData != null) { var info = new XunitSerializationInfo(); getTypeData.GetData(info); return info.ToSerializedString(); } var stringData = value as string; if (stringData != null) return ToBase64(stringData); var nullableIntData = value as int?; if (nullableIntData != null) return nullableIntData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture); var array = value as object[]; if (array != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(array); arraySer.GetData(info); return info.ToSerializedString(); } throw new ArgumentException("We don't know how to serialize type " + value.GetType().FullName, "value"); }
/// <inheritdoc/> public void GetData(XunitSerializationInfo info) { info.AddValue("MethodName", Method.Name); info.AddValue("TestClass", TestClass); }
public void GetData(XunitSerializationInfo info) { info.AddValue("Length", array.Length); if (array.Length > 0) info.AddValue("ElementType", array[0].GetType().AssemblyQualifiedName); for (var i = 0; i < array.Length; i++) info.AddValue("Item" + i, array[i]); }
/// <summary> /// Serializes an object. /// </summary> /// <param name="value">The value to be serialized.</param> /// <returns>The serialized object.</returns> public static string Serialize(object value) { if (value == null) { return(null); } if (value is IXunitSerializable serializable) { var info = new XunitSerializationInfo(); serializable.Serialize(info); return(info.ToSerializedString()); } var charData = value as char?; if (charData != null) { return(((ushort)charData.GetValueOrDefault()).ToString(CultureInfo.InvariantCulture)); } if (value is string stringData) { return(ToBase64(stringData)); } var byteData = value as byte?; if (byteData != null) { return(byteData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var sbyteData = value as sbyte?; if (sbyteData != null) { return(sbyteData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var ushortData = value as ushort?; if (ushortData != null) { return(ushortData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var shortData = value as short?; if (shortData != null) { return(shortData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var uintData = value as uint?; if (uintData != null) { return(uintData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var intData = value as int?; if (intData != null) { return(intData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var ulongData = value as ulong?; if (ulongData != null) { return(ulongData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var longData = value as long?; if (longData != null) { return(longData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var floatData = value as float?; if (floatData != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(BitConverter.GetBytes(floatData.GetValueOrDefault())); arraySer.Serialize(info); return(info.ToSerializedString()); } var doubleData = value as double?; if (doubleData != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(BitConverter.GetBytes(doubleData.GetValueOrDefault())); arraySer.Serialize(info); return(info.ToSerializedString()); } var decimalData = value as decimal?; if (decimalData != null) { return(decimalData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } #pragma warning disable CA1305 var booleanData = value as bool?; if (booleanData != null) { return(booleanData.GetValueOrDefault().ToString()); } #pragma warning restore CA1305 var datetimeData = value as DateTime?; if (datetimeData != null) { return(datetimeData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture)); // Round-trippable format } var datetimeoffsetData = value as DateTimeOffset?; if (datetimeoffsetData != null) { return(datetimeoffsetData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture)); // Round-trippable format } var typeData = value as Type; if (typeData != null) { return(SerializationHelper.GetTypeNameForSerialization(typeData)); } var valueType = value.GetType(); if (valueType.IsEnum()) { if (!valueType.IsFromLocalAssembly()) { throw new ArgumentException($"We cannot serialize enum {valueType.FullName}.{value} because it lives in the GAC", nameof(value)); } return(value.ToString()); } if (value is Array arrayData) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(arrayData); arraySer.Serialize(info); return(info.ToSerializedString()); } throw new ArgumentException($"We don't know how to serialize type {valueType.FullName}", nameof(value)); }
public void SetData(XunitSerializationInfo info) { var len = info.GetValue<int>("Length"); var arrTypeStr = info.GetString("ElementType"); var arrType = arrTypeStr != null ? Type.GetType(arrTypeStr) : typeof(object); array = (object[])Array.CreateInstance(arrType, len); for (var i = 0; i < array.Length; i++) array[i] = info.GetValue("Item" + i, arrType); }
/// <inheritdoc/> public void GetData(XunitSerializationInfo info) { info.AddValue("TestCollection", TestCollection); info.AddValue("ClassAssemblyName", Class.Assembly.Name); info.AddValue("ClassTypeName", Class.Name); }
/// <summary> /// Serializes an object. /// </summary> /// <param name="value">The value to be serialized</param> /// <returns>The serialized object</returns> public static string Serialize(object value) { if (value == null) { return(null); } var serializable = value as IXunitSerializable; if (serializable != null) { var info = new XunitSerializationInfo(); serializable.Serialize(info); return(info.ToSerializedString()); } var charData = value as char?; if (charData != null) { return(((ushort)charData.GetValueOrDefault()).ToString(CultureInfo.InvariantCulture)); } var stringData = value as string; if (stringData != null) { return(ToBase64(stringData)); } var byteData = value as byte?; if (byteData != null) { return(byteData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var ushortData = value as ushort?; if (ushortData != null) { return(ushortData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var shortData = value as short?; if (shortData != null) { return(shortData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var uintData = value as uint?; if (uintData != null) { return(uintData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var intData = value as int?; if (intData != null) { return(intData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var ulongData = value as ulong?; if (ulongData != null) { return(ulongData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var longData = value as long?; if (longData != null) { return(longData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var floatData = value as float?; if (floatData != null) { return(floatData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var doubleData = value as double?; if (doubleData != null) { return(doubleData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var decimalData = value as decimal?; if (decimalData != null) { return(decimalData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); } var booleanData = value as bool?; if (booleanData != null) { return(booleanData.GetValueOrDefault().ToString()); } var datetimeData = value as DateTime?; if (datetimeData != null) { return(datetimeData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture)); // Round-trippable format } var datetimeoffsetData = value as DateTimeOffset?; if (datetimeoffsetData != null) { return(datetimeoffsetData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture)); // Round-trippable format } var typeData = value as Type; if (typeData != null) { if (!typeData.IsFromLocalAssembly()) { throw new ArgumentException(String.Format("We cannot serialize type {0} because it lives in the GAC", typeData.FullName), "value"); } return(SerializationHelper.GetTypeNameForSerialization(typeData)); } var valueType = value.GetType(); if (valueType.IsEnum()) { if (!valueType.IsFromLocalAssembly()) { throw new ArgumentException(String.Format("We cannot serialize enum {0}.{1} because it lives in the GAC", valueType.FullName, value), "value"); } return(value.ToString()); } var arrayData = value as Array; if (arrayData != null) { var info = new XunitSerializationInfo(); var arraySer = new ArraySerializer(arrayData); arraySer.Serialize(info); return(info.ToSerializedString()); } throw new ArgumentException("We don't know how to serialize type " + valueType.FullName, "value"); }
/// <inheritdoc/> public void SetData(XunitSerializationInfo info) { FileName = info.GetString("FileName"); LineNumber = (int?)info.GetValue("LineNumber", typeof(int?)); }