Skip to content

Commit

Permalink
Fix instances of SelfAssertion bug pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirras committed Sep 25, 2024
1 parent 4eaa097 commit ba1fbc7
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 160 deletions.
5 changes: 5 additions & 0 deletions delphi-frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<artifactId>archunit-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,25 @@
*/
package au.com.integradev.delphi.msbuild.condition;

import static org.assertj.core.api.Assertions.assertThat;

import au.com.integradev.delphi.msbuild.utils.VersionUtils;
import java.util.stream.Stream;
import com.google.common.testing.EqualsTester;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

class VersionTest {
static class EqualVersionArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("1.0.0.0", "1.0.0.0"),
Arguments.of("1.1.0.0", "1.1.0.0"),
Arguments.of("1.1.1.0", "1.1.1.0"),
Arguments.of("1.1.1.1", "1.1.1.1"));
}
}

static class UnequalVersionArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("2.0.0.0", "1.0.0.0"),
Arguments.of("1.1.0.0", "1.0.0.0"),
Arguments.of("1.0.1.0", "1.0.0.0"),
Arguments.of("1.0.0.1", "1.0.0.0"));
}
}

@ParameterizedTest(name = "\"{0}\" should be equal to: {1}")
@ArgumentsSource(EqualVersionArgumentsProvider.class)
void testEqualVersions(String left, String right) {
assertThat(VersionUtils.parse(left)).isEqualTo(VersionUtils.parse(right));
}

@ParameterizedTest(name = "\"{0}\" should not be equal to: {1}")
@ArgumentsSource(UnequalVersionArgumentsProvider.class)
void testUnequalVersions(String left, String right) {
assertThat(VersionUtils.parse(left)).isNotEqualTo(VersionUtils.parse(right));
}

@Test
void testEqualToSameInstance() {
Version version = VersionUtils.parse("1.0.0.0").orElseThrow();
assertThat(version).isEqualTo(version);
}

