public void DefinitionsShouldHave_TypedModelNodes_v12() { Trace.WriteLine("Checking typed model nodes"); Trace.WriteLine(""); var passes = true; var showOnlyFalseOutput = true; TraceUtils.WithScope(trace => { foreach (var defType in AllDefinitionTypes.OrderBy(d => d.Name)) { var pureDefName = defType.Name.Replace("Definition", string.Empty); var targetModelNodeTypeName = string.Format("{0}ModelNode", pureDefName); var modelNodelType = AllModelNodeTypes.FirstOrDefault(n => n.Name == targetModelNodeTypeName); if (!showOnlyFalseOutput) { trace.WriteLine(defType.Name); } if (modelNodelType == null) { passes = false; if (showOnlyFalseOutput) { trace.WriteLine(defType.Name); } trace.WriteLine(string.Format("[FALSE] Missing model node: [{0}]", targetModelNodeTypeName)); trace.WriteLine(""); } } }); Assert.IsTrue(passes); }
public void DefinitionsShouldHaveAddXXX_DefinitionSyntax_v12() { if (!M2RegressionRuntime.IsV12) { return; } var passed = true; var showOnlyFalseOutput = true; var allCount = AllDefinitionTypes.Count; var missesCount = 0; // add definitions should have AddXXX() methods // - must be generic // - typed model node // - definitions type as a second param // - overload with typed node call back // - allback with typed model node // AddField<TModelNode>(this TModelNode model, FieldDefinition definition) // where TModelNode : ModelNode, IFieldHostModelNode, new() // AddField<TModelNode>(this TModelNode model, FieldDefinition definition, Action<FieldModelNode> action) // where TModelNode : ModelNode, IFieldHostModelNode, new() Trace.WriteLine("Checking AddXXX() method specs"); Trace.WriteLine(""); TraceUtils.WithScope(trace => { foreach (var defType in AllDefinitionTypes.OrderBy(d => d.Name)) { var isRootDef = (defType.GetCustomAttributes(typeof(ParentHostCapabilityAttribute)) .FirstOrDefault() as ParentHostCapabilityAttribute).IsRoot; var isSelfHostDefinition = (defType.GetCustomAttributes(typeof(SelfHostCapabilityAttribute)).Any()); if (isRootDef) { trace.WriteLine(string.Format("Skipping root def")); continue; } var completedtype = true; var pureDefName = defType.Name.Replace("Definition", string.Empty); var shouldCheckArrayOverload = defType.GetCustomAttributes(typeof(ExpectArrayExtensionMethod), false).Any(); var shouldCheckAddHostMethod = defType.GetCustomAttributes(typeof(ExpectAddHostExtensionMethod), false).Any(); var targetModelNodeTypeName = string.Format("{0}ModelNode", pureDefName); var modelNodelType = AllModelNodeTypes.FirstOrDefault(n => n.Name == targetModelNodeTypeName); trace.WriteLine(defType.Name); if (modelNodelType == null) { trace.WriteLine(string.Format("[FALSE] Missing model node: [{0}]", targetModelNodeTypeName)); } trace.WithTraceIndent(addXXXTrace => { if (!showOnlyFalseOutput) { trace.WriteLine("Checking AddXXX() method"); } var relationships = AllDefinitionRelationships.FirstOrDefault(r => r.DefinitionType == defType); var addXXXMethodName = string.Format("Add{0}", pureDefName); var addXXXArrayDefinitionMethodName = string.Format("Add{0}s", pureDefName); var addXXXHostMethodName = string.Format("AddHost{0}", pureDefName); var lastChar = pureDefName[pureDefName.Length - 1]; switch (lastChar) { case 'y': { addXXXArrayDefinitionMethodName = string.Format("Add{0}", pureDefName); addXXXArrayDefinitionMethodName = addXXXArrayDefinitionMethodName.Substring(0, addXXXArrayDefinitionMethodName.Length - 1); addXXXArrayDefinitionMethodName += "ies"; } ; break; case 'x': case 'e': { addXXXArrayDefinitionMethodName = string.Format("Add{0}", pureDefName); addXXXArrayDefinitionMethodName += "s"; } break; case 's': { addXXXArrayDefinitionMethodName = string.Format("Add{0}", pureDefName); addXXXArrayDefinitionMethodName += "es"; } break; } // host Func <MethodInfo, bool> addHostMethodPlainSpec = (m) => { // public static TModelNode AddAlternateUrl<TModelNode>(this TModelNode model, AlternateUrlDefinition definition) // where TModelNode : ModelNode, IWebApplicationHostModelNode, new() return(m.Name == addXXXHostMethodName && m.IsGenericMethod && (m.GetParameters().Count() == 2) && m.ReturnType.Name == "TModelNode" && m.GetGenericArguments()[0].Name == "TModelNode" && m.GetGenericArguments()[0].BaseType == typeof(ModelNode) && m.GetParameters()[1].ParameterType == defType ); }; Func <MethodInfo, bool> addHostMethodWithCallbackSpec = (m) => { // public static TModelNode AddAlternateUrl<TModelNode>(this TModelNode model, AlternateUrlDefinition definition, // Action<ContentTypeModelNode> action) // where TModelNode : ModelNode, IWebApplicationHostModelNode, new() var callbackType = typeof(Action <>); var typedCallbackType = callbackType.MakeGenericType(modelNodelType); Func <MethodInfo, bool> selfHostCallbackCheck = (method => true); selfHostCallbackCheck = (method) => method.GetParameters()[2].ParameterType == typedCallbackType; return(m.Name == addXXXHostMethodName && m.IsGenericMethod && (m.GetParameters().Count() == 3) && m.ReturnType.Name == "TModelNode" && m.GetGenericArguments()[0].Name == "TModelNode" && m.GetGenericArguments()[0].BaseType == typeof(ModelNode) && m.GetParameters()[1].ParameterType == defType && selfHostCallbackCheck(m)); }; // add Func <MethodInfo, bool> addMethodPlainSpec = (m) => { // public static TModelNode AddAlternateUrl<TModelNode>(this TModelNode model, AlternateUrlDefinition definition) // where TModelNode : ModelNode, IWebApplicationHostModelNode, new() return(m.Name == addXXXMethodName && m.IsGenericMethod && (m.GetParameters().Count() == 2) && m.ReturnType.Name == "TModelNode" && m.GetGenericArguments()[0].Name == "TModelNode" && m.GetGenericArguments()[0].BaseType == typeof(ModelNode) && m.GetParameters()[1].ParameterType == defType ); }; Func <MethodInfo, bool> addMethodWithCallbackSpec = (m) => { // public static TModelNode AddAlternateUrl<TModelNode>(this TModelNode model, AlternateUrlDefinition definition, // Action<ContentTypeModelNode> action) // where TModelNode : ModelNode, IWebApplicationHostModelNode, new() var callbackType = typeof(Action <>); var typedCallbackType = callbackType.MakeGenericType(modelNodelType); Func <MethodInfo, bool> selfHostCallbackCheck = (method => true); if (isSelfHostDefinition) { selfHostCallbackCheck = (method) => { // self return // callback has the same type as TModelNode // that works for ResetRoleInheritance/BreakRoleInheritance as they 'pass' the current object further to the chain // public static TModelNode AddResetRoleInheritance<TModelNode>(this TModelNode model, ResetRoleInheritanceDefinition definition) // where TModelNode : ModelNode, ISecurableObjectHostModelNode, new() return(typeof(Action <>).MakeGenericType(method.GetParameters()[0].ParameterType) == method.GetParameters()[2].ParameterType); }; } else { selfHostCallbackCheck = (method) => method.GetParameters()[2].ParameterType == typedCallbackType; } return(m.Name == addXXXMethodName && m.IsGenericMethod && (m.GetParameters().Count() == 3) && m.ReturnType.Name == "TModelNode" && m.GetGenericArguments()[0].Name == "TModelNode" && m.GetGenericArguments()[0].BaseType == typeof(ModelNode) && m.GetParameters()[1].ParameterType == defType && selfHostCallbackCheck(m)); }; Func <MethodInfo, bool> addMethodWithArraySpec = (m) => { // public static TModelNode AddAlternateUrls<TModelNode>(this TModelNode model, IEnumerable<AlternateUrlDefinition> definitions) // where TModelNode : ModelNode, IWebApplicationHostModelNode, new() var arrayType = typeof(IEnumerable <>); var arrayDefinitionType = arrayType.MakeGenericType(defType); return(m.Name == addXXXArrayDefinitionMethodName && m.IsGenericMethod && (m.GetParameters().Count() == 2) && m.ReturnType.Name == "TModelNode" && m.GetGenericArguments()[0].Name == "TModelNode" && m.GetGenericArguments()[0].BaseType == typeof(ModelNode) && m.GetParameters()[1].ParameterType == arrayDefinitionType ); }; var addMethodPlain = AllExtensionMethods.FirstOrDefault(addMethodPlainSpec); var addMethodPlainWithCallBack = AllExtensionMethods.FirstOrDefault(addMethodWithCallbackSpec); if (addMethodPlain == null) { passed = false; completedtype = false; addXXXTrace.WriteLine("[FALSE] AddXXX()"); } else { var missedRelationshipModelNodeTypes = GetUnsupportedRelationshipModelNodeTypes(addMethodPlainWithCallBack, relationships); if (missedRelationshipModelNodeTypes.Any()) { passed = false; completedtype = false; addXXXTrace.WriteLine(string.Format( "[FALSE] AddXXX() misses relationships: [{0}]", string.Join(",", missedRelationshipModelNodeTypes))); } } if (addMethodPlainWithCallBack == null) { passed = false; completedtype = false; addXXXTrace.WriteLine("[FALSE] AddXXX(callback)"); } else { var missedRelationshipModelNodeTypes = GetUnsupportedRelationshipModelNodeTypes(addMethodPlainWithCallBack, relationships); if (missedRelationshipModelNodeTypes.Any()) { passed = false; completedtype = false; addXXXTrace.WriteLine(string.Format( "[FALSE] AddXXX(callback) misses relationships: [{0}]", string.Join(",", missedRelationshipModelNodeTypes))); } } if (shouldCheckArrayOverload) { var addMethodWithArraySupport = AllExtensionMethods.FirstOrDefault(addMethodWithArraySpec); if (addMethodWithArraySupport == null) { passed = false; completedtype = false; addXXXTrace.WriteLine("[FALSE] AddXXXs()"); } else { var missedRelationshipModelNodeTypes = GetUnsupportedRelationshipModelNodeTypes(addMethodWithArraySupport, relationships); if (missedRelationshipModelNodeTypes.Any()) { passed = false; completedtype = false; addXXXTrace.WriteLine(string.Format( "[FALSE] AddXXXs() misses relationships: [{0}]", string.Join(",", missedRelationshipModelNodeTypes))); } } } if (shouldCheckAddHostMethod) { var addHostMethodPlain = AllExtensionMethods.FirstOrDefault(addHostMethodPlainSpec); var addHostMethodPlainWithCallBack = AllExtensionMethods.FirstOrDefault(addHostMethodWithCallbackSpec); if (addHostMethodPlain == null) { passed = false; completedtype = false; addXXXTrace.WriteLine("[FALSE] AddHostXXX()"); } else { var missedRelationshipModelNodeTypes = GetUnsupportedRelationshipModelNodeTypes(addHostMethodPlain, relationships); if (missedRelationshipModelNodeTypes.Any()) { passed = false; completedtype = false; addXXXTrace.WriteLine(string.Format( "[FALSE] AddHostXXX() misses relationships: [{0}]", string.Join(",", missedRelationshipModelNodeTypes))); } } if (addHostMethodPlainWithCallBack == null) { passed = false; completedtype = false; addXXXTrace.WriteLine("[FALSE] AddHostXXX(callback)"); } else { var missedRelationshipModelNodeTypes = GetUnsupportedRelationshipModelNodeTypes(addHostMethodPlainWithCallBack, relationships); if (missedRelationshipModelNodeTypes.Any()) { passed = false; completedtype = false; addXXXTrace.WriteLine(string.Format( "[FALSE] AddHostXXX(callback) misses relationships: [{0}]", string.Join(",", missedRelationshipModelNodeTypes))); } } } }); if (!showOnlyFalseOutput) { trace.WriteLine(""); } if (!completedtype) { missesCount++; } } }); Trace.WriteLine(""); Trace.WriteLine(string.Format("{0}/{1}", missesCount, allCount)); Assert.IsTrue(passed); }