//private bool AnySecurityAttributeAdded(IDifferences differences, IReference target, IEnumerable<ISecurityAttribute> attribues1, IEnumerable<ISecurityAttribute> attributes2) //{ // return AnyAttributeAdded(differences, target, attribues1.SelectMany(a => a.Attributes), attributes2.SelectMany(a => a.Attributes)); //} private void CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes) { AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings); attributeMapping.AddMapping(0, contractAttributes); attributeMapping.AddMapping(1, implAttributes); foreach (var group in attributeMapping.Attributes) { switch (group.Difference) { case DifferenceType.Added: { ITypeReference type = group.Representative.Attributes.First().Type; string attribName = type.FullName(); if (FilterAttribute(attribName)) { break; } AttributeAdded(target, attribName); differences.AddIncompatibleDifference("CannotAddAttribute", "Attribute '{0}' exists on '{1}' in the implementation but not the contract.", attribName, target.FullName()); break; } case DifferenceType.Changed: //TODO: Add some more logic to check the two lists of attributes which have the same type. break; case DifferenceType.Removed: { ITypeReference type = group.Representative.Attributes.First().Type; string attribName = type.FullName(); if (s_IgnorableAttributes.Contains(attribName)) { break; } differences.AddIncompatibleDifference("CannotRemoveAttribute", "Attribute '{0}' exists on '{1}' in the contract but not the implementation.", attribName, target.FullName()); break; } } } }
private bool AnyAttributeAdded(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes) { bool added = false; AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings); attributeMapping.AddMapping(0, implAttributes); attributeMapping.AddMapping(1, contractAttributes); foreach (var group in attributeMapping.Attributes) { switch (group.Difference) { case DifferenceType.Added: ITypeReference type = group.Representative.Attributes.First().Type; string attribName = type.FullName(); if (s_IgnorableAttributes.Contains(attribName)) { break; } differences.AddIncompatibleDifference(this, "Attribute '{0}' exists in the contract but not the implementation.", attribName, target.FullName()); added = true; break; case DifferenceType.Changed: //TODO: Add some more logic to check the two lists of attributes which have the same type. break; case DifferenceType.Removed: // Removing attributes is OK break; } } return(added); }
private DifferenceType CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes) { DifferenceType difference = DifferenceType.Unchanged; AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings); AttributeComparer attributeComparer = new AttributeComparer(); attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer)); attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer)); foreach (var group in attributeMapping.Attributes) { switch (group.Difference) { case DifferenceType.Added: { ITypeReference type = group.Representative.Attributes.First().Type; if (AttributeFilter.ShouldExclude(type.DocId())) { break; } // Allow for additions differences.Add(new Difference("AddedAttribute", $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Implementation} but not the {Contract}.")); break; } case DifferenceType.Changed: { ITypeReference type = group.Representative.Attributes.First().Type; if (AttributeFilter.ShouldExclude(type.DocId())) { break; } string contractKey = attributeComparer.GetKey(group[0].Attributes.First()); string implementationKey = attributeComparer.GetKey(group[1].Attributes.First()); differences.AddIncompatibleDifference("CannotChangeAttribute", $"Attribute '{type.FullName()}' on '{target.FullName()}' changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}."); difference = DifferenceType.Changed; break; } case DifferenceType.Removed: { ITypeReference type = group.Representative.Attributes.First().Type; if (AttributeFilter.ShouldExclude(type.DocId())) { break; } differences.AddIncompatibleDifference("CannotRemoveAttribute", $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Contract} but not the {Implementation}."); // removals of an attribute are considered a "change" of the type difference = DifferenceType.Changed; break; } } } return(difference); }
private bool CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes, IReference member = null) { AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings); AttributeComparer attributeComparer = new AttributeComparer(); attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer)); attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer)); string errString = $"'{target.FullName()}'"; if (target is IParameterDefinition || target is IGenericParameter) { errString = target is IGenericParameter ? "generic param" : "parameter"; errString += $" '{target.FullName()}' on member '{member?.FullName()}'"; } bool changed = false; foreach (var group in attributeMapping.Attributes) { switch (group.Difference) { case DifferenceType.Added: { ITypeReference type = group.Representative.Attributes.First().Type; if (AttributeFilter.ShouldExclude(type.DocId())) { break; } // Allow for additions differences.Add(new Difference("AddedAttribute", $"Attribute '{type.FullName()}' exists on {errString} in the {Implementation} but not the {Contract}.")); changed = true; break; } case DifferenceType.Changed: { ITypeReference type = group.Representative.Attributes.First().Type; if (AttributeFilter.ShouldExclude(type.DocId())) { break; } string contractKey = attributeComparer.GetKey(group[0].Attributes.First()); string implementationKey = attributeComparer.GetKey(group[1].Attributes.First()); differences.AddIncompatibleDifference("CannotChangeAttribute", $"Attribute '{type.FullName()}' on {errString} changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}."); changed = true; break; } case DifferenceType.Removed: { ITypeReference type = group.Representative.Attributes.First().Type; if (AttributeFilter.ShouldExclude(type.DocId())) { break; } differences.AddIncompatibleDifference("CannotRemoveAttribute", $"Attribute '{type.FullName()}' exists on {errString} in the {Contract} but not the {Implementation}."); // removals of an attribute are considered a "change" of the type changed = true; break; } } } return(changed); }
//private bool AnySecurityAttributeAdded(IDifferences differences, IReference target, IEnumerable<ISecurityAttribute> attribues1, IEnumerable<ISecurityAttribute> attributes2) //{ // return AnyAttributeAdded(differences, target, attribues1.SelectMany(a => a.Attributes), attributes2.SelectMany(a => a.Attributes)); //} private void CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable<ICustomAttribute> implAttributes, IEnumerable<ICustomAttribute> contractAttributes) { AttributesMapping<IEnumerable<ICustomAttribute>> attributeMapping = new AttributesMapping<IEnumerable<ICustomAttribute>>(_settings); attributeMapping.AddMapping(0, contractAttributes); attributeMapping.AddMapping(1, implAttributes); foreach (var group in attributeMapping.Attributes) { switch (group.Difference) { case DifferenceType.Added: { ITypeReference type = group.Representative.Attributes.First().Type; string attribName = type.FullName(); if (FilterAttribute(attribName)) break; AttributeAdded(target, attribName); differences.AddIncompatibleDifference("CannotAddAttribute", "Attribute '{0}' exists on '{1}' in the implementation but not the contract.", attribName, target.FullName()); break; } case DifferenceType.Changed: //TODO: Add some more logic to check the two lists of attributes which have the same type. break; case DifferenceType.Removed: { ITypeReference type = group.Representative.Attributes.First().Type; string attribName = type.FullName(); if (s_IgnorableAttributes.Contains(attribName)) break; differences.AddIncompatibleDifference("CannotRemoveAttribute", "Attribute '{0}' exists on '{1}' in the contract but not the implementation.", attribName, target.FullName()); break; } } } }
private bool AnyAttributeAdded(IDifferences differences, IReference target, IEnumerable<ICustomAttribute> implAttributes, IEnumerable<ICustomAttribute> contractAttributes) { bool added = false; AttributesMapping<IEnumerable<ICustomAttribute>> attributeMapping = new AttributesMapping<IEnumerable<ICustomAttribute>>(_settings); attributeMapping.AddMapping(0, implAttributes); attributeMapping.AddMapping(1, contractAttributes); foreach (var group in attributeMapping.Attributes) { switch (group.Difference) { case DifferenceType.Added: ITypeReference type = group.Representative.Attributes.First().Type; string attribName = type.FullName(); if (s_IgnorableAttributes.Contains(attribName)) break; differences.AddIncompatibleDifference(this, "Attribute '{0}' exists in the contract but not the implementation.", attribName, target.FullName()); added = true; break; case DifferenceType.Changed: //TODO: Add some more logic to check the two lists of attributes which have the same type. break; case DifferenceType.Removed: // Removing attributes is OK break; } } return added; }