Tests: added additional watcher test for #10092

This commit is contained in:
Oleg Agafonov 2023-04-01 23:42:23 +04:00
parent 407369ea83
commit e1c7139425

View file

@ -1,25 +1,22 @@
package mage;
import static mage.constants.WatcherScope.GAME;
import static org.junit.Assert.*;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.collect.ImmutableSortedSet;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.watchers.Watcher;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static mage.constants.WatcherScope.GAME;
import static org.junit.Assert.*;
public class WatcherTest {
@Test
@ -145,6 +142,33 @@ public class WatcherTest {
assertFalse(copyMapInMap.containsKey("k3"));
}
@Test
public void testDeepCopyMapOfSortedSet() {
// Given
Map<String, SortedSet<String>> sortedSetInMapField = new HashMap<>();
sortedSetInMapField.put("k1", new TreeSet<>(Arrays.asList("v1_1", "v1_2")));
sortedSetInMapField.put("k2", new TreeSet<>(Arrays.asList("v2_1", "v2_2")));
TestWatcher testWatcher = new TestWatcher(GAME);
testWatcher.setSortedSetInMapField(sortedSetInMapField);
// When
TestWatcher copy = testWatcher.copy();
// And
testWatcher.getSortedSetInMapField().get("k2").add("v2_3");
testWatcher.getSortedSetInMapField().put("k3", new TreeSet<>());
// Then
Map<String, SortedSet<String>> copySortedSetInMapField = copy.getSortedSetInMapField();
assertEquals(2, copySortedSetInMapField.size());
assertTrue(copySortedSetInMapField.containsKey("k1"));
assertEquals(ImmutableSortedSet.of("v1_1", "v1_2"), copySortedSetInMapField.get("k1"));
assertTrue(copySortedSetInMapField.containsKey("k2"));
assertEquals(ImmutableSortedSet.of("v2_1", "v2_2"), copySortedSetInMapField.get("k2"));
assertFalse(copySortedSetInMapField.containsKey("k3"));
}
private Set<String> set(String... values) {
return Stream.of(values).collect(Collectors.toSet());
}
@ -157,6 +181,8 @@ public class WatcherTest {
private Map<String, Set<String>> setInMapField = new HashMap<>();
private Map<String, Map<String, String>> mapInMapField = new HashMap<>();
private Map<String, SortedSet<String>> sortedSetInMapField = new HashMap<>();
public TestWatcher(WatcherScope scope) {
super(scope);
}
@ -213,5 +239,13 @@ public class WatcherTest {
public void setMapInMapField(Map<String, Map<String, String>> mapInMapField) {
this.mapInMapField = mapInMapField;
}
public Map<String, SortedSet<String>> getSortedSetInMapField() {
return sortedSetInMapField;
}
public void setSortedSetInMapField(Map<String, SortedSet<String>> sortedSetInMapField) {
this.sortedSetInMapField = sortedSetInMapField;
}
}
}