summaryrefslogtreecommitdiff
path: root/src/test/java/sevenUnits/utils/NameSymbolTest.java
blob: 3ae2448ec6b3f79c7395e496f83248321d2e8c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * 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));
	}
	
	/**
	 * 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<String> 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()");
	}
}