From 9b8a64ef7b2eb02d9433d01a9e7c29df1915e109 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Thu, 6 Aug 2020 17:46:31 +0300 Subject: [PATCH] [Gradle, JS] Add hasIntersection test ^KT-41054 fixed --- .../kotlin/gradle/targets/js/npm/NpmRange.kt | 8 +- .../gradle/targets/js/npm/NpmRangeTest.kt | 74 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt index 921e9199cb7..492bcd6d14b 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt @@ -111,7 +111,13 @@ infix fun NpmRange.intersect(other: NpmRange): NpmRange? { infix fun NpmRange.hasIntersection(other: NpmRange): Boolean { val maxStart = maxStart(this, other) val minEnd = minEnd(this, other) - return maxStart == null || minEnd == null || maxStart < minEnd + + return maxStart == null || minEnd == null || maxStart < minEnd || + run { + val startInclusive = if (maxStart == startVersion) this.startInclusive else other.startInclusive + val endInclusive = if (minEnd == endVersion) this.endInclusive else other.endInclusive + startInclusive && endInclusive && maxStart == minEnd + } } fun maxStart(a: NpmRange, b: NpmRange): SemVer? = diff --git a/libraries/tools/kotlin-gradle-plugin/src/test/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRangeTest.kt b/libraries/tools/kotlin-gradle-plugin/src/test/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRangeTest.kt index bd10a1cfe7e..da00a1d2db8 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/test/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRangeTest.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/test/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRangeTest.kt @@ -10,6 +10,80 @@ import kotlin.test.assertTrue class NpmRangeTest { + @Test + fun hasIntersectionTest() { + fun assertHasIntersection(range1: NpmRange, range2: NpmRange) = + assertTrue("Range $range1 and $range2 expected to have intersection, but actual is not") { + (range1 hasIntersection range2) && (range2 hasIntersection range1) + } + + assertHasIntersection(npmRange(endMajor = 1), npmRange(endMajor = 2)) + assertHasIntersection(npmRange(startMajor = 1), npmRange(startMajor = 2)) + assertHasIntersection(npmRange(startMajor = 1, endMajor = 4), npmRange(startMajor = 2, endMajor = 3)) + assertHasIntersection(npmRange(startMajor = 1, endMajor = 3), npmRange(startMajor = 2, endMajor = 4)) + assertHasIntersection( + npmRange( + startMajor = 1, + endMajor = 2, + endInclusive = true + ), + npmRange( + startMajor = 2, + endMajor = 3, + startInclusive = true + ) + ) + } + + @Test + fun hasNoIntersectionTest() { + fun assertHasNoIntersection(range1: NpmRange, range2: NpmRange) = + assertTrue("Range $range1 and $range2 expected to not have intersection, but actual is") { + !(range1 hasIntersection range2) && !(range2 hasIntersection range1) + } + + assertHasNoIntersection(npmRange(endMajor = 1), npmRange(startMajor = 2)) + assertHasNoIntersection(npmRange(startMajor = 1, endMajor = 2), npmRange(startMajor = 3, endMajor = 4)) + assertHasNoIntersection( + npmRange( + startMajor = 1, + endMajor = 2, + endInclusive = false + ), + npmRange( + startMajor = 2, + endMajor = 3, + startInclusive = true + ) + ) + + assertHasNoIntersection( + npmRange( + startMajor = 1, + endMajor = 2, + endInclusive = true + ), + npmRange( + startMajor = 2, + endMajor = 3, + startInclusive = false + ) + ) + + assertHasNoIntersection( + npmRange( + startMajor = 1, + endMajor = 2, + endInclusive = false + ), + npmRange( + startMajor = 2, + endMajor = 3, + startInclusive = false + ) + ) + } + @Test fun maxStartTest() { val nullRange1 = npmRange()