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")));
        }
Beispiel #2
0
        public static TypeBuilder NewTypeBuilder(Type targetType)
        {
            if (_moduleBuilder == null)
            {
                throw new InvalidOperationException("PrepareDump() was not called.");
            }

            return
                (_moduleBuilder.DefineType(IdentifierUtility.EscapeTypeName(targetType) + "SerializerLogics"));
        }
Beispiel #3
0
        public static void ResolveTypeName(
            bool useSequence,
            Type targetType,
            string callerNamespace,
            out string serializerTypeName,
            out string serializerTypeNamespace
            )
        {
            var sequence = useSequence ? Interlocked.Increment(ref _serializerNameSequence) : default(int?);

            serializerTypeName      = IdentifierUtility.EscapeTypeName(targetType) + "Serializer" + sequence;
            serializerTypeNamespace = callerNamespace + Delimiter + "Generated";
        }
        private static Dictionary <Type, MessagePackSerializerProvider> InitializeSerializers(IList <Type> knownTypes)
        {
            var result = new Dictionary <Type, MessagePackSerializerProvider>(knownTypes.Count);

            foreach (var knownType in knownTypes)
            {
                if (knownType.GetIsInterface() || knownType.GetIsAbstract())
                {
                    // skip
                    continue;
                }

                var serializerTypeName =
                    "MsgPack.Serialization.GeneratedSerializers." + IdentifierUtility.EscapeTypeName(knownType) + "Serializer";
                var serializerType = typeof(PreGeneratedSerializerActivator).GetAssembly().GetType(serializerTypeName);

                Type type = knownType;

                var serializer =
                    new LazyMessagePackSerializerProvider(
                        knownType,
                        serializerType != null
                                                        ? new Func <SerializationContext, PolymorphismSchema, MessagePackSerializer>(SerializerActivator.Create(knownType, serializerType, knownType).Activate)
                                                        : ((c, s) =>
                {
                    throw new Exception(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            "Pre-generated serializer '{0}' for type '{1}' does not exist in this project.",
                            serializerTypeName,
                            type
                            )
                        );
                }
                                                           )
                        );
                try
                {
                    result.Add(knownType, serializer);
                }
                catch (ArgumentException)
                {
                    // ReSharper disable once NotResolvedInText
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Key '{0}' is already added.", knownType), "key");
                }
            }

            return(result);
        }
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));
        }
Beispiel #6
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*$")));
        }