public void CodeGen_CustomAttrGen_CtrSelectorTest()
        {
            using (AssemblyGenerator asmGen = new AssemblyGenerator("CG_CUST_ATTR_CS", /*isCSharp*/ true, /*useFullTypeNames*/ false, new Type[] { typeof(DummyDomainService) }))
            {
                // Force the Attribute types to be shared
                asmGen.MockSharedCodeService.AddSharedType(typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib1));
                asmGen.MockSharedCodeService.AddSharedType(typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib2));
                asmGen.MockSharedCodeService.AddSharedType(typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib3));
                asmGen.MockSharedCodeService.AddSharedType(typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib4));

                asmGen.ReferenceAssemblies.Add(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName);

                string generatedCode = asmGen.GeneratedCode;
                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "Failed to generate code:\r\n" + asmGen.ConsoleLogger.Errors);

                Assembly assy = asmGen.GeneratedAssembly;
                Assert.IsNotNull(assy, "Assembly failed to build: " + asmGen.ConsoleLogger.Errors);

                Type clientEntityType = asmGen.GetGeneratedType(typeof(DummyEntityForAttribTest).FullName);

                MemberInfo[] prop1 = clientEntityType.GetMember("Prop1");
                IList <CustomAttributeData> cads1 = AssemblyGenerator.GetCustomAttributeData(prop1[0], typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib1));
                Assert.AreEqual(1, cads1.Count, "Expected Mock_CG_Attr_Gen_TestAttrib1 on " + clientEntityType + ".Prop1");
                //Check if the default constructor was used
                CustomAttributeData cad = cads1[0];
                IList <CustomAttributeTypedArgument> ctr1args = cad.ConstructorArguments;
                Assert.AreEqual(ctr1args.Count, 0);

                MemberInfo[] prop2 = clientEntityType.GetMember("Prop2");
                IList <CustomAttributeData> cads2 = AssemblyGenerator.GetCustomAttributeData(prop2[0], typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib2));
                Assert.AreEqual(1, cads2.Count, "Expected Mock_CG_Attr_Gen_TestAttrib2 on " + clientEntityType + ".Prop2");
                cad = cads2[0];
                //Check if the constructor with one int param was used
                IList <CustomAttributeTypedArgument> ctr2args = cad.ConstructorArguments;
                Assert.AreEqual(ctr2args.Count, 1);
                Assert.AreEqual(ctr2args[0].ArgumentType, typeof(int));
                Assert.AreEqual(ctr2args[0].Value, 0);

                MemberInfo[] prop3 = clientEntityType.GetMember("Prop3");
                IList <CustomAttributeData> cads3 = AssemblyGenerator.GetCustomAttributeData(prop3[0], typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib3));
                Assert.AreEqual(1, cads3.Count, "Expected Mock_CG_Attr_Gen_TestAttrib3 on " + clientEntityType + ".Prop3");
                cad = cads3[0];
                // Check if the ctor with one string param was used
                IList <CustomAttributeTypedArgument> ctr3args = cad.ConstructorArguments;
                Assert.AreEqual(ctr3args.Count, 1);
                Assert.AreEqual(ctr3args[0].ArgumentType, typeof(string));
                Assert.AreEqual(ctr3args[0].Value, null);

                MemberInfo[] prop4 = clientEntityType.GetMember("Prop4");
                IList <CustomAttributeData> cads4 = AssemblyGenerator.GetCustomAttributeData(prop4[0], typeof(System.ComponentModel.DataAnnotations.Mock_CG_Attr_Gen_TestAttrib4));
                Assert.AreEqual(1, cads4.Count, "Expected Mock_CG_Attr_Gen_TestAttrib4 on " + clientEntityType + ".Prop4");
                cad = cads4[0];
                // Check if the first ctor was used
                IList <CustomAttributeTypedArgument> ctr4args = cad.ConstructorArguments;
                Assert.AreEqual(ctr4args.Count, 1);
                Assert.AreEqual(ctr4args[0].ArgumentType, typeof(int));
                Assert.AreEqual(ctr4args[0].Value, 0);
            }
        }