@Test
void testNotEqualToNull() {
assertThat(VersionUtils.parse("1.0.0.0").orElseThrow()).isNotEqualTo(null);
void testEquals() {
new EqualsTester()
.addEqualityGroup(version("1.0"))
.addEqualityGroup(version("1.0.0"))
.addEqualityGroup(version("1.0.0.0"))
.addEqualityGroup(version("1.1.0.0"))
.addEqualityGroup(version("1.1.1.0"))
.addEqualityGroup(version("1.1.1.1"))
.addEqualityGroup(version("2.0.0.0"))
.testEquals();
}

@Test
void testNotEqualToUnrelatedObject() {
assertThat(VersionUtils.parse("1.0.0.0").orElseThrow()).isNotEqualTo(new Object());
private static Version version(String input) {
return VersionUtils.parse(input).orElseThrow(AssertionError::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import au.com.integradev.delphi.type.parameter.IntrinsicParameter;
import com.google.common.testing.EqualsTester;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -58,7 +59,7 @@ void testIsClassInvocable() {

@Test
void testEquals() {
OperatorIntrinsic equal =
OperatorIntrinsic base =
new OperatorIntrinsic("Foo", List.of(TypeFactory.untypedType()), TypeFactory.voidType());
OperatorIntrinsic differentName =
new OperatorIntrinsic("Bar", List.of(TypeFactory.untypedType()), TypeFactory.voidType());
Expand All @@ -67,17 +68,11 @@ void testEquals() {
OperatorIntrinsic differentReturnType =
new OperatorIntrinsic("Foo", List.of(TypeFactory.untypedType()), TypeFactory.untypedType());

assertThat(INTRINSIC)
.isEqualTo(INTRINSIC)
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isEqualTo(equal)
.hasSameHashCodeAs(equal)
.isNotEqualTo(differentName)
.doesNotHaveSameHashCodeAs(differentName)
.isNotEqualTo(differentParameters)
.doesNotHaveSameHashCodeAs(differentParameters)
.isNotEqualTo(differentReturnType)
.doesNotHaveSameHashCodeAs(differentReturnType);
new EqualsTester()
.addEqualityGroup(base)
.addEqualityGroup(differentName)
.addEqualityGroup(differentParameters)
.addEqualityGroup(differentReturnType)
.testEquals();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import au.com.integradev.delphi.antlr.ast.node.SimpleNameDeclarationNodeImpl;
import au.com.integradev.delphi.file.DelphiFile;
import au.com.integradev.delphi.utils.types.TypeFactoryUtils;
import com.google.common.testing.EqualsTester;
import org.antlr.runtime.CommonToken;
import org.junit.jupiter.api.Test;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.EnumElementNameDeclaration;
Expand All @@ -41,17 +42,18 @@ void testEquals() {
EnumElementNameDeclaration foo = createEnumElement("Foo");
EnumElementNameDeclaration otherFoo = createEnumElement("Foo");
EnumElementNameDeclaration bar = createEnumElement("Bar");
EnumElementNameDeclaration baz = createEnumElement("Baz");

new EqualsTester()
.addEqualityGroup(foo, otherFoo)
.addEqualityGroup(bar)
.addEqualityGroup(baz)
.testEquals();

assertThat(foo)
.isEqualTo(foo)
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isEqualTo(otherFoo)
.isEqualByComparingTo(otherFoo)
.hasSameHashCodeAs(otherFoo)
.isNotEqualTo(bar)
.isNotEqualByComparingTo(bar)
.doesNotHaveSameHashCodeAs(bar);
.isNotEqualByComparingTo(baz);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import au.com.integradev.delphi.symbol.SymbolicNode;
import au.com.integradev.delphi.type.factory.TypeFactoryImpl;
import au.com.integradev.delphi.utils.types.TypeFactoryUtils;
import com.google.common.testing.EqualsTester;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -80,7 +81,7 @@ class RoutineNameDeclarationTest {

@Test
void testEquals() {
RoutineNameDeclaration equals =
RoutineNameDeclaration base =
new RoutineNameDeclarationImpl(
LOCATION,
FULLY_QUALIFIED_NAME,
Expand Down Expand Up @@ -247,42 +248,31 @@ void testEquals() {
Collections.emptyList(),
ATTRIBUTE_TYPES);

new EqualsTester()
.addEqualityGroup(ROUTINE, base)
.addEqualityGroup(forwardDeclaration)
.addEqualityGroup(implementationDeclaration)
.addEqualityGroup(differentLocation)
.addEqualityGroup(differentFullyQualifiedName)
.addEqualityGroup(differentReturnType)
.addEqualityGroup(differentDirectives)
.addEqualityGroup(differentIsClassInvocable)
.addEqualityGroup(differentIsCallable)
.addEqualityGroup(differentRoutineType)
.addEqualityGroup(differentTypeParameters)
.testEquals();

assertThat(ROUTINE)
.isEqualTo(ROUTINE)
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isEqualTo(equals)
.hasSameHashCodeAs(equals)
.isEqualByComparingTo(equals)
.isNotEqualTo(forwardDeclaration)
.doesNotHaveSameHashCodeAs(forwardDeclaration)
.isEqualByComparingTo(base)
.isNotEqualByComparingTo(forwardDeclaration)
.isNotEqualTo(implementationDeclaration)
.doesNotHaveSameHashCodeAs(implementationDeclaration)
.isNotEqualByComparingTo(implementationDeclaration)
.isNotEqualTo(differentLocation)
.doesNotHaveSameHashCodeAs(differentLocation)
.isNotEqualByComparingTo(differentLocation)
.isNotEqualTo(differentFullyQualifiedName)
.doesNotHaveSameHashCodeAs(differentFullyQualifiedName)
.isNotEqualByComparingTo(differentFullyQualifiedName)
.isNotEqualTo(differentReturnType)
.doesNotHaveSameHashCodeAs(differentReturnType)
.isNotEqualByComparingTo(differentReturnType)
.isNotEqualTo(differentDirectives)
.doesNotHaveSameHashCodeAs(differentDirectives)
.isNotEqualByComparingTo(differentDirectives)
.isNotEqualTo(differentIsClassInvocable)
.doesNotHaveSameHashCodeAs(differentIsClassInvocable)
.isNotEqualByComparingTo(differentIsClassInvocable)
.isNotEqualTo(differentIsCallable)
.doesNotHaveSameHashCodeAs(differentIsCallable)
.isNotEqualByComparingTo(differentIsCallable)
.isNotEqualTo(differentRoutineType)
.doesNotHaveSameHashCodeAs(differentRoutineType)
.isNotEqualByComparingTo(differentRoutineType)
.isNotEqualTo(differentTypeParameters)
.doesNotHaveSameHashCodeAs(differentTypeParameters)
.isNotEqualByComparingTo(differentTypeParameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import au.com.integradev.delphi.antlr.DelphiLexer;
import au.com.integradev.delphi.antlr.ast.node.CommonDelphiNodeImpl;
import au.com.integradev.delphi.type.generic.TypeParameterTypeImpl;
import com.google.common.testing.EqualsTester;
import org.antlr.runtime.CommonToken;
import org.junit.jupiter.api.Test;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.TypeParameterNameDeclaration;
Expand All @@ -38,18 +39,13 @@ void testEquals() {
TypeParameterNameDeclaration fooWithDifferentTypeInstance = createTypeParameter("Foo");
TypeParameterNameDeclaration bar = createTypeParameter("Bar");

assertThat(foo)
.isEqualTo(foo)
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isEqualTo(otherFoo)
.isEqualByComparingTo(otherFoo)
.hasSameHashCodeAs(otherFoo)
.isNotEqualTo(fooWithDifferentTypeInstance)
.doesNotHaveSameHashCodeAs(fooWithDifferentTypeInstance)
.isNotEqualTo(bar)
.isNotEqualByComparingTo(bar)
.doesNotHaveSameHashCodeAs(bar);
new EqualsTester()
.addEqualityGroup(foo, otherFoo)
.addEqualityGroup(fooWithDifferentTypeInstance)
.addEqualityGroup(bar)
.testEquals();

assertThat(foo).isEqualByComparingTo(otherFoo).isNotEqualByComparingTo(bar);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import au.com.integradev.delphi.antlr.ast.node.QualifiedNameDeclarationNodeImpl;
import au.com.integradev.delphi.antlr.ast.node.UnitImportNodeImpl;
import au.com.integradev.delphi.file.DelphiFile;
import com.google.common.testing.EqualsTester;
import java.io.File;
import java.nio.file.Path;
import org.antlr.runtime.CommonToken;
Expand All @@ -45,17 +46,13 @@ void testEquals() {
UnitImportNameDeclaration differentName = createImport("Bar");
UnitImportNameDeclaration differentOriginalDeclaration = createImport("Foo", createUnit("Foo"));

assertThat(foo)
.isEqualTo(foo)
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isEqualTo(otherFoo)
.isEqualByComparingTo(otherFoo)
.hasSameHashCodeAs(otherFoo)
.isNotEqualTo(differentName)
.doesNotHaveSameHashCodeAs(differentName)
.isNotEqualTo(differentOriginalDeclaration)
.doesNotHaveSameHashCodeAs(differentOriginalDeclaration);
new EqualsTester()
.addEqualityGroup(foo, otherFoo)
.addEqualityGroup(differentName)
.addEqualityGroup(differentOriginalDeclaration)
.testEquals();

assertThat(foo).isEqualByComparingTo(otherFoo).isNotEqualByComparingTo(differentName);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import au.com.integradev.delphi.antlr.ast.node.IdentifierNodeImpl;
import au.com.integradev.delphi.antlr.ast.node.QualifiedNameDeclarationNodeImpl;
import au.com.integradev.delphi.file.DelphiFile;
import com.google.common.testing.EqualsTester;
import java.io.File;
import java.nio.file.Path;
import org.antlr.runtime.CommonToken;
Expand All @@ -42,18 +43,16 @@ void testEquals() {
UnitNameDeclaration fooWithDifferentPath = createUnit("Foo", "/bar/foo.pas");
UnitNameDeclaration bar = createUnit("Bar", "/bar.pas");

new EqualsTester()
.addEqualityGroup(foo, otherFoo)
.addEqualityGroup(fooWithDifferentPath)
.addEqualityGroup(bar)
.testEquals();

assertThat(foo)
.isEqualTo(foo)
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isEqualTo(otherFoo)
.hasSameHashCodeAs(otherFoo)
.isNotEqualTo(fooWithDifferentPath)
.isEqualByComparingTo(otherFoo)
.isNotEqualByComparingTo(fooWithDifferentPath)
.doesNotHaveSameHashCodeAs(fooWithDifferentPath)
.isNotEqualTo(bar)
.isNotEqualByComparingTo(bar)
.doesNotHaveSameHashCodeAs(bar);
.isNotEqualByComparingTo(bar);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.mockito.Mockito.mock;

import au.com.integradev.delphi.symbol.SymbolicNode;
import com.google.common.testing.EqualsTester;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.plugins.communitydelphi.api.symbol.NameOccurrence;
Expand Down Expand Up @@ -53,32 +54,32 @@ void testIsAttributeReference() {
@Test
void testEquals() {
SymbolicNode foo = SymbolicNode.imaginary("Foo", DelphiScope.unknownScope());

NameOccurrence occurrenceA = new NameOccurrenceImpl(foo);
assertThat(occurrenceA).isEqualTo(occurrenceA).isNotEqualTo(null).isNotEqualTo(new Object());

NameOccurrence occurrenceB = new NameOccurrenceImpl(foo);
assertThat(occurrenceA).isEqualTo(occurrenceB);

SymbolicNode bar = SymbolicNode.imaginary("Bar", DelphiScope.unknownScope());
NameOccurrence occurrenceC = new NameOccurrenceImpl(bar);
assertThat(occurrenceA).isNotEqualTo(occurrenceC);

NameOccurrenceImpl occurrenceD = new NameOccurrenceImpl(foo);
occurrenceD.setIsExplicitInvocation(true);
assertThat(occurrenceA).isNotEqualTo(occurrenceD);

NameOccurrenceImpl occurrenceE = new NameOccurrenceImpl(foo);
occurrenceE.setIsGeneric();
assertThat(occurrenceA).isNotEqualTo(occurrenceE);

NameOccurrenceImpl occurrenceF = new NameOccurrenceImpl(foo);
occurrenceF.setNameDeclaration(mock(NameDeclaration.class));
assertThat(occurrenceA).isNotEqualTo(occurrenceF);

NameOccurrenceImpl occurrenceG = new NameOccurrenceImpl(foo);
occurrenceG.setTypeArguments(List.of(TypeFactory.unknownType()));
assertThat(occurrenceA).isNotEqualTo(occurrenceG);

new EqualsTester()
.addEqualityGroup(occurrenceA, occurrenceB)
.addEqualityGroup(occurrenceC)
.addEqualityGroup(occurrenceD)
.addEqualityGroup(occurrenceE)
.addEqualityGroup(occurrenceF)
.addEqualityGroup(occurrenceG)
.testEquals();
}

@Test
Expand Down
Loading

0 comments on commit ba1fbc7

Please sign in to comment.