Beispiel #1
0
        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
                );
        }
        // 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
                );
        }
Beispiel #4
0
 // 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
         );
 }
Beispiel #5
0
        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));
        }
 // 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
         );
 }
Beispiel #7
0
        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*$")));
        }