Skip to content

Commit

Permalink
Merge pull request #2729 from wiremock/join_handlerbars_helper_upgrade
Browse files Browse the repository at this point in the history
Join handlerbars helper upgrade
  • Loading branch information
dieppa committed May 21, 2024
2 parents a378ee6 + a76d425 commit 1d3f0e6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.TagType;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class JoinHelper extends HandlebarsHelper<Object> {
Expand All @@ -31,11 +32,19 @@ public Object apply(Object context, Options options) throws IOException {

String separator = (String) context;

Object param = options.param(0, null);
if (param == null || !(Iterable.class.isAssignableFrom(param.getClass()))) {
List<Object> items;
Object firstParam = options.param(0, null);
if (firstParam == null) {
return handleError("The parameter must be list");
} else if (Iterable.class.isAssignableFrom(firstParam.getClass())) {
items = (List<Object>) firstParam;
} else {
items = Arrays.asList(options.params);
}
List<Object> items = (List<Object>) param;

String prefix = options.hash("prefix", "");
String suffix = options.hash("suffix", "");

if (items.isEmpty()) {
return "";
}
Expand All @@ -46,7 +55,7 @@ public Object apply(Object context, Options options) throws IOException {
result = processWithoutSection(separator, items);
}

return result;
return prefix + result + suffix;
}

private static String processWithoutSection(String separator, List<Object> items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public Object apply(Object context, Options options) throws IOException {
}
},

join {
arrayJoin {
private final JoinHelper helper = new JoinHelper();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,11 +1057,11 @@ public void joinWithObjectBody() {
+ " { \"id\": 3, \"name\": \"Three\" }\n"
+ "]\n"
+ "{{/parseJson}}"
+ "[{{#join ',' myThings as |item|}}"
+ "[{{#arrayJoin ',' myThings as |item|}}"
+ "{\n"
+ "\"name{{item.id}}\": \"{{item.name}}\"\n"
+ "}\n"
+ "{{/join}}]");
+ "{{/arrayJoin}}]");

assertThat(
result,
Expand All @@ -1071,56 +1071,100 @@ public void joinWithObjectBody() {

@Test
public void joinWithArrayOfStrings() {
String result = transform("{{join ',' (array 'One\n' 'Two' 'Three')}}");
String result = transform("{{arrayJoin ',' (array 'One\n' 'Two' 'Three')}}");

assertThat(result, equalToCompressingWhiteSpace("One\n,Two,Three"));
}

@Test
public void joinWithItemsListed() {
String result = transform("{{arrayJoin ',' 'One\n' 'Two' 'Three'}}");

assertThat(result, equalToCompressingWhiteSpace("One\n,Two,Three"));
}

@Test
public void joinWithNumbersListed() {
String result = transform("{{arrayJoin ',' 1 2 3}}");

assertThat(result, equalToCompressingWhiteSpace("1,2,3"));
}

@Test
public void joinWithEmptyArray() {
String result = transform("{{join ',' (array )}}");
String result = transform("{{arrayJoin ',' (array )}}");

assertThat(result, equalToCompressingWhiteSpace(""));
}

@Test
public void joinWithNoSeparatorShouldReturnError() {
String result = transform("{{join (array 'One' 'Two' 'Three')}}");
String result = transform("{{arrayJoin (array 'One' 'Two' 'Three')}}");

assertThat(
result, equalToCompressingWhiteSpace("[ERROR: Separator parameter must be a String]\n"));
}

@Test
public void joinWithNoParameterShouldReturnError() {
String result = transform("{{join ','}}");
String result = transform("{{arrayJoin ','}}");

assertThat(result, equalToCompressingWhiteSpace("[ERROR: The parameter must be list]\n"));
}

@Test
public void joinWithStringAsParameterShouldReturnError() {
String result = transform("{{join ',' \"blablabla\"}}");
String result = transform("{{arrayJoin ',' \"blablabla\"}}");

assertThat(result, equalToCompressingWhiteSpace("[ERROR: The parameter must be list]\n"));
assertThat(result, equalToCompressingWhiteSpace("blablabla"));
}

@Test
public void joinWithItemsListedAndPrefixAndSuffix() {
String result =
transform("{{arrayJoin ',' (array 'One\n' 'Two' 'Three') prefix=\"p..\" suffix=\"..s\"}}");

assertThat(result, equalToCompressingWhiteSpace("p..One\n,Two,Three..s"));
}

@Test
public void joinWithNumbersListedAndPrefix() {
String result = transform("{{arrayJoin ',' 1 2 3 prefix='p..'}}");

assertThat(result, equalToCompressingWhiteSpace("p..1,2,3"));
}

@Test
public void joinWithNumbersListedAndSuffix() {
String result = transform("{{arrayJoin ',' 1 2 3 suffix='..s'}}");

assertThat(result, equalToCompressingWhiteSpace("1,2,3..s"));
}

@Test
public void joinWithNumbersListedAndPrefixAndSuffix() {
String result = transform("{{arrayJoin ',' 1 2 3 prefix='p..' suffix='..s'}}");

assertThat(result, equalToCompressingWhiteSpace("p..1,2,3..s"));
}

@Test
public void joinWithDifferentSeparators() {
String result1 = transform("{{join (pickRandom ':') (array 'One' 'Two' 'Three')}}");
String result1 = transform("{{arrayJoin (pickRandom ':') (array 'One' 'Two' 'Three')}}");
assertThat(result1, equalToCompressingWhiteSpace("One:Two:Three"));

String result2 = transform("{{join '*' (array 1 2 3)}}");
String result2 = transform("{{arrayJoin '*' (array 1 2 3)}}");
assertThat(result2, equalToCompressingWhiteSpace("1*2*3"));

String result3 = transform("{{join ' ' (array 'WireMock' 'Rocks')}}");
String result3 = transform("{{arrayJoin ' ' (array 'WireMock' 'Rocks')}}");
assertThat(result3, equalToCompressingWhiteSpace("WireMock Rocks"));

String result4 =
transform("{{join '' (array 'W' 'i' 'r' 'e' 'M' 'o' 'c' 'k' ' ' 'R' 'o' 'c' 'k' 's')}}");
transform(
"{{arrayJoin '' (array 'W' 'i' 'r' 'e' 'M' 'o' 'c' 'k' ' ' 'R' 'o' 'c' 'k' 's')}}");
assertThat(result4, equalToCompressingWhiteSpace("WireMock Rocks"));

String result5 = transform("{{join \" - * - \" (array 'One' 'Two' 'Three')}}");
String result5 = transform("{{arrayJoin \" - * - \" (array 'One' 'Two' 'Three')}}");
assertThat(result5, equalToCompressingWhiteSpace("One - * - Two - * - Three"));
}

Expand Down

0 comments on commit 1d3f0e6

Please sign in to comment.