Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method param matching #158

Merged
merged 2 commits into from Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/PublicApiGenerator/MethodNameBuilder.cs
@@ -1,5 +1,7 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

namespace PublicApiGenerator
Expand All @@ -18,10 +20,24 @@ public static class MethodNameBuilder

var isNew = methodDefinition.IsNew(typeDef => typeDef?.Methods, e =>
e.Name.Equals(methodDefinition.Name, StringComparison.Ordinal) &&
e.Parameters.Count == methodDefinition.Parameters.Count);
e.Parameters.Count == methodDefinition.Parameters.Count &&
e.Parameters.SequenceEqual(methodDefinition.Parameters, new ParameterTypeComparer()));

return ModifierMarkerNameBuilder.Build(methodDefinition, attributes, isNew, name,
CodeNormalizer.MethodModifierMarkerTemplate);
}

class ParameterTypeComparer : IEqualityComparer<ParameterDefinition>
{
public bool Equals(ParameterDefinition x, ParameterDefinition y)
{
return x?.ParameterType == y?.ParameterType;
}

public int GetHashCode(ParameterDefinition obj)
{
return obj.GetHashCode();
}
}
}
}
28 changes: 28 additions & 0 deletions src/PublicApiGeneratorTests/Method_modifiers.cs
Expand Up @@ -299,6 +299,20 @@ public class ClassWithPublicMethodHiding : PublicApiGeneratorTests.Examples.Clas
}");
}

[Fact]
public void Should_not_output_new_when_base_differs_in_parameters()
{
AssertPublicApi<ClassWithBaseMethodConstraint>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ClassWithBaseMethodConstraint : PublicApiGeneratorTests.Examples.ClassWithBaseMethod
{
public ClassWithBaseMethodConstraint() { }
public void SomeMethod(PublicApiGeneratorTests.Examples.ClassWithBaseMethodConstraint input1, PublicApiGeneratorTests.Examples.ClassWithBaseMethodConstraint input2) { }
}
}");
}

[Fact]
public void Should_output_unsafe_modifier()
{
Expand Down Expand Up @@ -547,6 +561,20 @@ public new ClassWithMethodExtensions<T> Extend(string parameter)
return this;
}
}

public class ClassWithBaseMethod
{
public void SomeMethod(ClassWithBaseMethod input1, ClassWithBaseMethod input2)
{
}
}

public class ClassWithBaseMethodConstraint: ClassWithBaseMethod
{
public void SomeMethod(ClassWithBaseMethodConstraint input1, ClassWithBaseMethodConstraint input2)
{
}
}
}
// ReSharper restore ClassWithVirtualMembersNeverInherited.Global
// ReSharper restore RedundantOverridenMember
Expand Down