1
+ package org.kotlinlsp.index
2
+
3
+ import org.junit.jupiter.api.Assertions.assertEquals
4
+ import org.junit.jupiter.params.ParameterizedTest
5
+ import org.junit.jupiter.params.provider.Arguments
6
+ import org.junit.jupiter.params.provider.MethodSource
7
+ import org.kotlinlsp.index.db.File
8
+ import java.time.Instant
9
+ import java.util.stream.Stream
10
+
11
+ class FileTests {
12
+ companion object {
13
+ @JvmStatic
14
+ fun provideData (): Stream <Arguments > = Stream .of(
15
+ // Unmodified file
16
+ Arguments .of(
17
+ buildFile(modificationStamp = 0 , lastModified = Instant .ofEpochMilli(100 )),
18
+ buildFile(modificationStamp = 0 , lastModified = Instant .ofEpochMilli(100 )),
19
+ true
20
+ ),
21
+ // File modified on disk
22
+ Arguments .of(
23
+ buildFile(modificationStamp = 0 , lastModified = Instant .ofEpochMilli(50 )),
24
+ buildFile(modificationStamp = 0 , lastModified = Instant .ofEpochMilli(100 )),
25
+ false
26
+ ),
27
+ // File modified in memory
28
+ Arguments .of(
29
+ buildFile(modificationStamp = 0 , lastModified = Instant .ofEpochMilli(50 )),
30
+ buildFile(modificationStamp = 1 , lastModified = Instant .ofEpochMilli(100 )),
31
+ false
32
+ ),
33
+ Arguments .of(
34
+ buildFile(modificationStamp = 1 , lastModified = Instant .ofEpochMilli(50 )),
35
+ buildFile(modificationStamp = 2 , lastModified = Instant .ofEpochMilli(100 )),
36
+ false
37
+ ),
38
+ // Reloading file from disk (after being edited in memory but not saved)
39
+ Arguments .of(
40
+ buildFile(modificationStamp = 10 , lastModified = Instant .ofEpochMilli(100 )),
41
+ buildFile(modificationStamp = 0 , lastModified = Instant .ofEpochMilli(100 )),
42
+ false
43
+ ),
44
+ // Old in memory file trying to be indexed
45
+ Arguments .of(
46
+ buildFile(modificationStamp = 10 , lastModified = Instant .ofEpochMilli(50 )),
47
+ buildFile(modificationStamp = 9 , lastModified = Instant .ofEpochMilli(50 )),
48
+ true
49
+ ),
50
+ // New file should always be indexed
51
+ Arguments .of(
52
+ null ,
53
+ buildFile(modificationStamp = 9 , lastModified = Instant .ofEpochMilli(50 )),
54
+ false
55
+ ),
56
+ )
57
+ }
58
+
59
+ @ParameterizedTest
60
+ @MethodSource(" provideData" )
61
+ fun `test file index skip logic` (existingFile : File ? , newFile : File , result : Boolean ) {
62
+ assertEquals(File .shouldBeSkipped(existingFile = existingFile, newFile = newFile), result)
63
+ }
64
+
65
+ }
66
+
67
+ private fun buildFile (lastModified : Instant , modificationStamp : Long , indexed : Boolean = false): File {
68
+ val path = " /sample/path.kt"
69
+ val packageFqName = " com.example"
70
+ return File (
71
+ path = path,
72
+ modificationStamp = modificationStamp,
73
+ packageFqName = packageFqName,
74
+ lastModified = lastModified,
75
+ indexed = indexed
76
+ )
77
+ }
0 commit comments