Browse Source

Fix tests

Lortseam 4 năm trước cách đây
mục cha
commit
5ad0341be2

+ 18 - 58
lib/src/test/java/me/lortseam/completeconfig/data/ConfigTest.java

@@ -1,11 +1,7 @@
 package me.lortseam.completeconfig.data;
 
-import me.lortseam.completeconfig.api.ConfigContainer;
-import me.lortseam.completeconfig.test.data.containers.EmptyContainer;
 import nl.altindag.log.LogCaptor;
 import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -13,64 +9,28 @@ import static org.junit.jupiter.api.Assertions.*;
 
 public class ConfigTest {
 
-    @Test
-    public void builder_throwExceptionIfModIdNull() {
-        NullPointerException exception = assertThrows(NullPointerException.class, () -> Config.builder(null));
-        assertEquals("modId is marked non-null but is null", exception.getMessage());
-    }
-
-    @Nested
-    public class Builder {
-
-        private static final String MOD_ID = "test";
-
-        private Config.Builder builder;
-        private final LogCaptor logCaptor = LogCaptor.forName("CompleteConfig");
-
-        @BeforeEach
-        public void beforeEach() {
-            builder = Config.builder(MOD_ID);
-        }
-
-        @AfterEach
-        public void afterEach() {
-            logCaptor.clearLogs();
-        }
+    private static final String MOD_ID = "test";
 
-        @Test
-        public void setBranch_throwIfBranchNull() {
-            NullPointerException exception = assertThrows(NullPointerException.class, () -> builder.setBranch(null));
-            assertEquals("branch is marked non-null but is null", exception.getMessage());
-        }
+    private final LogCaptor logCaptor = LogCaptor.forName("CompleteConfig");
 
-        @Test
-        public void setBranch_throwIfBranchContainsNullElement() {
-            assertThrows(NullPointerException.class, () -> builder.setBranch(new String[]{null}));
-        }
-
-        @Test
-        public void add_throwIfContainersNull() {
-            NullPointerException exception = assertThrows(NullPointerException.class, () -> builder.add((ConfigContainer[]) null));
-            assertEquals("containers is marked non-null but is null", exception.getMessage());
-        }
-
-        @Test
-        public void add_throwIfContainersContainNullElement() {
-            assertThrows(NullPointerException.class, () -> builder.add((ConfigContainer) null));
-        }
-
-        @Test
-        public void build_logWarningAndReturnNullIfChildrenEmpty() {
-            assertNull(builder.build());
-            assertThat(logCaptor.getWarnLogs()).containsExactly("Empty config: " + MOD_ID + " []");
-        }
+    @AfterEach
+    public void afterEach() {
+        logCaptor.clearLogs();
+    }
 
-        @Test
-        public void build_logWarningAndReturnNullIfEmpty() {
-            assertNull(builder.add(new EmptyContainer()).build());
-            assertThat(logCaptor.getWarnLogs()).containsExactly("Empty config: " + MOD_ID + " []");
-        }
+    @Test
+    public void _throwExceptionIfArgNull() {
+        NullPointerException exception = assertThrows(NullPointerException.class, () -> new Config(null, false) {});
+        assertEquals("modId is marked non-null but is null", exception.getMessage());
+        exception = assertThrows(NullPointerException.class, () -> new Config(MOD_ID, null, false) {});
+        assertEquals("branch is marked non-null but is null", exception.getMessage());
+        assertThrows(NullPointerException.class, () -> new Config(MOD_ID, new String[]{null}, false) {});
+    }
 
+    @Test
+    public void _logWarningIfEmpty() {
+        new Config(MOD_ID, false) {};
+        assertThat(logCaptor.getWarnLogs()).containsExactly("Empty config: " + MOD_ID + " []");
     }
 
 }

+ 12 - 1
lib/src/test/java/me/lortseam/completeconfig/test/extension/FabricLoaderExtension.java

@@ -3,8 +3,12 @@ package me.lortseam.completeconfig.test.extension;
 import com.google.common.jimfs.Jimfs;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.loader.api.FabricLoader;
+import net.fabricmc.loader.api.ModContainer;
+import net.fabricmc.loader.api.metadata.ModMetadata;
 import org.junit.jupiter.api.extension.Extension;
 
+import java.util.Optional;
+
 import static org.mockito.Mockito.*;
 
 public class FabricLoaderExtension implements Extension {
@@ -23,7 +27,14 @@ public class FabricLoaderExtension implements Extension {
             default:
                 throw new IllegalArgumentException("Unknown environment property");
         }
-        when(loader.isModLoaded(any(String.class))).thenReturn(true);
+        when(loader.isModLoaded(anyString())).thenReturn(true);
+        when(loader.getModContainer(anyString())).thenAnswer(invocation -> {
+            ModMetadata metadata = mock(ModMetadata.class);
+            when(metadata.getId()).thenReturn(invocation.getArgument(0, String.class));
+            ModContainer mod = mock(ModContainer.class);
+            when(mod.getMetadata()).thenReturn(metadata);
+            return Optional.of(mod);
+        });
         when(loader.getConfigDir()).thenReturn(Jimfs.newFileSystem().getPath(""));
         mockStatic(FabricLoader.class).when(FabricLoader::getInstance).thenReturn(loader);
     }