package sevenUnits.utils; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; class NameSymbolTest { private static Stream testEqualsHashCode() { return Stream.of( Arguments.of(NameSymbol.ofName("test"), NameSymbol.ofName("test"), true), Arguments.of(NameSymbol.ofName("a"), NameSymbol.ofName("b"), false), Arguments.of(NameSymbol.ofSymbol("test"), NameSymbol.ofSymbol("test"), true), Arguments.of(NameSymbol.ofSymbol("a"), NameSymbol.ofSymbol("b"), false), Arguments.of(NameSymbol.ofName("test"), NameSymbol.ofSymbol("test"), false), Arguments.of(NameSymbol.of("main", "s"), NameSymbol.of("main", "s"), true), Arguments.of(NameSymbol.of("main", "s"), NameSymbol.of("main", "s", "m"), false), Arguments.of(new NameSymbol(null, null, new HashSet<>()), new NameSymbol(null, null, new HashSet<>()), true), Arguments.of(new NameSymbol( Optional.of("main"), Optional.of("s"), new HashSet<>()), new NameSymbol(null, null, new HashSet<>()), false), Arguments.of(new NameSymbol(null, null, new HashSet<>()), new NameSymbol(Optional.of("main"), Optional.of("s"), new HashSet<>()), false), Arguments.of(new NameSymbol(Optional.of("main"), null, new HashSet<>()), new NameSymbol(Optional.of("main"), Optional.of("s"), new HashSet<>()), false)); } /** * Tests that two NameSymbols are or are not equal. * If they are equal, also ensures they have the same hash code. * @param a first NameSymbol to test * @param b second NameSymbol to test * @param equal true iff a should be equal to be, otherwise false */ @ParameterizedTest @MethodSource public void testEqualsHashCode(NameSymbol a, NameSymbol b, boolean equal) { if (equal) { assertTrue(Objects.equals(a, b)); assertEquals(a.hashCode(), b.hashCode(), "Equal NameSymbol instances have different hash codes."); } else { assertFalse(Objects.equals(a, b)); } } @Test public void testCreate() { Set names = Set.of("a", "b", "c"); NameSymbol ns = NameSymbol.ofNullable(null, null, names); assertTrue(ns.getPrimaryName().isPresent(), "NameSymbol created without primary name."); assertTrue(names.contains(ns.getPrimaryName().orElseThrow()), String.format("Primary name (%s) was not obtained from names set.", ns.getPrimaryName())); assertFalse(ns.getOtherNames().contains(ns.getPrimaryName().orElseThrow()), String.format("Primary name (%s) was included in other names set.", ns.getPrimaryName())); assertEquals(Set.of("a", "b", "c"), names, "names input was changed by ofNullable()"); } }