Reorganize existing Closeable/AutoCloseable.use tests

This commit is contained in:
Ilya Gorbunov
2019-12-27 22:59:32 +03:00
parent 941de655c4
commit a98f36bce4
6 changed files with 166 additions and 107 deletions
@@ -1,25 +0,0 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.jdk7.test
import org.junit.Test
import kotlin.test.assertEquals
class AutoCloseableContractsTest {
@Test
fun shouldHaveBlockExactlyOnceContract() {
class TestAutoCloseable : AutoCloseable {
override fun close() {
}
}
val i: Int
TestAutoCloseable().use {
i = 1
}
assertEquals(1, i)
}
}
@@ -1,29 +1,17 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.jdk7.test
import java.io.*
import org.junit.Test
import java.util.*
import kotlin.test.*
class TryWithResourcesAutoCloseableTest {
class UseAutoCloseableResourceTest {
@Suppress("HasPlatformType") fun <T> platformNull() = Collections.singletonList(null as T).first()
@Suppress("HasPlatformType", "UNCHECKED_CAST") fun <T> platformNull() = Collections.singletonList(null as T).first()
class Resource(val faultyClose: Boolean = false) : AutoCloseable {
@@ -125,4 +113,15 @@ class TryWithResourcesAutoCloseableTest {
assertTrue(e.suppressed.isEmpty())
}
@Test
fun contractCallsInPlace() {
val i: Int
Resource().use { _ ->
Resource().use { _ ->
i = 1
}
}
assertEquals(1, i)
}
}
@@ -1,30 +1,17 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.jdk7.test
import org.junit.Ignore
import java.io.*
import org.junit.Test
import java.util.*
import kotlin.test.*
class TryWithResourcesCloseableTest {
class UseCloseableResourceTest {
@Suppress("HasPlatformType") fun <T> platformNull() = Collections.singletonList(null as T).first()
@Suppress("HasPlatformType", "UNCHECKED_CAST") fun <T> platformNull() = Collections.singletonList(null as T).first()
class Resource(val faultyClose: Boolean = false) : Closeable {
@@ -124,4 +111,15 @@ class TryWithResourcesCloseableTest {
assertTrue(e is IllegalArgumentException)
assertTrue(e.suppressed.isEmpty())
}
@Test
fun contractCallsInPlace() {
val i: Int
Resource().use { _ ->
Resource().use { _ ->
i = 1
}
}
assertEquals(1, i)
}
}