Java Code Examples for com.google.common.truth.Correspondence#from()

The following examples show how to use com.google.common.truth.Correspondence#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntentCorrespondences.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Beta
public static Correspondence<Intent, Intent> all(
    final Correspondence<Intent, Intent>... correspondences) {
  StringBuilder combinedString = new StringBuilder();
  for (int i = 0; i < correspondences.length; i++) {
    combinedString.append(correspondences[i]);
    if ((i + 1) < correspondences.length) {
      combinedString.append(" and ");
    }
  }
  return Correspondence.from(
      (actual, expected) -> {
        for (Correspondence<Intent, Intent> innerCorrespondence : correspondences) {
          if (!innerCorrespondence.compare(actual, expected)) {
            return false;
          }
        }
        return true;
      },
      combinedString.toString());
}
 
Example 2
Source File: LocationCorrespondences.java    From android-test with Apache License 2.0 5 votes vote down vote up
public static Correspondence<Location, Location> at() {
  return Correspondence.from(
      (actual, expected) ->
          actual.getLatitude() == expected.getLatitude()
              && actual.getLongitude() == expected.getLongitude(),
      "has lat/lon at");
}
 
Example 3
Source File: BasicAnnotationProcessorTest.java    From auto with Apache License 2.0 5 votes vote down vote up
private static <K, V>
    Correspondence<SetMultimap<K, V>, SetMultimap<K, String>> setMultimapValuesByString() {
  return Correspondence.from(
      (actual, expected) ->
          ImmutableSetMultimap.copyOf(transformValues(actual, Object::toString)).equals(expected),
      "is equivalent comparing multimap values by `toString()` to");
}
 
Example 4
Source File: BundleModuleMergerTest.java    From bundletool with Apache License 2.0 4 votes vote down vote up
private static Correspondence<BundleModuleName, String> equalsBundleModuleName() {
  return Correspondence.from(
      (BundleModuleName bundleModuleName, String moduleName) ->
          bundleModuleName.getName().equals(moduleName),
      "equals");
}
 
Example 5
Source File: IntentCorrespondences.java    From android-test with Apache License 2.0 4 votes vote down vote up
public static Correspondence<Intent, Intent> action() {
  return Correspondence.from(
      (actual, expected) -> Objects.equal(actual.getAction(), expected.getAction()),
      "has getAction() equal to");
}
 
Example 6
Source File: IntentCorrespondences.java    From android-test with Apache License 2.0 4 votes vote down vote up
public static Correspondence<Intent, Intent> data() {
  return Correspondence.from(
      (actual, expected) -> Objects.equal(actual.getData(), expected.getData()),
      "has getData() equal to");
}
 
Example 7
Source File: LocationCorrespondences.java    From android-test with Apache License 2.0 4 votes vote down vote up
public static Correspondence<Location, Location> equality() {
  return Correspondence.from(
      (actual, expected) -> {
        if (actual == expected) {
          return true;
        }
        if (actual == null || expected == null) {
          return false;
        }
        if (!Objects.equal(actual.getProvider(), expected.getProvider())) {
          return false;
        }
        if (actual.getTime() != expected.getTime()) {
          return false;
        }
        if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
          if (actual.getElapsedRealtimeNanos() != expected.getElapsedRealtimeNanos()) {
            return false;
          }
        }
        if (actual.getLatitude() != expected.getLatitude()) {
          return false;
        }
        if (actual.getLongitude() != expected.getLongitude()) {
          return false;
        }
        if (actual.getAltitude() != expected.getAltitude()) {
          return false;
        }
        if (actual.getSpeed() != expected.getSpeed()) {
          return false;
        }
        if (actual.getBearing() != expected.getBearing()) {
          return false;
        }
        if (actual.getAccuracy() != expected.getAccuracy()) {
          return false;
        }
        if (Build.VERSION.SDK_INT >= VERSION_CODES.O) {
          if (actual.getVerticalAccuracyMeters() != expected.getVerticalAccuracyMeters()) {
            return false;
          }
          if (actual.getSpeedAccuracyMetersPerSecond()
              != expected.getSpeedAccuracyMetersPerSecond()) {
            return false;
          }
          if (actual.getBearingAccuracyDegrees() != expected.getBearingAccuracyDegrees()) {
            return false;
          }
        }
        return true;
      },
      "is equal to");
}
 
Example 8
Source File: LocationCorrespondences.java    From android-test with Apache License 2.0 4 votes vote down vote up
public static Correspondence<Location, Location> nearby(float distanceM) {
  return Correspondence.from(
      (actual, expected) -> actual.distanceTo(expected) <= distanceM, "has lat/lon near");
}
 
