Exemple #1
0
  /**
   * 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());
  }
Exemple #2
0
 /** Tests that {@link Type#convert} fails on selector inputs. */
 @Test
 public void testConvertDoesNotAcceptSelectables() throws Exception {
   Object selectableInput =
       SelectorList.of(
           new SelectorValue(
               ImmutableMap.of("//conditions:a", Arrays.asList("//a:a1", "//a:a2"))));
   try {
     Type.LABEL_LIST.convert(selectableInput, null, currentRule);
     fail("Expected conversion to fail on a selectable input");
   } catch (ConversionException e) {
     assertThat(e.getMessage()).contains("expected value of type 'list(label)'");
   }
 }