Skip to content

Commit

Permalink
Prepare some common.collect tests and testlib classes to build unde…
Browse files Browse the repository at this point in the history
…r J2CL.

RELNOTES=n/a
PiperOrigin-RevId: 570444724
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 3, 2023
1 parent 54e9276 commit ec0ca1b
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.collect.testing;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -31,7 +32,7 @@
* parameterize the test.
* @author George van den Driessche
*/
@GwtCompatible
@GwtCompatible(emulated = true)
public class AbstractTester<G> extends TestCase {
private G subjectGenerator;
private String suiteName;
Expand Down Expand Up @@ -73,10 +74,12 @@ public G getSubjectGenerator() {
}

/** Returns the name of the test method invoked by this test instance. */
@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
public final String getTestMethodName() {
return super.getName();
}

@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
@Override
public String getName() {
return Platform.format("%s[%s]", super.getName(), suiteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.google.common.collect.testing;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;

import com.google.common.annotations.GwtCompatible;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.ListIterator;
import java.util.Set;

Expand Down Expand Up @@ -49,12 +52,12 @@ public enum IteratorFeature {
* A set containing none of the optional features of the {@link Iterator} or {@link ListIterator}
* interfaces.
*/
public static final Set<IteratorFeature> UNMODIFIABLE = Collections.emptySet();
public static final Set<IteratorFeature> UNMODIFIABLE = emptySet();

/**
* A set containing all of the optional features of the {@link Iterator} and {@link ListIterator}
* interfaces.
*/
public static final Set<IteratorFeature> MODIFIABLE =
Collections.unmodifiableSet(EnumSet.allOf(IteratorFeature.class));
unmodifiableSet(new LinkedHashSet<>(asList(values())));
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ private ListExample getExample() {
return example == null ? new ImmutableListExample("test") : example;
}

@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
@Override
public String getName() {
return example == null ? super.getName() : buildTestName();
}

@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
private String buildTestName() {
return super.getName() + ":" + example.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.collect.testing;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -31,7 +32,7 @@
* parameterize the test.
* @author George van den Driessche
*/
@GwtCompatible
@GwtCompatible(emulated = true)
public class AbstractTester<G> extends TestCase {
private G subjectGenerator;
private String suiteName;
Expand Down Expand Up @@ -73,10 +74,12 @@ public G getSubjectGenerator() {
}

/** Returns the name of the test method invoked by this test instance. */
@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
public final String getTestMethodName() {
return super.getName();
}

@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
@Override
public String getName() {
return Platform.format("%s[%s]", super.getName(), suiteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.google.common.collect.testing;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;

import com.google.common.annotations.GwtCompatible;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.ListIterator;
import java.util.Set;

Expand Down Expand Up @@ -49,12 +52,12 @@ public enum IteratorFeature {
* A set containing none of the optional features of the {@link Iterator} or {@link ListIterator}
* interfaces.
*/
public static final Set<IteratorFeature> UNMODIFIABLE = Collections.emptySet();
public static final Set<IteratorFeature> UNMODIFIABLE = emptySet();

/**
* A set containing all of the optional features of the {@link Iterator} and {@link ListIterator}
* interfaces.
*/
public static final Set<IteratorFeature> MODIFIABLE =
Collections.unmodifiableSet(EnumSet.allOf(IteratorFeature.class));
unmodifiableSet(new LinkedHashSet<>(asList(values())));
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder;
import static com.google.common.collect.testing.Helpers.assertEqualInOrder;
import static com.google.common.collect.testing.Platform.format;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;
import static java.util.Comparator.naturalOrder;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
Expand All @@ -34,8 +36,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -199,6 +202,9 @@ <E> void forEach(GeneralSpliterator<E> spliterator, Consumer<? super E> consumer
};

abstract <E> void forEach(GeneralSpliterator<E> spliterator, Consumer<? super E> consumer);

static final Set<SpliteratorDecompositionStrategy> ALL_STRATEGIES =
unmodifiableSet(new LinkedHashSet<>(asList(values())));
}

private static <E> @Nullable GeneralSpliterator<E> trySplitTestingSize(
Expand Down Expand Up @@ -289,7 +295,7 @@ public final Ordered expect(Iterable<?> elements) {
int characteristics = spliterator.characteristics();
long estimatedSize = spliterator.estimateSize();
for (SpliteratorDecompositionStrategy strategy :
EnumSet.allOf(SpliteratorDecompositionStrategy.class)) {
SpliteratorDecompositionStrategy.ALL_STRATEGIES) {
List<E> resultsForStrategy = new ArrayList<>();
strategy.forEach(spliteratorSupplier.get(), resultsForStrategy::add);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@

package com.google.common.collect;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Stream;

@GwtCompatible
public class ImmutableBiMapFloodingTest extends AbstractHashFloodingTest<BiMap<Object, Object>> {
public ImmutableBiMapFloodingTest() {
super(
EnumSet.allOf(ConstructionPathway.class).stream()
ConstructionPathway.ALL_PATHWAYS.stream()
.flatMap(
path ->
Stream.<Construction<BiMap<Object, Object>>>of(
Expand Down Expand Up @@ -110,5 +114,8 @@ public ImmutableBiMap<Object, Object> create(List<Entry<?, ?>> entries) {

@CanIgnoreReturnValue
public abstract ImmutableBiMap<Object, Object> create(List<Entry<?, ?>> entries);

static final Set<ConstructionPathway> ALL_PATHWAYS =
unmodifiableSet(new LinkedHashSet<>(asList(values())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.testing.google.ListMultimapTestSuiteBuilder;
import com.google.common.collect.testing.google.TestStringListMultimapGenerator;
import com.google.common.collect.testing.google.UnmodifiableCollectionTests;
import com.google.common.primitives.Chars;
import com.google.common.testing.CollectorTester;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
Expand Down Expand Up @@ -413,7 +414,7 @@ public void testToImmutableListMultimap() {
public void testFlatteningToImmutableListMultimap() {
Collector<String, ?, ImmutableListMultimap<Character, Character>> collector =
ImmutableListMultimap.flatteningToImmutableListMultimap(
str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c));
str -> str.charAt(0), str -> Chars.asList(str.substring(1).toCharArray()).stream());
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.testing.google.SetMultimapTestSuiteBuilder;
import com.google.common.collect.testing.google.TestStringSetMultimapGenerator;
import com.google.common.collect.testing.google.UnmodifiableCollectionTests;
import com.google.common.primitives.Chars;
import com.google.common.testing.CollectorTester;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
Expand Down Expand Up @@ -428,7 +429,7 @@ public void testToImmutableSetMultimap() {
public void testFlatteningToImmutableSetMultimap() {
Collector<String, ?, ImmutableSetMultimap<Character, Character>> collector =
ImmutableSetMultimap.flatteningToImmutableSetMultimap(
str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c));
str -> str.charAt(0), str -> Chars.asList(str.substring(1).toCharArray()).stream());
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet()))
Expand Down
2 changes: 2 additions & 0 deletions guava-tests/test/com/google/common/collect/ListsImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ private ListExample getExample() {
return example == null ? new ImmutableListExample("test") : example;
}

@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
@Override
public String getName() {
return example == null ? super.getName() : buildTestName();
}

@GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
private String buildTestName() {
return super.getName() + ":" + example.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.common.collect.Maps.EntryTransformer;
import com.google.common.collect.testing.IteratorTester;
import com.google.common.collect.testing.google.UnmodifiableCollectionTests;
import com.google.common.primitives.Chars;
import com.google.common.testing.CollectorTester;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void testFlatteningToMultimap() {
Collector<String, ?, ListMultimap<Character, Character>> collector =
Multimaps.flatteningToMultimap(
str -> str.charAt(0),
str -> str.substring(1).chars().mapToObj(c -> (char) c),
str -> Chars.asList(str.substring(1).toCharArray()).stream(),
MultimapBuilder.linkedHashKeys().arrayListValues()::build);
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
Expand Down

0 comments on commit ec0ca1b

Please sign in to comment.