diff options
Diffstat (limited to 'src/test/kotlin/testutil')
| -rw-r--r-- | src/test/kotlin/testutil/AutoBootstrapExtension.kt | 14 | ||||
| -rw-r--r-- | src/test/kotlin/testutil/ItemResources.kt | 85 | ||||
| -rw-r--r-- | src/test/kotlin/testutil/KotestPlugin.kt | 16 |
3 files changed, 83 insertions, 32 deletions
diff --git a/src/test/kotlin/testutil/AutoBootstrapExtension.kt b/src/test/kotlin/testutil/AutoBootstrapExtension.kt new file mode 100644 index 0000000..6f225a0 --- /dev/null +++ b/src/test/kotlin/testutil/AutoBootstrapExtension.kt @@ -0,0 +1,14 @@ +package moe.nea.firmament.test.testutil + +import com.google.auto.service.AutoService +import org.junit.jupiter.api.extension.BeforeAllCallback +import org.junit.jupiter.api.extension.Extension +import org.junit.jupiter.api.extension.ExtensionContext +import moe.nea.firmament.test.FirmTestBootstrap + +@AutoService(Extension::class) +class AutoBootstrapExtension : Extension, BeforeAllCallback { + override fun beforeAll(p0: ExtensionContext) { + FirmTestBootstrap.bootstrapMinecraft() + } +} diff --git a/src/test/kotlin/testutil/ItemResources.kt b/src/test/kotlin/testutil/ItemResources.kt index 107b565..5f08847 100644 --- a/src/test/kotlin/testutil/ItemResources.kt +++ b/src/test/kotlin/testutil/ItemResources.kt @@ -1,15 +1,24 @@ package moe.nea.firmament.test.testutil -import net.minecraft.item.ItemStack -import net.minecraft.nbt.NbtCompound -import net.minecraft.nbt.NbtElement +import com.mojang.datafixers.DSL +import com.mojang.serialization.Dynamic +import com.mojang.serialization.JsonOps +import net.minecraft.SharedConstants +import net.minecraft.util.datafix.DataFixers +import net.minecraft.util.datafix.fixes.References +import net.minecraft.world.item.ItemStack +import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.Tag import net.minecraft.nbt.NbtOps -import net.minecraft.nbt.StringNbtReader -import net.minecraft.registry.RegistryOps -import net.minecraft.text.Text -import net.minecraft.text.TextCodecs +import net.minecraft.nbt.StringTag +import net.minecraft.nbt.TagParser +import net.minecraft.resources.RegistryOps +import net.minecraft.network.chat.Component +import net.minecraft.network.chat.ComponentSerialization +import moe.nea.firmament.features.debug.ExportedTestConstantMeta import moe.nea.firmament.test.FirmTestBootstrap import moe.nea.firmament.util.MC +import moe.nea.firmament.util.mc.MCTabListAPI object ItemResources { init { @@ -23,19 +32,63 @@ object ItemResources { .readAllBytes().decodeToString() } - fun loadSNbt(path: String): NbtCompound { - return StringNbtReader.parse(loadString(path)) + fun loadSNbt(path: String): CompoundTag { + return TagParser.parseCompoundFully(loadString(path)) } - fun getNbtOps(): RegistryOps<NbtElement> = MC.currentOrDefaultRegistries.getOps(NbtOps.INSTANCE) - fun loadText(name: String): Text { - return TextCodecs.CODEC.parse(getNbtOps(), loadSNbt("testdata/chat/$name.snbt")) - .getOrThrow { IllegalStateException("Could not load test chat '$name': $it") } + fun getNbtOps(): RegistryOps<Tag> = MC.currentOrDefaultRegistries.createSerializationContext(NbtOps.INSTANCE) + + fun tryMigrateNbt( + nbtCompound: CompoundTag, + typ: DSL.TypeReference?, + ): Tag { + val source = nbtCompound.read("source", ExportedTestConstantMeta.CODEC) + nbtCompound.remove("source") + if (source.isPresent) { + val wrappedNbtSource = if (typ == References.TEXT_COMPONENT && source.get().dataVersion < 4325) { + // Per 1.21.5 text components are wrapped in a string, which firmament unwrapped in the snbt files + StringTag.valueOf( + NbtOps.INSTANCE.convertTo(JsonOps.INSTANCE, nbtCompound) + .toString() + ) + } else { + nbtCompound + } + if (typ != null) { + return DataFixers.getDataFixer() + .update( + typ, + Dynamic(NbtOps.INSTANCE, wrappedNbtSource), + source.get().dataVersion, + SharedConstants.getCurrentVersion().dataVersion().version + ).value + } else { + wrappedNbtSource + } + } + return nbtCompound + } + + fun loadTablist(name: String): MCTabListAPI.CurrentTabList { + return MCTabListAPI.CurrentTabList.CODEC.parse( + getNbtOps(), + tryMigrateNbt(loadSNbt("testdata/tablist/$name.snbt"), null), + ).getOrThrow { IllegalStateException("Could not load tablist '$name': $it") } + } + + fun loadText(name: String): Component { + return ComponentSerialization.CODEC.parse( + getNbtOps(), + tryMigrateNbt(loadSNbt("testdata/chat/$name.snbt"), References.TEXT_COMPONENT) + ).getOrThrow { IllegalStateException("Could not load test chat '$name': $it") } } fun loadItem(name: String): ItemStack { - // TODO: make the load work with enchantments - return ItemStack.CODEC.parse(getNbtOps(), loadSNbt("testdata/items/$name.snbt")) - .getOrThrow { IllegalStateException("Could not load test item '$name': $it") } + try { + val itemNbt = loadSNbt("testdata/items/$name.snbt") + return ItemStack.CODEC.parse(getNbtOps(), tryMigrateNbt(itemNbt, References.ITEM_STACK)).orThrow + } catch (ex: Exception) { + throw RuntimeException("Could not load item resource '$name'", ex) + } } } diff --git a/src/test/kotlin/testutil/KotestPlugin.kt b/src/test/kotlin/testutil/KotestPlugin.kt deleted file mode 100644 index 6db50fb..0000000 --- a/src/test/kotlin/testutil/KotestPlugin.kt +++ /dev/null @@ -1,16 +0,0 @@ -package moe.nea.firmament.test.testutil - -import io.kotest.core.config.AbstractProjectConfig -import io.kotest.core.extensions.Extension -import moe.nea.firmament.test.FirmTestBootstrap - -class KotestPlugin : AbstractProjectConfig() { - override fun extensions(): List<Extension> { - return listOf() - } - - override suspend fun beforeProject() { - FirmTestBootstrap.bootstrapMinecraft() - super.beforeProject() - } -} |
