-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63a51c0
commit 4cf401f
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
meteor-core/src/test/java/dev/pixelib/meteor/core/executor/ImplementationWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters