blob: f19a31ee1db3b4fad785d156d2acb8d4bddc109e [file] [log] [blame]
{
assertPlaceholderCollection("%{foo", "%%{foo");
}
@Test
public void testCompleteAndIncompletePlaceholder() throws Exception {
assertPlaceholderCollection("%{foo}%{bar", "%s%%{bar", "foo");
}
@Test
public void testPlaceholderLooksLikeNestedIncompletePlaceholder() throws Exception {
assertPlaceholderCollection("%{%{foo", "%%{%%{foo");
}
@Test
public void testPlaceholderLooksLikeNestedPlaceholder() throws Exception {
assertPlaceholderCollection("%{%{foo}", "%s", "%{foo");
}
@Test
public void testEscapesJustPercentSign() throws Exception {
assertPlaceholderCollection("%", "%%");
}
@Test
public void testEscapesPrintfPlaceholder() throws Exception {
assertPlaceholderCollection("%{x}%s%{y}", "%s%%s%s", "x", "y");
}
@Test
public void testEscapesPercentSign() throws Exception {
assertPlaceholderCollection("foo%{bar}%baz", "foo%s%%baz", "bar");
}
private static AttributeValueGetter attrs(
final Map<String, ? extends Collection<String>> values) {
return new AttributeValueGetter() {
@Override
public Set<String> get(AttributeMap ignored, String attr) {
return new LinkedHashSet<>(Preconditions.checkNotNull(values.get(attr)));
}
};
}
private void assertPlaceholderSubtitution(
String template,
AttributeValueGetter attrValues,
String[] expectedSubstitutions,
String[] expectedFoundPlaceholders)
throws Exception {
List<String> foundAttributes = new ArrayList<>();
List<String> substitutions =
ImplicitOutputsFunction.substitutePlaceholderIntoTemplate(
template, null, attrValues, foundAttributes);
assertThat(foundAttributes)
.containsExactlyElementsIn(Arrays.asList(expectedFoundPlaceholders))
.inOrder();
assertThat(substitutions).containsExactlyElementsIn(Arrays.asList(expectedSubstitutions));
}
@Test
public void testSingleScalarElementSubstitution() throws Exception {
assertPlaceholderSubtitution(
"%{x}",
attrs(ImmutableMap.of("x", ImmutableList.of("a"))),
new String[] {"a"},
new String[] {"x"});
}
@Test
public void testSingleVectorElementSubstitution() throws Exception {
assertPlaceholderSubtitution(
"%{x}",
attrs(ImmutableMap.of("x", ImmutableList.of("a", "b", "c"))),
new String[] {"a", "b", "c"},
new String[] {"x"});
}
@Test
public void testMultipleElementsSubstitution() throws Exception {
assertPlaceholderSubtitution(
"%{x}-%{y}-%{z}",
attrs(
ImmutableMap.of(
"x", ImmutableList.of("foo", "bar", "baz"),
"y", ImmutableList.of("meow"),
"z", ImmutableList.of("1", "2"))),
new String[] {
"foo-meow-1", "foo-meow-2", "bar-meow-1", "bar-meow-2", "baz-meow-1", "baz-meow-2"
},
new String[] {"x", "y", "z"});
}
@Test
public void testEmptyElementSubstitution() throws Exception {
assertPlaceholderSubtitution(
"a-%{x}",
attrs(ImmutableMap.of("x", ImmutableList.<String>of())),
new String[0],
new String[] {"x"});
}
@Test
public void testSamePlaceholderMultipleTimes() throws Exception {
assertPlaceholderSubtitution(
"%{x}-%{y}-%{x}",
attrs(ImmutableMap.of("x", ImmutableList.of("a", "b"), "y", ImmutableList.of("1", "2"))),
new String[] {"a-1-a", "a-1-b", "a-2-a", "a-2-b", "b-1-a", "b-1-b", "b-2-a", "b-2-b"},
new String[] {"x", "y", "x"});
}
@Test
public void testRepeatingPlaceholderValue() throws Exception {
assertPlaceholderSubtitution(
"%{x}",
attrs(ImmutableMap.of("x", ImmutableList.of("a", "a"))),
new String[] {"a"},
new String[] {"x"});
}
@Test
public void testIncompletePlaceholderTreatedAsText() throws Exception {
assertPlaceholderSubtitution(
"%{x}-%{y-%{z",
attrs(ImmutableMap.of("x", ImmutableList.of("a", "b"))),
new String[] {"a-%{y-%{z", "b-%{y-%{z"},
new String[] {"x"});
}
}