Update to new maven and add tests #2

Merged
Etzelia merged 2 commits from refs/pull/2/head into main 2021-06-11 02:50:39 +00:00
11 changed files with 95 additions and 33 deletions

View File

@ -12,6 +12,7 @@ steps:
image: gradle:6.6
commands:
- gradle build
- gradle test
---
kind: pipeline

View File

@ -4,8 +4,8 @@ A small, no-dependency Discord utility library. Currently used for webhooks/embe
## Releases
[Reposilite](https://repo.etztech.xyz/releases/xyz/etztech/javacord)
[Reposilite](https://mvn.birbmc.com/releases/xyz/etztech/javacord)
## License
[MIT](LICENSE``)
[MIT](LICENSE)

View File

@ -7,6 +7,19 @@ group = 'xyz.etztech'
version = '0.2.2'
sourceCompatibility = '8'
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2")
}
publishing {
publications {
maven(MavenPublication) {
@ -15,10 +28,10 @@ publishing {
}
repositories {
maven {
name = "etztech"
url = "https://repo.etztech.xyz/releases"
name = "birbmc"
url = "https://mvn.birbmc.com/releases"
credentials {
username = "etzelia"
username = "admin"
password = System.getenv("MAVEN_PASSWORD")
}
}

View File

@ -19,8 +19,8 @@ public class Webhook {
public String toString() {
StringBuilder builder = new StringBuilder("{");
List<String> json = new ArrayList<>();
if (!"".equals(content)) json.add(String.format("\"content\": \"%s\"", Javacord.escapeQuote(content)));
if (embeds.size() > 0) json.add(String.format("\"embeds\": [%s]", embeds.stream().map(Embed::toJSON).collect(Collectors.joining(","))));
if (!"".equals(content)) json.add(String.format("\"content\":\"%s\"", Javacord.escapeQuote(content)));
if (embeds.size() > 0) json.add(String.format("\"embeds\":[%s]", embeds.stream().map(Embed::toJSON).collect(Collectors.joining(","))));
builder.append(String.join(",", json));
return builder.append("}").toString();
}

View File

@ -21,10 +21,10 @@ public class Author {
public String toJSON() {
StringBuilder builder = new StringBuilder("{");
List<String> json = new ArrayList<>();
if (!"".equals(name)) json.add(String.format("\"name\": \"%s\"", Javacord.escapeQuote(name)));
if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url)));
if (!"".equals(iconURL)) json.add(String.format("\"icon_url\": \"%s\"", Javacord.escapeQuote(iconURL)));
if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\": \"%s\"", Javacord.escapeQuote(proxyIconURL)));
if (!"".equals(name)) json.add(String.format("\"name\":\"%s\"", Javacord.escapeQuote(name)));
if (!"".equals(url)) json.add(String.format("\"url\":\"%s\"", Javacord.escapeQuote(url)));
if (!"".equals(iconURL)) json.add(String.format("\"icon_url\":\"%s\"", Javacord.escapeQuote(iconURL)));
if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\":\"%s\"", Javacord.escapeQuote(proxyIconURL)));
builder.append(String.join(",", json));
return builder.append("}").toString();
}

View File

