@@ -15,6 +15,8 @@ import dev.mtib.aoc.util.Year
15
15
import dev.mtib.aoc.util.styleResult
16
16
import kotlinx.coroutines.CoroutineDispatcher
17
17
import kotlinx.coroutines.Dispatchers
18
+ import kotlinx.serialization.encodeToString
19
+ import kotlinx.serialization.json.Json
18
20
import okhttp3.OkHttpClient
19
21
import okhttp3.Request
20
22
import org.reflections.Reflections
@@ -178,6 +180,76 @@ open class AocDay(
178
180
val waitDuration = (releaseTime.toInstant().toEpochMilli() - System .currentTimeMillis()).milliseconds
179
181
}
180
182
183
+ fun createTestFile () {
184
+ if (year < 2024 ) {
185
+ return
186
+ }
187
+ if (releaseTime.isAfter(ZonedDateTime .now())) {
188
+ return
189
+ }
190
+ val token = System .getenv(" SESSION" )
191
+ if (token.isNullOrBlank()) {
192
+ return
193
+ }
194
+ val outPath = Path (" src/test/kotlin/dev/mtib/aoc/aoc${year% 100 } /days/Day${day} Test.kt" )
195
+ if (outPath.exists()) {
196
+ return
197
+ }
198
+ val request = Request .Builder ()
199
+ .url(" https://adventofcode.com/$year /day/$day " )
200
+ .get()
201
+ .addHeader(" Cookie" , " session=$token " )
202
+ .build()
203
+ val client = OkHttpClient ()
204
+ val response = client.newCall(request).execute()
205
+ val body = response.body!! .string()
206
+ val codeRegex = Regex (""" <pre><code>(.*?)</code></pre>""" , RegexOption .DOT_MATCHES_ALL )
207
+ val codeSnippets = codeRegex.findAll(body)
208
+ .map { it.groupValues[1 ].replace(" <" , " <" ).replace(" >" , " >" ) }
209
+ .filterNot { it.contains(" <em>" ) }
210
+ .distinct()
211
+
212
+ logger.log(identity) { " creating ${outPath.fileName} with code snippets" }
213
+
214
+ outPath.writeText(buildString {
215
+ appendLine("""
216
+ package dev.mtib.aoc.aoc24.days
217
+
218
+ import io.kotest.core.spec.style.FunSpec
219
+
220
+ class Day${day} Test : FunSpec({
221
+ """ .trimIndent())
222
+ codeSnippets.forEachIndexed { index, code ->
223
+ appendLine(
224
+ " val snippet${index} = \"\"\"\n ${
225
+ code.trimEnd().lines().joinToString(" \n " ) { " " .repeat(8 ) + it }
226
+ } \n \"\"\" .trimIndent()"
227
+ )
228
+ }
229
+ appendLine("""
230
+ context("part1") {
231
+ test("doesn't throw") {
232
+ try {
233
+ Day${day} .part1()
234
+ } catch (e: NotImplementedError) {
235
+ // Ignore allowed exception
236
+ }
237
+ }
238
+ }
239
+ context("part2") {
240
+ test("doesn't throw") {
241
+ try {
242
+ Day${day} .part2()
243
+ } catch (e: NotImplementedError) {
244
+ // Ignore allowed exception
245
+ }
246
+ }
247
+ }
248
+ })
249
+ """ .trimIndent())
250
+ })
251
+ }
252
+
181
253
suspend fun <T > withInput (input : String , block : suspend AocDay .() -> T ): T {
182
254
fakedInput = input.some()
183
255
return this .block().also { fakedInput = None }
0 commit comments