public static void MultipleRightsNoDifferences() { string leftSyntax = @" namespace CompatTests { public class First { public class FirstNested { public class SecondNested { public class ThirdNested { public string MyField; } } } } } "; string[] rightSyntaxes = new[] { leftSyntax, leftSyntax, leftSyntax, leftSyntax }; ApiComparer differ = new(); ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(leftSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); AssertExtensions.MultiRightEmptyDifferences(left.MetadataInformation, rightSyntaxes.Length, differences); }
public void MultipleRightsMissingTypeForwardIsReported() { string forwardedTypeSyntax = @" namespace CompatTests { public class ForwardedTestType { } } "; string rightWithForward = @" [assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(CompatTests.ForwardedTestType))] "; string[] rightSyntaxes = new[] { rightWithForward, "namespace CompatTests { internal class Foo { } }", rightWithForward }; IEnumerable <string> references = new[] { forwardedTypeSyntax }; ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(forwardedTypeSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes, references); ApiComparer differ = new(); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); CompatDifference[][] expected = { Array.Empty <CompatDifference>(), new[] { new CompatDifference(DiagnosticIds.TypeMustExist,string.Empty, DifferenceType.Removed, "T:CompatTests.ForwardedTestType"), }, Array.Empty <CompatDifference>(), }; AssertExtensions.MultiRightResult(left.MetadataInformation, expected, differences); }
public static void MultipleRightsMissingTypesOnLeftAreReported() { string leftSyntax = @" namespace CompatTests { public class First { } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public class First { } } ", @" namespace CompatTests { public class First { } public class Second { } } ", @" namespace CompatTests { public class First { } public class Third { } } " }; ApiComparer differ = new(); differ.StrictMode = true; ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(leftSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); CompatDifference[][] expected = { Array.Empty <CompatDifference>(), new[] { new CompatDifference(DiagnosticIds.TypeMustExist,string.Empty, DifferenceType.Added, "T:CompatTests.Second"), }, new[] { new CompatDifference(DiagnosticIds.TypeMustExist,string.Empty, DifferenceType.Added, "T:CompatTests.Third"), }, }; AssertExtensions.MultiRightResult(left.MetadataInformation, expected, differences); }
public void MultipleRightsNoDifferences() { string leftSyntax = @" namespace CompatTests { public class First { } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public class First { } }", @" namespace CompatTests { public class First { protected First() { } } }", @" namespace CompatTests { public class First { internal First() { } } }" }; IAssemblySymbol left = SymbolFactory.GetAssemblyFromSyntax(leftSyntax); MetadataInformation leftMetadata = new("left", "net6.0", "ref/a.dll"); ElementContainer <IAssemblySymbol> leftContainer = new(left, leftMetadata); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); ApiComparer differ = new(); differ.IncludeInternalSymbols = true; IEnumerable <(MetadataInformation left, MetadataInformation right, IEnumerable <CompatDifference> differences)> result = differ.GetDifferences(leftContainer, right); AssertExtensions.MultiRightEmptyDifferences(leftMetadata, 3, result); }
public void MultipleRightsTypeForwardExistsOnAll() { string forwardedTypeSyntax = @" namespace CompatTests { public class ForwardedTestType { } } "; string rightSyntax = @" [assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(CompatTests.ForwardedTestType))] "; string[] rightSyntaxes = new[] { rightSyntax, rightSyntax, rightSyntax, rightSyntax, rightSyntax }; IEnumerable <string> references = new[] { forwardedTypeSyntax }; ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(forwardedTypeSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes, references); ApiComparer differ = new(); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); AssertExtensions.MultiRightEmptyDifferences(left.MetadataInformation, rightSyntaxes.Length, differences); }
public static void MultipleRightsMissingMembersAreReported() { string leftSyntax = @" namespace CompatTests { public class First { public class FirstNested { public string MyProperty { get; } public class SecondNested { public int MyMethod() => 0; public class ThirdNested { public string MyField; } } } } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public class First { public class FirstNested { public string MyProperty { get; } public class SecondNested { public class ThirdNested { } } } } } ", @" namespace CompatTests { public class First { public class FirstNested { public class SecondNested { public int MyMethod() => 0; public class ThirdNested { public string MyField; } } } } } ", @" namespace CompatTests { public class First { public class FirstNested { public string MyProperty { get; } public class SecondNested { public class ThirdNested { public string MyField; } } } } } " }; ApiComparer differ = new(); ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(leftSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); CompatDifference[][] expectedDiffs = { new[] { new CompatDifference(DiagnosticIds.MemberMustExist, string.Empty, DifferenceType.Removed, "F:CompatTests.First.FirstNested.SecondNested.ThirdNested.MyField"), new CompatDifference(DiagnosticIds.MemberMustExist, string.Empty, DifferenceType.Removed, "M:CompatTests.First.FirstNested.SecondNested.MyMethod"), }, new[] { new CompatDifference(DiagnosticIds.MemberMustExist, string.Empty, DifferenceType.Removed, "M:CompatTests.First.FirstNested.get_MyProperty"), }, new[] { new CompatDifference(DiagnosticIds.MemberMustExist, string.Empty, DifferenceType.Removed, "M:CompatTests.First.FirstNested.SecondNested.MyMethod"), }, }; AssertExtensions.MultiRightResult(left.MetadataInformation, expectedDiffs, differences); }
public void MultipleRightsMetadataInformationIsUsedAsName() { string leftSyntax = @" namespace CompatTests { public class First { public class FirstNested { public class SecondNested { public class ThirdNested { public string MyField; } } } } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public class First { public class FirstNested { public class SecondNested { public class ThirdNested { } } } } } ", @" namespace CompatTests { public class First { public class FirstNested { public class SecondNested { public class ThirdNested { } } } } } ", @" namespace CompatTests { public class First { public class FirstNested { public class SecondNested { public class ThirdNested { } } } } } " }; ApiComparer differ = new(); ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(leftSyntax), new MetadataInformation(string.Empty, string.Empty, "ref/net6.0/a.dll")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); int i = 0; foreach ((MetadataInformation, MetadataInformation, IEnumerable <CompatDifference> differences)diff in differences) { Assert.Single(diff.differences); AssertNames(diff.differences.First(), left.MetadataInformation.AssemblyId, right[i++].MetadataInformation.AssemblyId); } }
public void MultipleRightsAreReportedCorrectly() { string leftSyntax = @" namespace CompatTests { public class First { } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public class First { } }", @" namespace CompatTests { public class First { protected First() { } } }", @" namespace CompatTests { public sealed class First { } }", @" namespace CompatTests { public class First { private First() { } } }", }; IAssemblySymbol left = SymbolFactory.GetAssemblyFromSyntax(leftSyntax); MetadataInformation leftMetadata = new("left", "net6.0", "ref/a.dll"); ElementContainer <IAssemblySymbol> leftContainer = new(left, leftMetadata); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); ApiComparer differ = new(); IEnumerable <(MetadataInformation left, MetadataInformation right, IEnumerable <CompatDifference> differences)> result = differ.GetDifferences(leftContainer, right); CompatDifference[][] expectedDiffs = { Array.Empty <CompatDifference>(), Array.Empty <CompatDifference>(), new[] { new CompatDifference(DiagnosticIds.CannotSealType, string.Empty, DifferenceType.Changed, "T:CompatTests.First"), }, new[] { new CompatDifference(DiagnosticIds.CannotSealType, string.Empty, DifferenceType.Changed, "T:CompatTests.First"), new CompatDifference(DiagnosticIds.MemberMustExist,string.Empty, DifferenceType.Removed, "M:CompatTests.First.#ctor"), }, }; AssertExtensions.MultiRightResult(leftMetadata, expectedDiffs, result); }
public void MultipleRightsAreReported() { string leftSyntax = @" namespace CompatTests { public abstract class First { public abstract class FirstNested { public class SecondNested { public void SomeMethod() { } } } } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public abstract class First { public abstract class FirstNested { public class SecondNested { public void SomeMethod() { } } } } } ", @" namespace CompatTests { public abstract class First { public abstract class FirstNested { public abstract class SecondNested { public void SomeMethod() { } public abstract void SomeAbstractMethod(); } } } } ", @" namespace CompatTests { public abstract class First { public abstract class FirstNested { public abstract void FirstNestedAbstract(); public class SecondNested { public void SomeMethod() { } } } } } " }; ApiComparer differ = new(); ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(leftSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); CompatDifference[][] expectedDiffs = { Array.Empty <CompatDifference>(), new[] { new CompatDifference(DiagnosticIds.CannotAddAbstractMember,string.Empty, DifferenceType.Added, "M:CompatTests.First.FirstNested.SecondNested.SomeAbstractMethod"), }, new[] { new CompatDifference(DiagnosticIds.CannotAddAbstractMember,string.Empty, DifferenceType.Added, "M:CompatTests.First.FirstNested.FirstNestedAbstract"), }, }; AssertExtensions.MultiRightResult(left.MetadataInformation, expectedDiffs, differences); }
public void MultiRightReportsRightDifferences() { string leftSyntax = @" namespace CompatTests { public class First : FirstBase, IFirstInterface { } public class FirstBase : SecondBase { } public class SecondBase : ThirdBase, ISecondInterface { } public class ThirdBase { } public interface IFirstInterface { } internal interface ISecondInterface { } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public class First : FirstBase, IFirstInterface { } public class FirstBase : SecondBase { } public class SecondBase : ThirdBase, ISecondInterface { } public class ThirdBase { } public interface IFirstInterface { } internal interface ISecondInterface { } } ", @" namespace CompatTests { public class First : FirstBase { } public class FirstBase : NewSecondBase { } public class NewSecondBase : SecondBase, IFirstInterface { } public class SecondBase : NewThirdBase, ISecondInterface { } public class NewThirdBase { } public class ThirdBase { } public interface IFirstInterface { } internal interface ISecondInterface { } } ", @" namespace CompatTests { public class First : FirstBase { } public class FirstBase : NewSecondBase { } public class NewSecondBase : SecondBase { } public class SecondBase : NewThirdBase, ISecondInterface { } public class NewThirdBase : ThirdBase { } public class ThirdBase { } public interface IFirstInterface { } internal interface ISecondInterface { } } " }; IAssemblySymbol left = SymbolFactory.GetAssemblyFromSyntax(leftSyntax); MetadataInformation leftMetadata = new("left", "net6.0", "ref/a.dll"); ElementContainer <IAssemblySymbol> leftContainer = new(left, leftMetadata); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); ApiComparer differ = new(); IEnumerable <(MetadataInformation left, MetadataInformation right, IEnumerable <CompatDifference> differences)> result = differ.GetDifferences(leftContainer, right); CompatDifference[][] expectedDiffs = { Array.Empty <CompatDifference>(), new[] { new CompatDifference(DiagnosticIds.CannotRemoveBaseType, string.Empty, DifferenceType.Changed, "T:CompatTests.SecondBase"), }, new[] { new CompatDifference(DiagnosticIds.CannotRemoveBaseInterface,string.Empty, DifferenceType.Changed, "T:CompatTests.First"), }, }; AssertExtensions.MultiRightResult(leftMetadata, expectedDiffs, result); }
public void MultipleRightsAreReported() { string leftSyntax = @" namespace CompatTests { public interface IFoo { string MyMethod(); int MyProperty { get; set; } event System.EventHandler MyEvent; } } "; string[] rightSyntaxes = new[] { @" namespace CompatTests { public interface IFoo { string MyMethod(); int MyProperty { get; set; } event System.EventHandler MyEvent; // .NET Framework doesn't support default implementations. #if !NETFRAMEWORK int MyPropertyWithDIM { get => 0; set { } } #endif } } ", @" namespace CompatTests { public interface IFoo { string MyMethod(); int MyProperty { get; set; } event System.EventHandler MyEvent; event System.EventHandler MyOtherEvent; // .NET Framework doesn't support default implementations. #if !NETFRAMEWORK static int MyField = 3; #endif } } ", @" namespace CompatTests { public interface IFoo { string MyMethod(); string MyOtherMethod(); int MyProperty { get; set; } event System.EventHandler MyEvent; // .NET Framework doesn't support default implementations. #if !NETFRAMEWORK string MyOtherMethodWithDIM() => string.Empty; event System.EventHandler MyOtherEventWithDIM { add { } remove { } } static int MyField = 3; #endif } } " }; ApiComparer differ = new(); ElementContainer <IAssemblySymbol> left = new(SymbolFactory.GetAssemblyFromSyntax(leftSyntax), new MetadataInformation(string.Empty, string.Empty, "ref")); IList <ElementContainer <IAssemblySymbol> > right = SymbolFactory.GetElementContainersFromSyntaxes(rightSyntaxes); IEnumerable <(MetadataInformation, MetadataInformation, IEnumerable <CompatDifference>)> differences = differ.GetDifferences(left, right); CompatDifference[][] expectedDiffs = { Array.Empty <CompatDifference>(), new[] { new CompatDifference(DiagnosticIds.CannotAddMemberToInterface,string.Empty, DifferenceType.Added, "E:CompatTests.IFoo.MyOtherEvent"), }, new[] { new CompatDifference(DiagnosticIds.CannotAddMemberToInterface,string.Empty, DifferenceType.Added, "M:CompatTests.IFoo.MyOtherMethod"), }, }; AssertExtensions.MultiRightResult(left.MetadataInformation, expectedDiffs, differences); }