public void FindMatchesDoesNotReturnMatchOnMovedTypeWhenGenericTypeCountsAreDifferent() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition().Set(x => { x.RawName = oldItem.RawName; var updatedGenericTypeParameters = new List <string>(x.GenericTypeParameters) { Guid.NewGuid().ToString() }; x.GenericTypeParameters = updatedGenericTypeParameters.AsReadOnly(); }); var oldItems = new List <ITypeDefinition> { oldItem }; var newItems = new List <ITypeDefinition> { newItem }; var sut = new TypeEvaluator(); var actual = sut.FindMatches(oldItems, newItems); actual.ItemsRemoved.Should().BeEquivalentTo(oldItems); actual.ItemsAdded.Should().BeEquivalentTo(newItems); actual.MatchingItems.Should().BeEmpty(); }
public void CompareMatchReturnsBreakingWhenGenericTypeParameterAddedWithGenericConstraints() { var parameters = new List <string> { "T" }.AsReadOnly(); var constraintList = new TestConstraintListDefinition { Name = "T", Constraints = new List <string> { "struct", "Enum" }.AsReadOnly() }; var constraints = new List <IConstraintListDefinition> { constraintList }.AsReadOnly(); var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition().Set(x => { x.GenericTypeParameters = parameters; x.GenericConstraints = constraints; }); var match = new ItemMatch <IGenericTypeElement>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().HaveCount(1); actual[0].ChangeType.Should().Be(SemVerChangeType.Breaking); actual[0].Message.Should().Contain("added"); }
public void FindMatchesReturnsMatchOnMovedTypeWithDeclaringTypes() { var oldParent = new TestClassDefinition(); var newParent = new TestClassDefinition().Set(x => { x.RawName = oldParent.RawName; }); var oldItem = new TestClassDefinition().Set(x => x.DeclaringType = oldParent); var newItem = new TestClassDefinition().Set(x => { x.RawName = oldItem.RawName; x.DeclaringType = newParent; }); var oldItems = new List <ITypeDefinition> { oldItem }; var newItems = new List <ITypeDefinition> { newItem }; var sut = new TypeEvaluator(); var actual = sut.FindMatches(oldItems, newItems); actual.ItemsRemoved.Should().BeEmpty(); actual.ItemsAdded.Should().BeEmpty(); actual.MatchingItems.Should().HaveCount(1); var match = actual.MatchingItems.First(); match.OldItem.Should().Be(oldItem); match.NewItem.Should().Be(newItem); }
public void CompareReturnsReturnsChangeTypeWhenAtLeastOneItemHidden(bool firstItemVisible, bool secondItemVisible, SemVerChangeType?changeType) { var oldMember = new TestClassDefinition() .Set(x => { x.IsVisible = firstItemVisible; }); var newMember = oldMember.JsonClone() .Set(x => { x.IsVisible = secondItemVisible; }); var match = new ItemMatch <IClassDefinition>(oldMember, newMember); var options = ComparerOptions.Default; var attributeProcessor = Substitute.For <IAttributeMatchProcessor>(); var sut = new Wrapper <IClassDefinition>(attributeProcessor, null); var actual = sut.CompareMatch(match, options).ToList(); _output.WriteResults(actual); if (changeType == null) { actual.Should().BeEmpty(); } else { actual.Should().HaveCount(1); actual[0].ChangeType.Should().Be(changeType); } }
public void CompareMatchReturnsEmptyWhenGenericTypeConstraintsMatch() { var parameters = new List <string> { "T" }.AsReadOnly(); var constraintList = new TestConstraintListDefinition { Name = "T", Constraints = new List <string> { "struct", "Enum" }.AsReadOnly() }; var constraints = new List <IConstraintListDefinition> { constraintList }.AsReadOnly(); var oldItem = new TestClassDefinition().Set(x => { x.GenericTypeParameters = parameters; x.GenericConstraints = constraints; }); var newItem = new TestClassDefinition().Set(x => { x.GenericTypeParameters = parameters; x.GenericConstraints = constraints; }); var match = new ItemMatch <IGenericTypeElement>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().BeEmpty(); }
public void FindMatchesReturnsMatchWhenTypesHaveSameSignatures() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition().Set(x => { x.RawName = oldItem.RawName; x.Namespace = oldItem.Namespace; }); var oldItems = new List <ITypeDefinition> { oldItem }; var newItems = new List <ITypeDefinition> { newItem }; var sut = new TypeEvaluator(); var actual = sut.FindMatches(oldItems, newItems); actual.ItemsRemoved.Should().BeEmpty(); actual.ItemsAdded.Should().BeEmpty(); actual.MatchingItems.Should().HaveCount(1); var match = actual.MatchingItems.First(); match.OldItem.Should().Be(oldItem); match.NewItem.Should().Be(newItem); }
public void CanCreateWithMatches() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var matchingItem = new ItemMatch <IClassDefinition>(oldItem, newItem); var matchingItems = new List <ItemMatch <IClassDefinition> > { matchingItem }; var removedItem = new TestClassDefinition(); var removedItems = new List <IClassDefinition> { removedItem }; var addedItem = new TestClassDefinition(); var addedItems = new List <IClassDefinition> { addedItem }; var sut = new MatchResults <IClassDefinition>(matchingItems, removedItems, addedItems); sut.MatchingItems.Should().BeEquivalentTo(matchingItems); sut.ItemsRemoved.Should().BeEquivalentTo(removedItems); sut.ItemsAdded.Should().BeEquivalentTo(addedItems); }
public void FindMatchesDoesNotReturnMatchWhenDeclaringTypesAreDifferent() { var oldParent = new TestClassDefinition(); var newParent = new TestClassDefinition(); var oldItem = new TestClassDefinition().Set(x => x.DeclaringType = oldParent); var newItem = new TestClassDefinition().Set(x => { x.RawName = oldItem.RawName; x.Namespace = oldItem.Namespace; x.DeclaringType = newParent; }); var oldItems = new List <ITypeDefinition> { oldItem }; var newItems = new List <ITypeDefinition> { newItem }; var sut = new TypeEvaluator(); var actual = sut.FindMatches(oldItems, newItems); actual.ItemsRemoved.Should().BeEquivalentTo(oldItems); actual.ItemsAdded.Should().BeEquivalentTo(newItems); actual.MatchingItems.Should().BeEmpty(); }
public void CalculateChangesReturnsResultsBasedOnElementVisibility(bool isVisible, bool expected) { var oldItems = Array.Empty <IClassDefinition>(); var newItem = new TestClassDefinition().Set(x => x.IsVisible = isVisible); var newItems = new List <IClassDefinition> { newItem }; var options = ComparerOptions.Default; var matchResults = new MatchResults <IClassDefinition>(Array.Empty <IClassDefinition>(), newItems); Service <IEvaluator <IClassDefinition> >().FindMatches(oldItems, newItems).Returns(matchResults); var actual = SUT.CalculateChanges(oldItems, newItems, options).ToList(); if (expected) { actual.Should().HaveCount(1); actual[0].ChangeType.Should().Be(SemVerChangeType.Feature); } else { actual.Should().BeEmpty(); } }
public void CompareReturnsBreakingWhenReturnTypeIsChangedAndParentTypesExist() { var oldGrandparent = new TestClassDefinition().Set(x => x.GenericTypeParameters = new List <string> { "TOld" }.AsReadOnly()); var oldParent = new TestClassDefinition().Set(x => x.DeclaringType = oldGrandparent); var oldMember = new TestMethodDefinition().Set(x => { x.DeclaringType = oldParent; x.ReturnType = "string"; }); var newGrandparent = new TestClassDefinition().Set(x => x.GenericTypeParameters = new List <string> { "TNew" }.AsReadOnly()); var newParent = new TestClassDefinition().Set(x => x.DeclaringType = newGrandparent); var newMember = new TestMethodDefinition().Set(x => { x.DeclaringType = newParent; x.ReturnType = "DateTime"; }); var match = new ItemMatch <TestMethodDefinition>(oldMember, newMember); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().NotBeEmpty(); actual[0].ChangeType.Should().Be(SemVerChangeType.Breaking); }
public void CompareReturnsEmptyWhenReturnTypeIsRenamedGenericTypeOnParentType() { var oldGrandparent = new TestClassDefinition().Set(x => x.GenericTypeParameters = new List <string> { "TOld" }.AsReadOnly()); var oldParent = new TestClassDefinition().Set(x => x.DeclaringType = oldGrandparent); var oldMember = new TestMethodDefinition().Set(x => { x.DeclaringType = oldParent; x.ReturnType = "TOld"; }); var newGrandparent = new TestClassDefinition().Set(x => x.GenericTypeParameters = new List <string> { "TNew" }.AsReadOnly()); var newParent = new TestClassDefinition().Set(x => x.DeclaringType = newGrandparent); var newMember = new TestMethodDefinition().Set(x => { x.DeclaringType = newParent; x.ReturnType = "TNew"; }); var match = new ItemMatch <TestMethodDefinition>(oldMember, newMember); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().BeEmpty(); }
public void CompareMatchThrowsExceptionWithNullOptions() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var match = new ItemMatch <IGenericTypeElement>(oldItem, newItem); Action action = () => SUT.CompareMatch(match, null !); action.Should().Throw <ArgumentNullException>(); }
public void CompareMatchReturnsEmptyWhenTypesAreEqual() { var item = new TestClassDefinition(); var match = new ItemMatch <IClassDefinition>(item, item); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options); actual.Should().BeEmpty(); }
public IEnumerable <TestClassDefinition> LoadClass(SyntaxTree tree) { if (!(tree.GetRoot() is CompilationUnitSyntax)) { yield break; } var root = tree.GetRoot() as CompilationUnitSyntax; if (root.Members.Count != 1 && !(root.Members.First() is NamespaceDeclarationSyntax)) { yield break; } var baseNamespace = root.Members.First() as NamespaceDeclarationSyntax; // Search for interfaces and classes var interfaces = baseNamespace.Members.OfType <InterfaceDeclarationSyntax>(); var classes = baseNamespace.Members.OfType <ClassDeclarationSyntax>(); // Search for constructors and methods foreach (var classDeclaration in classes) { var testClass = new TestClassDefinition { Namespace = $"{RemoveNewLine(baseNamespace.Name.GetText().ToString())}.Tests", TargetClassName = classDeclaration.Identifier.Text, TargetBaseType = GetTargetBaseType(classDeclaration) }; var constructors = classDeclaration.Members.OfType <ConstructorDeclarationSyntax>().ToList(); var constructorWithDependencies = constructors.OrderByDescending(_ => _.ParameterList.Parameters.Count).FirstOrDefault(); if (constructorWithDependencies != null) { foreach (var dependency in constructorWithDependencies.ParameterList.Parameters) { testClass.Dependencies.Add(new Dependency { Type = RemoveNewLine(dependency.Type.GetText().ToString()), Name = SanitizeTypeName(dependency.Type.GetText().ToString()) }); } } var publicMethods = classDeclaration.Members.OfType <MethodDeclarationSyntax>() .Where(method => method.Modifiers.Any(modifier => modifier.Text == "public")) .ToList(); publicMethods.ForEach(method => testClass.Methods.Add(method.Identifier.Text)); yield return(testClass); } }
public void CompareMatchReturnsEmptyWhenElementIsNotGenericType() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var match = new ItemMatch <IGenericTypeElement>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); actual.Should().BeEmpty(); }
public string ScaffoldTest(TestClassDefinition testClassDefinition, string testProject, string testPath) { var template = new UnitTestTemplate(testClassDefinition); var content = template.TransformText(); var directory = DetermineDirectory(testProject, testPath); var fileName = Path.Combine(directory, $"{testClassDefinition.ClassName}.cs"); File.WriteAllText(fileName, content); return(fileName); }
public void ThrowsExceptionWhenCreatedWithNullAddedMembersWithoutMatches() { var removedItem = new TestClassDefinition(); var removedItems = new List <IClassDefinition> { removedItem }; // ReSharper disable once ObjectCreationAsStatement Action action = () => new MatchResults <IClassDefinition>(removedItems, null !); action.Should().Throw <ArgumentNullException>(); }
public void CompareMatchReturnsEmptyWhenGenericTypeParametersHaveNoConstraints() { var parameters = new List <string> { "T" }.AsReadOnly(); var oldItem = new TestClassDefinition().Set(x => x.GenericTypeParameters = parameters); var newItem = new TestClassDefinition().Set(x => x.GenericTypeParameters = parameters); var match = new ItemMatch <IGenericTypeElement>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); actual.Should().BeEmpty(); }
public void AddElementChangedResultThrowsExceptionWithNullArguments() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var match = new ItemMatch <IClassDefinition>(oldItem, newItem); var aggregator = Substitute.For <IChangeResultAggregator>(); var formatter = Substitute.For <IMessageFormatter>(); Action action = () => aggregator.AddElementChangedResult(SemVerChangeType.Breaking, match, formatter, null !); action.Should().Throw <ArgumentNullException>(); }
public void CompareMatchReturnsBreakingWhenNamespaceChanged() { var oldItem = new TestClassDefinition(); var newItem = oldItem.JsonClone().Set(x => x.Namespace = Guid.NewGuid().ToString()); var match = new ItemMatch <IClassDefinition>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().HaveCount(1); actual[0].ChangeType.Should().Be(SemVerChangeType.Breaking); actual[0].Message.Should().NotBeNullOrWhiteSpace(); }
public void CompareMatchReturnsBreakingChangeWhenTypeChangedToStruct() { var oldItem = new TestClassDefinition(); var newItem = new TestStructDefinition(); var match = new ItemMatch <ITypeDefinition>(oldItem, newItem); var options = Model.Create <ComparerOptions>(); var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().HaveCount(1); actual[0].ChangeType.Should().Be(SemVerChangeType.Breaking); actual[0].Message.Should().NotBeNullOrWhiteSpace(); }
public void AddElementChangedResultThrowsExceptionWithNullMessageFormatter() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var match = new ItemMatch <IClassDefinition>(oldItem, newItem); var arguments = new FormatArguments(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), oldItem.Name, newItem.Name); var aggregator = Substitute.For <IChangeResultAggregator>(); Action action = () => aggregator.AddElementChangedResult(SemVerChangeType.Breaking, match, null !, arguments); action.Should().Throw <ArgumentNullException>(); }
public void CompareMatchReturnsResultBasedOnImplementedTypeChanges(string oldTypes, string newTypes, int expected) { var oldImplementedTypes = oldTypes.Split(',', StringSplitOptions.RemoveEmptyEntries); var newImplementedTypes = newTypes.Split(',', StringSplitOptions.RemoveEmptyEntries); var oldItem = new TestClassDefinition().Set(x => x.ImplementedTypes = oldImplementedTypes); var newItem = oldItem.JsonClone().Set(x => x.ImplementedTypes = newImplementedTypes); var match = new ItemMatch <IClassDefinition>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().HaveCount(expected); actual.All(x => x.ChangeType == SemVerChangeType.Breaking).Should().BeTrue(); }
public void CompareMatchReturnsBreakingWhenGenericTypeParameterAdded() { var parameters = new List <string> { "TKey", "TValue" }.AsReadOnly(); var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition().Set(x => x.GenericTypeParameters = parameters); var match = new ItemMatch <IGenericTypeElement>(oldItem, newItem); var options = ComparerOptions.Default; var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().HaveCount(1); actual[0].ChangeType.Should().Be(SemVerChangeType.Breaking); actual[0].Message.Should().Contain("added"); }
public void CanCreateWithoutMatches() { var removedItem = new TestClassDefinition(); var removedItems = new List <IClassDefinition> { removedItem }; var addedItem = new TestClassDefinition(); var addedItems = new List <IClassDefinition> { addedItem }; var sut = new MatchResults <IClassDefinition>(removedItems, addedItems); sut.MatchingItems.Should().BeEmpty(); sut.ItemsRemoved.Should().BeEquivalentTo(removedItems); sut.ItemsAdded.Should().BeEquivalentTo(addedItems); }
public void ThrowsExceptionWhenCreatedWithNullRemovedMembersWithMatches() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var matchingItem = new ItemMatch <IClassDefinition>(oldItem, newItem); var matchingItems = new List <ItemMatch <IClassDefinition> > { matchingItem }; var addedItem = new TestClassDefinition(); var addedItems = new List <IClassDefinition> { addedItem }; // ReSharper disable once ObjectCreationAsStatement Action action = () => new MatchResults <IClassDefinition>(matchingItems, null !, addedItems); action.Should().Throw <ArgumentNullException>(); }
public void FindMatchesDoesNotReturnMatchWhenNamespaceAndRawNameAreDifferent() { var oldItem = new TestClassDefinition(); var newItem = new TestClassDefinition(); var oldItems = new List <ITypeDefinition> { oldItem }; var newItems = new List <ITypeDefinition> { newItem }; var sut = new TypeEvaluator(); var actual = sut.FindMatches(oldItems, newItems); actual.ItemsRemoved.Should().BeEquivalentTo(oldItems); actual.ItemsAdded.Should().BeEquivalentTo(newItems); actual.MatchingItems.Should().BeEmpty(); }
public void CompareMatchReturnsClassModifierComparerResults() { var item = new TestClassDefinition(); var match = new ItemMatch <IClassDefinition>(item, item); var options = ComparerOptions.Default; var changeType = Model.Create <SemVerChangeType>(); var message = Guid.NewGuid().ToString(); var result = new ComparisonResult(changeType, item, item, message); var results = new[] { result }; Service <IClassModifiersComparer>() .CompareMatch( Arg.Is <ItemMatch <IModifiersElement <ClassModifiers> > >(x => x.OldItem == item && x.NewItem == item), options).Returns(results); var actual = SUT.CompareMatch(match, options).ToList(); _output.WriteResults(actual); actual.Should().HaveCount(1); actual[0].Should().BeEquivalentTo(result); }
public void CalculateChangesReturnsResultFromComparer(SemVerChangeType changeType, bool expected) { var oldItem = new TestClassDefinition(); var oldItems = new List <IClassDefinition> { oldItem }; var newItem = new TestClassDefinition(); var newItems = new List <IClassDefinition> { newItem }; var match = new ItemMatch <IClassDefinition>(oldItem, newItem); var matches = new List <ItemMatch <IClassDefinition> > { match }; var options = ComparerOptions.Default; var matchResults = new MatchResults <IClassDefinition>(matches, Array.Empty <IClassDefinition>(), Array.Empty <IClassDefinition>()); var message = Guid.NewGuid().ToString(); var result = new ComparisonResult(changeType, oldItem, newItem, message); var results = new List <ComparisonResult> { result }; Service <IEvaluator <IClassDefinition> >().FindMatches(oldItems, newItems).Returns(matchResults); Service <IItemComparer <IClassDefinition> >().CompareMatch(match, options).Returns(results); var actual = SUT.CalculateChanges(oldItems, newItems, options); if (expected) { actual.Should().BeEquivalentTo(results); } else { actual.Should().BeEmpty(); } }
public void SetUp() { _classDefinition1 = new TestClassDefinition(typeof(BaseType1)); _classDefinition2 = new TestClassDefinition(typeof(BT1Mixin1)); _methodInfo1 = typeof(BaseType1).GetMethod("VirtualMethod", Type.EmptyTypes); _methodInfo2 = typeof(BT1Mixin1).GetMethod("VirtualMethod"); _methodInfo3 = typeof(BT1Mixin2).GetMethod("VirtualMethod"); _methodInfoProtected = typeof(ClassWithDifferentMemberVisibilities).GetMethod( "ProtectedMethod", BindingFlags.NonPublic | BindingFlags.Instance); _methodInfoProtectedInternal = typeof(ClassWithDifferentMemberVisibilities).GetMethod( "ProtectedInternalMethod", BindingFlags.NonPublic | BindingFlags.Instance); _propertyInfoWithGetAndSet = typeof(ClassWithDifferentPropertyKinds).GetProperty("PropertyWithGetAndSet"); _propertyInfoWithGet = typeof(ClassWithDifferentPropertyKinds).GetProperty("PropertyWithGet"); _propertyInfoWithSet = typeof(ClassWithDifferentPropertyKinds).GetProperty("PropertyWithSet"); _eventInfo1 = typeof(ClassWithEvents).GetEvent("Event1"); _eventInfo2 = typeof(ClassWithEvents).GetEvent("Event2"); }