From 694f5cac431b9d301fde3920d6512c65444c6d47 Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Thu, 22 Jul 2021 17:17:32 +0200 Subject: [PATCH] Actually fix unit tests --- .../cca/internal/base/CcaTesting.java | 40 +++++++++++++++++++ .../base/ComponentRegistryImplTest.java | 17 +++----- .../base/QualifiedComponentFactoryTest.java | 39 ++++++++---------- 3 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 src/test/java/dev/onyxstudios/cca/internal/base/CcaTesting.java diff --git a/src/test/java/dev/onyxstudios/cca/internal/base/CcaTesting.java b/src/test/java/dev/onyxstudios/cca/internal/base/CcaTesting.java new file mode 100644 index 00000000..a3116ded --- /dev/null +++ b/src/test/java/dev/onyxstudios/cca/internal/base/CcaTesting.java @@ -0,0 +1,40 @@ +/* + * Cardinal-Components-API + * Copyright (C) 2019-2021 OnyxStudios + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE + * OR OTHER DEALINGS IN THE SOFTWARE. + */ +package dev.onyxstudios.cca.internal.base; + +import dev.onyxstudios.cca.internal.base.asm.CcaBootstrapTest; +import net.minecraft.util.Identifier; + +public class CcaTesting { + public static final Identifier TEST_ID_1 = new Identifier("testmod:test"); + public static final Identifier TEST_ID_2 = new Identifier("testmod:test_2"); + public static final Identifier TEST_ID_3 = new Identifier("testmod:test_3"); + + public static void init() { + CcaBootstrapTest.addStaticComponentInitializers( + CcaTesting.TEST_ID_1, + CcaTesting.TEST_ID_2, + CcaTesting.TEST_ID_3 + ); + } +} diff --git a/src/test/java/dev/onyxstudios/cca/internal/base/ComponentRegistryImplTest.java b/src/test/java/dev/onyxstudios/cca/internal/base/ComponentRegistryImplTest.java index d296b3f5..02d8225b 100644 --- a/src/test/java/dev/onyxstudios/cca/internal/base/ComponentRegistryImplTest.java +++ b/src/test/java/dev/onyxstudios/cca/internal/base/ComponentRegistryImplTest.java @@ -24,7 +24,6 @@ import dev.onyxstudios.cca.api.v3.component.Component; import dev.onyxstudios.cca.api.v3.component.ComponentKey; -import dev.onyxstudios.cca.internal.base.asm.CcaBootstrapTest; import net.minecraft.nbt.NbtCompound; import net.minecraft.util.Identifier; import org.junit.After; @@ -36,28 +35,22 @@ public class ComponentRegistryImplTest { - public static final Identifier TEST_ID_1 = new Identifier("testmod:test"); - public static final Identifier TEST_ID_2 = new Identifier("testmod:test_2"); - @BeforeClass public static void beforeAll() { - CcaBootstrapTest.addStaticComponentInitializers( - TEST_ID_1, - TEST_ID_2 - ); + CcaTesting.init(); } @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void checksRegisteredClasses() { ComponentRegistryImpl registry = ComponentRegistryImpl.INSTANCE; - assertThrows("Component class must extend Component", IllegalArgumentException.class, () -> registry.getOrCreate(TEST_ID_1, (Class) TestNotComponentItf.class)); - registry.getOrCreate(TEST_ID_1, TestComponentNotItf.class); - registry.getOrCreate(TEST_ID_2, TestComponentItf.class); + assertThrows("Component class must extend Component", IllegalArgumentException.class, () -> registry.getOrCreate(CcaTesting.TEST_ID_1, (Class) TestNotComponentItf.class)); + registry.getOrCreate(CcaTesting.TEST_ID_1, TestComponentNotItf.class); + registry.getOrCreate(CcaTesting.TEST_ID_2, TestComponentItf.class); } @Test public void doesNotDuplicateComponentTypes() { ComponentRegistryImpl registry = ComponentRegistryImpl.INSTANCE; - Identifier id = TEST_ID_1; + Identifier id = CcaTesting.TEST_ID_1; ComponentKey type = registry.getOrCreate(id, TestComponentItf.class); assertThrows(IllegalStateException.class, () -> registry.getOrCreate(id, TestComponentItf2.class)); assertThrows(IllegalStateException.class, () -> registry.getOrCreate(id, TestComponentItf3.class)); diff --git a/src/test/java/dev/onyxstudios/cca/internal/base/QualifiedComponentFactoryTest.java b/src/test/java/dev/onyxstudios/cca/internal/base/QualifiedComponentFactoryTest.java index 5963d50b..d88e36f2 100644 --- a/src/test/java/dev/onyxstudios/cca/internal/base/QualifiedComponentFactoryTest.java +++ b/src/test/java/dev/onyxstudios/cca/internal/base/QualifiedComponentFactoryTest.java @@ -24,9 +24,8 @@ import dev.onyxstudios.cca.api.v3.component.ComponentKey; import dev.onyxstudios.cca.api.v3.component.ComponentRegistry; -import dev.onyxstudios.cca.internal.base.asm.CcaBootstrapTest; import dev.onyxstudios.cca.internal.base.asm.StaticComponentLoadingException; -import net.minecraft.util.Identifier; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; @@ -38,25 +37,21 @@ import static org.junit.Assert.*; public class QualifiedComponentFactoryTest { - public static final Identifier TEST_ID_1 = new Identifier("testmod:test"); - public static final Identifier TEST_ID_2 = new Identifier("testmod:test_2"); - public static final Identifier TEST_ID_3 = new Identifier("testmod:test_3"); - @BeforeClass public static void beforeAll() { - CcaBootstrapTest.addStaticComponentInitializers( - TEST_ID_1, - TEST_ID_2, - TEST_ID_3 - ); + CcaTesting.init(); + } + + @After public void tearDown() { + ComponentRegistryImpl.INSTANCE.clear(); } @Test public void sortKeepsOrderByDefault() { Map, QualifiedComponentFactory> map = new LinkedHashMap<>(); - var key1 = ComponentRegistry.getOrCreate(TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); - var key2 = ComponentRegistry.getOrCreate(TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); - var key3 = ComponentRegistry.getOrCreate(TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class); + var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); + var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); + var key3 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class); map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of())); map.put(key2, new QualifiedComponentFactory<>(new Object(), key2.getComponentClass(), Set.of())); map.put(key3, new QualifiedComponentFactory<>(new Object(), key3.getComponentClass(), Set.of())); @@ -75,8 +70,8 @@ public void sortKeepsOrderByDefault() { @Test public void sortThrowsOnUnsatisfiedDependency() { Map, QualifiedComponentFactory> map = new LinkedHashMap<>(); - var key1 = ComponentRegistry.getOrCreate(TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); - var key2 = ComponentRegistry.getOrCreate(TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); + var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); + var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2))); assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.checkDependenciesSatisfied(map)); map.put(key2, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of())); @@ -86,9 +81,9 @@ public void sortThrowsOnUnsatisfiedDependency() { @Test public void sortThrowsOnCircularDependency() { Map, QualifiedComponentFactory> map = new LinkedHashMap<>(); - var key1 = ComponentRegistry.getOrCreate(TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); - var key2 = ComponentRegistry.getOrCreate(TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); - var key3 = ComponentRegistry.getOrCreate(TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class); + var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); + var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); + var key3 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class); map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key1))); assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map)); map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2))); @@ -105,9 +100,9 @@ public void sortThrowsOnCircularDependency() { @Test public void sortRespectsDependencyOrdering() { Map, QualifiedComponentFactory> map = new LinkedHashMap<>(); - var key1 = ComponentRegistry.getOrCreate(TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); - var key2 = ComponentRegistry.getOrCreate(TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); - var key3 = ComponentRegistry.getOrCreate(TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class); + var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class); + var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class); + var key3 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class); map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2))); map.put(key2, new QualifiedComponentFactory<>(new Object(), key2.getComponentClass(), Set.of())); assertEquals(List.of(key2, key1), List.copyOf(QualifiedComponentFactory.sort(map).keySet()));