Skip to content

Commit 5fc9d8e

Browse files
committed
chore: simplify test setup
fix a bug with energy storage insertion checks
1 parent 631e183 commit 5fc9d8e

25 files changed

+1030
-947
lines changed

src/main/java/dev/galacticraft/machinelib/impl/storage/MachineEnergyStorageImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public boolean canExtract(long amount) {
7575

7676
@Override
7777
public boolean canInsert(long amount) {
78-
return this.amount <= amount;
78+
return this.amount + amount <= this.capacity;
7979
}
8080

8181
@Override

src/test/java/dev/galacticraft/machinelib/test/JUnitTest.java src/test/java/dev/galacticraft/machinelib/test/MinecraftTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import net.minecraft.server.Bootstrap;
2727
import org.junit.jupiter.api.BeforeAll;
2828

29-
public interface JUnitTest {
29+
/**
30+
* Test that requires Minecraft to be initialized.
31+
*/
32+
public interface MinecraftTest {
3033
@BeforeAll
3134
static void initializeMinecraft() {
3235
SharedConstants.tryDetectVersion();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.test.misc;
24+
25+
import dev.galacticraft.machinelib.api.config.Config;
26+
import dev.galacticraft.machinelib.test.MinecraftTest;
27+
import net.fabricmc.loader.api.FabricLoader;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
import java.io.File;
32+
33+
import static org.junit.jupiter.api.Assertions.*;
34+
35+
public class ConfigTest implements MinecraftTest {
36+
private static final File FILE = FabricLoader.getInstance().getGameDir().resolve(".machinelib_test_config.json").toFile();
37+
38+
private Config config;
39+
40+
@BeforeEach
41+
public void setup() {
42+
FILE.delete();
43+
this.config = Config.loadFrom(FILE);
44+
}
45+
46+
@Test
47+
public void saveOnOpen() {
48+
assertTrue(FILE.exists());
49+
}
50+
51+
@Test
52+
public void load() {
53+
boolean enabled = !this.config.enableColoredVanillaFluidNames();
54+
this.config.setEnableColoredVanillaFluidNames(enabled);
55+
this.config.save();
56+
57+
Config newConfig = Config.loadFrom(FILE);
58+
assertEquals(enabled, newConfig.enableColoredVanillaFluidNames());
59+
}
60+
61+
@Test
62+
public void reload() {
63+
boolean enabled = !this.config.enableColoredVanillaFluidNames();
64+
this.config.setEnableColoredVanillaFluidNames(enabled);
65+
this.config.reload();
66+
67+
assertNotEquals(enabled, this.config.enableColoredVanillaFluidNames());
68+
}
69+
}

src/test/java/dev/galacticraft/machinelib/test/misc/EmptyDeserializationTests.java src/test/java/dev/galacticraft/machinelib/test/misc/EmptyDeserializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.junit.jupiter.api.Assertions;
3030
import org.junit.jupiter.api.Test;
3131

32-
public class EmptyDeserializationTests {
32+
public class EmptyDeserializationTest {
3333
@Test
3434
public void securitySettings() {
3535
SecuritySettings securitySettings = new SecuritySettings();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.test.storage;
24+
25+
import dev.galacticraft.machinelib.api.storage.MachineEnergyStorage;
26+
import dev.galacticraft.machinelib.impl.storage.MachineEnergyStorageImpl;
27+
import dev.galacticraft.machinelib.test.MinecraftTest;
28+
import io.netty.buffer.ByteBuf;
29+
import io.netty.buffer.Unpooled;
30+
import net.minecraft.nbt.LongTag;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
34+
import static org.junit.jupiter.api.Assertions.*;
35+
36+
public class EnergyStorageTests implements MinecraftTest {
37+
private static final long CAPACITY = 2000;
38+
private MachineEnergyStorage storage;
39+
40+
@BeforeEach
41+
public void setup() {
42+
this.storage = new MachineEnergyStorageImpl(CAPACITY, 0, 0, true, true);
43+
}
44+
45+
@Test
46+
void canExtract() {
47+
this.storage.setEnergy(1000);
48+
49+
assertTrue(this.storage.canExtract(1000));
50+
assertTrue(this.storage.canExtract(500));
51+
assertFalse(this.storage.canExtract(1001));
52+
}
53+
54+
@Test
55+
void canInsert() {
56+
this.storage.setEnergy(1000);
57+
58+
assertTrue(this.storage.canInsert(1000));
59+
assertTrue(this.storage.canInsert(500));
60+
assertFalse(this.storage.canInsert(1001));
61+
}
62+
63+
@Test
64+
void tryExtract() {
65+
this.storage.setEnergy(1000);
66+
67+
assertEquals(1000, this.storage.tryExtract(1000));
68+
assertEquals(500, this.storage.tryExtract(500));
69+
assertEquals(1000, this.storage.tryExtract(1001));
70+
}
71+
72+
@Test
73+
void tryInsert() {
74+
this.storage.setEnergy(1000);
75+
76+
assertEquals(1000, this.storage.tryInsert(1000));
77+
assertEquals(500, this.storage.tryInsert(500));
78+
assertEquals(1000, this.storage.tryInsert(1001));
79+
}
80+
81+
@Test
82+
void extract() {
83+
this.storage.setEnergy(1000);
84+
assertEquals(1000, this.storage.extract(1000));
85+
assertEquals(0, this.storage.getAmount());
86+
87+
this.storage.setEnergy(1000);
88+
assertEquals(500, this.storage.extract(500));
89+
assertEquals(500, this.storage.getAmount());
90+
91+
this.storage.setEnergy(1000);
92+
assertEquals(1000, this.storage.extract(1001));
93+
assertEquals(0, this.storage.getAmount());
94+
}
95+
96+
@Test
97+
void insert() {
98+
this.storage.setEnergy(1000);
99+
assertEquals(1000, this.storage.insert(1000));
100+
assertEquals(2000, this.storage.getAmount());
101+
102+
this.storage.setEnergy(1000);
103+
assertEquals(500, this.storage.insert(500));
104+
assertEquals(1500, this.storage.getAmount());
105+
106+
this.storage.setEnergy(1000);
107+
assertEquals(1000, this.storage.insert(1001));
108+
assertEquals(2000, this.storage.getAmount());
109+
}
110+
111+
@Test
112+
void extractExact() {
113+
this.storage.setEnergy(1000);
114+
assertTrue(this.storage.extractExact(1000));
115+
assertEquals(0, this.storage.getAmount());
116+
117+
this.storage.setEnergy(1000);
118+
assertTrue(this.storage.extractExact(500));
119+
assertEquals(500, this.storage.getAmount());
120+
121+
this.storage.setEnergy(1000);
122+
assertFalse(this.storage.extractExact(1001));
123+
assertEquals(1000, this.storage.getAmount());
124+
}
125+
126+
@Test
127+
void insertExact() {
128+
this.storage.setEnergy(1000);
129+
assertTrue(this.storage.insertExact(1000));
130+
assertEquals(2000, this.storage.getAmount());
131+
132+
this.storage.setEnergy(1000);
133+
assertTrue(this.storage.insertExact(500));
134+
assertEquals(1500, this.storage.getAmount());
135+
136+
this.storage.setEnergy(1000);
137+
assertFalse(this.storage.insertExact(1001));
138+
assertEquals(1000, this.storage.getAmount());
139+
}
140+
141+
@Test
142+
void getAmount() {
143+
this.storage.setEnergy(1000);
144+
assertEquals(1000, this.storage.getAmount());
145+
}
146+
147+
@Test
148+
void getCapacity() {
149+
assertEquals(CAPACITY, this.storage.getCapacity());
150+
}
151+
152+
@Test
153+
void isFull() {
154+
this.storage.setEnergy(1000);
155+
assertFalse(this.storage.isFull());
156+
157+
this.storage.setEnergy(2000);
158+
assertTrue(this.storage.isFull());
159+
}
160+
161+
@Test
162+
void isEmpty() {
163+
this.storage.setEnergy(1000);
164+
assertFalse(this.storage.isEmpty());
165+
166+
this.storage.setEnergy(0);
167+
assertTrue(this.storage.isEmpty());
168+
}
169+
170+
@Test
171+
void nbtSerialization() {
172+
this.storage.setEnergy(1000);
173+
174+
LongTag tag = this.storage.createTag();
175+
this.storage.setEnergy(0);
176+
this.storage.readTag(tag);
177+
178+
assertEquals(1000, this.storage.getAmount());
179+
}
180+
181+
@Test
182+
void packetSerialization() {
183+
this.storage.setEnergy(1000);
184+
ByteBuf buf = Unpooled.buffer();
185+
186+
this.storage.writePacket(buf);
187+
this.storage.setEnergy(0);
188+
this.storage.readPacket(buf);
189+
190+
assertEquals(1000, this.storage.getAmount());
191+
}
192+
}

src/test/java/dev/galacticraft/machinelib/test/storage/builder/FluidResourceSlotBuilderTest.java src/test/java/dev/galacticraft/machinelib/test/storage/builder/FluidResourceSlotBuilderTests.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
import dev.galacticraft.machinelib.api.filter.ResourceFilters;
2626
import dev.galacticraft.machinelib.api.storage.slot.FluidResourceSlot;
2727
import dev.galacticraft.machinelib.api.transfer.InputType;
28-
import dev.galacticraft.machinelib.test.JUnitTest;
28+
import dev.galacticraft.machinelib.test.MinecraftTest;
2929
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
3030
import net.minecraft.world.level.material.Fluids;
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Test;
3333

3434
import static org.junit.jupiter.api.Assertions.*;
3535

36-
public class FluidResourceSlotBuilderTest implements JUnitTest {
36+
public class FluidResourceSlotBuilderTests implements MinecraftTest {
3737
private FluidResourceSlot.Builder builder;
3838

3939
@BeforeEach
@@ -53,13 +53,39 @@ public void capacity() {
5353
assertEquals(FluidConstants.BUCKET * 64, slot.getCapacity());
5454
}
5555

56+
@Test
57+
public void hidden() {
58+
builder.hidden();
59+
60+
assertThrows(UnsupportedOperationException.class, () -> builder.x(0));
61+
assertThrows(UnsupportedOperationException.class, () -> builder.y(0));
62+
assertThrows(UnsupportedOperationException.class, () -> builder.width(0));
63+
assertThrows(UnsupportedOperationException.class, () -> builder.height(0));
64+
65+
assertNull(builder.build().getDisplay());
66+
}
67+
68+
@Test
69+
public void hiddenPost() {
70+
builder.x(10).hidden();
71+
72+
assertThrows(UnsupportedOperationException.class, () -> builder.build());
73+
}
74+
5675
@Test
5776
public void height() {
5877
FluidResourceSlot slot = builder.height(16).build();
5978

6079
assertEquals(16, slot.getDisplay().height());
6180
}
6281

82+
@Test
83+
public void invalidHeight() {
84+
builder.height(-4);
85+
86+
assertThrows(IllegalArgumentException.class, () -> builder.build());
87+
}
88+
6389
@Test
6490
public void displayPosition() {
6591
FluidResourceSlot slot = builder.pos(11, 43).build();

src/test/java/dev/galacticraft/machinelib/test/storage/builder/ItemResourceSlotBuilderTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
import dev.galacticraft.machinelib.api.filter.ResourceFilters;
2727
import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot;
2828
import dev.galacticraft.machinelib.api.transfer.InputType;
29-
import dev.galacticraft.machinelib.test.JUnitTest;
29+
import dev.galacticraft.machinelib.test.MinecraftTest;
3030
import net.minecraft.resources.ResourceLocation;
3131
import net.minecraft.world.item.Items;
3232
import org.junit.jupiter.api.BeforeEach;
3333
import org.junit.jupiter.api.Test;
3434

3535
import static org.junit.jupiter.api.Assertions.*;
3636

37-
public class ItemResourceSlotBuilderTest implements JUnitTest {
37+
public class ItemResourceSlotBuilderTest implements MinecraftTest {
3838
private ItemResourceSlot.Builder builder;
3939

4040
@BeforeEach

0 commit comments

Comments
 (0)