Skip to content

Commit ff7b747

Browse files
Add File.children, resourceOrNull and resource functions
1 parent 2764934 commit ff7b747

File tree

13 files changed

+342
-1
lines changed

13 files changed

+342
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Added
66

7+
- `resource(name: String): File` function
8+
- `resourceOrNull(name: String): File?` function
9+
- `File.children: Sequence<File>`
710
- `String.transformstring` function
811
- `String.TRANSFORMSTRING` function
912
- `String.transformString` function

kotlin-stdlib/android/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
This file was deleted.

kotlin-stdlib/api/jvm/kotlin-stdlib.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ public final class com/javiersc/kotlin/stdlib/CollectionsKt {
159159
public static final fun thirdOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
160160
}
161161

162+
public final class com/javiersc/kotlin/stdlib/FilesKt {
163+
public static final fun getChildren (Ljava/io/File;)Lkotlin/sequences/Sequence;
164+
public static final fun resource (Ljava/lang/String;)Ljava/io/File;
165+
public static final fun resourceOrNull (Ljava/lang/String;)Ljava/io/File;
166+
}
167+
162168
public final class com/javiersc/kotlin/stdlib/StringsKt {
163169
public static final fun endWithNewLine (Ljava/lang/String;)Ljava/lang/String;
164170
public static final fun getEmpty (Lkotlin/jvm/internal/StringCompanionObject;)Ljava/lang/String;

kotlin-stdlib/common/test/resources/children-test/A.txt

Whitespace-only changes.

kotlin-stdlib/common/test/resources/children-test/B.txt

Whitespace-only changes.

kotlin-stdlib/common/test/resources/children-test/sub-children/C.txt

Whitespace-only changes.

kotlin-stdlib/common/test/resources/children-test/sub-children/D.txt

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.javiersc.kotlin.stdlib
2+
3+
import java.io.File
4+
5+
public fun resource(name: String): File = resourceOrNull(name) ?: error("File not found")
6+
7+
public fun resourceOrNull(name: String): File? =
8+
Thread.currentThread().contextClassLoader?.getResource(name)?.file?.let(::File)
9+
10+
public val File.children: Sequence<File>
11+
get() = walkTopDown().maxDepth(1) - this
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.javiersc.kotlin.stdlib
2+
3+
import java.io.File
4+
import kotlin.test.Test
5+
import kotlin.test.assertFailsWith
6+
import kotlin.test.assertNotNull
7+
import kotlin.test.assertNull
8+
import kotlin.test.assertTrue
9+
10+
internal class FilesTest {
11+
12+
@Test
13+
fun `given a directory when it exists then resource is not null`() {
14+
val childrenTestDir = resourceOrNull("children-test")
15+
assertNotNull(childrenTestDir)
16+
assertTrue { childrenTestDir.exists() }
17+
assertTrue { resource("children-test").exists() }
18+
}
19+
20+
@Test
21+
fun `given a directory when it does not exist then resource is null`() {
22+
assertFailsWith<IllegalStateException> { resource("children-test-foo") }
23+
assertNull(resourceOrNull("children-test-foo"))
24+
}
25+
26+
@Test
27+
fun `given a directory when it has children a sub-children then children does not use sub children`() {
28+
val childrenTestDir = resource("children-test")
29+
30+
val children: Sequence<File> = childrenTestDir.children
31+
val actualChildren: List<String> = children.toList().map { it.name }.sorted()
32+
val expectChildren: List<String> = listOf("A.txt", "B.txt", "sub-children").sorted()
33+
assertTrue { actualChildren == expectChildren }
34+
35+
val subChildren = children.first { it.isDirectory }.children
36+
val actualSubChildren: List<String> = subChildren.toList().map { it.name }.sorted()
37+
val expectSubChildren: List<String> = listOf("C.txt", "D.txt").sorted()
38+
assertTrue { actualSubChildren == expectSubChildren }
39+
}
40+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreAndroidNativeArm32 : java/lang/annotation/Annotation {
2+
}
3+
4+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreAndroidNativeArm64 : java/lang/annotation/Annotation {
5+
}
6+
7+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreAndroidNativeX64 : java/lang/annotation/Annotation {
8+
}
9+
10+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreAndroidNativeX86 : java/lang/annotation/Annotation {
11+
}
12+
13+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreCommon : java/lang/annotation/Annotation {
14+
}
15+
16+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreIosArm64 : java/lang/annotation/Annotation {
17+
}
18+
19+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreIosSimulatorArm64 : java/lang/annotation/Annotation {
20+
}
21+
22+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreIosX64 : java/lang/annotation/Annotation {
23+
}
24+
25+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreJs : java/lang/annotation/Annotation {
26+
}
27+
28+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreJvm : java/lang/annotation/Annotation {
29+
}
30+
31+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreLinuxArm64 : java/lang/annotation/Annotation {
32+
}
33+
34+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreLinuxX64 : java/lang/annotation/Annotation {
35+
}
36+
37+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreMacosArm64 : java/lang/annotation/Annotation {
38+
}
39+
40+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreMacosX64 : java/lang/annotation/Annotation {
41+
}
42+
43+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreMingwX64 : java/lang/annotation/Annotation {
44+
}
45+
46+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreTvosArm64 : java/lang/annotation/Annotation {
47+
}
48+
49+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreTvosSimulatorArm64 : java/lang/annotation/Annotation {
50+
}
51+
52+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreTvosX64 : java/lang/annotation/Annotation {
53+
}
54+
55+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreWasm : java/lang/annotation/Annotation {
56+
}
57+
58+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreWatchosArm32 : java/lang/annotation/Annotation {
59+
}
60+
61+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreWatchosArm64 : java/lang/annotation/Annotation {
62+
}
63+
64+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreWatchosDeviceArm64 : java/lang/annotation/Annotation {
65+
}
66+
67+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreWatchosSimulatorArm64 : java/lang/annotation/Annotation {
68+
}
69+
70+
public abstract interface annotation class com/javiersc/kotlin/test/IgnoreWatchosX64 : java/lang/annotation/Annotation {
71+
}
72+

0 commit comments

Comments
 (0)