Skip to content

Commit

Permalink
Add activity serialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 11, 2023
1 parent 2e00ea5 commit bc97ca8
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/data/DataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,4 +911,20 @@ else if (value instanceof String && stringParse != null)
throw new ParsingException(Helpers.format("Cannot parse value for %s into type %s: %s instance of %s",
key, type.getSimpleName(), value, value.getClass().getSimpleName()));
}

@Override
public boolean equals(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof DataObject))
return false;
return ((DataObject) obj).toMap().equals(this.toMap());
}

@Override
public int hashCode()
{
return toMap().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public DataObject getFullPresence()
.put("status", getStatus().getKey());
}

private DataObject getGameJson(Activity activity)
public static DataObject getGameJson(Activity activity)
{
if (activity == null || activity.getName() == null || activity.getType() == null)
return null;
Expand Down
98 changes: 98 additions & 0 deletions src/test/java/net/dv8tion/jda/entities/ActivityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.entities;

import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.managers.PresenceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ActivityTest
{
private static DataObject formatActivity(int type, String name, String state)
{
DataObject json = DataObject.empty()
.put("type", type)
.put("name", name);

if (state != null)
json.put("state", state);
if (name != null)
json.put("name", name);

return json;
}

@Test
public void activitySerializationTest()
{
Assertions.assertEquals(
formatActivity(0, "playing test", null),
PresenceImpl.getGameJson(Activity.playing("playing test"))
);
Assertions.assertEquals(
formatActivity(0, "playing test", "playing state"),
PresenceImpl.getGameJson(Activity.playing("playing test").withState("playing state"))
);

Assertions.assertEquals(
formatActivity(1, "streaming test", null).put("url", "https://twitch.tv/discord"),
PresenceImpl.getGameJson(Activity.streaming("streaming test", "https://twitch.tv/discord"))
);
Assertions.assertEquals(
formatActivity(1, "streaming test", "streaming state").put("url", "https://twitch.tv/discord"),
PresenceImpl.getGameJson(Activity.streaming("streaming test", "https://twitch.tv/discord").withState("streaming state"))
);

Assertions.assertEquals(
formatActivity(2, "listening test", null),
PresenceImpl.getGameJson(Activity.listening("listening test"))
);
Assertions.assertEquals(
formatActivity(2, "listening test", "listening state"),
PresenceImpl.getGameJson(Activity.listening("listening test").withState("listening state"))
);

Assertions.assertEquals(
formatActivity(3, "watching test", null),
PresenceImpl.getGameJson(Activity.watching("watching test"))
);
Assertions.assertEquals(
formatActivity(3, "watching test", "watching state"),
PresenceImpl.getGameJson(Activity.watching("watching test").withState("watching state"))
);

Assertions.assertEquals(
formatActivity(4, "Custom Status", "custom status test"),
PresenceImpl.getGameJson(Activity.customStatus("custom status test"))
);
Assertions.assertEquals(
formatActivity(4, "Custom Status", "custom status test"),
PresenceImpl.getGameJson(Activity.customStatus("custom status test").withState("should be ignored"))
);

Assertions.assertEquals(
formatActivity(5, "competing test", null),
PresenceImpl.getGameJson(Activity.competing("competing test"))
);
Assertions.assertEquals(
formatActivity(5, "competing test", "competing state"),
PresenceImpl.getGameJson(Activity.competing("competing test").withState("competing state"))
);
}
}

0 comments on commit bc97ca8

Please sign in to comment.