Ejemplo n.º 1
0
  @Test
  public void invalidSourcePathShouldGiveSpecificErrorMsg() throws NoSuchFieldException {
    Type type = TestFields.class.getField("setOfSourcePaths").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    Path baratheon = Paths.get("Baratheon.java");
    Path lannister = Paths.get("Lannister.java");
    Path stark = Paths.get("Stark.java");
    Path targaryen = Paths.get("Targaryen.java");

    ImmutableList<Path> input = ImmutableList.of(baratheon, lannister, stark, targaryen);

    for (Path p : input) {
      if (!p.equals(baratheon)) {
        filesystem.touch(p);
      }
    }

    try {
      coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    } catch (CoerceFailedException e) {
      String result = e.getMessage();
      String expected = "cannot coerce 'Baratheon.java'";
      for (Path p : input) {
        if (!p.equals(baratheon)) {
          assertFalse(result.contains(p.toString()));
        }
      }
      assertTrue(result.contains(expected));
    }
  }
Ejemplo n.º 2
0
  /** Traverse visits every element of an input value without coercing to the output type. */
  @Test
  public void traverseShouldVisitEveryObject() throws NoSuchFieldException {
    Type type = TestFields.class.getField("stringMapOfLists").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    final ImmutableMap<String, ImmutableList<String>> input =
        ImmutableMap.of(
            "foo", ImmutableList.of("//foo:bar", "//foo:baz"),
            "bar", ImmutableList.of(":bar", "//foo:foo"));

    TestTraversal traversal = new TestTraversal();
    coercer.traverse(input, traversal);
    List<Object> objects = traversal.getObjects();

    assertThat(
        objects,
        Matchers.<Object>contains(
            ImmutableList.of(
                sameInstance((Object) input),
                is((Object) "foo"),
                sameInstance((Object) input.get("foo")),
                is((Object) "//foo:bar"),
                is((Object) "//foo:baz"),
                is((Object) "bar"),
                sameInstance((Object) input.get("bar")),
                is((Object) ":bar"),
                is((Object) "//foo:foo"))));
  }
Ejemplo n.º 3
0
  @Test
  public void coercingHeterogeneousAppleSourceGroups()
      throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("listOfAppleSources").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<?> input =
        ImmutableList.of(
            ImmutableList.of(
                "Group1", ImmutableList.of("foo.m", ImmutableList.of("bar.m", "-Wall"))),
            ImmutableList.of(
                "Group2", ImmutableList.of("baz.m", ImmutableList.of("blech.m", "-fobjc-arc"))));
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    ImmutableList<AppleSource> expectedResult =
        ImmutableList.of(
            AppleSource.ofSourceGroup(
                new Pair<>(
                    "Group1",
                    ImmutableList.of(
                        AppleSource.ofSourcePath(new TestSourcePath("foo.m")),
                        AppleSource.ofSourcePathWithFlags(
                            new Pair<SourcePath, String>(new TestSourcePath("bar.m"), "-Wall"))))),
            AppleSource.ofSourceGroup(
                new Pair<>(
                    "Group2",
                    ImmutableList.of(
                        AppleSource.ofSourcePath(new TestSourcePath("baz.m")),
                        AppleSource.ofSourcePathWithFlags(
                            new Pair<SourcePath, String>(
                                new TestSourcePath("blech.m"), "-fobjc-arc"))))));
    assertEquals(expectedResult, result);
  }
Ejemplo n.º 4
0
  @Test
  public void hasElementTypesForPrimitives() throws NoSuchFieldException {
    Type type = TestFields.class.getField("primitiveString").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    assertTrue(coercer.hasElementClass(String.class));
    assertFalse(coercer.hasElementClass(Integer.class));
  }
Ejemplo n.º 5
0
  @Test
  public void shouldAllowMapTypeToBeSuperclassOfResult()
      throws CoerceFailedException, NoSuchFieldException {
    Type type = TestFields.class.getField("superclassOfImmutableMap").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableMap<String, String> input = ImmutableMap.of("a", "b");
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    assertEquals(input, result);
  }
