Skip to content

Commit

Permalink
Extract base test class for WorldEdit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TomyLobo committed May 13, 2023
1 parent df13de3 commit 4e190f5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 71 deletions.
@@ -0,0 +1,66 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit;

import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.util.test.ResourceLockKeys;
import com.sk89q.worldedit.world.registry.BundledRegistries;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.parallel.ResourceLock;

import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ResourceLock(ResourceLockKeys.WORLDEDIT_PLATFORM)
public abstract class BaseWorldEditTest {
protected static final Platform MOCKED_PLATFORM = mock(Platform.class);

@BeforeAll
static void setUpPlatform() {
when(MOCKED_PLATFORM.getRegistries()).thenReturn(new BundledRegistries() {
});
when(MOCKED_PLATFORM.getCapabilities()).thenReturn(
Stream.of(Capability.values())
.collect(Collectors.toMap(Function.identity(), __ -> Preference.NORMAL))
);
when(MOCKED_PLATFORM.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override
public void load() {
}
});
WorldEdit.getInstance().getPlatformManager().register(MOCKED_PLATFORM);
WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent());
assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized");
}

@AfterAll
static void tearDown() {
WorldEdit.getInstance().getPlatformManager().unregister(MOCKED_PLATFORM);
}
}
Expand Up @@ -19,58 +19,23 @@

package com.sk89q.worldedit.internal.expression;

import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.BaseWorldEditTest;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.util.test.ResourceLockKeys;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.parallel.ResourceLock;

import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Common setup code for expression tests.
*/
@ResourceLock(ResourceLockKeys.WORLDEDIT_PLATFORM)
class BaseExpressionTest {

static double readSlot(Expression expr, String name) {
return expr.getSlots().getSlotValue(name).orElseThrow(IllegalStateException::new);
}

private final Platform mockPlat = mock(Platform.class);

class BaseExpressionTest extends BaseWorldEditTest {
@BeforeEach
void setup() {
when(mockPlat.getCapabilities()).thenReturn(
Stream.of(Capability.values())
.collect(Collectors.toMap(Function.identity(), __ -> Preference.NORMAL))
);
when(mockPlat.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override
public void load() {
}
});
WorldEdit.getInstance().getPlatformManager().register(mockPlat);
WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent());
assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized");
WorldEdit.getInstance().getConfiguration().calculationTimeout = 1_000;
}

@AfterEach
void tearDown() {
WorldEdit.getInstance().getPlatformManager().unregister(mockPlat);
static double readSlot(Expression expr, String name) {
return expr.getSlots().getSlotValue(name).orElseThrow(IllegalStateException::new);
}

void checkTestCase(String expression, double result) {
Expand Down
Expand Up @@ -20,21 +20,16 @@
package com.sk89q.worldedit.util.collection;

import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.BaseWorldEditTest;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.PlatformManager;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.Registry;
import com.sk89q.worldedit.util.test.ResourceLockKeys;
import com.sk89q.worldedit.util.test.VariedVectorGenerator;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.registry.BundledRegistries;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -55,9 +50,7 @@
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -70,41 +63,18 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@Execution(ExecutionMode.CONCURRENT)
@ResourceLock(ResourceLockKeys.WORLDEDIT_PLATFORM)
@DisplayName("An ordered block map")
class BlockMapTest {

private static final Platform MOCKED_PLATFORM = mock(Platform.class);

class BlockMapTest extends BaseWorldEditTest {
@BeforeAll
static void setupFakePlatform() {
when(MOCKED_PLATFORM.getRegistries()).thenReturn(new BundledRegistries() {
});
when(MOCKED_PLATFORM.getCapabilities()).thenReturn(
Stream.of(Capability.values())
.collect(Collectors.toMap(Function.identity(), __ -> Preference.NORMAL))
);
when(MOCKED_PLATFORM.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override
public void load() {
}
});
PlatformManager platformManager = WorldEdit.getInstance().getPlatformManager();
platformManager.register(MOCKED_PLATFORM);
WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent());

assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized");

registerBlock("minecraft:air");
registerBlock("minecraft:oak_wood");
}

@AfterAll
static void tearDownFakePlatform() throws Exception {
WorldEdit.getInstance().getPlatformManager().unregister(MOCKED_PLATFORM);
Field map = Registry.class.getDeclaredField("map");
map.setAccessible(true);
((Map<?, ?>) map.get(BlockType.REGISTRY)).clear();
Expand Down

0 comments on commit 4e190f5

Please sign in to comment.