Example #2
0
        private Type ValidateGeneratedEnum(Type serverEnumType, AssemblyGenerator asmGen)
        {
            Type clientEnumType = asmGen.GetGeneratedType(serverEnumType.FullName);

            Assert.IsNotNull(clientEnumType, "Expected to see generated " + serverEnumType + " but saw " + asmGen.GeneratedTypeNames);

            // validate the enum integral type is the same
            Type serverUnderlyingType = serverEnumType.GetEnumUnderlyingType();
            Type clientUnderlyingType = clientEnumType.GetEnumUnderlyingType();

            Assert.AreEqual(serverUnderlyingType.FullName, clientUnderlyingType.FullName, "Mismatch in server enum type's underlying type and generated form");

            DataContractAttribute serverDCAttr = (DataContractAttribute)Attribute.GetCustomAttribute(serverEnumType, typeof(DataContractAttribute));

            if (serverDCAttr != null)
            {
                IList <CustomAttributeData> cads = AssemblyGenerator.GetCustomAttributeData(clientEnumType, typeof(DataContractAttribute));
                Assert.AreEqual(1, cads.Count, "Expected DataContract on " + clientEnumType);
                CustomAttributeData cad    = cads[0];
                string serverAttrName      = serverDCAttr.Name;
                string serverAttrNamespace = serverDCAttr.Namespace;
                string clientAttrName      = AssemblyGenerator.GetCustomAttributeValue <string>(cad, "Name");
                string clientAttrNamespace = AssemblyGenerator.GetCustomAttributeValue <string>(cad, "Namespace");

                Assert.AreEqual(serverAttrName, clientAttrName, "Expected DC.Name to be the same on " + clientEnumType);
                Assert.AreEqual(serverAttrNamespace, clientAttrNamespace, "Expected DC.Namespace to be the same on " + clientEnumType);
            }

            string[] serverMemberNames = Enum.GetNames(serverEnumType);
            string[] clientMemberNames = Enum.GetNames(clientEnumType);
            Assert.AreEqual(serverMemberNames.Length, clientMemberNames.Length, "Different number of fields generated");

            for (int i = 0; i < serverMemberNames.Length; ++i)
            {
                Assert.AreEqual(serverMemberNames[i], clientMemberNames[i], "Member name difference");

                // We have to use GetRawConstantValue because the ReflectionOnlyLoad does not support Enum.GetValues
                FieldInfo serverFieldInfo = serverEnumType.GetField(serverMemberNames[i]);
                Assert.IsNotNull(serverFieldInfo, "Failed to find server's " + serverMemberNames[i] + " as field info");
                object serverMemberValue = serverFieldInfo.GetRawConstantValue();

                FieldInfo clientFieldInfo = clientEnumType.GetField(clientMemberNames[i]);
                Assert.IsNotNull(clientFieldInfo, "Failed to find client's " + clientMemberNames[i] + " as field info");
                object clientMemberValue = clientFieldInfo.GetRawConstantValue();

                Assert.AreEqual(serverMemberValue, clientMemberValue, "Different values for field " + serverMemberNames[i]);

                EnumMemberAttribute enumMemberAttr = (EnumMemberAttribute)Attribute.GetCustomAttribute(serverFieldInfo, typeof(EnumMemberAttribute));
                if (enumMemberAttr != null)
                {
                    IList <CustomAttributeData> cads = AssemblyGenerator.GetCustomAttributeData(clientFieldInfo, typeof(EnumMemberAttribute));
                    Assert.AreEqual(1, cads.Count, "Expected EnumMember on " + clientEnumType + "." + clientMemberNames[i]);
                    CustomAttributeData cad = cads[0];
                    string clientValue      = null;
                    AssemblyGenerator.TryGetCustomAttributeValue <string>(cad, "Value", out clientValue);

                    string serverValue = enumMemberAttr.Value;
                    Assert.AreEqual(serverValue, clientValue, "EnumMember had different values for Value arg for " + clientEnumType + "." + clientMemberNames[i]);
                }

                // Validate Display custom attribute propagates correctly
                DisplayAttribute displayAttr = (DisplayAttribute)Attribute.GetCustomAttribute(serverFieldInfo, typeof(DisplayAttribute));
                if (displayAttr != null)
                {
                    IList <CustomAttributeData> cads = AssemblyGenerator.GetCustomAttributeData(clientFieldInfo, typeof(DisplayAttribute));
                    Assert.AreEqual(1, cads.Count, "Expected [Display] on " + clientEnumType + "." + clientMemberNames[i]);
                    CustomAttributeData cad = cads[0];
                    string clientValue      = null;
                    AssemblyGenerator.TryGetCustomAttributeValue <string>(cad, "Name", out clientValue);

                    string serverValue = displayAttr.Name;
                    Assert.AreEqual(serverValue, clientValue, "[Display] had different values for Name arg for " + clientEnumType + "." + clientMemberNames[i]);
                }

                // Validate Description custom attribute propagates correctly
                ComponentModelDescriptionAttribute descriptionAttr = (ComponentModelDescriptionAttribute)Attribute.GetCustomAttribute(serverFieldInfo, typeof(ComponentModelDescriptionAttribute));
                if (descriptionAttr != null)
                {
                    IList <CustomAttributeData> cads = AssemblyGenerator.GetCustomAttributeData(clientFieldInfo, typeof(ComponentModelDescriptionAttribute));
                    Assert.AreEqual(1, cads.Count, "Expected [Description] on " + clientEnumType + "." + clientMemberNames[i]);
                    CustomAttributeData cad = cads[0];
                    string clientValue      = null;
                    AssemblyGenerator.TryGetCustomAttributeValue <string>(cad, "Description", out clientValue);

                    string serverValue = descriptionAttr.Description;
                    Assert.AreEqual(serverValue, clientValue, "[Description] had different values for Description arg for " + clientEnumType + "." + clientMemberNames[i]);
                }

                // Validate ServerOnlyAttribute does not propagate
                ServerOnlyAttribute serverOnlyAttr = (ServerOnlyAttribute)Attribute.GetCustomAttribute(serverFieldInfo, typeof(ServerOnlyAttribute));
                if (serverOnlyAttr != null)
                {
                    IList <CustomAttributeData> cads = AssemblyGenerator.GetCustomAttributeData(clientFieldInfo, typeof(ServerOnlyAttribute));
                    Assert.AreEqual(0, cads.Count, "Expected [ServerOnlyAttribute] *not* to be generated on " + clientEnumType + "." + clientMemberNames[i]);
                }
            }

            bool serverHasFlags = serverEnumType.GetCustomAttributes(false).OfType <FlagsAttribute>().Any();

            // Have to use CustomAttributeData due to ReflectionOnly load
            IList <CustomAttributeData> clientFlagsAttributes = AssemblyGenerator.GetCustomAttributeData(clientEnumType, typeof(FlagsAttribute));
            bool clientHasFlags = clientFlagsAttributes.Any();

            Assert.AreEqual(serverHasFlags, clientHasFlags, "Server and client differ in appearance of [Flags]");

            return(clientEnumType);
        }