Ejemplo n.º 6
0
  @Test
  public void coercingSortedSetsShouldActuallyCreateSortedSets()
      throws CoerceFailedException, NoSuchFieldException {
    Type type = TestFields.class.getField("sortedSetOfStrings").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<String> input = ImmutableList.of("c", "a", "d", "b");
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    ImmutableSortedSet<String> expectedResult = ImmutableSortedSet.copyOf(input);
    assertEquals(expectedResult, result);
  }
Ejemplo n.º 7
0
  @Test
  public void pairTypeCoercerCanCoerceFromTwoElementLists()
      throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("pairOfPathsAndStrings").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<?> input = ImmutableList.of("foo.m", "-foo -bar");
    assertEquals(
        new Pair<>(Paths.get("foo.m"), "-foo -bar"),
        coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input));
  }
Ejemplo n.º 8
0
 private CoerceFailedException getCoerceException(Type type, Object object) {
   // First just coerce the raw type and save the coercion exception that gets thrown.
   TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
   try {
     coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), object);
     fail("should throw");
     throw new RuntimeException(); // Suppress "missing return statement" errors
   } catch (CoerceFailedException e) {
     return e;
   }
 }
Ejemplo n.º 9
0
  @Test
  public void coercingNestedListOfSetsShouldActuallyCreateSets()
      throws CoerceFailedException, NoSuchFieldException {
    Type type = TestFields.class.getField("listOfSets").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<ImmutableList<Integer>> input =
        ImmutableList.of(ImmutableList.of(4, 4, 5), ImmutableList.of(6, 7));
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    ImmutableList<ImmutableSet<Integer>> expectedResult =
        ImmutableList.of(ImmutableSet.of(4, 5), ImmutableSet.of(6, 7));
    assertEquals(expectedResult, result);
  }
Ejemplo n.º 10
0
  @Test
  public void coercingStringMapOfIntListsShouldBeIdentity()
      throws CoerceFailedException, NoSuchFieldException {
    Type type = TestFields.class.getField("stringMapOfLists").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableMap<String, ImmutableList<Integer>> input =
        ImmutableMap.of(
            "foo", ImmutableList.of(4, 5),
            "bar", ImmutableList.of(6, 7));
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    assertEquals(input, result);
  }
Ejemplo n.º 11
0
  @Test
  public void coerceToEnumsShouldWorkWithUpperAndLowerCaseValues()
      throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("listOfTestEnums").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
    ImmutableList<String> input = ImmutableList.of("grey", "YELLOW", "red", "PURPLE");

    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    ImmutableList<TestEnum> expected =
        ImmutableList.of(TestEnum.grey, TestEnum.yellow, TestEnum.RED, TestEnum.PURPLE);

    assertEquals(expected, result);
  }
Ejemplo n.º 12
0
  @Test
  public void coercingAppleSourcePaths() throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("listOfAppleSources").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<String> input = ImmutableList.of("foo.m", "bar.m");
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    ImmutableList<AppleSource> expectedResult =
        ImmutableList.of(
            AppleSource.ofSourcePath(new TestSourcePath("foo.m")),
            AppleSource.ofSourcePath(new TestSourcePath("bar.m")));
    assertEquals(expectedResult, result);
  }
Ejemplo n.º 13
0
  @Test
  public void traverseWithPair() throws NoSuchFieldException {
    Type type = TestFields.class.getField("pairOfPathsAndStrings").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    TestTraversal traversal = new TestTraversal();
    ImmutableList<?> input = ImmutableList.of("foo", "bar");
    coercer.traverse(input, traversal);
    assertThat(
        traversal.getObjects(),
        Matchers.<Object>contains(
            ImmutableList.of(sameInstance(input.get(0)), sameInstance(input.get(1)))));
  }
Ejemplo n.º 14
0
  @Test
  public void coercingSortedSetsShouldThrowOnDuplicates()
      throws CoerceFailedException, NoSuchFieldException {
    Type type = TestFields.class.getField("sortedSetOfStrings").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<String> input = ImmutableList.of("a", "a");
    try {
      coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
      fail();
    } catch (CoerceFailedException e) {
      assertEquals("duplicate element \"a\"", e.getMessage());
    }
  }
