Skip to content

Commit

Permalink
Actually fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jul 22, 2021
1 parent e028796 commit 694f5ca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
40 changes: 40 additions & 0 deletions src/test/java/dev/onyxstudios/cca/internal/base/CcaTesting.java
Original file line number Diff line number Diff line change
@@ -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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<ComponentKey<?>, QualifiedComponentFactory<Object>> 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()));
Expand All @@ -75,8 +70,8 @@ public void sortKeepsOrderByDefault() {
@Test
public void sortThrowsOnUnsatisfiedDependency() {
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> 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()));
Expand All @@ -86,9 +81,9 @@ public void sortThrowsOnUnsatisfiedDependency() {
@Test
public void sortThrowsOnCircularDependency() {
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> 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)));
Expand All @@ -105,9 +100,9 @@ public void sortThrowsOnCircularDependency() {
@Test
public void sortRespectsDependencyOrdering() {
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> 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()));
Expand Down

0 comments on commit 694f5ca

Please sign in to comment.