Skip to content

Commit

Permalink
Expand test framework (#2638)
Browse files Browse the repository at this point in the history
* Use assertj typed assertions
* Add getTestEmbed
* Remove public visibility from tests
* Expand JsonTest and fix bugs in DataArray
* Add more tests for ser/de
* Refactor LocalizationTest
* Improve test package layout
* Add basic RestAction tests
  • Loading branch information
MinnDevelopment authored Apr 4, 2024
1 parent e2ef2da commit 13abdcb
Show file tree
Hide file tree
Showing 33 changed files with 2,453 additions and 1,577 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.1")
testImplementation("org.reflections:reflections:0.10.2")
testImplementation("org.mockito:mockito-core:5.8.0")
testImplementation("org.assertj:assertj-core:3.25.3")
}

val compileJava: JavaCompile by tasks
Expand Down
33 changes: 26 additions & 7 deletions src/main/java/net/dv8tion/jda/api/utils/data/DataArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package net.dv8tion.jda.api.utils.data;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.CollectionType;
import net.dv8tion.jda.api.exceptions.ParsingException;
Expand Down Expand Up @@ -218,7 +218,7 @@ public static DataArray fromETF(@Nonnull byte[] data)
*/
public boolean isNull(int index)
{
return data.get(index) == null;
return index >= length() || data.get(index) == null;
}

/**
Expand Down Expand Up @@ -782,12 +782,12 @@ public String toString()
@Nonnull
public String toPrettyString()
{
DefaultPrettyPrinter.Indenter indent = new DefaultIndenter(" ", DefaultIndenter.SYS_LF);
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.withObjectIndenter(indent).withArrayIndenter(indent);
try
{
return mapper.writer(printer).writeValueAsString(data);
return mapper.writer(new DefaultPrettyPrinter())
.with(SerializationFeature.INDENT_OUTPUT)
.with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
.writeValueAsString(data);
}
catch (JsonProcessingException e)
{
Expand Down Expand Up @@ -820,7 +820,9 @@ private <T> T get(@Nonnull Class<T> type, int index)
@Nullable
private <T> T get(@Nonnull Class<T> type, int index, @Nullable Function<String, T> stringMapper, @Nullable Function<Number, T> numberMapper)
{
Object value = data.get(index);
if (index < 0)
throw new IndexOutOfBoundsException("Index out of range: " + index);
Object value = index < data.size() ? data.get(index) : null;
if (value == null)
return null;
if (type.isInstance(value))
Expand Down Expand Up @@ -857,4 +859,21 @@ public DataArray toDataArray()
{
return this;
}

@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof DataArray))
return false;
DataArray objects = (DataArray) o;
return Objects.equals(data, objects.data);
}

@Override
public int hashCode()
{
return Objects.hash(data);
}
}
12 changes: 8 additions & 4 deletions src/main/java/net/dv8tion/jda/api/utils/data/DataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package net.dv8tion.jda.api.utils.data;

import com.fasterxml.jackson.core.FormatFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.MapType;
import net.dv8tion.jda.api.exceptions.ParsingException;
Expand Down Expand Up @@ -850,12 +854,12 @@ public String toString()
@Nonnull
public String toPrettyString()
{
DefaultPrettyPrinter.Indenter indent = new DefaultIndenter(" ", DefaultIndenter.SYS_LF);
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.withObjectIndenter(indent).withArrayIndenter(indent);
try
{
return mapper.writer(printer).writeValueAsString(data);
return mapper.writer(new DefaultPrettyPrinter())
.with(SerializationFeature.INDENT_OUTPUT)
.with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
.writeValueAsString(data);
}
catch (JsonProcessingException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,15 @@ private static ByteBuffer pack(ByteBuffer buffer, Object value)
if (value instanceof Byte)
return packSmallInt(buffer, (byte) value);
if (value instanceof Integer || value instanceof Short)
return packInt(buffer, (int) value);
return packInt(buffer, ((Number) value).intValue());
if (value instanceof Long)
return packLong(buffer, (long) value);
if (value instanceof Float || value instanceof Double)
return packFloat(buffer, (double) value);
return packFloat(buffer, ((Number) value).doubleValue());
if (value instanceof Boolean)
return packAtom(buffer, String.valueOf(value));
if (value == null)
return packAtom(buffer, "nil");
// imagine we had templates :O
if (value instanceof long[])
return packArray(buffer, (long[]) value);
if (value instanceof int[])
Expand Down
205 changes: 0 additions & 205 deletions src/test/java/CommandDataTest.java

This file was deleted.

Loading

0 comments on commit 13abdcb

Please sign in to comment.