Skip to content

Commit

Permalink
Add initial game tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 7, 2024
1 parent 1a16155 commit 711049e
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ org.gradle.daemon=false
org.gradle.caching=true

# Common dependencies
cyclopscore_version=1.25.1-627
cyclopscore_version=1.25.2-634
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.cyclops.everlastingabilities.gametest;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameType;
import org.cyclops.everlastingabilities.EverlastingAbilitiesInstance;
import org.cyclops.everlastingabilities.Reference;
import org.cyclops.everlastingabilities.api.Ability;
import org.cyclops.everlastingabilities.api.IAbilityType;
import org.cyclops.everlastingabilities.api.capability.IMutableAbilityStore;
import org.cyclops.everlastingabilities.helper.IAbilityHelpers;

import java.util.Optional;

/**
* @author rubensworks
*/
public class GameTestsCommon {

public static final String TEMPLATE_EMPTY = "cyclopscore:empty";
public static final BlockPos POS = BlockPos.ZERO;

@GameTest(template = TEMPLATE_EMPTY)
public void testPlayerAbilityNotEnoughXp(GameTestHelper helper) {
helper.succeedIf(() -> {
Player player = helper.makeMockPlayer(GameType.SURVIVAL);

// Assign XP to player
player.totalExperience = 0;

// Determine an ability
Registry<IAbilityType> registry = getAbilityHelpers().getRegistry(helper.getLevel().registryAccess());
Holder<IAbilityType> abilityType = registry.getHolderOrThrow(ResourceKey.create(registry.key(), ResourceLocation.fromNamespaceAndPath(Reference.MOD_ID, "effect/speed")));
Ability ability = new Ability(abilityType, 3);

// Add ability
helper.assertTrue(getAbilityHelpers().addPlayerAbility(
player,
ability,
false,
true
).isEmpty(), "Expected not to be addable in simulate-mode");

// Check if player has an ability store
Optional<IMutableAbilityStore> store = getAbilityHelpers().getEntityAbilityStore(player);
helper.assertTrue(store.isPresent(), "Player has no ability store");

// Check if ability was not added
helper.assertValueEqual(store.get().getAbilities().size(), 0, "Expect ability store of size 1");
});
}

@GameTest(template = TEMPLATE_EMPTY)
public void testPlayerAbilityValid(GameTestHelper helper) {
helper.succeedIf(() -> {
Player player = helper.makeMockPlayer(GameType.SURVIVAL);

// Assign XP to player
player.totalExperience = 300;

// Determine an ability
Registry<IAbilityType> registry = getAbilityHelpers().getRegistry(helper.getLevel().registryAccess());
Holder<IAbilityType> abilityType = registry.getHolderOrThrow(ResourceKey.create(registry.key(), ResourceLocation.fromNamespaceAndPath(Reference.MOD_ID, "effect/speed")));
Ability ability = new Ability(abilityType, 3);

// Add ability
helper.assertTrue(!getAbilityHelpers().addPlayerAbility(
player,
ability,
false,
true
).isEmpty(), "Expected to be addable in simulate-mode");
helper.assertTrue(!getAbilityHelpers().addPlayerAbility(
player,
ability,
true,
true
).isEmpty(), "Expected to be addable");

// Check if player has an ability store
Optional<IMutableAbilityStore> store = getAbilityHelpers().getEntityAbilityStore(player);
helper.assertTrue(store.isPresent(), "Player has no ability store");

// Check if ability was added
helper.assertValueEqual(store.get().getAbilities().size(), 1, "Expect ability store of size 1");
helper.assertTrue(store.get().getAbility(abilityType).getAbilityType() != null, "Expect ability type to be contained");
helper.assertValueEqual(store.get().getAbility(abilityType).getAbilityTypeHolder(), abilityType, "Expect ability type to be correct");
helper.assertValueEqual(store.get().getAbility(abilityType).getLevel(), 3, "Expect ability level to be correct");
});
}

// TODO: max level exceeded

// TODO: add same ab multiple times

// TODO: add multiple abs

// TODO: remove ab

// TODO: onPlayerClone

// TODO: interact with abilities of a totem/bottle item

// TODO: commands?

public static IAbilityHelpers getAbilityHelpers() {
return EverlastingAbilitiesInstance.MOD.getAbilityHelpers();
}

}
3 changes: 3 additions & 0 deletions loader-fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"entrypoints": {
"main": [
"org.cyclops.everlastingabilities.EverlastingAbilitiesFabric"
],
"fabric-gametest": [
"org.cyclops.everlastingabilities.gametest.GameTestsCommon"
]
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.cyclops.everlastingabilities.gametest;

import net.minecraft.gametest.framework.GameTestGenerator;
import net.minecraft.gametest.framework.TestFunction;
import net.minecraftforge.gametest.GameTestHolder;
import org.cyclops.everlastingabilities.Reference;
import org.cyclops.cyclopscore.gametest.GameTestLoaderHelpers;

import java.util.Collection;

/**
* @author rubensworks
*/
@GameTestHolder(Reference.MOD_ID)
public class GameTestsLoaderForge extends GameTestsCommon {
@GameTestGenerator
public Collection<TestFunction> generateCommonTests() throws InstantiationException, IllegalAccessException {
return GameTestLoaderHelpers.generateCommonTests(Reference.MOD_ID, new Class[]{
GameTestsCommon.class
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.cyclops.everlastingabilities.gametest;

import net.minecraft.gametest.framework.GameTestGenerator;
import net.minecraft.gametest.framework.TestFunction;
import net.neoforged.neoforge.gametest.GameTestHolder;
import org.cyclops.everlastingabilities.Reference;
import org.cyclops.cyclopscore.gametest.GameTestLoaderHelpers;

import java.util.Collection;

/**
* @author rubensworks
*/
@GameTestHolder(Reference.MOD_ID)
public class GameTestsLoaderNeoForge extends GameTestsCommon {
@GameTestGenerator
public Collection<TestFunction> generateCommonTests() throws InstantiationException, IllegalAccessException {
return GameTestLoaderHelpers.generateCommonTests(Reference.MOD_ID, new Class[]{
GameTestsCommon.class
});
}
}

0 comments on commit 711049e

Please sign in to comment.