Skip to content

Commit

Permalink
Merge pull request #495 from KingOfSquares/tristate-state
Browse files Browse the repository at this point in the history
Add Tristate method to TextDecoration.State
  • Loading branch information
zml2008 committed Nov 10, 2021
2 parents 390dbde + 456c9c8 commit f24595d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -25,9 +25,12 @@

import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.Index;
import net.kyori.adventure.util.TriState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.Objects.requireNonNull;

/**
* An enumeration of decorations which may be applied to a {@link Component}.
*
Expand Down Expand Up @@ -166,5 +169,22 @@ public String toString() {
public static @NotNull State byBoolean(final @Nullable Boolean flag) {
return flag == null ? NOT_SET : byBoolean(flag.booleanValue());
}

/**
* Gets a state from a {@link net.kyori.adventure.util.TriState}.
*
* @param flag the tristate
* @return the state
* @since 4.10.0
*/
public static @NotNull State byTriState(final @NotNull TriState flag) {
requireNonNull(flag);
switch (flag) {
case TRUE: return TRUE;
case FALSE: return FALSE;
case NOT_SET: return NOT_SET;
}
throw new IllegalArgumentException("Unable to turn TriState: " + flag + " into a TextDecoration.State");
}
}
}
Expand Up @@ -23,9 +23,13 @@
*/
package net.kyori.adventure.text.format;

import java.util.Arrays;
import net.kyori.adventure.util.TriState;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TextDecorationTest {
@Test
Expand All @@ -34,4 +38,14 @@ void testByBoolean() {
assertEquals(TextDecoration.State.FALSE, TextDecoration.State.byBoolean(false));
assertEquals(TextDecoration.State.TRUE, TextDecoration.State.byBoolean(true));
}

@Test
void testByTristate() {
assertEquals(TextDecoration.State.NOT_SET, TextDecoration.State.byTriState(TriState.NOT_SET));
assertEquals(TextDecoration.State.FALSE, TextDecoration.State.byTriState(TriState.FALSE));
assertEquals(TextDecoration.State.TRUE, TextDecoration.State.byTriState(TriState.TRUE));

assertThrows(NullPointerException.class, () -> TextDecoration.State.byTriState(null));
assertDoesNotThrow(() -> Arrays.stream(TriState.values()).forEach(TextDecoration.State::byTriState));
}
}

0 comments on commit f24595d

Please sign in to comment.