Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft
TestCommon
DataSets
Types
GenericTypeAssert.cs
HttpAssert.cs
MediaTypeAssert.cs
MediaTypeHeaderValueComparer.cs
ParsedMediaTypeHeaderValue.cs
RegexReplacement.cs
RuntimeEnvironment.cs
SerializerAssert.cs
StreamAssert.cs
TaskAssert.cs
TestDataSetAttribute.cs
TimeoutConstant.cs
TypeAssert.cs
XmlAssert.cs
Properties
AppDomainUtils.cs
AssertEx.cs
CultureReplacer.cs
DefaultTimeoutFactAttribute.cs
DefaultTimeoutTheoryAttribute.cs
DictionaryEqualityComparer.cs
ExceptionAssertions.cs
ForceGCAttribute.cs
MemberHelper.cs
Microsoft.TestCommon.csproj
PartialTrustRunner.cs
PreAppStartTestHelper.cs
PreserveSyncContextAttribute.cs
ReflectionAssert.cs
RestoreThreadPrincipalAttribute.cs
TaskExtensions.cs
TestFile.cs
TestHelper.cs
TheoryDataSet.cs
ThreadPoolSyncContext.cs
WebUtils.cs
packages.config
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h

153 lines
6.3 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Xml.Serialization;
namespace Microsoft.TestCommon
{
/// <summary>
/// MSTest utility for testing code operating against a stream.
/// </summary>
public class SerializerAssert
{
private static SerializerAssert singleton = new SerializerAssert();
public static SerializerAssert Singleton { get { return singleton; } }
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="XmlSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingXmlSerializer(Type type, object objectInstance, Action<Stream> codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
XmlSerializer serializer = new XmlSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="XmlSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <typeparam name="T">The type to serialize.</typeparam>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingXmlSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
{
UsingXmlSerializer(typeof(T), objectInstance, codeThatChecks);
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingDataContractSerializer(Type type, object objectInstance, Action<Stream> codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
DataContractSerializer serializer = new DataContractSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <typeparam name="T">The type to serialize.</typeparam>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingDataContractSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
{
UsingDataContractSerializer(typeof(T), objectInstance, codeThatChecks);
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractJsonSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public static void UsingDataContractJsonSerializer(Type type, object objectInstance, Action<Stream> codeThatChecks)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (codeThatChecks == null)
{
throw new ArgumentNullException("codeThatChecks");
}
DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, objectInstance);
stream.Flush();
stream.Seek(0L, SeekOrigin.Begin);
codeThatChecks(stream);
}
}
/// <summary>
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
/// <see cref="DataContractJsonSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
/// </summary>
/// <typeparam name="T">The type to serialize.</typeparam>
/// <param name="objectInstance">The value to serialize.</param>
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
public void UsingDataContractJsonSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
{
UsingDataContractJsonSerializer(typeof(T), objectInstance, codeThatChecks);
}
}
}