All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gitlab.arturbosch.detekt.test.FindingsAssertions.kt Maven / Gradle / Ivy

There is a newer version: 1.23.7
Show newest version
package io.gitlab.arturbosch.detekt.test

import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.api.TextLocation
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.AbstractListAssert
import org.assertj.core.util.Objects.areEqual

fun assertThat(findings: List) = FindingsAssert(findings)

fun assertThat(finding: Finding) = FindingAssert(finding)

fun List.assert() = FindingsAssert(this)

class FindingsAssert(actual: List) :
        AbstractListAssert,
                Finding, FindingAssert>(actual, FindingsAssert::class.java) {

    override fun newAbstractIterableAssert(iterable: MutableIterable): FindingsAssert {
        throw UnsupportedOperationException("not implemented")
    }

    override fun toAssert(value: Finding?, description: String?): FindingAssert =
            FindingAssert(value).`as`(description)

    fun hasSourceLocations(vararg expected: SourceLocation) = apply {
        isNotNull

        val actualSources = actual.asSequence()
                .map { it.location.source }
                .sortedWith(compareBy({ it.line }, { it.column }))

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

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

    fun hasSourceLocation(line: Int, column: Int) = apply {
        hasSourceLocations(SourceLocation(line, column))
    }

    fun hasTextLocations(vararg expected: Pair) = apply {
        isNotNull

        val actualSources = actual.asSequence()
                .map { it.location.text }
                .sortedWith(compareBy({ it.start }, { it.end }))

        val expectedSources = expected.asSequence()
                .map { (start, end) -> TextLocation(start, end) }
                .sortedWith(compareBy({ it.start }, { it.end }))

        if (!areEqual(actualSources.toList(), expectedSources.toList())) {
            failWithMessage("Expected text locations to be ${expectedSources.toList()} but was ${actualSources.toList()}")
        }
    }

    fun hasTextLocations(vararg expected: String): FindingsAssert {
        isNotNull

        val finding = actual.firstOrNull()
        if (finding == null) {
            if (expected.isEmpty()) {
                return this
            } else {
                failWithMessage("Expected ${expected.size} findings but was 0")
            }
        }
        val code = finding!!.entity.ktElement?.containingKtFile?.text
        if (code == null) {
            failWithMessage("Expected ${expected.size} findings but was 0")
        }

        val textLocations = expected.map { snippet ->
            val index = code!!.indexOf(snippet)
            if (index < 0) {
                failWithMessage("The snippet \"$snippet\" doesn't exist in the code")
            } else {
                if (code.indexOf(snippet, index + 1) >= 0) {
                    failWithMessage("The snippet \"$snippet\" appears multiple times in the code")
                }
            }
            index to index + snippet.length
        }.toTypedArray()

        return hasTextLocations(*textLocations)
    }
}

class FindingAssert(val actual: Finding?) : AbstractAssert(actual, FindingAssert::class.java)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy