Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/main/kotlin/com/github/rushyverse/api/extension/ComponentExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.rushyverse.api.extension

import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer

/**
Expand All @@ -11,6 +12,142 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
*/
public fun Component.toText(): String = LegacyComponentSerializer.legacySection().serialize(this)

/**
* Add the [bold][TextDecoration.BOLD] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withBold(): Component = this.decoration(TextDecoration.BOLD, TextDecoration.State.TRUE)

/**
* Remove the [bold][TextDecoration.BOLD] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withoutBold(): Component = this.decoration(TextDecoration.BOLD, TextDecoration.State.FALSE)

/**
* Set as [not set][TextDecoration.State.NOT_SET] the [bold][TextDecoration.BOLD] decoration of the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.undefineBold(): Component = this.decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET)

/**
* Add the [italic][TextDecoration.ITALIC] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withItalic(): Component = this.decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE)

/**
* Remove the [italic][TextDecoration.ITALIC] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withoutItalic(): Component = this.decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE)

/**
* Set as [not set][TextDecoration.State.NOT_SET] the [italic][TextDecoration.ITALIC] decoration of the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.undefineItalic(): Component = this.decoration(TextDecoration.ITALIC, TextDecoration.State.NOT_SET)

/**
* Add the [underlined][TextDecoration.UNDERLINED] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withUnderlined(): Component = this.decoration(TextDecoration.UNDERLINED, TextDecoration.State.TRUE)

/**
* Remove the [underlined][TextDecoration.UNDERLINED] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withoutUnderlined(): Component =
this.decoration(TextDecoration.UNDERLINED, TextDecoration.State.FALSE)

/**
* Set as [not set][TextDecoration.State.NOT_SET] the [underlined][TextDecoration.UNDERLINED] decoration of the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.undefineUnderlined(): Component =
this.decoration(TextDecoration.UNDERLINED, TextDecoration.State.NOT_SET)

/**
* Add the [strikethrough][TextDecoration.STRIKETHROUGH] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withStrikethrough(): Component =
this.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.TRUE)

/**
* Remove the [strikethrough][TextDecoration.STRIKETHROUGH] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withoutStrikethrough(): Component =
this.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.FALSE)

/**
* Set as [not set][TextDecoration.State.NOT_SET] the [strikethrough][TextDecoration.STRIKETHROUGH] decoration of the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.undefineStrikethrough(): Component =
this.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.NOT_SET)

/**
* Add the [obfuscated][TextDecoration.OBFUSCATED] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withObfuscated(): Component = this.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.TRUE)

/**
* Remove the [obfuscated][TextDecoration.OBFUSCATED] decoration to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withoutObfuscated(): Component =
this.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.FALSE)

/**
* Set as [not set][TextDecoration.State.NOT_SET] the [obfuscated][TextDecoration.OBFUSCATED] decoration of the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.undefineObfuscated(): Component =
this.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.NOT_SET)

/**
* Add all decorations to the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withDecorations(): Component =
withBold().withItalic().withUnderlined().withStrikethrough().withObfuscated()

/**
* Remove all decorations from the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.withoutDecorations(): Component =
withoutBold().withoutItalic().withoutUnderlined().withoutStrikethrough().withoutObfuscated()

/**
* Set as [not set][TextDecoration.State.NOT_SET] all decorations of the component.
* @receiver Component to transform.
* @return The same component.
*/
public fun Component.undefineDecorations(): Component =
undefineBold().undefineItalic().undefineUnderlined().undefineStrikethrough().undefineObfuscated()

