Skip to content

Commit fe5db65

Browse files
committed
test(stdlib): test .listfiles
1 parent 005365d commit fe5db65

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

quarkdown-stdlib/src/test/kotlin/com/quarkdown/stdlib/DataTest.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.quarkdown.core.flavor.quarkdown.QuarkdownFlavor
88
import com.quarkdown.core.function.value.data.Range
99
import com.quarkdown.core.pipeline.PipelineOptions
1010
import com.quarkdown.core.util.toPlainText
11+
import com.quarkdown.stdlib.internal.Ordering
1112
import java.io.File
1213
import kotlin.test.BeforeTest
1314
import kotlin.test.Test
@@ -16,6 +17,7 @@ import kotlin.test.assertFails
1617
import kotlin.test.assertIs
1718

1819
private const val DATA_FOLDER = "src/test/resources/data"
20+
private const val LIST_FILES_FOLDER = "listfiles"
1921

2022
/**
2123
* [Data] module tests.
@@ -145,4 +147,46 @@ class DataTest {
145147
}
146148
}
147149
}
150+
151+
@Test
152+
fun `list files unsorted`() {
153+
val files = listFiles(context, LIST_FILES_FOLDER)
154+
val names = files.unwrappedValue.map { it.unwrappedValue }.toSet()
155+
assertEquals(setOf("a.txt", "b.txt", "c.txt"), names)
156+
}
157+
158+
@Test
159+
fun `list files sorted by name ascending`() {
160+
val files = listFiles(context, LIST_FILES_FOLDER, sortBy = FileSorting.NAME)
161+
val names = files.unwrappedValue.map { it.unwrappedValue }.toList()
162+
assertEquals(listOf("a.txt", "b.txt", "c.txt"), names)
163+
}
164+
165+
@Test
166+
fun `list files sorted by name descending`() {
167+
val files = listFiles(context, LIST_FILES_FOLDER, sortBy = FileSorting.NAME, order = Ordering.DESCENDING)
168+
val names = files.unwrappedValue.map { it.unwrappedValue }.toList()
169+
assertEquals(listOf("c.txt", "b.txt", "a.txt"), names)
170+
}
171+
172+
@Test
173+
fun `list files with full path`() {
174+
val files = listFiles(context, LIST_FILES_FOLDER, fullPath = true, sortBy = FileSorting.NAME)
175+
val paths = files.unwrappedValue.map { it.unwrappedValue }.toList()
176+
paths.forEach { path ->
177+
assert(path.contains(LIST_FILES_FOLDER))
178+
assert(path.endsWith("a.txt") || path.endsWith("b.txt") || path.endsWith("c.txt"))
179+
assert(File(path).isAbsolute)
180+
}
181+
}
182+
183+
@Test
184+
fun `list files non-existent directory`() {
185+
assertFails { listFiles(context, "nonexistent") }
186+
}
187+
188+
@Test
189+
fun `list files on a file instead of directory`() {
190+
assertFails { listFiles(context, "test.txt") }
191+
}
148192
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
content a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
content b
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
content c
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.quarkdown.test
2+
3+
import com.quarkdown.test.util.execute
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
/**
8+
* Tests for IO functions.
9+
*/
10+
class IOTest {
11+
@Test
12+
fun `read file`() {
13+
execute(".read {code.txt}") {
14+
assertEquals(
15+
"<p>Line 1\nLine 2\n\nLine 3</p>",
16+
it,
17+
)
18+
}
19+
}
20+
21+
@Test
22+
fun `list files`() {
23+
execute(".listfiles {include} sortby:{name} order:{descending}") {
24+
assertEquals(
25+
"<ol>" +
26+
(7 downTo 1).joinToString(separator = "") { n -> "<li><p>include-$n.md</p></li>" } +
27+
"</ol>",
28+
it,
29+
)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)