Ejemplo n.º 15
0
  @Test
  public void coerceToLabels() throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("labels").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    ImmutableList<String> input = ImmutableList.of("cheese", "cake", "tastes", "good");

    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    ImmutableSortedSet<Label> expected =
        ImmutableSortedSet.of(
            new Label("cake"), new Label("cheese"), new Label("good"), new Label("tastes"));

    assertEquals(expected, result);
  }
Ejemplo n.º 16
0
  @Test
  public void coerceToEitherLeftOrRight() throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("eitherStringOrStringList").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    String inputString = "a_string";
    ImmutableList<String> inputList = ImmutableList.of("a", "b");

    assertEquals(
        Either.ofLeft(inputString),
        coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), inputString));
    assertEquals(
        Either.ofRight(inputList),
        coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), inputList));
  }
Ejemplo n.º 17
0
  @Test
  public void traverseWithEitherAndContainer() throws NoSuchFieldException {
    Type type = TestFields.class.getField("eitherStringOrStringList").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    TestTraversal traversal = new TestTraversal();
    ImmutableList<String> input = ImmutableList.of("foo");
    coercer.traverse(input, traversal);
    assertThat(
        traversal.getObjects(),
        Matchers.<Object>contains(
            ImmutableList.of(sameInstance((Object) input), sameInstance((Object) input.get(0)))));

    traversal = new TestTraversal();
    String input2 = "foo";
    coercer.traverse(input2, traversal);
    assertThat(traversal.getObjects(), hasSize(1));
    assertThat(traversal.getObjects().get(0), sameInstance((Object) "foo"));
  }
Ejemplo n.º 18
0
  @Test
  public void coerceFromTurkishIsShouldWork() throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("listOfTestEnums").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
    String pinkWithLowercaseTurkishI = "p\u0131nk";
    String pinkWithUppercaseTurkishI = "P\u0130NK";
    String whiteWithLowercaseTurkishI = "wh\u0131te";
    String whiteWithUppercaseTurkishI = "WH\u0130TE";

    ImmutableList<String> input =
        ImmutableList.of(
            pinkWithLowercaseTurkishI,
            pinkWithUppercaseTurkishI,
            whiteWithLowercaseTurkishI,
            whiteWithUppercaseTurkishI);
    ImmutableList<TestEnum> expected =
        ImmutableList.of(TestEnum.PINK, TestEnum.PINK, TestEnum.white, TestEnum.white);
    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    assertEquals(expected, result);
  }
Ejemplo n.º 19
0
  @Test
  @Ignore(
      "Test compiles and passes but checkstyle 4 barfs on the commented-out line assigning "
          + "the expected var: try reinstating after arc lint moves to checkstyle 5.5")
  public void coerceToTurkishIsShouldWork() throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("listOfTestEnums").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
    String violetWithLowerCaseTurkishI = "v\u0131olet";
    String violetWithUpperCaseTurkishI = "V\u0130OLET";
    ImmutableList<String> input =
        ImmutableList.of(
            "violet", "VIOLET", violetWithLowerCaseTurkishI, violetWithUpperCaseTurkishI);
    ImmutableList<TestEnum> expected =
        ImmutableList.of(
            // Remove @ignore and uncomment line below once we have checkstyle 5.5
            // Also reinstate the extra value for TestEnum
            // TestEnum.V\u0130OLET, TestEnum.V\u0130OLET, TestEnum.V\u0130OLET,
            // TestEnum.V\u0130OLET
            );

    Object result = coercer.coerce(buildRuleResolver, filesystem, Paths.get(""), input);
    assertEquals(expected, result);
  }
Ejemplo n.º 20
0
  @Test
  public void hasElementTypesForContainers() throws NoSuchFieldException {
    Type type = TestFields.class.getField("stringMapOfLists").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);

    assertTrue(coercer.hasElementClass(String.class));
    assertTrue(coercer.hasElementClass(Integer.class));
    assertTrue(coercer.hasElementClass(Integer.class, String.class));
    assertTrue(coercer.hasElementClass(Integer.class, SourcePath.class));
    assertFalse(coercer.hasElementClass(SourcePath.class));
  }