Example 9
Source File: OptionsDataTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test
public void optionsDefinitionsAreSharedBetweenOptionsBases() throws Exception {
  Class<FieldNamesDifferOptions> class1 = FieldNamesDifferOptions.class;
  Class<EndOfAlphabetOptions> class2 = EndOfAlphabetOptions.class;
  Class<ReverseOrderedOptions> class3 = ReverseOrderedOptions.class;

  // Construct the definitions once and accumulate them so we can test that these are not
  // recomputed during the construction of the options data.
  ImmutableList<OptionDefinition> optionDefinitions =
      new ImmutableList.Builder<OptionDefinition>()
          .addAll(OptionsData.getAllOptionDefinitionsForClass(class1))
          .addAll(OptionsData.getAllOptionDefinitionsForClass(class2))
          .addAll(OptionsData.getAllOptionDefinitionsForClass(class3))
          .build();

  // Construct the data all together.
  IsolatedOptionsData data = construct(class1, class2, class3);
  ArrayList<OptionDefinition> optionDefinitionsFromData =
      new ArrayList<>(optionDefinitions.size());
  data.getAllOptionDefinitions()
      .forEach(entry -> optionDefinitionsFromData.add(entry.getValue()));

  Correspondence<Object, Object> referenceEquality =
      Correspondence.from((obj1, obj2) -> obj1 == obj2, "is the same object as");
  assertThat(optionDefinitionsFromData)
      .comparingElementsUsing(referenceEquality)
      .containsAtLeastElementsIn(optionDefinitions);

  // Construct options data for each class separately, and check again.
  IsolatedOptionsData data1 = construct(class1);
  IsolatedOptionsData data2 = construct(class2);
  IsolatedOptionsData data3 = construct(class3);
  ArrayList<OptionDefinition> optionDefinitionsFromGroupedData =
      new ArrayList<>(optionDefinitions.size());
  data1
      .getAllOptionDefinitions()
      .forEach(entry -> optionDefinitionsFromGroupedData.add(entry.getValue()));
  data2
      .getAllOptionDefinitions()
      .forEach(entry -> optionDefinitionsFromGroupedData.add(entry.getValue()));
  data3
      .getAllOptionDefinitions()
      .forEach(entry -> optionDefinitionsFromGroupedData.add(entry.getValue()));

  assertThat(optionDefinitionsFromGroupedData)
      .comparingElementsUsing(referenceEquality)
      .containsAtLeastElementsIn(optionDefinitions);
}
 
Example 10
Source File: StarlarkIntegrationTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test
public void testTransitiveAnalysisFailureInfo() throws Exception {
  scratch.file(
      "test/extension.bzl",
      "def custom_rule_impl(ctx):",
      "   fail('This Is My Failure Message')",
      "",
      "custom_rule = rule(implementation = custom_rule_impl)",
      "",
      "def depending_rule_impl(ctx):",
      "   return []",
      "",
      "depending_rule = rule(implementation = depending_rule_impl,",
      "     attrs = {'deps' : attr.label_list()})");

  scratch.file(
      "test/BUILD",
      "load('//test:extension.bzl', 'custom_rule', 'depending_rule')",
      "",
      "custom_rule(name = 'one')",
      "custom_rule(name = 'two')",
      "depending_rule(name = 'failures_are_direct_deps',",
      "    deps = [':one', ':two'])",
      "depending_rule(name = 'failures_are_indirect_deps',",
      "    deps = [':failures_are_direct_deps'])");

  useConfiguration("--allow_analysis_failures=true");

  ConfiguredTarget target = getConfiguredTarget("//test:failures_are_indirect_deps");
  AnalysisFailureInfo info =
      (AnalysisFailureInfo) target.get(AnalysisFailureInfo.STARLARK_CONSTRUCTOR.getKey());

  Correspondence<AnalysisFailure, AnalysisFailure> correspondence =
      Correspondence.from(
          (actual, expected) ->
              actual.getLabel().equals(expected.getLabel())
                  && actual.getMessage().contains(expected.getMessage()),
          "is equivalent to");

  AnalysisFailure expectedOne =
      new AnalysisFailure(
          Label.parseAbsoluteUnchecked("//test:one"), "This Is My Failure Message");
  AnalysisFailure expectedTwo =
      new AnalysisFailure(
          Label.parseAbsoluteUnchecked("//test:two"), "This Is My Failure Message");

  assertThat(info.getCauses().getSet(AnalysisFailure.class).toList())
      .comparingElementsUsing(correspondence)
      .containsExactly(expectedOne, expectedTwo);
}