public CodeMemberShareKind GetTypeShareKind(string typeName) { string fullName = MockSharedCodeService.AssemblyQualifiedTypeNameToFullTypeName(typeName); if (this._unknowableTypes.Any(t => string.Equals(t.FullName, fullName))) { return(CodeMemberShareKind.Unknown); } if (this._unsharedTypes.Any(t => string.Equals(t.FullName, fullName))) { return(CodeMemberShareKind.NotShared); } if (this._sharedTypes.Any(t => string.Equals(t.FullName, fullName))) { return(CodeMemberShareKind.SharedBySource); } // Check for file sharing needs fully qualified name if (this.IsTypeSharedInFile(typeName)) { return(CodeMemberShareKind.SharedBySource); } if (IsSharedFrameworkType(typeName)) { return(CodeMemberShareKind.SharedByReference); } return(CodeMemberShareKind.NotShared); }
public void CodeGen_CustomAttrGen_AttributeType_NotShared() { ConsoleLogger logger = new ConsoleLogger(); // Create a shared type service that says the entity's attribute is "unshared" when asked whether it is shared MockSharedCodeService mockSts = new MockSharedCodeService( new Type[] { typeof(Mock_CG_Attr_Gen_Type) }, Array.Empty <MethodBase>(), Array.Empty <string>()); mockSts.AddUnsharedType(typeof(Mock_CG_Attr_Gen_TestAttribute)); string generatedCode = TestHelper.GenerateCode("C#", new Type[] { typeof(Mock_CG_Attr_Gen_DomainService) }, logger, mockSts); string expectedWarning = string.Format( CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_RequiresDataAnnotations, typeof(Mock_CG_Attr_Gen_TestAttribute), "MockProject"); TestHelper.AssertContainsWarnings(logger, expectedWarning); string warningComment = string.Format( CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_RequiresShared, typeof(Mock_CG_Attr_Gen_TestAttribute), "MockProject"); TestHelper.AssertGeneratedCodeContains(generatedCode, warningComment); }
public void CodeGen_CustomAttrGen_Attribute_References_Type_NotShared() { ConsoleLogger logger = new ConsoleLogger(); // Create a shared type service that says the entity's attribute is "shared" when asked whether it is shared MockSharedCodeService mockSts = new MockSharedCodeService( new Type[] { typeof(Mock_CG_Attr_Gen_TestAttribute) }, Array.Empty <MethodBase>(), Array.Empty <string>()); // Explicitly make the typeof() ref in the attribute say it is unshared mockSts.AddUnsharedType(typeof(Mock_CG_Attr_Gen_Type)); string generatedCode = TestHelper.GenerateCode("C#", new Type[] { typeof(Mock_CG_Attr_Gen_DomainService) }, logger, mockSts); TestHelper.AssertNoErrorsOrWarnings(logger); string warningComment = string.Format( CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_RequiresShared, typeof(Mock_CG_Attr_Gen_TestAttribute), "MockProject"); TestHelper.AssertGeneratedCodeContains(generatedCode, "[Mock_CG_Attr_Gen_Test(typeof(global::OpenRiaServices.DomainServices.Tools.Test.Mock_CG_Attr_Gen_Type))]"); }
public void CodeGen_CustomAttrGen_AttributeType_Shared_Unknowable() { ConsoleLogger logger = new ConsoleLogger(); // Create a shared type service that says the entity's attribute is "unknowable" when asked whether it is shared MockSharedCodeService mockSts = new MockSharedCodeService( new Type[] { typeof(Mock_CG_Attr_Gen_Type) }, Array.Empty <MethodBase>(), Array.Empty <string>()); mockSts.AddUnknowableType(typeof(Mock_CG_Attr_Gen_TestAttribute)); string generatedCode = TestHelper.GenerateCode("C#", new Type[] { typeof(Mock_CG_Attr_Gen_DomainService) }, logger, mockSts); TestHelper.AssertNoErrorsOrWarnings(logger); string warningComment = string.Format( CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_RequiresShared_NoPDB, typeof(Mock_CG_Attr_Gen_TestAttribute), typeof(Mock_CG_Attr_Gen_TestAttribute).Assembly.GetName().Name, "MockProject"); // CodeDom injects comments after line breaks warningComment = warningComment.Replace("\r\n ", "\r\n // "); TestHelper.AssertGeneratedCodeContains(generatedCode, warningComment); }
public void CodeGen_Attribute_DisplayAttribute_Resourced_DifferentNamespace() { MockSharedCodeService sts = TestHelper.CreateCommonMockSharedCodeService(); string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] { typeof(Mock_CG_DisplayAttr_Entity_Shared_ResourceType_DifferentNamespace_DomainService) }, null, sts); TestHelper.AssertGeneratedCodeContains(generatedCode, @"[Display(Description=""Resource4"", Name=""Resource2"", Prompt=""Resource3"", ResourceType=typeof(Mock_CG_DisplayAttr_Shared_ResourceType_DifferentNamespace), ShortName=""Resource1"")]"); TestHelper.AssertGeneratedCodeContains(generatedCode, @"[Display(Description=""Literal4"", Name=""Literal2"", Prompt=""Literal3"", ShortName=""Literal1"")]"); }
public void CodeGen_Attribute_StringLength_Valid_ResourceType_And_Name() { MockSharedCodeService sts = TestHelper.CreateCommonMockSharedCodeService(); string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] { typeof(Mock_CG_Attr_Entity_StringLength_Valid_DomainService) }, null, sts); TestHelper.AssertGeneratedCodeContains(generatedCode, "[StringLength(10, ErrorMessageResourceName=\"TheResource\", ErrorMessageResourceType=typeof(Mock_CG_Attr_Entity_StringLength_ResourceType))] " + "public string StringProperty"); }
private static bool IsSharedFrameworkType(string typeName) { // If this isn't a system assembly, we know immediately that // it's not a shared framework type Type systemType = Type.GetType(typeName, /*throwOnError*/ false); if (systemType != null) { if (!SystemWebDomainServices::OpenRiaServices.DomainServices.TypeUtility.IsSystemAssembly(systemType.Assembly)) { return(false); } // Mock matches the real shared assemblies in allowing all mscorlib to match if (AssemblyUtilities.IsAssemblyMsCorlib(systemType.Assembly.GetName())) { return(true); } // The mock declares that anything in System is also shared // Anything in EntityFramework.dll is not shared. string assemblyName = systemType.Assembly.FullName; int comma = assemblyName.IndexOf(','); if (comma >= 0) { assemblyName = assemblyName.Substring(0, comma); if (string.Equals("System", assemblyName, StringComparison.OrdinalIgnoreCase)) { return(true); } if (string.Equals("EntityFramework", assemblyName, StringComparison.OrdinalIgnoreCase)) { return(false); } } } typeName = MockSharedCodeService.AssemblyQualifiedTypeNameToFullTypeName(typeName); int dot = typeName.LastIndexOf('.'); string namespaceName = dot < 0 ? string.Empty : typeName.Substring(0, dot); string shortTypeName = typeName.Substring(dot + 1); if (string.Equals("System.ComponentModel.DataAnnotations", namespaceName, StringComparison.OrdinalIgnoreCase)) { return(true); } foreach (Type t in MockSharedCodeService._knownSharedAttributeTypes) { if (string.Equals(t.FullName, typeName)) { return(true); } } return(false); }
public void CodeGen_CustomAttrGen_AttributeType_Shared() { // Create a shared type service that says the entity's attribute is "shared" when asked whether it is shared MockSharedCodeService mockSts = new MockSharedCodeService( new Type[] { typeof(Mock_CG_Attr_Gen_Type), typeof(Mock_CG_Attr_Gen_TestAttribute) }, Array.Empty <MethodBase>(), Array.Empty <string>()); string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] { typeof(Mock_CG_Attr_Gen_DomainService) }, mockSts); TestHelper.AssertGeneratedCodeContains(generatedCode, "[Mock_CG_Attr_Gen_Test(typeof(Mock_CG_Attr_Gen_Type))]"); }
public void CodeGen_Attribute_CustomValidation_Multiple() { ISharedCodeService sts = new MockSharedCodeService( new Type[] { typeof(Mock_CG_Attr_Validator) }, new MethodBase[] { typeof(Mock_CG_Attr_Validator).GetMethod("IsValid"), typeof(Mock_CG_Attr_Validator).GetMethod("IsValid2") }, new string[0]); string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] { typeof(Mock_CG_Attr_Entity_Multiple_DomainService) }, sts); TestHelper.AssertGeneratedCodeContains(generatedCode, @"[CustomValidation(typeof(Mock_CG_Attr_Validator), ""IsValid"")]", @"[CustomValidation(typeof(Mock_CG_Attr_Validator), ""IsValid2"")]"); }
public void CodeGen_CustomAttrGen_BindableAttribute() { // Create a shared type service that says the entity's attribute is "shared" when asked whether it is shared MockSharedCodeService mockSts = new MockSharedCodeService( new Type[] { typeof(System.ComponentModel.BindableAttribute) }, Array.Empty <MethodBase>(), Array.Empty <string>()); string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] { typeof(Mock_CG_Attr_Entity_Bindable_DomainService) }, new ConsoleLogger(), mockSts, /*useFullNames*/ false); TestHelper.AssertGeneratedCodeContains(generatedCode, "[Bindable(true, BindingDirection.TwoWay)]"); generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", new Type[] { typeof(Mock_CG_Attr_Entity_Bindable_DomainService) }, new ConsoleLogger(), mockSts, true); TestHelper.AssertGeneratedCodeContains(generatedCode, "[global::System.ComponentModel.BindableAttribute(true, global::System.ComponentModel.BindingDirection.TwoWay)]"); }
public void CodeGen_Attribute_KeyAttribute_SharedAttribute() { ConsoleLogger logger = new ConsoleLogger(); // For this test, consider K2 shared and K1 not shared ISharedCodeService sts = new MockSharedCodeService( new Type[] { typeof(Mock_CG_Attr_Entity_Shared_Key) }, new MethodInfo[] { typeof(Mock_CG_Attr_Entity_Shared_Key).GetProperty("K2").GetGetMethod() }, new string[0]); string generatedCode = TestHelper.GenerateCode("C#", new Type[] { typeof(Mock_CG_Attr_Entity_Shared_Key_DomainService) }, logger, sts); Assert.IsTrue(!string.IsNullOrEmpty(generatedCode)); TestHelper.AssertGeneratedCodeDoesNotContain(generatedCode, "GetIdentity"); }
/// <summary> /// Basic success test. Method verifies that domain service compiles. /// </summary> /// <param name="domainServices">DomainService to compile</param> /// <param name="codeContains">strings that this code must contain, can be c>null</c>.</param> /// <param name="codeNotContains">strings that this code must not contain, can be <c>null</c>.</param> public static void BaseSuccessTest(Type[] domainServices, string[] codeContains, string[] codeNotContains) { MockSharedCodeService sts = TestHelper.CreateCommonMockSharedCodeService(); ConsoleLogger logger = new ConsoleLogger(); string generatedCode = TestHelper.GenerateCodeAssertSuccess("C#", domainServices, logger, sts); if (codeContains != null) { TestHelper.AssertGeneratedCodeContains(generatedCode, codeContains); } if (codeNotContains != null) { TestHelper.AssertGeneratedCodeDoesNotContain(generatedCode, codeNotContains); } }
public void SharedTypes_CodeGen_Warns_Unshared_Property_Type() { ConsoleLogger logger = new ConsoleLogger(); // Create a shared type service that says the entity's attribute is "shared" when asked whether it is shared MockSharedCodeService mockSts = new MockSharedCodeService( new Type[0], new MethodBase[0], new string[0]); string generatedCode = TestHelper.GenerateCode("C#", new Type[] { typeof(Mock_CG_Shared_DomainService) }, logger, mockSts); Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "Code should have been generated"); string entityWarning = String.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_PropertyType_Not_Shared, "XElementProperty", typeof(Mock_CG_Shared_Entity).FullName, typeof(System.Xml.Linq.XElement).FullName, "MockProject"); string complexTypeWarning = String.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_PropertyType_Not_Shared, "ComplexXElementProperty", typeof(Mock_CG_Shared_Complex_Type).FullName, typeof(System.Xml.Linq.XElement).FullName, "MockProject"); TestHelper.AssertContainsWarnings(logger, entityWarning, complexTypeWarning); }
// Creates a MockSharedCodeService with some common files and types considered as shared internal static MockSharedCodeService CreateCommonMockSharedCodeService() { IEnumerable <Type> sharedTypes = new Type[] { // Known test types typeof(Mock_CG_Attr_Entity_StringLength_ResourceType), typeof(Mock_CG_DisplayAttr_Shared_ResourceType), typeof(DifferentNamespace.Mock_CG_DisplayAttr_Shared_ResourceType_DifferentNamespace), typeof(TestDomainServices.SharedResource) }; IEnumerable <MethodBase> sharedMethods = new MethodBase[] { // Known test methods. typeof(Mock_CG_Attr_Entity_StringLength_ResourceType).GetProperty("TheResource").GetGetMethod(), typeof(TestDomainServices.SharedResource).GetProperty("String").GetGetMethod() }; IEnumerable <string> sharedFiles = new string[] { }; MockSharedCodeService mockSharedCodeService = new MockSharedCodeService(sharedTypes, sharedMethods, sharedFiles); return(mockSharedCodeService); }
public void CodeGen_CustomAttrGen_DomainServiceAttributeThrows() { MockSharedCodeService sts = TestHelper.CreateCommonMockSharedCodeService(); ConsoleLogger logger = new ConsoleLogger(); string generatedCode = TestHelper.GenerateCode("C#", new Type[] { typeof(AttributeThrowingDomainService) }, logger, sts); Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "Should have generated code despite warnings"); AttributeBuilderException expectedException = new AttributeBuilderException( new ThrowingServiceAttributeException(ThrowingServiceAttribute.ExceptionMessage), typeof(ThrowingServiceAttribute), ThrowingServiceAttribute.ThrowingPropertyName); string expectedBuildWarning = string.Format( CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_ThrewException_CodeType, expectedException.Message, AttributeThrowingDomainService.DomainContextTypeName, expectedException.InnerException.Message); TestHelper.AssertGeneratedCodeContains(generatedCode, expectedException.Message); TestHelper.AssertContainsWarnings(logger, expectedBuildWarning); }
// Creates a MockSharedCodeService with some common files and types considered as shared internal static MockSharedCodeService CreateCommonMockSharedCodeService() { IEnumerable<Type> sharedTypes = new Type[] { // Known test types typeof(Mock_CG_Attr_Entity_StringLength_ResourceType), typeof(Mock_CG_DisplayAttr_Shared_ResourceType), typeof(DifferentNamespace.Mock_CG_DisplayAttr_Shared_ResourceType_DifferentNamespace), typeof(TestDomainServices.SharedResource) }; IEnumerable<MethodBase> sharedMethods = new MethodBase[] { // Known test methods. typeof(Mock_CG_Attr_Entity_StringLength_ResourceType).GetProperty("TheResource").GetGetMethod(), typeof(TestDomainServices.SharedResource).GetProperty("String").GetGetMethod() }; IEnumerable<string> sharedFiles = new string[] { }; MockSharedCodeService mockSharedCodeService = new MockSharedCodeService(sharedTypes, sharedMethods, sharedFiles); return mockSharedCodeService; }
public void T4CodeGenTest_VBCodeGenTest() { Type[] domainServiceTypes = new Type[] {}; ClientCodeGenerationOptions options = new ClientCodeGenerationOptions() { Language = "VB", ClientProjectPath = "MockProject.proj", ClientRootNamespace = "TestRootNS", UseFullTypeNames = false }; ConsoleLogger consoleLogger = new ConsoleLogger(); MockSharedCodeService mockSharedCodeService = new MockSharedCodeService(new Type[0], new MethodBase[0], new string[0]); MockCodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(consoleLogger, mockSharedCodeService); ClientCodeGenerator generator = (ClientCodeGenerator)new VBTestClientCodeGenerator(); DomainServiceCatalog domainServiceCatalog = new DomainServiceCatalog(domainServiceTypes, consoleLogger); string generatedCode = generator.GenerateCode(host, domainServiceCatalog.DomainServiceDescriptions, options); Assert.IsTrue(string.IsNullOrEmpty(generatedCode)); Assert.IsTrue(string.IsNullOrEmpty(consoleLogger.Errors)); }