Skip to content

Commit

Permalink
Add tests for decorationIfAbsent
Browse files Browse the repository at this point in the history
  • Loading branch information
KingOfSquares committed Jun 11, 2022
1 parent 4e5fe81 commit 50195bb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) {
@Override
default @NotNull Component decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state) {
requireNonNull(state, "state");
//Not delegating this method prevents object creation if decoration is absent
//Not delegating this method prevents object creation if decoration is NOT absent
final TextDecoration.@NotNull State thisState = this.decoration(decoration);
if (thisState == TextDecoration.State.NOT_SET) {
return this.style(this.style().decoration(decoration, state));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text;

import com.google.common.collect.ImmutableSet;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import org.junit.jupiter.api.Test;

import static net.kyori.adventure.text.TextAssertions.assertDecorations;

public class ComponentDecorationTest {

@Test
void testDecorationIfAbsent() {
final Style s0 = Style.style(TextDecoration.BOLD);
final Component c0 = Component.text("tuba time", s0)
.decorationIfAbsent(TextDecoration.BOLD, TextDecoration.State.FALSE)
.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
assertDecorations(c0.style(), ImmutableSet.of(TextDecoration.BOLD), ImmutableSet.of(TextDecoration.ITALIC));
}

@Test
void testDecorationIfAbsentWithBuilder() {
final Style s0 = Style.style(TextDecoration.BOLD);
final Component c0 = Component.text().style(s0).content("tuba time")
.decorationIfAbsent(TextDecoration.BOLD, TextDecoration.State.FALSE)
.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE)
.build();
assertDecorations(c0.style(), ImmutableSet.of(TextDecoration.BOLD), ImmutableSet.of(TextDecoration.ITALIC));
}
}
17 changes: 17 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/format/StyleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ void testOf_decorations() {
assertDecorations(s0, ImmutableSet.of(TextDecoration.BOLD, TextDecoration.ITALIC), ImmutableSet.of());
}

@Test
void testDecorationIfAbsent() {
final Style s0 = Style.style(TextDecoration.BOLD)
.decorationIfAbsent(TextDecoration.BOLD, TextDecoration.State.FALSE)
.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE);
assertDecorations(s0, ImmutableSet.of(TextDecoration.BOLD), ImmutableSet.of(TextDecoration.ITALIC));
}

@Test
void testDecorationIfAbsentWithBuilder() {
final Style s0 = Style.style().decoration(TextDecoration.BOLD, TextDecoration.State.TRUE)
.decorationIfAbsent(TextDecoration.BOLD, TextDecoration.State.FALSE)
.decorationIfAbsent(TextDecoration.ITALIC, TextDecoration.State.FALSE)
.build();
assertDecorations(s0, ImmutableSet.of(TextDecoration.BOLD), ImmutableSet.of(TextDecoration.ITALIC));
}

@Test
void testOf_colorAndDecorations() {
final Style s0 = Style.style(NamedTextColor.GREEN, TextDecoration.BOLD, TextDecoration.ITALIC);
Expand Down

0 comments on commit 50195bb

Please sign in to comment.