提交 3efbc944 编写于 作者: C Cyrus Najmabadi

Add tests.

上级 f1c9b4fe
...@@ -1575,5 +1575,50 @@ public override bool Equals(object obj) ...@@ -1575,5 +1575,50 @@ public override bool Equals(object obj)
} }
}"); }");
} }
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEqualsAndGetHashCode)]
public async Task TestEqualsBaseWithOverriddenEquals_Patterns()
{
await TestInRegularAndScript1Async(
@"using System.Collections.Generic;
class Base
{
public override bool Equals(object o)
{
}
}
class Program : Base
{
[|int i;
string S { get; }|]
}",
@"using System.Collections.Generic;
class Base
{
public override bool Equals(object o)
{
}
}
class Program : Base
{
int i;
string S { get; }
public override bool Equals(object obj)
{
return obj is Program program &&
base.Equals(obj) &&
i == program.i &&
S == program.S;
}
}",
index: 0);
}
} }
} }
...@@ -157,18 +157,19 @@ public static IMethodSymbol CreateEqualsMethod(this Compilation compilation, Imm ...@@ -157,18 +157,19 @@ public static IMethodSymbol CreateEqualsMethod(this Compilation compilation, Imm
// //
// myType != null // myType != null
expressions.Add(factory.ReferenceNotEqualsExpression(localNameExpression, factory.NullLiteralExpression())); expressions.Add(factory.ReferenceNotEqualsExpression(localNameExpression, factory.NullLiteralExpression()));
if (HasExistingBaseEqualsMethod(containingType, cancellationToken)) }
{
// If we're overriding something that also provided an overridden 'Equals', if (!containingType.IsValueType && HasExistingBaseEqualsMethod(containingType, cancellationToken))
// then ensure the base type thinks it is equals as well. {
// // If we're overriding something that also provided an overridden 'Equals',
// base.Equals(obj) // then ensure the base type thinks it is equals as well.
expressions.Add(factory.InvocationExpression( //
factory.MemberAccessExpression( // base.Equals(obj)
factory.BaseExpression(), expressions.Add(factory.InvocationExpression(
factory.IdentifierName(EqualsName)), factory.MemberAccessExpression(
objNameExpression)); factory.BaseExpression(),
} factory.IdentifierName(EqualsName)),
objNameExpression));
} }
AddMemberChecks(factory, compilation, members, localNameExpression, expressions); AddMemberChecks(factory, compilation, members, localNameExpression, expressions);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册