Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jul 4, 2022
1 parent 247584e commit 51d8f7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
13 changes: 6 additions & 7 deletions src/main/java/io/github/ladysnake/locki/Locki.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand Down Expand Up @@ -153,23 +152,23 @@ public static Stream<String> streamNodeNames() {
return nodes.keySet().stream();
}

@VisibleForTesting
Function<PlayerEntity, InventoryKeeper> keeperFunction = InventoryKeeper::get;

@Override
public void onInitialize(ModContainer mod) {
DefaultInventoryNodes.init();
ModdedInventoryNodes.init();

ServerArgumentType.register(new Identifier("locki", "inventory_lock"), InventoryLockArgumentType.class, SingletonArgumentInfo.contextFree(InventoryLockArgumentType::inventoryLock), t -> IdentifierArgumentType.identifier());
ServerArgumentType.register(new Identifier("locki", "inventory_node"), InventoryNodeArgumentType.class, SingletonArgumentInfo.contextFree(InventoryNodeArgumentType::inventoryNode), t -> StringArgumentType.string());
if (mod != null) { // Unit testing
ServerArgumentType.register(new Identifier("locki", "inventory_lock"), InventoryLockArgumentType.class, SingletonArgumentInfo.contextFree(InventoryLockArgumentType::inventoryLock), t -> IdentifierArgumentType.identifier());
ServerArgumentType.register(new Identifier("locki", "inventory_node"), InventoryNodeArgumentType.class, SingletonArgumentInfo.contextFree(InventoryNodeArgumentType::inventoryNode), t -> StringArgumentType.string());
}

CommandRegistrationCallback.EVENT.register((dispatcher, ctx, dedicated) -> LockiCommand.register(dispatcher));
PermissionCheckEvent.EVENT.register((source, permission) -> {
if (source instanceof ServerCommandSource && permission.startsWith("locki.access.")) {
Entity entity = ((ServerCommandSource) source).getEntity();
if (entity instanceof PlayerEntity) {
InventoryNode node = getNode(permission.substring(13));
if (node != null && keeperFunction.apply((PlayerEntity) entity).isLocked(node)) {
if (node != null && InventoryKeeper.get((PlayerEntity) entity).isLocked(node)) {
return TriState.FALSE;
}
}
Expand Down
31 changes: 2 additions & 29 deletions src/test/java/io/github/ladysnake/locki/LockiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
*/
package io.github.ladysnake.locki;

import io.github.ladysnake.locki.impl.InventoryKeeperBase;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -38,7 +33,7 @@ public class LockiTest {

@BeforeClass
public static void setUpGlobal() {
instance.onInitialize();
instance.onInitialize(null);
}

@Before
Expand All @@ -54,7 +49,7 @@ public void registerLock() {
.map(String::valueOf)
.map(Identifier::new)
.map(Locki::registerLock)
.collect(Collectors.toList());
.toList();
assertEquals(IntStream.range(0, 2000).boxed().collect(Collectors.toList()), locks.stream().map(InventoryLock::getRawId).sorted().collect(Collectors.toList()));
}

Expand Down Expand Up @@ -91,26 +86,4 @@ public void getNode() {
assertEquals(node, Locki.getNode("inventory.armor.test-b"));
assertNull(Locki.getNode("inventory.armor.test-a"));
}

@Test
public void checkPermission() {
InventoryKeeper keeper = new InventoryKeeperBase();
instance.keeperFunction = p -> keeper;

ServerPlayerEntity player = Mockito.mock(ServerPlayerEntity.class);
ServerCommandSource source = Mockito.mock(ServerCommandSource.class);
Mockito.when(source.getEntity()).thenReturn(player);
Mockito.when(player.getCommandSource()).thenReturn(source);

assertTrue(Permissions.check(player, "locki.access.inventory", true));
InventoryLock lock = Locki.registerLock(new Identifier("test", "test"));
InventoryNode node = Locki.registerNode(InventoryNode.ROOT, "test");
Locki.registerNode(node, "child");
Locki.registerNode(InventoryNode.ROOT, "test-foo");
keeper.addLock(lock, node);

assertFalse(Permissions.check(player, "locki.access.test", true));
assertFalse(Permissions.check(player, "locki.access.test.child", true));
assertTrue(Permissions.check(player, "locki.access.test-foo", true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,43 @@

import io.github.ladysnake.locki.DefaultInventoryNodes;
import io.github.ladysnake.locki.InventoryLock;
import io.github.ladysnake.locki.InventoryNode;
import io.github.ladysnake.locki.Locki;
import io.github.ladysnake.locki.impl.LockiComponents;
import io.github.ladysnake.lockii.Lockii;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;
import net.minecraft.util.Identifier;

import static io.github.ladysnake.elmendorf.ByteBufChecker.any;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class LockiTestSuite implements FabricGameTest {
public static final InventoryLock lock = Locki.registerLock(Lockii.id("test_suite"));

@GameTest(structureName = EMPTY_STRUCTURE)
public void checkPermission(TestContext ctx) {
ServerPlayerEntity player = ctx.spawnServerPlayer(0, 0, 0);

assertTrue(Permissions.check(player, "locki.access.inventory", true));
InventoryLock lock = Locki.registerLock(new Identifier("test", "test"));
InventoryNode node = Locki.registerNode(InventoryNode.ROOT, "test");
Locki.registerNode(node, "child");
Locki.registerNode(InventoryNode.ROOT, "test-foo");
player.getInventory().addLock(lock, node);

assertFalse(Permissions.check(player, "locki.access.test", true));
assertFalse(Permissions.check(player, "locki.access.test.child", true));
assertTrue(Permissions.check(player, "locki.access.test-foo", true));
ctx.complete();
}

@GameTest(structureName = EMPTY_STRUCTURE)
public void lockingPreventsItemPickup(TestContext ctx) {
ServerPlayerEntity player = ctx.spawnServerPlayer(1, 0, 1);
Expand Down

0 comments on commit 51d8f7b

Please sign in to comment.