@Test public void describeNestedMismatchesEllipsisBeforeAndAfterAMiddleElement() throws Exception { Set<String> allKeys = new LinkedHashSet<>(asList("first", "second", "third")); StringDescription description = new StringDescription(); Map<String, Consumer<Description>> mismatchedKeys = ImmutableMap.of("second", desc -> desc.appendText("mismatch!")); BiConsumer<String, Description> describeKey = (str, desc) -> desc.appendText(str); DescriptionUtils.describeNestedMismatches(allKeys, description, mismatchedKeys, describeKey); assertThat(description.toString(), is( "{\n" + " ...\n" + " second: mismatch!\n" + " ...\n" + "}" )); }
public static void describeNestedMismatches( Set<String> allKeys, Description mismatchDescription, Map<String, Consumer<Description>> mismatchedKeys, BiConsumer<String, Description> describeKey) { checkArgument(!mismatchedKeys.isEmpty(), "mismatchKeys must not be empty"); String previousMismatchKey = null; String previousKey = null; mismatchDescription.appendText("{\n"); for (String key : allKeys) { if (mismatchedKeys.containsKey(key)) { if (previousKey != null && !Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } describeMismatchForKey( key, mismatchDescription, describeKey, mismatchedKeys.get(key)); previousMismatchKey = key; } previousKey = key; } if (!Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } mismatchDescription.appendText("}"); }
DescriptionUtils { public static void describeNestedMismatches( Set<String> allKeys, Description mismatchDescription, Map<String, Consumer<Description>> mismatchedKeys, BiConsumer<String, Description> describeKey) { checkArgument(!mismatchedKeys.isEmpty(), "mismatchKeys must not be empty"); String previousMismatchKey = null; String previousKey = null; mismatchDescription.appendText("{\n"); for (String key : allKeys) { if (mismatchedKeys.containsKey(key)) { if (previousKey != null && !Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } describeMismatchForKey( key, mismatchDescription, describeKey, mismatchedKeys.get(key)); previousMismatchKey = key; } previousKey = key; } if (!Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } mismatchDescription.appendText("}"); } }
DescriptionUtils { public static void describeNestedMismatches( Set<String> allKeys, Description mismatchDescription, Map<String, Consumer<Description>> mismatchedKeys, BiConsumer<String, Description> describeKey) { checkArgument(!mismatchedKeys.isEmpty(), "mismatchKeys must not be empty"); String previousMismatchKey = null; String previousKey = null; mismatchDescription.appendText("{\n"); for (String key : allKeys) { if (mismatchedKeys.containsKey(key)) { if (previousKey != null && !Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } describeMismatchForKey( key, mismatchDescription, describeKey, mismatchedKeys.get(key)); previousMismatchKey = key; } previousKey = key; } if (!Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } mismatchDescription.appendText("}"); } private  DescriptionUtils(); }
DescriptionUtils { public static void describeNestedMismatches( Set<String> allKeys, Description mismatchDescription, Map<String, Consumer<Description>> mismatchedKeys, BiConsumer<String, Description> describeKey) { checkArgument(!mismatchedKeys.isEmpty(), "mismatchKeys must not be empty"); String previousMismatchKey = null; String previousKey = null; mismatchDescription.appendText("{\n"); for (String key : allKeys) { if (mismatchedKeys.containsKey(key)) { if (previousKey != null && !Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } describeMismatchForKey( key, mismatchDescription, describeKey, mismatchedKeys.get(key)); previousMismatchKey = key; } previousKey = key; } if (!Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } mismatchDescription.appendText("}"); } private  DescriptionUtils(); static void indentDescription(Description description, Description innerDescription); static void describeNestedMismatches(
      Set<String> allKeys,
      Description mismatchDescription,
      Map<String, Consumer<Description>> mismatchedKeys,
      BiConsumer<String, Description> describeKey); }
DescriptionUtils { public static void describeNestedMismatches( Set<String> allKeys, Description mismatchDescription, Map<String, Consumer<Description>> mismatchedKeys, BiConsumer<String, Description> describeKey) { checkArgument(!mismatchedKeys.isEmpty(), "mismatchKeys must not be empty"); String previousMismatchKey = null; String previousKey = null; mismatchDescription.appendText("{\n"); for (String key : allKeys) { if (mismatchedKeys.containsKey(key)) { if (previousKey != null && !Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } describeMismatchForKey( key, mismatchDescription, describeKey, mismatchedKeys.get(key)); previousMismatchKey = key; } previousKey = key; } if (!Objects.equals(previousMismatchKey, previousKey)) { mismatchDescription.appendText(" ...\n"); } mismatchDescription.appendText("}"); } private  DescriptionUtils(); static void indentDescription(Description description, Description innerDescription); static void describeNestedMismatches(
      Set<String> allKeys,
      Description mismatchDescription,
      Map<String, Consumer<Description>> mismatchedKeys,
      BiConsumer<String, Description> describeKey);  }