|
| 1 | +package chapi.ast.kotlinast |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Nested |
| 4 | +import org.junit.jupiter.api.Test |
| 5 | +import kotlin.test.assertEquals |
| 6 | +import kotlin.test.assertTrue |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests for modern Kotlin syntax features: |
| 10 | + * - Range until operator (..<) - Kotlin 1.7.20+ |
| 11 | + * - Definitely non-nullable types (T & Any) - Kotlin 1.7+ |
| 12 | + * - Data objects - Kotlin 1.9+ |
| 13 | + * - Suspend anonymous functions |
| 14 | + */ |
| 15 | +internal class KotlinModernSyntaxTest { |
| 16 | + private fun analyse(code: String, fileName: String = "modern.kt") = |
| 17 | + KotlinAnalyser().analysis(code, fileName, AnalysisMode.Basic) |
| 18 | + |
| 19 | + @Nested |
| 20 | + inner class RangeUntilOperator { |
| 21 | + @Test |
| 22 | + fun `should parse range until operator`() { |
| 23 | + val code = """ |
| 24 | +package test |
| 25 | +
|
| 26 | +fun rangeUntilExample() { |
| 27 | + for (i in 0..<10) { |
| 28 | + println(i) |
| 29 | + } |
| 30 | +} |
| 31 | +""" |
| 32 | + val codeContainer = analyse(code) |
| 33 | + assertEquals(1, codeContainer.DataStructures.size) |
| 34 | + assertEquals("rangeUntilExample", codeContainer.DataStructures[0].Functions[0].Name) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `should parse range until in variable assignment`() { |
| 39 | + val code = """ |
| 40 | +package test |
| 41 | +
|
| 42 | +class RangeTest { |
| 43 | + fun getRange(): IntRange { |
| 44 | + return 0..<100 |
| 45 | + } |
| 46 | +} |
| 47 | +""" |
| 48 | + val codeContainer = analyse(code) |
| 49 | + assertEquals("RangeTest", codeContainer.DataStructures[0].NodeName) |
| 50 | + assertEquals("getRange", codeContainer.DataStructures[0].Functions[0].Name) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Nested |
| 55 | + inner class DefinitelyNonNullableTypes { |
| 56 | + @Test |
| 57 | + fun `should parse definitely non-nullable type`() { |
| 58 | + val code = """ |
| 59 | +package test |
| 60 | +
|
| 61 | +fun <T> elvisLike(x: T, y: T & Any): T & Any = x ?: y |
| 62 | +""" |
| 63 | + val codeContainer = analyse(code) |
| 64 | + assertEquals(1, codeContainer.DataStructures.size) |
| 65 | + assertEquals("elvisLike", codeContainer.DataStructures[0].Functions[0].Name) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `should parse definitely non-nullable type in class`() { |
| 70 | + val code = """ |
| 71 | +package test |
| 72 | +
|
| 73 | +class NonNullableProcessor<T> { |
| 74 | + fun process(value: T & Any): T & Any { |
| 75 | + return value |
| 76 | + } |
| 77 | +} |
| 78 | +""" |
| 79 | + val codeContainer = analyse(code) |
| 80 | + assertEquals("NonNullableProcessor", codeContainer.DataStructures[0].NodeName) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Nested |
| 85 | + inner class DataObjects { |
| 86 | + @Test |
| 87 | + fun `should parse data object declaration`() { |
| 88 | + val code = """ |
| 89 | +package test |
| 90 | +
|
| 91 | +data object MySingleton { |
| 92 | + val name = "Singleton" |
| 93 | +} |
| 94 | +""" |
| 95 | + val codeContainer = analyse(code) |
| 96 | + assertEquals(1, codeContainer.DataStructures.size) |
| 97 | + assertEquals("MySingleton", codeContainer.DataStructures[0].NodeName) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `should parse data object in sealed hierarchy`() { |
| 102 | + val code = """ |
| 103 | +package test |
| 104 | +
|
| 105 | +sealed interface Status |
| 106 | +
|
| 107 | +data object Loading : Status |
| 108 | +data class Success(val data: String) : Status |
| 109 | +data object Error : Status |
| 110 | +""" |
| 111 | + val codeContainer = analyse(code) |
| 112 | + assertTrue(codeContainer.DataStructures.size >= 1) |
| 113 | + // Should contain Status, Loading, Success, and Error |
| 114 | + val nodeNames = codeContainer.DataStructures.map { it.NodeName } |
| 115 | + assertTrue(nodeNames.contains("Status")) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Nested |
| 120 | + inner class SuspendAnonymousFunctions { |
| 121 | + @Test |
| 122 | + fun `should parse suspend anonymous function`() { |
| 123 | + val code = """ |
| 124 | +package test |
| 125 | +
|
| 126 | +import kotlinx.coroutines.delay |
| 127 | +
|
| 128 | +class CoroutineExample { |
| 129 | + val suspendFunc = suspend fun() { |
| 130 | + delay(1000) |
| 131 | + } |
| 132 | +} |
| 133 | +""" |
| 134 | + val codeContainer = analyse(code) |
| 135 | + assertEquals("CoroutineExample", codeContainer.DataStructures[0].NodeName) |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun `should parse suspend anonymous function with receiver`() { |
| 140 | + val code = """ |
| 141 | +package test |
| 142 | +
|
| 143 | +class SuspendBuilder { |
| 144 | + val block = suspend fun String.(): Int { |
| 145 | + return this.length |
| 146 | + } |
| 147 | +} |
| 148 | +""" |
| 149 | + val codeContainer = analyse(code) |
| 150 | + assertEquals("SuspendBuilder", codeContainer.DataStructures[0].NodeName) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Nested |
| 155 | + inner class ValueClasses { |
| 156 | + @Test |
| 157 | + fun `should parse value class`() { |
| 158 | + val code = """ |
| 159 | +package test |
| 160 | +
|
| 161 | +@JvmInline |
| 162 | +value class Password(private val s: String) |
| 163 | +""" |
| 164 | + val codeContainer = analyse(code) |
| 165 | + assertEquals(1, codeContainer.DataStructures.size) |
| 166 | + assertEquals("Password", codeContainer.DataStructures[0].NodeName) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Nested |
| 171 | + inner class SuspendFunctionTypes { |
| 172 | + @Test |
| 173 | + fun `should parse suspend function type in delegation`() { |
| 174 | + val code = """ |
| 175 | +package test |
| 176 | +
|
| 177 | +interface SuspendAction { |
| 178 | + suspend fun execute() |
| 179 | +} |
| 180 | +
|
| 181 | +class SuspendActionImpl : SuspendAction { |
| 182 | + override suspend fun execute() { |
| 183 | + println("Executing") |
| 184 | + } |
| 185 | +} |
| 186 | +""" |
| 187 | + val codeContainer = analyse(code) |
| 188 | + assertTrue(codeContainer.DataStructures.size >= 1) |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments