public void TestGenerateAssemblyFile_WithDirectory_DllIsGeneratedOnSpecifiedDirectory() { var name = new AssemblyName(MethodBase.GetCurrentMethod().Name); var target = new SerializerGenerator(typeof(GeneratorTestObject), name); var directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); target.GenerateAssemblyFile(directory); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); try { TestOnWorkerAppDomain( Path.Combine(directory, name.Name + ".dll"), PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte )'A' }, TestType.GeneratorTestObject ); } finally { Directory.Delete(directory, true); } }
public void TestGenerateAssemblyFile_WithMethod_OptionsAreAsSpecified() { var name = new AssemblyName(MethodBase.GetCurrentMethod().Name); var target = new SerializerGenerator(typeof(GeneratorTestObject), name); target.Method = SerializationMethod.Map; var filePath = Path.GetFullPath("." + Path.DirectorySeparatorChar + name.Name + ".dll"); target.GenerateAssemblyFile(); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); try { TestOnWorkerAppDomain( filePath, PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedMap + 1, MessagePackCode.MinimumFixedRaw + 3, ( byte )'V', ( byte )'a', ( byte )'l', MessagePackCode.MinimumFixedRaw + 1, ( byte )'A' }, TestType.GeneratorTestObject ); } finally { File.Delete(filePath); } }
public void TestGenerateAssembly_WithPackerOption_OptionsAreAsSpecified() { var name = new AssemblyName(MethodBase.GetCurrentMethod().Name); var filePath = Path.GetFullPath("." + Path.DirectorySeparatorChar + name.Name + ".dll"); var result = SerializerGenerator.GenerateAssembly( new SerializerAssemblyGenerationConfiguration { AssemblyName = name }, typeof(GeneratorTestObject) ); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); Assert.That(result, Is.EqualTo(filePath)); try { TestOnWorkerAppDomain( filePath, PackerCompatibilityOptions.None, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.Bin8, 1, ( byte )'A' }, TestType.GeneratorTestObject ); } finally { File.Delete(filePath); } }
public void TestGenerateCode_WithDefault_CSFileGeneratedOnAppBase() { var filePathCS = Path.GetFullPath( String.Join( Path.DirectorySeparatorChar.ToString(), new [] { ".", "MsgPack", "Serialization", "GeneratedSerializers", IdentifierUtility.EscapeTypeName(typeof(GeneratorTestObject)) + "Serializer.cs" } ) ); var resultCS = SerializerGenerator.GenerateCode( typeof(GeneratorTestObject) ); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); Assert.That(resultCS.Single(), Is.EqualTo(filePathCS)); var linesCS = File.ReadAllLines(filePathCS); // BracingStyle, IndentString Assert.That(!linesCS.Any(l => Regex.IsMatch(l, @"^\t+if.+\{\s*$"))); // Nemespace Assert.That(linesCS.Any(l => Regex.IsMatch(l, @"^\s*namespace\s+MsgPack\.Serialization\.GeneratedSerializers\s+"))); // Array Assert.That(linesCS.Any(l => Regex.IsMatch(l, @"\.PackArrayHeader"))); }
// To test generated serializers, enable this test and run, then copy bin/Debug/MsgPack contents to project's Serialization/GeneratedSerializers/ directory. //[Test] public void GenerateFiles() { // Note: if you face to PathTooLongException here, specify OutputDirectory of bellow object initializers. SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers.ArrayBased", SerializationMethod = SerializationMethod.Array, //OutputDirectory = "\\temp-gen", IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo }, PreGeneratedSerializerActivator.KnownTypes ); SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers.MapBased", SerializationMethod = SerializationMethod.Map, //OutputDirectory = "\\temp-gen", IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo }, PreGeneratedSerializerActivator.KnownTypes ); }
public void TestComplexType_ChildGeneratorsAreContainedAutomatically() { var name = new AssemblyName(MethodBase.GetCurrentMethod().Name); var target = new SerializerGenerator(typeof(RootGeneratorTestObject), name); var directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); target.GenerateAssemblyFile(directory); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(RootGeneratorTestObject)), Is.False); try { TestOnWorkerAppDomain( Path.Combine(directory, "." + Path.DirectorySeparatorChar + name.Name + ".dll"), PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 2, MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte )'A', MessagePackCode.NilValue }, TestType.RootGeneratorTestObject ); } finally { Directory.Delete(directory, true); } }
// To test generated serializers, enable this test and run, then copy bin/Debug/MsgPack contents to project's Serialization/GeneratedSerializers/ directory. //[Test] public void GenerateFiles() { // Note: if you face to PathTooLongException here, specify OutputDirectory of bellow object initializers. SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers", #if NETFX_35 OutputDirectory = "\\temp-gen35", #else OutputDirectory = "\\temp-gen", #endif IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo }, PreGeneratedSerializerActivator.KnownTypes ); }
// To test generated serializers, enable this test and run, then copy bin/Debug/MsgPack contents to project's Serialization/GeneratedSerializers/ directory. // [Test] public void GenerateFiles() { SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers.ArrayBased", SerializationMethod = SerializationMethod.Array }, PreGeneratedSerializerActivator.KnownTypes ); SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers.MapBased", SerializationMethod = SerializationMethod.Map }, PreGeneratedSerializerActivator.KnownTypes ); }
public void TestGenerateCode_ComplexType_MultipleTypes() { var filePathCS1 = Path.GetFullPath( String.Join( Path.DirectorySeparatorChar.ToString(), new [] { ".", "MsgPack", "Serialization", "GeneratedSerializers", IdentifierUtility.EscapeTypeName(typeof(GeneratorTestObject)) + "Serializer.cs" } ) ); var filePathCS2 = Path.GetFullPath( String.Join( Path.DirectorySeparatorChar.ToString(), new [] { ".", "MsgPack", "Serialization", "GeneratedSerializers", IdentifierUtility.EscapeTypeName(typeof(AnotherGeneratorTestObject)) + "Serializer.cs" } ) ); var resultCS = SerializerGenerator.GenerateCode( typeof(GeneratorTestObject), typeof(AnotherGeneratorTestObject) ).ToArray(); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); Assert.That(SerializationContext.Default.ContainsSerializer(typeof(AnotherGeneratorTestObject)), Is.False); Assert.That(SerializationContext.Default.ContainsSerializer(typeof(RootGeneratorTestObject)), Is.False); Assert.That(resultCS.Length, Is.EqualTo(2)); Assert.That(resultCS, Contains.Item(filePathCS1).And.Contains(filePathCS2)); }
public void TestGenerateAssembly_ComplexType_MultipleTypes() { var name = new AssemblyName(MethodBase.GetCurrentMethod().Name); var directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); var filePath = Path.Combine(directory, name.Name + ".dll"); var result = SerializerGenerator.GenerateAssembly( new SerializerAssemblyGenerationConfiguration { AssemblyName = name, OutputDirectory = directory }, typeof(GeneratorTestObject), typeof(AnotherGeneratorTestObject) ); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); Assert.That(SerializationContext.Default.ContainsSerializer(typeof(AnotherGeneratorTestObject)), Is.False); Assert.That(result, Is.EqualTo(filePath)); try { TestOnWorkerAppDomainForMultiple( filePath, PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte )'A', }, new byte[] { ( byte )'B' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte )'B', } ); } finally { Directory.Delete(directory, true); } }
public void Issue145() { var results = SerializerGenerator.GenerateSerializerSourceCodes( new SerializerCodeGenerationConfiguration { EnumSerializationMethod = EnumSerializationMethod.ByUnderlyingValue, IsRecursive = true, OutputDirectory = Path.GetTempPath(), WithNullableSerializers = false, PreferReflectionBasedSerializer = true, SerializationMethod = SerializationMethod.Array }, typeof(Issue145Class) ).ToArray(); foreach (var result in results) { File.Delete(result.FilePath); } }
// To test generated serializers, enable this test and run, then copy bin/Debug/MsgPack contents to project's Serialization/GeneratedSerializers/ directory. // [Test] public void GenerateFiles() { SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers.ArrayBased", SerializationMethod = SerializationMethod.Array, IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo }, PreGeneratedSerializerActivator.KnownTypes ); SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Namespace = "MsgPack.Serialization.GeneratedSerializers.MapBased", SerializationMethod = SerializationMethod.Map, IsInternalToMsgPackLibrary = true // because of InternalsVisibleTo }, PreGeneratedSerializerActivator.KnownTypes ); }
public void TestGenerateAssemblyFile_WithDirectory_DllIsGeneratedOnSpecifiedDirectory() { var name = new AssemblyName( MethodBase.GetCurrentMethod().Name ); var target = new SerializerGenerator( typeof( GeneratorTestObject ), name ); var directory = Path.Combine( Path.GetTempPath(), Guid.NewGuid().ToString() ); target.GenerateAssemblyFile( directory ); // Assert is not polluted. Assert.That( SerializationContext.Default.ContainsSerializer( typeof( GeneratorTestObject ) ), Is.False ); try { TestOnWorkerAppDomain( Path.Combine( directory, name.Name + ".dll" ), PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte )'A' }, TestType.GeneratorTestObject ); } finally { Directory.Delete( directory, true ); } }
public void TestGenerateAssemblyFile_WithDefault_DllIsGeneratedOnAppBase() { var name = new AssemblyName( MethodBase.GetCurrentMethod().Name ); var target = new SerializerGenerator( typeof( GeneratorTestObject ), name ); var filePath = Path.GetFullPath( "." + Path.DirectorySeparatorChar + name.Name + ".dll" ); target.GenerateAssemblyFile(); // Assert is not polluted. Assert.That( SerializationContext.Default.ContainsSerializer( typeof( GeneratorTestObject ) ), Is.False ); try { TestOnWorkerAppDomain( filePath, PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte )'A' }, TestType.GeneratorTestObject ); } finally { File.Delete( filePath ); } }
public void TestGenerateAssemblyFile_ComplexType_MultipleTypes() { var name = new AssemblyName( MethodBase.GetCurrentMethod().Name ); var target = new SerializerGenerator( name ); target.TargetTypes.Add( typeof( GeneratorTestObject ) ); target.TargetTypes.Add( typeof( AnotherGeneratorTestObject ) ); var directory = Path.Combine( Path.GetTempPath(), Guid.NewGuid().ToString() ); target.GenerateAssemblyFile( directory ); // Assert is not polluted. Assert.That( SerializationContext.Default.ContainsSerializer( typeof( GeneratorTestObject ) ), Is.False ); Assert.That( SerializationContext.Default.ContainsSerializer( typeof( AnotherGeneratorTestObject ) ), Is.False ); try { TestOnWorkerAppDomainForMultiple( Path.Combine( directory, "." + Path.DirectorySeparatorChar + name.Name + ".dll" ), PackerCompatibilityOptions.Classic, new byte[] { ( byte )'A' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte ) 'A', }, new byte[] { ( byte )'B' }, new byte[] { MessagePackCode.MinimumFixedArray + 1, MessagePackCode.MinimumFixedRaw + 1, ( byte ) 'B', } ); } finally { Directory.Delete( directory, true ); } }
public void TestGeneratCode_WithOptions_OptionsAreValid() { var directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); var filePathCS = String.Join( Path.DirectorySeparatorChar.ToString(), new [] { directory, "Test", IdentifierUtility.EscapeTypeName(typeof(GeneratorTestObject)) + "Serializer.cs" } ); var resultCS = SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { CodeIndentString = "\t", Namespace = "Test", SerializationMethod = SerializationMethod.Map, OutputDirectory = directory, }, typeof(GeneratorTestObject) ); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); Assert.That(resultCS.Single(), Is.EqualTo(filePathCS)); Console.WriteLine(File.ReadAllText(filePathCS)); var linesCS = File.ReadAllLines(filePathCS); // BracingStyle, IndentString Assert.That(linesCS.Any(l => Regex.IsMatch(l, @"^\t+[^\{\s]+.+\{\s*$"))); // Nemespace Assert.That(linesCS.Any(l => Regex.IsMatch(l, @"^\s*namespace\s+Test\s+"))); // Map Assert.That(linesCS.Any(l => Regex.IsMatch(l, @"\.PackMapHeader"))); // Language var filePathVB = String.Join( Path.DirectorySeparatorChar.ToString(), new [] { directory, "MsgPack", "Serialization", "GeneratedSerializers", IdentifierUtility.EscapeTypeName(typeof(GeneratorTestObject)) + "Serializer.vb" } ); var resultVB = SerializerGenerator.GenerateCode( new SerializerCodeGenerationConfiguration { Language = "VB", OutputDirectory = directory, }, typeof(GeneratorTestObject) ); // Assert is not polluted. Assert.That(SerializationContext.Default.ContainsSerializer(typeof(GeneratorTestObject)), Is.False); Assert.That(resultVB.Single(), Is.EqualTo(filePathVB)); var linesVB = File.ReadAllLines(filePathVB); // CheckVB Assert.That(linesVB.Any(l => Regex.IsMatch(l, @"^\s*End Sub\s*$"))); }