Skip to content

Commit

Permalink
📓 Add test for #32
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Dec 1, 2024
1 parent 0f7ed0d commit 122912b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
compileOnly(libs.tab.api)
compileOnly(libs.packet.events)
implementation(libs.entity.lib)
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
}

tasks {
Expand Down
Empty file added logs/latest.log
Empty file.
14 changes: 6 additions & 8 deletions src/main/java/com/mattmx/nametags/config/TextFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ public enum TextFormatter {
)
;

// Converts legacy hex format &x&9&0&0&c&3&f to modern hex format &#900c3f
// Converts legacy hex format &x&9&0&0&c&3&f -> &#900c3f modern hex format
// https://github.com/Matt-MX/DisplayNameTags/issues/32#issuecomment-2509403581
private static String convertLegacyHex(String input) {
//regex to match the legacy hex format
String legacyHexPattern = "&x(&[0-9a-fA-F]){6}";
Pattern pattern = Pattern.compile(legacyHexPattern);
Matcher matcher = pattern.matcher(input);
private static final Pattern LEGACY_HEX_PATTERN = Pattern.compile("&x(&[0-9a-fA-F]){6}");
public static String convertLegacyHex(String input) {
Matcher matcher = LEGACY_HEX_PATTERN.matcher(input);

StringBuilder result = new StringBuilder();
while (matcher.find()) {
String legacyHex = matcher.group();
//extract hex digits from the legacy format
// Extract hex digits from the legacy format
String hexColor = legacyHex.replace("&x", "")
.replace("&", "");
//replace with modern format
// Replace with modern format
String modernHex = "&#" + hexColor;
matcher.appendReplacement(result, modernHex);
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/mattmx/nametags/config/TextFormatterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mattmx.nametags.config;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class TextFormatterTest {

@ParameterizedTest
@CsvSource({
"&x&c&7&0&0&3&9[Admin],&#c70039[Admin]",
"&x&9&0&0&c&3&fCoyotea&r,&#900c3fCoyotea&r",
"&x&9&0&0&c&3&fCoyotea,&#900c3fCoyotea",
"&x&c&7&0&0&3&9[Admin] &x&9&0&0&c&3&fCoyotea&r,&#c70039[Admin] &#900c3fCoyotea&r"
})
public void test_convertLegacyHex(String input, String expectedOut) {
final String out = TextFormatter.convertLegacyHex(input);

Assertions.assertEquals(expectedOut, out);
}

}

0 comments on commit 122912b

Please sign in to comment.