/**
* Create a new component using the string content.
* @receiver String to transform.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextColor
import net.kyori.adventure.text.format.TextDecoration
import org.junit.jupiter.api.Nested
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
Expand All @@ -12,6 +13,199 @@ import kotlin.test.assertEquals

class ComponentExtTest {

@Nested
inner class ChangeDecoration {

@Nested
inner class Bold {

@Test
fun `should add bold`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.BOLD, TextDecoration.State.TRUE),
Component.text("Hello").withBold()
)
}

@Test
fun `should remove bold`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.BOLD, TextDecoration.State.FALSE),
Component.text("Hello").withoutBold()
)
}

@Test
fun `should undefine bold`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET),
Component.text("Hello").undefineBold()
)
}

}

@Nested
inner class Italic {

@Test
fun `should add italic`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE),
Component.text("Hello").withItalic()
)
}

@Test
fun `should remove italic`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE),
Component.text("Hello").withoutItalic()
)
}

@Test
fun `should undefine italic`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.ITALIC, TextDecoration.State.NOT_SET),
Component.text("Hello").undefineItalic()
)
}

}

@Nested
inner class Underlined {

@Test
fun `should add underlined`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.UNDERLINED, TextDecoration.State.TRUE),
Component.text("Hello").withUnderlined()
)
}

@Test
fun `should remove underlined`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.UNDERLINED, TextDecoration.State.FALSE),
Component.text("Hello").withoutUnderlined()
)
}

@Test
fun `should undefine underlined`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.UNDERLINED, TextDecoration.State.NOT_SET),
Component.text("Hello").undefineUnderlined()
)
}

}

@Nested
inner class Strikethrough {

@Test
fun `should add strikethrough`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.TRUE),
Component.text("Hello").withStrikethrough()
)
}

@Test
fun `should remove strikethrough`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.FALSE),
Component.text("Hello").withoutStrikethrough()
)
}

@Test
fun `should undefine strikethrough`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.NOT_SET),
Component.text("Hello").undefineStrikethrough()
)
}

}

@Nested
inner class Obfuscated {

@Test
fun `should add obfuscated`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.OBFUSCATED, TextDecoration.State.TRUE),
Component.text("Hello").withObfuscated()
)
}

@Test
fun `should remove obfuscated`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.OBFUSCATED, TextDecoration.State.FALSE),
Component.text("Hello").withoutObfuscated()
)
}

@Test
fun `should undefine obfuscated`() {
assertEquals(
Component.text("Hello").decoration(TextDecoration.OBFUSCATED, TextDecoration.State.NOT_SET),
Component.text("Hello").undefineObfuscated()
)
}

}

@Nested
inner class Decorations {

@Test
fun `should add all decorations`() {
assertEquals(
Component.text("Hello")
.decoration(TextDecoration.BOLD, TextDecoration.State.TRUE)
.decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE)
.decoration(TextDecoration.UNDERLINED, TextDecoration.State.TRUE)
.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.TRUE)
.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.TRUE),
Component.text("Hello").withDecorations()
)
}

@Test
fun `should remove all decorations`() {
assertEquals(
Component.text("Hello")
.decoration(TextDecoration.BOLD, TextDecoration.State.FALSE)
.decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE)
.decoration(TextDecoration.UNDERLINED, TextDecoration.State.FALSE)
.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.FALSE)
.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.FALSE),
Component.text("Hello").withoutDecorations()
)
}

@Test
fun `should undefine all decorations`() {
assertEquals(
Component.text("Hello")
.decoration(TextDecoration.BOLD, TextDecoration.State.NOT_SET)
.decoration(TextDecoration.ITALIC, TextDecoration.State.NOT_SET)
.decoration(TextDecoration.UNDERLINED, TextDecoration.State.NOT_SET)
.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.NOT_SET)
.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.NOT_SET),
Component.text("Hello").undefineDecorations()
)
}

}
}

@Nested
inner class StringToComponent {

Expand All @@ -30,9 +224,12 @@ class ComponentExtTest {

@Test
fun `should separate text and url`() {
val component = "Hello https://www.youtube.com &#3b81f1".toComponent(extractUrls = true, extractColors = false)
val component =
"Hello https://www.youtube.com &#3b81f1".toComponent(extractUrls = true, extractColors = false)
val expected = Component.text("Hello ")
.append(Component.text("https://www.youtube.com").clickEvent(ClickEvent.openUrl("https://www.youtube.com")))
.append(
Component.text("https://www.youtube.com").clickEvent(ClickEvent.openUrl("https://www.youtube.com"))
)
.append(Component.text(" &#3b81f1"))
assertEquals(expected, component)
}
Expand Down Expand Up @@ -91,7 +288,9 @@ class ComponentExtTest {

@Test
fun `should return text with url`() {
val text = Component.text("Hello ").append(Component.text("https://www.youtube.com").clickEvent(ClickEvent.openUrl("https://www.youtube.com"))).toText()
val text = Component.text("Hello ").append(
Component.text("https://www.youtube.com").clickEvent(ClickEvent.openUrl("https://www.youtube.com"))
).toText()
assertEquals("Hello https://www.youtube.com", text)
}
}
Expand Down