Skip to content

Commit

Permalink
Add test for using multiple Kotlin frameworks in single application
Browse files Browse the repository at this point in the history
  • Loading branch information
SvyatoslavScherbina committed Oct 18, 2019
1 parent 9180bef commit 4d7f39b
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
36 changes: 36 additions & 0 deletions backend.native/tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3735,6 +3735,42 @@ if (isAppleTarget(project)) {
}
swiftSources = ['framework/stdlib/stdlib.swift']
}

task testMultipleFrameworks(type: FrameworkTest) {
testName = "MultipleFrameworks"
final String firstFrameworkName = 'First'
final String secondFrameworkName = 'Second'
frameworkNames = [firstFrameworkName, secondFrameworkName]

final String dir = "$testOutputFramework/$testName"

konanArtifacts {
framework(firstFrameworkName, targets: [ targetName ]) {
srcDir 'framework/multiple/framework1'
srcDir 'framework/multiple/shared'
baseDir dir

if (!useCustomDist) {
dependsOn ":${targetName}CrossDistRuntime", ':commonDistRuntime', ':distCompiler'
}

extraOpts project.globalTestArgs
}

framework(secondFrameworkName, targets: [ targetName ]) {
srcDir 'framework/multiple/framework2'
srcDir 'framework/multiple/shared'
baseDir dir

if (!useCustomDist) {
dependsOn ":${targetName}CrossDistRuntime", ':commonDistRuntime', ':distCompiler'
}

extraOpts project.globalTestArgs
}
}
swiftSources = ['framework/multiple/multiple.swift']
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions backend.native/tests/framework/multiple/framework1/first.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/

@file:Suppress("UNUSED")

package multiple

interface I1 {
fun getFortyTwo(): Int
}

class I1Impl : I1 {
override fun getFortyTwo(): Int = 42
}

class C

fun getUnit(): Unit? = Unit
10 changes: 10 additions & 0 deletions backend.native/tests/framework/multiple/framework1/test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/

@file:Suppress("UNUSED")

package multiple

val name = "first"
18 changes: 18 additions & 0 deletions backend.native/tests/framework/multiple/framework2/second.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/

@file:Suppress("UNUSED")

package multiple

interface I2 {
fun getFortyTwo(): Int
}

fun getFortyTwoFrom(i2: I2): Int = i2.getFortyTwo()

class C

fun isUnit(obj: Any?): Boolean = (obj === Unit)
10 changes: 10 additions & 0 deletions backend.native/tests/framework/multiple/framework2/test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/

@file:Suppress("UNUSED")

package multiple

val name = "second"
51 changes: 51 additions & 0 deletions backend.native/tests/framework/multiple/multiple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/

import First
import Second

func testClashingNames() throws {
try assertEquals(actual: "first", expected: First.TestKt.name)
try assertEquals(actual: "second", expected: Second.TestKt.name)

let c1 = First.C()
let c2 = Second.C()
try assertTrue(type(of: c1) == First.C.self)
try assertTrue(type(of: c2) == Second.C.self)
try assertTrue(First.C.self != Second.C.self)
try assertTrue(objc_getClass(class_getName(First.C.self)) as AnyObject === First.C.self)
try assertTrue(objc_getClass(class_getName(Second.C.self)) as AnyObject === Second.C.self)
}

extension I1Impl : I2 {}

func testInteraction() throws {
try assertEquals(actual: SecondKt.getFortyTwoFrom(i2: I1Impl()), expected: 42)
}

func testIsolation() throws {
try assertFalse(SecondKt.isUnit(obj: FirstKt.getUnit()))

// Ensure frameworks don't share the same runtime (state):
try assertFalse(First.RuntimeState().consumeChange())
try assertFalse(Second.RuntimeState().consumeChange())
Second.RuntimeState().produceChange()
try assertFalse(First.RuntimeState().consumeChange())
try assertTrue(Second.RuntimeState().consumeChange())
}

class MultipleFrameworksTests : TestProvider {
var tests: [TestCase] = []

init() {
tests = [
TestCase(name: "TestClashingNames", method: withAutorelease(testClashingNames)),
TestCase(name: "TestInteraction", method: withAutorelease(testInteraction)),
TestCase(name: "TestIsolation", method: withAutorelease(testIsolation)),
]
providers.append(self)
}

}
11 changes: 11 additions & 0 deletions backend.native/tests/framework/multiple/shared/shared.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import kotlin.native.concurrent.Worker

object RuntimeState {
fun produceChange() {
Worker.current.executeAfter {}
}

fun consumeChange(): Boolean {
return Worker.current.processQueue()
}
}

0 comments on commit 4d7f39b

Please sign in to comment.