Skip to content

Commit

Permalink
Add option to assert end source locations
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Jul 23, 2022
1 parent 855065b commit cd5d604
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions detekt-test/api/detekt-test.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ public final class io/gitlab/arturbosch/detekt/test/FindingAssert : org/assertj/

public final class io/gitlab/arturbosch/detekt/test/FindingsAssert : org/assertj/core/api/AbstractListAssert {
public fun <init> (Ljava/util/List;)V
public final fun hasEndSourceLocation (II)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasEndSourceLocations ([Lio/gitlab/arturbosch/detekt/api/SourceLocation;)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasSourceLocation (II)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasSourceLocations ([Lio/gitlab/arturbosch/detekt/api/SourceLocation;)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasStartSourceLocation (II)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasStartSourceLocations ([Lio/gitlab/arturbosch/detekt/api/SourceLocation;)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasTextLocations ([Ljava/lang/String;)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public final fun hasTextLocations ([Lkotlin/Pair;)Lio/gitlab/arturbosch/detekt/test/FindingsAssert;
public synthetic fun newAbstractIterableAssert (Ljava/lang/Iterable;)Lorg/assertj/core/api/AbstractIterableAssert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.gitlab.arturbosch.detekt.api.TextLocation
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.AbstractListAssert
import org.assertj.core.util.CheckReturnValue
import java.util.Objects
import java.util.*

@CheckReturnValue
fun assertThat(findings: List<Finding>) = FindingsAssert(findings)
Expand All @@ -27,7 +27,10 @@ class FindingsAssert(actual: List<Finding>) :
override fun toAssert(value: Finding?, description: String?): FindingAssert =
FindingAssert(value).`as`(description)

fun hasSourceLocations(vararg expected: SourceLocation) = apply {
@Deprecated("Use hasStartSourceLocations instead", ReplaceWith("hasStartSourceLocations(*expected)"))
fun hasSourceLocations(vararg expected: SourceLocation) = hasStartSourceLocations(*expected)

fun hasStartSourceLocations(vararg expected: SourceLocation) = apply {
val actualSources = actual.asSequence()
.map { it.location.source }
.sortedWith(compareBy({ it.line }, { it.column }))
Expand All @@ -37,13 +40,35 @@ class FindingsAssert(actual: List<Finding>) :

if (!Objects.deepEquals(actualSources.toList(), expectedSources.toList())) {
failWithMessage(
"Expected source locations to be ${expectedSources.toList()} but was ${actualSources.toList()}"
"Expected start source locations to be ${expectedSources.toList()} but was ${actualSources.toList()}"
)
}
}

fun hasEndSourceLocations(vararg expected: SourceLocation) = apply {
val actualSources = actual.asSequence()
.map { it.location.endSource }
.sortedWith(compareBy({ it.line }, { it.column }))

val expectedSources = expected.asSequence()
.sortedWith(compareBy({ it.line }, { it.column }))

if (!Objects.deepEquals(actualSources.toList(), expectedSources.toList())) {
failWithMessage(
"Expected end source locations to be ${expectedSources.toList()} but was ${actualSources.toList()}"
)
}
}

fun hasSourceLocation(line: Int, column: Int) = apply {
hasSourceLocations(SourceLocation(line, column))
@Deprecated("Use hasStartSourceLocation instead", ReplaceWith("hasStartSourceLocation(line, column)"))
fun hasSourceLocation(line: Int, column: Int) = hasStartSourceLocation(line, column)

fun hasStartSourceLocation(line: Int, column: Int) = apply {
hasStartSourceLocations(SourceLocation(line, column))
}

fun hasEndSourceLocation(line: Int, column: Int) = apply {
hasEndSourceLocations(SourceLocation(line, column))
}

fun hasTextLocations(vararg expected: Pair<Int, Int>) = apply {
Expand Down

0 comments on commit cd5d604

Please sign in to comment.