// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.persistence.typeHandling.reflection;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.testUtil.ModuleManagerFactory;
import org.terasology.engine.unittest.ExampleInterface;
import org.terasology.reflection.ModuleTypeRegistry;
import org.terasology.reflection.TypeRegistry;

import static org.terasology.engine.testUtil.Assertions.assertNotEmpty;

public class TestModuleEnvironmentSandbox {

    private ModuleEnvironmentSandbox sandbox;
    private TypeRegistry typeRegistry;

    @BeforeEach
    protected void provideSandbox() throws Exception {
        ModuleManager moduleManager = ModuleManagerFactory.create();
        // TODO: make unit test more focused by not relying on complex ModuleManager interactions
        typeRegistry = new ModuleTypeRegistry(moduleManager.getEnvironment());

        sandbox = new ModuleEnvironmentSandbox(moduleManager, typeRegistry);
    }

    @Test
    public void findSubtypeOfGlobalClass() {
        assertNotEmpty(sandbox.findSubTypeOf("org.joml.Vector3f", org.joml.Vector3fc.class));
    }

    @Test
    public void findSubtypeOfEngineClass() {
        assertNotEmpty(sandbox.findSubTypeOf("engine:BlockComponent", Component.class));
    }

    @Test
    public void findSubtypeOfModuleClass() {
        assertNotEmpty(sandbox.findSubTypeOf("unittest:ExampleClass", ExampleInterface.class));
    }
}

@@ -416,40 +416,40 @@
 ogy.
-engine.unittest.ExampleInterface
+gestalt.module.ModuleEnvironment
 ;%0Aim
@@ -545,16 +545,65 @@
 egistry;
+%0Aimport org.terasology.unittest.ExampleInterface;
 %0A%0Aimport
@@ -669,16 +669,53 @@
 Empty;%0A%0A
+@SuppressWarnings(%22FieldCanBeLocal%22)%0A
 public c
@@ -835,16 +835,100 @@
 egistry;
+%0A    private ModuleManager moduleManager;%0A    private ModuleEnvironment environment;
 %0A%0A    @B
@@ -1000,17 +1000,17 @@
         
-M
+m
 oduleMan
@@ -1006,33 +1006,35 @@
   moduleManager 
-m
+= M
 oduleManager = M
@@ -1021,36 +1021,73 @@
  = ModuleManager
+Factory.create();%0A        environment
  = 
-M
+m
 oduleManagerFact
@@ -1082,34 +1082,36 @@
 eManager
-Factory.create
+.getEnvironment
 ();%0A
+%0A
         
@@ -1246,34 +1246,17 @@
 egistry(
-moduleManager.getE
+e
 nvironme
@@ -1257,18 +1257,16 @@
 ironment
-()
 );%0A%0A    
@@ -1337,16 +1337,76 @@
 gistry);
+%0A%0A        // module = environment.get(new Name(%22unittest%22));
 %0A    %7D%0A%0A

fix ExampleInterface import