diff options
author | Michajlo Matijkiw <michajlo@google.com> | 2016-09-20 21:31:44 +0000 |
---|---|---|
committer | Laszlo Csomor <laszlocsomor@google.com> | 2016-09-21 07:09:02 +0000 |
commit | 7b7066100189440710885c17ab9aafe2dd73de55 (patch) | |
tree | 0ab1f15156cc8f4023a3d33459c9a6e324e81802 /src/test | |
parent | 5b8b3de8a2c835e4cf7eb8c5678baf4ff837aa8b (diff) |
Implement label visitation using visitor
As opposed to building up a collection. These collections, and all their iterators,
add up creating a lot of garbage. This saves us at least an ImmutableList + Iterator
per label.
--
MOS_MIGRATED_REVID=133754998
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java | 38 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/TypeTest.java | 46 |
2 files changed, 54 insertions, 30 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java b/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java index dfd4f28cef..f01fc05945 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java @@ -30,17 +30,16 @@ import com.google.devtools.build.lib.syntax.EvalUtils; import com.google.devtools.build.lib.syntax.Printer; import com.google.devtools.build.lib.syntax.SelectorList; import com.google.devtools.build.lib.syntax.SelectorValue; +import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.syntax.Type.ConversionException; - +import java.util.Arrays; +import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - /** * Test of type-conversions for build-specific types. */ @@ -66,7 +65,7 @@ public class BuildTypeTest { /* symlinkBehavior */ null, /* stripPrefix */ null); assertEquals(input, BuildType.FILESET_ENTRY.convert(input, null, currentRule)); - assertThat(BuildType.FILESET_ENTRY.extractLabels(input)).containsExactly(entryLabel); + assertThat(collectLabels(BuildType.FILESET_ENTRY, input)).containsExactly(entryLabel); } @Test @@ -90,7 +89,7 @@ public class BuildTypeTest { /* symlinkBehavior */ null, /* stripPrefix */ null)); assertEquals(input, BuildType.FILESET_ENTRY_LIST.convert(input, null, currentRule)); - assertThat(BuildType.FILESET_ENTRY_LIST.extractLabels(input)).containsExactly( + assertThat(collectLabels(BuildType.FILESET_ENTRY_LIST, input)).containsExactly( entry1Label, entry2Label); } @@ -124,7 +123,7 @@ public class BuildTypeTest { "//conditions:b", "also not a label", BuildType.Selector.DEFAULT_CONDITION_KEY, "whatever"); try { - new Selector<Label>(input, null, currentRule, BuildType.LABEL); + new Selector<>(input, null, currentRule, BuildType.LABEL); fail("Expected Selector instantiation to fail since the input isn't a selection of labels"); } catch (ConversionException e) { assertThat(e.getMessage()).contains("invalid label 'not a label'"); @@ -140,7 +139,7 @@ public class BuildTypeTest { "not a label", "//a:a", BuildType.Selector.DEFAULT_CONDITION_KEY, "whatever"); try { - new Selector<Label>(input, null, currentRule, BuildType.LABEL); + new Selector<>(input, null, currentRule, BuildType.LABEL); fail("Expected Selector instantiation to fail since the key isn't a label"); } catch (ConversionException e) { assertThat(e.getMessage()).contains("invalid label 'not a label'"); @@ -158,7 +157,7 @@ public class BuildTypeTest { BuildType.Selector.DEFAULT_CONDITION_KEY, "//d:d"); assertEquals( Label.create("@//d", "d"), - new Selector<Label>(input, null, currentRule, BuildType.LABEL).getDefault()); + new Selector<>(input, null, currentRule, BuildType.LABEL).getDefault()); } @Test @@ -271,19 +270,19 @@ public class BuildTypeTest { @Test public void testUnconditionalSelects() throws Exception { assertFalse( - new Selector<Label>( + new Selector<>( ImmutableMap.of("//conditions:a", "//a:a"), null, currentRule, BuildType.LABEL ).isUnconditional()); assertFalse( - new Selector<Label>( + new Selector<>( ImmutableMap.of( "//conditions:a", "//a:a", BuildType.Selector.DEFAULT_CONDITION_KEY, "//b:b"), null, currentRule, BuildType.LABEL ).isUnconditional()); assertTrue( - new Selector<Label>( + new Selector<>( ImmutableMap.of( BuildType.Selector.DEFAULT_CONDITION_KEY, "//b:b"), null, currentRule, BuildType.LABEL @@ -415,4 +414,17 @@ public class BuildTypeTest { assertEquals("FilesetEntry", EvalUtils.getDataTypeName(makeFilesetEntry())); assertFalse(EvalUtils.isImmutable(makeFilesetEntry())); } + + private static <T> Iterable<T> collectLabels(Type<T> type, Object value) + throws InterruptedException { + ImmutableList.Builder<T> result = ImmutableList.builder(); + type.visitLabels(new Type.LabelVisitor() { + @SuppressWarnings("unchecked") + @Override + public void visit(Object object) throws InterruptedException { + result.add((T) object); + } + }, value); + return result.build(); + } } diff --git a/src/test/java/com/google/devtools/build/lib/syntax/TypeTest.java b/src/test/java/com/google/devtools/build/lib/syntax/TypeTest.java index 0714c83362..4179666e47 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/TypeTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/TypeTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -31,17 +32,15 @@ import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.Type.ConversionException; import com.google.devtools.build.lib.testutil.MoreAsserts; - +import java.util.Arrays; +import java.util.List; +import java.util.Map; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - /** * Test of type-conversions using Type. */ @@ -59,7 +58,7 @@ public class TypeTest { public void testInteger() throws Exception { Object x = 3; assertEquals(x, Type.INTEGER.convert(x, null)); - assertThat(Type.INTEGER.extractLabels(x)).isEmpty(); + assertThat(collectLabels(Type.INTEGER, x)).isEmpty(); } @Test @@ -91,7 +90,7 @@ public class TypeTest { public void testString() throws Exception { Object s = "foo"; assertEquals(s, Type.STRING.convert(s, null)); - assertThat(Type.STRING.extractLabels(s)).isEmpty(); + assertThat(collectLabels(Type.STRING, s)).isEmpty(); } @Test @@ -114,7 +113,7 @@ public class TypeTest { assertTrue(Type.BOOLEAN.convert(myTrue, null)); assertFalse(Type.BOOLEAN.convert(false, null)); assertFalse(Type.BOOLEAN.convert(myFalse, null)); - assertThat(Type.BOOLEAN.extractLabels(myTrue)).isEmpty(); + assertThat(collectLabels(Type.BOOLEAN, myTrue)).isEmpty(); } @Test @@ -151,7 +150,7 @@ public class TypeTest { assertEquals(TriState.YES, BuildType.TRISTATE.convert(TriState.YES, null)); assertEquals(TriState.NO, BuildType.TRISTATE.convert(TriState.NO, null)); assertEquals(TriState.AUTO, BuildType.TRISTATE.convert(TriState.AUTO, null)); - assertThat(BuildType.TRISTATE.extractLabels(TriState.YES)).isEmpty(); + assertThat(collectLabels(BuildType.TRISTATE, TriState.YES)).isEmpty(); } @Test @@ -227,7 +226,7 @@ public class TypeTest { Label label = Label .parseAbsolute("//foo:bar"); assertEquals(label, BuildType.LABEL.convert("//foo:bar", null, currentRule)); - assertThat(BuildType.LABEL.extractLabels(label)).containsExactly(label); + assertThat(collectLabels(BuildType.LABEL, label)).containsExactly(label); } @Test @@ -235,7 +234,7 @@ public class TypeTest { Label label = Label .parseAbsolute("//foo:bar"); assertEquals(label, BuildType.NODEP_LABEL.convert("//foo:bar", null, currentRule)); - assertThat(BuildType.NODEP_LABEL.extractLabels(label)).containsExactly(label); + assertThat(collectLabels(BuildType.NODEP_LABEL, label)).containsExactly(label); } @Test @@ -279,7 +278,7 @@ public class TypeTest { Type.STRING_LIST.convert(input, null); assertEquals(input, converted); assertNotSame(input, converted); - assertThat(Type.STRING_LIST.extractLabels(input)).isEmpty(); + assertThat(collectLabels(Type.STRING_LIST, input)).isEmpty(); } @Test @@ -289,7 +288,7 @@ public class TypeTest { Map<String, String> converted = Type.STRING_DICT.convert(input, null); assertEquals(input, converted); assertNotSame(input, converted); - assertThat(Type.STRING_DICT.extractLabels(converted)).isEmpty(); + assertThat(collectLabels(Type.STRING_DICT, converted)).isEmpty(); } @Test @@ -336,7 +335,7 @@ public class TypeTest { Label.parseAbsolute("//quux:wiz")); assertEquals(expected, converted); assertNotSame(expected, converted); - assertThat(BuildType.LABEL_LIST.extractLabels(converted)).containsExactlyElementsIn(expected); + assertThat(collectLabels(BuildType.LABEL_LIST, converted)).containsExactlyElementsIn(expected); } @Test @@ -385,7 +384,7 @@ public class TypeTest { "wiz", Arrays.asList("bang")); assertEquals(expected, converted); assertNotSame(expected, converted); - assertThat(Type.STRING_LIST_DICT.extractLabels(converted)).isEmpty(); + assertThat(collectLabels(Type.STRING_LIST_DICT, converted)).isEmpty(); } @Test @@ -438,7 +437,7 @@ public class TypeTest { "wiz", "bang"); assertEquals(expected, converted); assertNotSame(expected, converted); - assertThat(Type.STRING_DICT_UNARY.extractLabels(converted)).isEmpty(); + assertThat(collectLabels(Type.STRING_DICT_UNARY, converted)).isEmpty(); } @Test @@ -487,4 +486,17 @@ public class TypeTest { assertThat(e).hasMessage("Expected a map for dictionary but got a java.lang.String"); } } -} + + private static <T> Iterable<T> collectLabels(Type<T> type, Object value) + throws InterruptedException { + ImmutableList.Builder<T> result = ImmutableList.builder(); + type.visitLabels(new Type.LabelVisitor() { + @SuppressWarnings("unchecked") + @Override + public void visit(Object object) throws InterruptedException { + result.add((T) object); + } + }, value); + return result.build(); + } +}
\ No newline at end of file |