Skip to content

samueloluwadoromi/ModuleTestingEnvironment

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ModuleTestingEnvironment

A test helper to instantiate a full headless TerasologyEngine instance

Usage

Just write a test class that extends ModuleTestingEnvironment.

For complete docs please see the documentation on Github Pages

For more examples see the test suite

Here's an example taken from the test suite:

public class MyModuleEngineTest extends ModuleTestingEnvironment {
    protected Logger logger = Logger.getLogger(MyModuleEngineTest.class.getName());
    protected EntityManager entityManager;

    @Override
    public Set<String> getDependencies() {
        return Sets.newHashSet("MyModule");
    }

    @Override
    public String getWorldGeneratorUri() {
        return "mymodule:mymodulesworldgenerator";
    }

    @Before
    public void beforeMyModuleTests() {
        entityManager = getHostContext().get(EntityManager.class);
        runUntil(()-> getHostContext().get(MyModuleReadySystem.class).isMyModuleReady());
    }
    
    @Test
    public void testExample() {
        WorldProvider worldProvider = getHostContext().get(WorldProvider.class);
        BlockManager blockManager = getHostContext().get(BlockManager.class);

        // create some clients (the library connects them automatically)
        Context clientContext1 = createClient();
        Context clientContext2 = createClient();

        // assert that both clients are known to the server
        EntityManager hostEntityManager = getHostContext().get(EntityManager.class);
        List<EntityRef> clientEntities = Lists.newArrayList(hostEntityManager.getEntitiesWith(ClientComponent.class));
        Assert.assertEquals(2, clientEntities.size());

        // send an event to a client's local player just for fun
        clientContext1.get(LocalPlayer.class).getClientEntity().send(new ResetCameraEvent());

        // wait for a chunk to be generated
        forceAndWaitForGeneration(Vector3i.zero());

        // set a block's type and immediately read it back
        worldProvider.setBlock(Vector3i.zero(), blockManager.getBlock("engine:air"));
        Assert.assertEquals("engine:air", worldProvider.getBlock(Vector3f.zero()).getURI().toString());
    }
}

Receiving events

You can use a TestEventReceiver to inspect events fired against the engine context.

TestEventReceiver receiver = new TestEventReceiver<>(context, DropItemEvent.class, (event, entity) -> {
  // do something with the event or entity
});

Check the JavaDoc and test suite for more usage examples.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%