diff options
Diffstat (limited to 'src/test/java/sevenUnits/utils/NameSymbolTest.java')
-rw-r--r-- | src/test/java/sevenUnits/utils/NameSymbolTest.java | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/test/java/sevenUnits/utils/NameSymbolTest.java b/src/test/java/sevenUnits/utils/NameSymbolTest.java new file mode 100644 index 0000000..f8843e0 --- /dev/null +++ b/src/test/java/sevenUnits/utils/NameSymbolTest.java @@ -0,0 +1,108 @@ +/** + * Copyright (C) 2025 Adrien Hopkins + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +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; + +/** + * Tests for the {@link NameSymbol} class. + * + * @since v1.0.0 + */ +class NameSymbolTest { + private static Stream<Arguments> 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)); + } + + @Test + public void testCreate() { + final Set<String> names = Set.of("a", "b", "c"); + final var 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()"); + } + + /** + * 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)); + } + } +} |