@ -100,16 +100,16 @@ public class Embed {
public String toJSON() {
StringBuilder builder = new StringBuilder("{");
List<String> json = new ArrayList<>();
if (!"".equals(title)) json.add(String.format("\"title\": \"%s\"", Javacord.escapeQuote(title)));
if (!"".equals(description)) json.add(String.format("\"description\": \"%s\"", Javacord.escapeQuote(description)));
if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url)));
if (color != 0) json.add(String.format("\"color\": \"%d\"", color));
if (timestamp != null) json.add(String.format("\"timestamp\": \"%s\"", timestamp.format(DateTimeFormatter.ISO_INSTANT)));
if (footer != null) json.add(String.format("\"footer\": %s", footer.toJSON()));
if (thumbnail != null) json.add(String.format("\"thumbnail\": %s", thumbnail.toJSON()));
if (image != null) json.add(String.format("\"image\": %s", image.toJSON()));
if (author != null) json.add(String.format("\"author\": %s", author.toJSON()));
if (fields.size() > 0) json.add(String.format("\"fields\": [%s]", fields.stream().map(Field::toJSON).collect(Collectors.joining(","))));
if (!"".equals(title)) json.add(String.format("\"title\":\"%s\"", Javacord.escapeQuote(title)));
if (!"".equals(description)) json.add(String.format("\"description\":\"%s\"", Javacord.escapeQuote(description)));
if (!"".equals(url)) json.add(String.format("\"url\":\"%s\"", Javacord.escapeQuote(url)));
if (color != 0) json.add(String.format("\"color\":\"%d\"", color));
if (timestamp != null) json.add(String.format("\"timestamp\":\"%s\"", timestamp.format(DateTimeFormatter.ISO_INSTANT)));
if (footer != null) json.add(String.format("\"footer\":%s", footer.toJSON()));
if (thumbnail != null) json.add(String.format("\"thumbnail\":%s", thumbnail.toJSON()));
if (image != null) json.add(String.format("\"image\":%s", image.toJSON()));
if (author != null) json.add(String.format("\"author\":%s", author.toJSON()));
if (fields.size() > 0) json.add(String.format("\"fields\":[%s]", fields.stream().map(Field::toJSON).collect(Collectors.joining(","))));
builder.append(String.join(",", json));
return builder.append("}").toString();
}
@ -117,8 +117,8 @@ public class Embed {
public String toString() {
StringBuilder builder = new StringBuilder("{");
List<String> json = new ArrayList<>();
if (!"".equals(content)) json.add(String.format("\"content\": \"%s\"", Javacord.escapeQuote(content)));
if (!"{}".equals(toJSON())) json.add(String.format("\"embed\": %s", toJSON()));
if (!"".equals(content)) json.add(String.format("\"content\":\"%s\"", Javacord.escapeQuote(content)));
if (!"{}".equals(toJSON())) json.add(String.format("\"embed\":%s", toJSON()));
builder.append(String.join(",", json));
return builder.append("}").toString();
}

View File

@ -25,9 +25,9 @@ public class Field {
public String toJSON() {
StringBuilder builder = new StringBuilder("{");
List<String> json = new ArrayList<>();
json.add(String.format("\"name\": \"%s\"", Javacord.escapeQuote(name)));
json.add(String.format("\"value\": \"%s\"", Javacord.escapeQuote(value)));
if (inline) json.add(String.format("\"inline\": %s", true));
json.add(String.format("\"name\":\"%s\"", Javacord.escapeQuote(name)));
json.add(String.format("\"value\":\"%s\"", Javacord.escapeQuote(value)));
if (inline) json.add(String.format("\"inline\":%s", true));
builder.append(String.join(",", json));
return builder.append("}").toString();
}

View File

@ -25,10 +25,10 @@ public class Footer {
public String toJSON() {
StringBuilder builder = new StringBuilder("{");
builder.append(String.format("\"text\": \"%s\"", Javacord.escapeQuote(text)));
builder.append(String.format("\"text\":\"%s\"", Javacord.escapeQuote(text)));
List<String> json = new ArrayList<>();
if (!"".equals(iconURL)) json.add(String.format("\"icon_url\": \"%s\"", Javacord.escapeQuote(iconURL)));
if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\": \"%s\"", Javacord.escapeQuote(proxyIconURL)));
if (!"".equals(iconURL)) json.add(String.format("\"icon_url\":\"%s\"", Javacord.escapeQuote(iconURL)));
if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\":\"%s\"", Javacord.escapeQuote(proxyIconURL)));
builder.append(String.join(",", json));
return builder.append("}").toString();
}

View File

@ -21,10 +21,10 @@ public class Image {
public String toJSON() {
StringBuilder builder = new StringBuilder("{");
List<String> json = new ArrayList<>();
if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url)));
if (!"".equals(proxyURL)) json.add(String.format("\"proxy_url\": \"%s\"", Javacord.escapeQuote(proxyURL)));
if (height != 0) json.add(String.format("\"height\": \"%d\"", height));
if (width != 0) json.add(String.format("\"width\": \"%d\"", width));
if (!"".equals(url)) json.add(String.format("\"url\":\"%s\"", Javacord.escapeQuote(url)));
if (!"".equals(proxyURL)) json.add(String.format("\"proxy_url\":\"%s\"", Javacord.escapeQuote(proxyURL)));
if (height != 0) json.add(String.format("\"height\":\"%d\"", height));
if (width != 0) json.add(String.format("\"width\":\"%d\"", width));
builder.append(String.join(",", json));
return builder.append("}").toString();
}

View File

@ -0,0 +1,23 @@
package xyz.etztech;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class JavacordTest {
@Test
void TestEscapeFormat() {
String raw = "*Test* **Test** _Test_ __Test__ ~Test~ ~~Test~~ |Test| ||Test||";
String escaped = Javacord.escapeFormat(raw);
String expected = "\\\\*Test\\\\* \\\\*\\\\*Test\\\\*\\\\* \\\\_Test\\\\_ \\\\_\\\\_Test\\\\_\\\\_ \\\\~Test\\\\~ \\\\~\\\\~Test\\\\~\\\\~ \\\\|Test\\\\| \\\\|\\\\|Test\\\\|\\\\|";
Assertions.assertEquals(expected, escaped);
}
@Test
void TestEscapeQuote() {
String raw = "\"Test\"";
String escaped = Javacord.escapeQuote(raw);
String expected = "\\\"Test\\\"";
Assertions.assertEquals(expected, escaped);
}
}

View File

@ -0,0 +1,25 @@
package xyz.etztech.embed;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public class EmbedTest {
@Test
void TestEmbed() {
Embed embed = new Embed()
.color(0x151515)
.author(new Author("Etzelia", "https://etzel.ia", "" ,""))
.title("Test \"Title\"")
.description("Test Description")
.fields(Arrays.asList(
new Field("Field1", "foo"),
new Field("Field2", "bar"),
new Field("Field3", "baz", true)
));
String expected = "{\"embed\":{\"title\":\"Test \\\"Title\\\"\",\"description\":\"Test Description\",\"color\":\"1381653\",\"author\":{\"name\":\"Etzelia\",\"url\":\"https://etzel.ia\"},\"fields\":[{\"name\":\"Field1\",\"value\":\"foo\"},{\"name\":\"Field2\",\"value\":\"bar\"},{\"name\":\"Field3\",\"value\":\"baz\",\"inline\":true}]}}";
Assertions.assertEquals(expected, embed.toString());
}
}