/** * Tests that {@link Type#selectableConvert} returns either the native type or a selector on that * type, in accordance with the provided input. */ @SuppressWarnings("unchecked") @Test public void testSelectableConvert() throws Exception { Object nativeInput = Arrays.asList("//a:a1", "//a:a2"); Object selectableInput = SelectorList.of( new SelectorValue( ImmutableMap.of( "//conditions:a", nativeInput, Type.Selector.DEFAULT_CONDITION_KEY, nativeInput))); List<Label> expectedLabels = ImmutableList.of(Label.create("a", "a1"), Label.create("a", "a2")); // Conversion to direct type: Object converted = Type.LABEL_LIST.selectableConvert(nativeInput, null, currentRule); assertTrue(converted instanceof List<?>); assertSameContents(expectedLabels, (List<Label>) converted); // Conversion to selectable type: converted = Type.LABEL_LIST.selectableConvert(selectableInput, null, currentRule); Type.SelectorList<?> selectorList = (Type.SelectorList<?>) converted; assertSameContents( ImmutableMap.of( Label.parseAbsolute("//conditions:a"), expectedLabels, Label.parseAbsolute(Type.Selector.DEFAULT_CONDITION_KEY), expectedLabels) .entrySet(), ((Type.Selector<Label>) selectorList.getSelectors().get(0)).getEntries().entrySet()); }
@Test public void testFilesetEntry() throws Exception { Label srcDir = Label.create("foo", "src"); Label entryLabel = Label.create("foo", "entry"); FilesetEntry input = new FilesetEntry(srcDir, ImmutableList.of(entryLabel), null, null, null, null); assertEquals(input, Type.FILESET_ENTRY.convert(input, null, currentRule)); assertThat(Type.FILESET_ENTRY.flatten(input)).containsExactly(entryLabel); }
@Test public void testLabelList() throws Exception { Object input = Arrays.asList("//foo:bar", ":wiz"); List<Label> converted = Type.LABEL_LIST.convert(input, null, currentRule); List<Label> expected = Arrays.asList(Label.parseAbsolute("//foo:bar"), Label.parseAbsolute("//quux:wiz")); assertEquals(expected, converted); assertNotSame(expected, converted); assertThat(Type.LABEL_LIST.flatten(converted)).containsExactlyElementsIn(expected); }
@Test public void testRelativeLabel() throws Exception { assertEquals(Label.parseAbsolute("//quux:wiz"), Type.LABEL.convert(":wiz", null, currentRule)); assertEquals(Label.parseAbsolute("//quux:wiz"), Type.LABEL.convert("wiz", null, currentRule)); try { Type.LABEL.convert("wiz", null); fail(); } catch (NullPointerException e) { /* ok */ } }
@Test public void testLabelListDict() throws Exception { Object input = ImmutableMap.of("foo", Arrays.asList("//foo:bar"), "wiz", Arrays.asList(":bang")); Map<String, List<Label>> converted = Type.LABEL_LIST_DICT.convert(input, null, currentRule); Label fooLabel = Label.parseAbsolute("//foo:bar"); Label bangLabel = Label.parseAbsolute("//quux:bang"); Map<?, ?> expected = ImmutableMap.<String, List<Label>>of( "foo", Arrays.<Label>asList(fooLabel), "wiz", Arrays.<Label>asList(bangLabel)); assertEquals(expected, converted); assertNotSame(expected, converted); assertThat(Type.LABEL_LIST_DICT.flatten(converted)).containsExactly(fooLabel, bangLabel); }
@Override public Label load(String labelString) throws PackageDeserializationException { try { return Label.parseAbsolute(labelString); } catch (LabelSyntaxException e) { throw new PackageDeserializationException( "Invalid label: " + e.getMessage(), e); } }
/** Tests the "convenience factories" (string, label, etc) for types. */ @Test public void testConvenienceFactoriesTypes() throws Exception { assertType(INTEGER, attr("x", INTEGER).build()); assertType(INTEGER, attr("x", INTEGER).value(42).build()); assertType(STRING, attr("x", STRING).build()); assertType(STRING, attr("x", STRING).value("foo").build()); Label label = Label.parseAbsolute("//foo:bar"); assertType(LABEL, attr("x", LABEL).legacyAllowAnyFileType().build()); assertType(LABEL, attr("x", LABEL).legacyAllowAnyFileType().value(label).build()); List<String> slist = Arrays.asList("foo", "bar"); assertType(STRING_LIST, attr("x", STRING_LIST).build()); assertType(STRING_LIST, attr("x", STRING_LIST).value(slist).build()); List<Label> llist = Arrays.asList(Label.parseAbsolute("//foo:bar"), Label.parseAbsolute("//foo:wiz")); assertType(LABEL_LIST, attr("x", LABEL_LIST).legacyAllowAnyFileType().build()); assertType(LABEL_LIST, attr("x", LABEL_LIST).legacyAllowAnyFileType().value(llist).build()); }
/** Tests the "convenience factories" (string, label, etc) for default values. */ @Test public void testConvenienceFactoriesDefaultValues() throws Exception { assertDefaultValue(0, attr("x", INTEGER).build()); assertDefaultValue(42, attr("x", INTEGER).value(42).build()); assertDefaultValue("", attr("x", STRING).build()); assertDefaultValue("foo", attr("x", STRING).value("foo").build()); Label label = Label.parseAbsolute("//foo:bar"); assertDefaultValue(null, attr("x", LABEL).legacyAllowAnyFileType().build()); assertDefaultValue(label, attr("x", LABEL).legacyAllowAnyFileType().value(label).build()); List<String> slist = Arrays.asList("foo", "bar"); assertDefaultValue(Collections.emptyList(), attr("x", STRING_LIST).build()); assertDefaultValue(slist, attr("x", STRING_LIST).value(slist).build()); List<Label> llist = Arrays.asList(Label.parseAbsolute("//foo:bar"), Label.parseAbsolute("//foo:wiz")); assertDefaultValue( Collections.emptyList(), attr("x", LABEL_LIST).legacyAllowAnyFileType().build()); assertDefaultValue(llist, attr("x", LABEL_LIST).legacyAllowAnyFileType().value(llist).build()); }
/** Tests that {@link Type.Selector} correctly references its default value. */ @Test public void testSelectorDefault() throws Exception { Object input = ImmutableMap.of( "//conditions:a", "//a:a", "//conditions:b", "//b:b", Type.Selector.DEFAULT_CONDITION_KEY, "//d:d"); assertEquals( Label.create("d", "d"), new Type.Selector<Label>(input, null, currentRule, Type.LABEL).getDefault()); }
/** Tests basic {@link Type.Selector} functionality. */ @Test public void testSelector() throws Exception { Object input = ImmutableMap.of( "//conditions:a", "//a:a", "//conditions:b", "//b:b", Type.Selector.DEFAULT_CONDITION_KEY, "//d:d"); Type.Selector<Label> selector = new Type.Selector<>(input, null, currentRule, Type.LABEL); assertEquals(Type.LABEL, selector.getOriginalType()); Map<Label, Label> expectedMap = ImmutableMap.of( Label.parseAbsolute("//conditions:a"), Label.create("a", "a"), Label.parseAbsolute("//conditions:b"), Label.create("b", "b"), Label.parseAbsolute(Type.Selector.DEFAULT_CONDITION_KEY), Label.create("d", "d")); assertSameContents(expectedMap.entrySet(), selector.getEntries().entrySet()); }
/** Tests for "reserved" key labels (i.e. not intended to map to actual targets). */ @Test public void testReservedKeyLabels() throws Exception { assertFalse(Type.Selector.isReservedLabel(Label.parseAbsolute("//condition:a"))); assertTrue( Type.Selector.isReservedLabel(Label.parseAbsolute(Type.Selector.DEFAULT_CONDITION_KEY))); }
@Test public void testSelectorList() throws Exception { Object selector1 = new SelectorValue( ImmutableMap.of( "//conditions:a", ImmutableList.of("//a:a"), "//conditions:b", ImmutableList.of("//b:b"))); Object selector2 = new SelectorValue( ImmutableMap.of( "//conditions:c", ImmutableList.of("//c:c"), "//conditions:d", ImmutableList.of("//d:d"))); Type.SelectorList<List<Label>> selectorList = new Type.SelectorList<>( ImmutableList.of(selector1, selector2), null, currentRule, Type.LABEL_LIST); assertEquals(Type.LABEL_LIST, selectorList.getOriginalType()); assertSameContents( ImmutableSet.of( Label.parseAbsolute("//conditions:a"), Label.parseAbsolute("//conditions:b"), Label.parseAbsolute("//conditions:c"), Label.parseAbsolute("//conditions:d")), selectorList.getKeyLabels()); List<Type.Selector<List<Label>>> selectors = selectorList.getSelectors(); assertSameContents( ImmutableMap.of( Label.parseAbsolute("//conditions:a"), ImmutableList.of(Label.create("a", "a")), Label.parseAbsolute("//conditions:b"), ImmutableList.of(Label.create("b", "b"))) .entrySet(), selectors.get(0).getEntries().entrySet()); assertSameContents( ImmutableMap.of( Label.parseAbsolute("//conditions:c"), ImmutableList.of(Label.create("c", "c")), Label.parseAbsolute("//conditions:d"), ImmutableList.of(Label.create("d", "d"))) .entrySet(), selectors.get(1).getEntries().entrySet()); }
@Before public void setUp() throws Exception { this.currentRule = Label.parseAbsolute("//quux:baz"); }
@Test public void testNodepLabel() throws Exception { Label label = Label.parseAbsolute("//foo:bar"); assertEquals(label, Type.NODEP_LABEL.convert("//foo:bar", null, currentRule)); assertThat(Type.NODEP_LABEL.flatten(label)).containsExactly(label); }