diffplug / diffplug/selfie

[Enhancment] Calling `expectSelfie()` from another package

Aperta
#541 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
jvm question
Lingua principale
Kotlin
Stelle
101
Fork
18
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hello, the library is pretty useful, but I'm stumbling into a problem that I'm not sure how to fix.

Using Kotest, I want to create a reusable `Spec` to snapshot tests my custom exceptions and their localization.
It ends up looking kind of like this:

```kt
package mypackage.errors

abstract class ErrorSpec(
private val clazz: KClass,
private val create: () -> T
) :
FunSpec({
context("Localisation") {
forAll(Exhaustive.collection(SUPPORTED_LOCALES)) {

expectSelfie(json.encodeToString(clazz.serializer(), create())).toMatchDisk()

}
}
})

class ErrorTest : ErrorSpec(
clazz = Error::class,
create = { Error() }
)
```

This work perfectly, until I put `ErrorSpec` in another package.
The main reason I want to do this is to use a `testFixtures`.

This then ends up failing at runtime with:

```
org.opentest4j.AssertionFailedError:
Couldn't find source file for CallLocation(
clazz=mypackage.ErrorSpec$1$1,
method=invokeSuspend,
fileName=Specs.kt,
line=36
),

looked in TypedPath(absolutePath=[...]/mypackage/src/test/kotlin/)
and [[...]/mypackage/src/test/resources],

maybe there are other source roots?
```

What I don't understand is that, if I use `toMatchDisk_TODO()` instead of `toMatchDisk()`, the snapshot files end up being written, but the tests fails on `writeLine`.

Manually changing the value of `isTodo` to `true` in `SelfieImplementations/DiskSelfie.toMatchDisk():38`, the tests pass successfully, with the snapshot file being written at the correct place (albeit being overwritten every run, which kind of defeat the point).

Is there a way to do what I want using the public API?
If not, would it be possible to implement?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia riproducendo l’esempio ErrorSpec tra package e ispeziona SelfieImplementations/DiskSelfie.toMatchDisk(), in particolare il comportamento intorno alla riga 38 e la ricerca del sorgente di CallLocation. Confrontalo con toMatchDisk_TODO(). È completato quando l’API pubblica può convalidare e scrivere snapshot da un altro package senza basarsi sul comportamento di TODO o sovrascriverli a ogni esecuzione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
kotlin
Ambito
testing
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.