-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Add MTE tests for ItemCommands #12
Open
skaldarnar
wants to merge
7
commits into
develop
Choose a base branch
from
test/item-commands-test
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b2a514
test: Add optional dependency on MTE and CoreAssets; Upgrade to JUnit 5
skaldarnar c278f8e
test: Add MTE test skeleton for ItemCommands
skaldarnar c83ff9e
test: migrate to JUnit 5
skaldarnar d6a3076
. attempt to make the `give` command work in in the test...
skaldarnar 75d9841
Merge branch 'develop' into test/item-commands-test
skaldarnar 3b3037e
Merge branch 'develop' into test/item-commands-test
jdrueckert a418d6c
Merge remote-tracking branch 'origin/develop' into test/item-commands…
jdrueckert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/test/java/org/terasology/logic/inventory/ItemCommandsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2020 MovingBlocks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.terasology.logic.inventory; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.terasology.context.Context; | ||
import org.terasology.entitySystem.entity.EntityManager; | ||
import org.terasology.entitySystem.entity.EntityRef; | ||
import org.terasology.logic.players.LocalPlayer; | ||
import org.terasology.math.TeraMath; | ||
import org.terasology.moduletestingenvironment.ModuleTestingEnvironment; | ||
import org.terasology.world.block.BlockManager; | ||
import org.terasology.world.block.items.BlockItemFactory; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Verify that the {@link ItemCommands} are working as expected. | ||
*/ | ||
public class ItemCommandsTest { | ||
|
||
static final String URI_DIRT = "CoreAssets:Dirt"; | ||
|
||
private static ModuleTestingEnvironment mte; | ||
|
||
private Context hostContext; | ||
private EntityManager entityManager; | ||
private InventoryManager inventoryManager; | ||
private BlockManager blockManager; | ||
private ItemCommands itemCommands; | ||
|
||
@BeforeAll | ||
public static void setup() throws Exception { | ||
mte = new ModuleTestingEnvironment() { | ||
@Override | ||
public Set<String> getDependencies() { | ||
return Sets.newHashSet("CoreAssets", "Inventory"); | ||
} | ||
}; | ||
mte.setup(); | ||
} | ||
|
||
@AfterAll | ||
public static void tearDown() throws Exception { | ||
mte.tearDown(); | ||
} | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
hostContext = mte.getHostContext(); | ||
entityManager = hostContext.get(EntityManager.class); | ||
inventoryManager = hostContext.get(InventoryManager.class); | ||
blockManager = hostContext.get(BlockManager.class); | ||
itemCommands = hostContext.get(ItemCommands.class); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {0, 1, 99, 100}) | ||
public void giveItemSingleBlockStack(int amount) { | ||
|
||
Context clientContext = mte.createClient(); | ||
LocalPlayer localPlayer = clientContext.get(LocalPlayer.class); | ||
final EntityRef player = localPlayer.getClientEntity(); | ||
final EntityRef character = localPlayer.getCharacterEntity(); | ||
character.upsertComponent(InventoryComponent.class, maybeInventory -> maybeInventory.orElse(new InventoryComponent(40))); | ||
|
||
final BlockItemFactory factory = new BlockItemFactory(entityManager); | ||
final EntityRef blockItem = factory.newInstance(blockManager.getBlockFamily(URI_DIRT), amount); | ||
|
||
Assert.assertNotEquals("Cannot create a block item instance for '" + URI_DIRT + "'", EntityRef.NULL, blockItem); | ||
|
||
//TODO: I don't know what to pass in there, whether it needs to be the player, character, or something else | ||
itemCommands.give(player, URI_DIRT, amount, null); | ||
|
||
final List<EntityRef> filledSlots = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be moved to |
||
character.getComponent(InventoryComponent.class).itemSlots.stream() | ||
.filter(entityRef -> entityRef != EntityRef.NULL) | ||
.collect(Collectors.toList()); | ||
|
||
final int maxStackSize = Byte.toUnsignedInt(blockItem.getComponent(ItemComponent.class).maxStackSize); | ||
final int expectedFilledSlots = TeraMath.ceilToInt(amount / (float) maxStackSize); | ||
Assert.assertEquals("Unexpected number of filled inventory slots", expectedFilledSlots, filledSlots.size()); | ||
|
||
filledSlots.forEach(item -> | ||
Assert.assertEquals("Slot should contain exactly the added block", | ||
blockItem, item)); | ||
|
||
filledSlots.forEach(item -> | ||
Assert.assertEquals("Item quantity diverges from requested amount", | ||
amount, Byte.toUnsignedInt(item.getComponent(ItemComponent.class).stackCount))); | ||
|
||
for (int i = 0; i < filledSlots.size(); i++) { | ||
int actualStackCount = Byte.toUnsignedInt(filledSlots.get(i).getComponent(ItemComponent.class).stackCount); | ||
if (i == amount / maxStackSize) { | ||
Assert.assertEquals(amount % maxStackSize, actualStackCount); | ||
} else { | ||
Assert.assertEquals(maxStackSize, actualStackCount); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're doing this in #12 as well. We could consider to create a
TestUtils
class 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this not needed here... at least not as of now... will be tricky enough to figure out what classifies the added block to be "correct"