/** 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")))); }
@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")); }
@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))))); }