Skip to content
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

Change or revise the testing framework #62

Closed
TheMeinerLP opened this issue Jun 15, 2024 · 3 comments · Fixed by #65
Closed

Change or revise the testing framework #62

TheMeinerLP opened this issue Jun 15, 2024 · 3 comments · Fixed by #65
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@TheMeinerLP
Copy link
Collaborator

TheMeinerLP commented Jun 15, 2024

After waiting for an internal tool and interesting errors from Intellij, the testing framework should be converted to extensions instead of annotations.

Before:

@EnvironmentTest
class NotificationIntegrationTest {

    @Test
    void testBuilder() {
        var notification = Notification.builder()
                .icon(Material.ITEM_FRAME)
                .title(Component.text("unit test"))
                .frameType(FrameType.TASK)
                .build();
        assertEquals(FrameType.TASK, notification.type());
        assertEquals(ItemStack.of(Material.ITEM_FRAME), notification.icon());
        assertEquals(Component.text("unit test"), notification.title());
    }

    @Test
    void testSend(Env env) {
        var instance = env.createFlatInstance();
        var connection = env.createConnection();
        Collector<AdvancementsPacket> advancementsPacketCollector = connection.trackIncoming(AdvancementsPacket.class);
        var player = connection.connect(instance, new Pos(0, 42, 0)).join();
        var notification = Notification.builder()
                .icon(Material.ITEM_FRAME)
                .title(Component.text("unit test"))
                .frameType(FrameType.TASK)
                .build();
        notification.send(player);
        advancementsPacketCollector.assertCount(2);
        AdvancementsPacket advancementsPacket = advancementsPacketCollector.collect().get(1);
        assertNotNull(advancementsPacket);
        Optional<AdvancementsPacket.AdvancementMapping> advancementMapping = advancementsPacket.advancementMappings().stream().findFirst();
        advancementMapping.ifPresent(advancementMapping1 -> {
            AdvancementsPacket.Advancement advancement = advancementMapping1.value();
            assertFalse(advancement.sendTelemetryData());
            var displayData = advancement.displayData();
            assertEquals(ItemStack.of(Material.ITEM_FRAME), displayData.icon());
            assertEquals(Component.text("unit test"), displayData.title());
            assertEquals(FrameType.TASK, displayData.frameType());
        });
    }
}

After:

@ExtendWith(MicrotusExtension.class)
class NotificationIntegrationTest {

    @Test
    void testBuilder() {
        var notification = Notification.builder()
                .icon(Material.ITEM_FRAME)
                .title(Component.text("unit test"))
                .frameType(FrameType.TASK)
                .build();
        assertEquals(FrameType.TASK, notification.type());
        assertEquals(ItemStack.of(Material.ITEM_FRAME), notification.icon());
        assertEquals(Component.text("unit test"), notification.title());
    }

    @Test
    void testSend(Env env) {
        var instance = env.createFlatInstance();
        var connection = env.createConnection();
        Collector<AdvancementsPacket> advancementsPacketCollector = connection.trackIncoming(AdvancementsPacket.class);
        var player = connection.connect(instance, new Pos(0, 42, 0)).join();
        var notification = Notification.builder()
                .icon(Material.ITEM_FRAME)
                .title(Component.text("unit test"))
                .frameType(FrameType.TASK)
                .build();
        notification.send(player);
        advancementsPacketCollector.assertCount(2);
        AdvancementsPacket advancementsPacket = advancementsPacketCollector.collect().get(1);
        assertNotNull(advancementsPacket);
        Optional<AdvancementsPacket.AdvancementMapping> advancementMapping = advancementsPacket.advancementMappings().stream().findFirst();
        advancementMapping.ifPresent(advancementMapping1 -> {
            AdvancementsPacket.Advancement advancement = advancementMapping1.value();
            assertFalse(advancement.sendTelemetryData());
            var displayData = advancement.displayData();
            assertEquals(ItemStack.of(Material.ITEM_FRAME), displayData.icon());
            assertEquals(Component.text("unit test"), displayData.title());
            assertEquals(FrameType.TASK, displayData.frameType());
        });
    }
}

Extension Class:

public class MicrotusExtension extends TypeBasedParameterResolver<Env> implements InvocationInterceptor {

    @Override
    public Env resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
            throws ParameterResolutionException {
        return Env.createInstance(MinecraftServer.updateProcess());
    }

    @Override
    public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
        invocation.proceed();
        List<Object> arguments = invocationContext.getArguments();
        arguments.stream().filter(Env.class::isInstance).findFirst().ifPresent(o -> {
            Env env = (Env) o;
            env.cleanup();
        });
    }
}
@TheMeinerLP TheMeinerLP added documentation Improvements or additions to documentation enhancement New feature or request labels Jun 15, 2024
@yannicklamprecht
Copy link

yannicklamprecht commented Jun 15, 2024

Seems like a useful change. Can we expect other Microtus extensions popping up in the future?
If that's the case naming of the class should be adjusted. MicrotusEnvExtension or MicrotusEnvironmentExtension?

@TheMeinerLP
Copy link
Collaborator Author

So Mockito puts them together in a Java file. So good question, would have to be specified more precisely.

The reason for the change is this

Screenshot_20240616_014745

@yannicklamprecht
Copy link

Yeah a parameterized test wouldn't be the right way to address that. Extensions are totally fine and made for that.

@TheMeinerLP TheMeinerLP self-assigned this Jun 16, 2024
@TheMeinerLP TheMeinerLP mentioned this issue Jun 16, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants