Skip to content

Commit

Permalink
Improved code coverage in core
Browse files Browse the repository at this point in the history
  • Loading branch information
Duckelekuuk committed Aug 29, 2023
1 parent 63a51c0 commit 4cf401f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.pixelib.meteor.core.executor;

import dev.pixelib.meteor.core.transport.packets.InvocationDescriptor;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class ImplementationWrapperTest {

@Test
void invokeOn_success() throws NoSuchMethodException {

ImplementationWrapper wrapper = new ImplementationWrapper(new MathFunctionImplementation(), "namespace");
Class<?>[] argTypes = new Class<?>[] { int.class, int.class };

InvocationDescriptor descriptor = new InvocationDescriptor("math", MathFunctionImplementation.class, "add", new Object[]{1, 2},argTypes, int.class);
assertEquals(3, wrapper.invokeOn(descriptor, int.class));
}

@Test
void invokeOn_withBoxParams() throws NoSuchMethodException {
ImplementationWrapper wrapper = new ImplementationWrapper(new MathFunctionImplementation(), "namespace");
Class<?>[] argTypes = new Class<?>[] { Integer.class, Integer.class };

InvocationDescriptor descriptor = new InvocationDescriptor("math", MathFunctionImplementation.class, "add", new Object[]{1, 2},argTypes, int.class);
assertEquals(3, wrapper.invokeOn(descriptor, int.class));
}

@Test
void invokeOn_unknownMethod() {
ImplementationWrapper wrapper = new ImplementationWrapper(new MathFunctionImplementation(), "namespace");
Class<?>[] argTypes = new Class<?>[] { Integer.class, Integer.class };

InvocationDescriptor descriptor = new InvocationDescriptor("math", MathFunctionImplementation.class, "unknown", new Object[]{1, 2},argTypes, int.class);
NoSuchMethodException noSuchMethodException = assertThrowsExactly(NoSuchMethodException.class, () -> {
wrapper.invokeOn(descriptor, int.class);
});

assertEquals("No method found with name unknown and compatible arguments (on " + MathFunctionImplementation.class.getName() + ").", noSuchMethodException.getMessage());
}
@Test
void getImplementation_success() {
MathFunctionImplementation implementation = new MathFunctionImplementation();

ImplementationWrapper implementationWrapper = new ImplementationWrapper(implementation, "math");

assertSame(implementation, implementationWrapper.getImplementation());
}

@Test
void getNamespace_success() {
ImplementationWrapper implementationWrapper = new ImplementationWrapper(new MathFunctionImplementation(), "math");
assertEquals("math", implementationWrapper.getNamespace());
}

static class MathFunctionImplementation implements MathFunction {
@Override
public int add(int a, int b) {
return a + b;
}

@Override
public int add(int... a) {
return Arrays.stream(a).sum();
}

@Override
public int add(int a, Integer... b) {
return a + Arrays.stream(b).mapToInt(Integer::intValue).sum();
}

@Override
public int add(int a, Double... b) {
return a + Arrays.stream(b).mapToInt(Double::intValue).sum();
}

@Override
public int sub(int a, int b) {
return a - b;
}
}

public interface MathFunction {
int add(int a, int b);
int add(int... a);
int add(int a, Integer... b);


int add(int a, Double... b);

int sub(int a, int b);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.pixelib.meteor.core.utils;

import dev.pixelib.meteor.core.Meteor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -92,6 +93,7 @@ private void singleParamMethod(Integer integer) { }
private void multipleParamsMethod(Integer integer, String str, Double dd) { }
private void oneArrayMethod(int[] a) { }
private void optionalArrayMethod(int a, int... b) {}
private void sampleMethod(String a, Integer... b) {}
}

private static Stream<Arguments> provideArgumentsForTest() throws NoSuchMethodException {
Expand Down Expand Up @@ -124,4 +126,19 @@ void testOverflowArguments(Method method, Object[] allArguments, Object[] expect
void testOverflowArguments_WithEmptyOrNullExceptions(Object[] allArguments) {
assertThrows(NullPointerException.class, () -> ArgumentMapper.overflowArguments(null, allArguments));
}

@Test
void testOverflowArguments() throws NoSuchMethodException {
Method method = Example.class.getDeclaredMethod("sampleMethod", String.class, Integer[].class);
final Object[] allArguments = new Object[] { "test", new Integer[] {1, 2, 3}, new Integer[] {4, 5, 6}};

//Valid path
Assertions.assertDoesNotThrow(() -> ArgumentMapper.overflowArguments(method, allArguments));

//Invalid path - Type mismatch
Object[] allArguments2 = new Object[] { "test", new Boolean[] {true, false}, new Integer[] {4, 5, 6}};
Assertions.assertThrows(RuntimeException.class,
() -> ArgumentMapper.overflowArguments(method, allArguments2),
"Argument type mismatch. java.lang.Integer expected, got java.lang.Boolean instead for argument 0.");
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</modules>

<properties>
<revision>localbuild</revision>
<revision>1.0.0-localbuild</revision>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down

0 comments on commit 4cf401f

Please sign in to comment.