Skip to content

Commit

Permalink
Add initial bulk cell inventory unit test
Browse files Browse the repository at this point in the history
TODO: Add compression test after runtime issues with dependencies have been resolved
  • Loading branch information
62832 committed Jan 29, 2025
1 parent 792eb44 commit 8879d08
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
15 changes: 15 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ dependencies {

compileOnly(libs.appbot)
compileOnly(libs.botania)

testImplementation(testlibs.junit.jupiter)
testImplementation(testlibs.assertj)
testImplementation(testlibs.neoforge.test)
testRuntimeOnly(testlibs.junit.platform)
}

sourceSets {
Expand Down Expand Up @@ -92,6 +97,11 @@ neoForge {
sourceSet = sourceSets.getByName("data")
}
}

unitTest {
enable()
testedMod = mods.getByName(modId)
}
}

tasks {
Expand All @@ -115,6 +125,10 @@ tasks {
expand(props)
}
}

test {
useJUnitPlatform()
}
}

spotless {
Expand All @@ -133,6 +147,7 @@ spotless {
palantirJavaFormat()
importOrderFile(file("mega.importorder"))
toggleOffOn()
trimTrailingWhitespace()

// courtesy of diffplug/spotless#240
// https://github.com/diffplug/spotless/issues/240#issuecomment-385206606
Expand Down
15 changes: 12 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ run {
}

versionCatalogs {
val mc = "1.21.1"
val maj = mc.substringAfter('.')
val nf = "${maj + (if (!maj.contains('.')) ".0" else "")}.104"

create("libs") {
val mc = "1.21.1"
version("minecraft", mc)

val nf = mc.substringAfter('.')
version("neoforge", "${nf + (if (!nf.contains('.')) ".0" else "")}.104")
version("neoforge", nf)
version("parchment", "2024.07.28")

version("ae2", "19.1.3-beta")
Expand Down Expand Up @@ -119,6 +121,13 @@ run {
library("appex", "curse.maven", "applied-experienced-1157608").version("6080443")
library("explib", "curse.maven", "experiencelib-1156551").version("5992832")
}

create("testlibs") {
library("neoforge-test", "net.neoforged", "testframework").version(nf)
library("junit-jupiter", "org.junit.jupiter", "junit-jupiter").version("5.7.1")
library("junit-platform", "org.junit.platform", "junit-platform-launcher").version("1.11.4")
library("assertj", "org.assertj", "assertj-core").version("3.26.0")
}
}
}
}
78 changes: 78 additions & 0 deletions src/test/java/gripe/_90/megacells/test/BulkCellInventoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package gripe._90.megacells.test;

import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigInteger;
import java.util.Objects;

import org.junit.jupiter.api.Test;

import net.minecraft.world.item.Items;

import appeng.api.config.Actionable;
import appeng.api.networking.security.IActionSource;
import appeng.api.stacks.AEItemKey;
import appeng.api.storage.StorageCells;
import appeng.me.helpers.BaseActionSource;

import gripe._90.megacells.definition.MEGAComponents;
import gripe._90.megacells.definition.MEGAItems;
import gripe._90.megacells.item.cell.BulkCellInventory;

public class BulkCellInventoryTest {
private static final IActionSource SRC = new BaseActionSource();

@Test
void testNoopWhenUnfiltered() {
var item = MEGAItems.BULK_ITEM_CELL.asItem();
var stack = item.getDefaultInstance();

var cell = Objects.requireNonNull(StorageCells.getCellInventory(stack, null));
var content = AEItemKey.of(Items.STICK);
assertThat(cell.insert(content, 1, Actionable.MODULATE, SRC)).isZero();

item.getConfigInventory(stack).addFilter(content);
cell = Objects.requireNonNull(StorageCells.getCellInventory(stack, null));
assertThat(cell.insert(content, 1, Actionable.MODULATE, SRC)).isEqualTo(1);
assertThat(cell.extract(content, 1, Actionable.MODULATE, SRC)).isEqualTo(1);
assertThat(cell.insert(content, 1, Actionable.MODULATE, SRC)).isEqualTo(1);

item.getConfigInventory(stack).clear();
cell = Objects.requireNonNull(StorageCells.getCellInventory(stack, null));
assertThat(cell.extract(content, 1, Actionable.MODULATE, SRC)).isZero();
}

@Test
void testGreaterCapacity() {
var item = MEGAItems.BULK_ITEM_CELL.asItem();
var stack = item.getDefaultInstance();

var content = AEItemKey.of(Items.STICK);
item.getConfigInventory(stack).addFilter(content);

var cell = Objects.requireNonNull(StorageCells.getCellInventory(stack, null));
var max = Long.MAX_VALUE;
assertThat(cell.insert(content, max, Actionable.MODULATE, SRC)).isEqualTo(max);
assertThat(cell.insert(content, max, Actionable.MODULATE, SRC)).isEqualTo(max);

var amount = stack.getComponents().get(MEGAComponents.BULK_CELL_UNIT_COUNT);
assertThat(amount).isEqualTo(BigInteger.valueOf(max).multiply(BigInteger.TWO));

var reported = cell.getAvailableStacks().get(content);
assertThat(reported).isEqualTo(BulkCellInventory.STACK_LIMIT);
}

@Test
void testFilterLimit() {
var item = MEGAItems.BULK_ITEM_CELL.asItem();
var stack = item.getDefaultInstance();

var allowed = AEItemKey.of(Items.STONE);
var rejected = AEItemKey.of(Items.COBBLESTONE);
item.getConfigInventory(stack).addFilter(allowed).addFilter(rejected);

var cell = Objects.requireNonNull(StorageCells.getCellInventory(stack, null));
assertThat(cell.insert(allowed, 1, Actionable.MODULATE, SRC)).isEqualTo(1);
assertThat(cell.insert(rejected, 1, Actionable.MODULATE, SRC)).isZero();
}
}

0 comments on commit 8879d08

Please sign in to comment.