добавлено тестирование Text
This commit is contained in:
@@ -1,20 +1,11 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-06-11
|
|
||||||
*/
|
|
||||||
package mc.core.text;
|
package mc.core.text;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.StringJoiner;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@EqualsAndHashCode
|
|
||||||
public class Text {
|
public class Text {
|
||||||
private static final Text EMPTY = new Text();
|
private static final Text EMPTY = new Text();
|
||||||
private static final Text NEW_LINE = new Text("\n", null, null, null);
|
private static final Text NEW_LINE = new Text("\n", null, null, null);
|
||||||
@@ -39,7 +30,7 @@ public class Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
boolean result = content.isEmpty();
|
boolean result = (content == null || content.isEmpty());
|
||||||
|
|
||||||
if (children != null && !children.isEmpty()) {
|
if (children != null && !children.isEmpty()) {
|
||||||
for (Text child : children) {
|
for (Text child : children) {
|
||||||
@@ -60,6 +51,19 @@ public class Text {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Text text = (Text) o;
|
||||||
|
return Objects.equals(toPlain(), text.toPlain());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(toPlain());
|
||||||
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
@Getter
|
@Getter
|
||||||
private String content;
|
private String content;
|
||||||
@@ -81,6 +85,8 @@ public class Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Builder(Object... objects) {
|
public Builder(Object... objects) {
|
||||||
|
this.children = new ArrayList<>();
|
||||||
|
|
||||||
for(Object obj : objects) {
|
for(Object obj : objects) {
|
||||||
if (obj instanceof String) {
|
if (obj instanceof String) {
|
||||||
if (this.content == null) {
|
if (this.content == null) {
|
||||||
@@ -96,10 +102,10 @@ public class Text {
|
|||||||
}
|
}
|
||||||
} else if (obj instanceof TextColor) {
|
} else if (obj instanceof TextColor) {
|
||||||
this.color = (TextColor) obj;
|
this.color = (TextColor) obj;
|
||||||
|
} else if (obj instanceof Text) {
|
||||||
|
children.add((Text) obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.children = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Text> getChildren() {
|
public List<Text> getChildren() {
|
||||||
@@ -133,8 +139,14 @@ public class Text {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder append(String string) {
|
||||||
|
return append(Text.of(string));
|
||||||
|
}
|
||||||
|
|
||||||
public Builder append(Text child) {
|
public Builder append(Text child) {
|
||||||
this.children.add(child);
|
if (child != null) {
|
||||||
|
this.children.add(child);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,16 +156,20 @@ public class Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Text build() {
|
public Text build() {
|
||||||
if (children.isEmpty()) {
|
if (children.isEmpty() && (content == null || content.isEmpty())) {
|
||||||
return Text.EMPTY;
|
return Text.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Text(
|
if (children.size() == 1 && children.get(0) != null) {
|
||||||
content,
|
return children.get(0);
|
||||||
color,
|
} else {
|
||||||
style,
|
return new Text(
|
||||||
ImmutableList.copyOf(children)
|
content,
|
||||||
);
|
color,
|
||||||
|
style,
|
||||||
|
ImmutableList.copyOf(children)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
76
core/src/test/java/mc/core/text/TestText.java
Normal file
76
core/src/test/java/mc/core/text/TestText.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package mc.core.text;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class TestText {
|
||||||
|
@Test
|
||||||
|
public void testToPlain() {
|
||||||
|
final String m1 = "mes";
|
||||||
|
final String m2 = "sage";
|
||||||
|
final String message = m1 + m2;
|
||||||
|
|
||||||
|
assertEquals(message, Text.of(message).toPlain());
|
||||||
|
assertEquals(message, Text.builder(message).build().toPlain());
|
||||||
|
assertEquals(message, Text.builder(Text.of(message)).build().toPlain());
|
||||||
|
assertEquals(message, Text.builder().append(message).build().toPlain());
|
||||||
|
assertEquals(message, Text.builder().append(Text.of(message)).build().toPlain());
|
||||||
|
|
||||||
|
assertEquals(message, Text.builder(m1, m2).build().toPlain());
|
||||||
|
assertEquals(message, Text.builder(Text.of(m1), Text.of(m2)).build().toPlain());
|
||||||
|
assertEquals(message, Text.builder().append(Text.of(m1), Text.of(m2)).build().toPlain());
|
||||||
|
assertEquals(message, Text.builder().append(Text.of(m1)).append(Text.of(m2)).build().toPlain());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEquals() {
|
||||||
|
assertEquals(Text.of(), Text.of(""));
|
||||||
|
assertEquals(Text.of(), Text.builder().build());
|
||||||
|
assertEquals(Text.of(), Text.builder("").build());
|
||||||
|
assertEquals(Text.of(), Text.builder().append().build());
|
||||||
|
assertEquals(Text.of(), Text.builder().append("").build());
|
||||||
|
|
||||||
|
assertNotEquals(Text.of(), Text.of("??"));
|
||||||
|
assertNotEquals(Text.of(), Text.builder("??").build());
|
||||||
|
assertNotEquals(Text.of(), Text.builder().append("??").build());
|
||||||
|
|
||||||
|
assertEquals(Text.of("message"), Text.builder("message").build());
|
||||||
|
assertEquals(Text.of("message"), Text.builder(Text.of("message")).build());
|
||||||
|
assertEquals(Text.of("message"), Text.builder().append("message").build());
|
||||||
|
assertEquals(Text.of("message"), Text.builder().append(Text.of("message")).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmpty() {
|
||||||
|
assertTrue(Text.of().isEmpty());
|
||||||
|
assertTrue(Text.of((String) null).isEmpty());
|
||||||
|
assertTrue(Text.of((Text) null).isEmpty());
|
||||||
|
assertTrue(Text.of("").isEmpty());
|
||||||
|
assertTrue(Text.of("", "").isEmpty());
|
||||||
|
|
||||||
|
assertTrue(Text.builder().build().isEmpty());
|
||||||
|
assertTrue(Text.builder((String) null).build().isEmpty());
|
||||||
|
assertTrue(Text.builder((Text) null).build().isEmpty());
|
||||||
|
assertTrue(Text.builder("").build().isEmpty());
|
||||||
|
assertTrue(Text.builder("", "").build().isEmpty());
|
||||||
|
assertTrue(Text.builder(Text.of()).build().isEmpty());
|
||||||
|
assertTrue(Text.builder(Text.of(), Text.of()).build().isEmpty());
|
||||||
|
|
||||||
|
assertTrue(Text.builder().append().build().isEmpty());
|
||||||
|
assertTrue(Text.builder().append((String) null).build().isEmpty());
|
||||||
|
assertTrue(Text.builder().append((Text) null).build().isEmpty());
|
||||||
|
assertTrue(Text.builder().append("").build().isEmpty());
|
||||||
|
assertTrue(Text.builder().append(Text.of()).build().isEmpty());
|
||||||
|
assertTrue(Text.builder().append(Text.of(), Text.of()).build().isEmpty());
|
||||||
|
assertTrue(Text.builder().append(Text.of()).append(Text.of()).build().isEmpty());
|
||||||
|
|
||||||
|
assertFalse(Text.of("??").isEmpty());
|
||||||
|
assertFalse(Text.builder("??").build().isEmpty());
|
||||||
|
assertFalse(Text.builder(Text.of("??")).build().isEmpty());
|
||||||
|
assertFalse(Text.builder().append("??").build().isEmpty());
|
||||||
|
assertFalse(Text.builder().append(Text.of("??")).build().isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user