wiremock / wiremock

A tool for mocking HTTP services
https://wiremock.org/
Apache License 2.0
6.19k stars 1.41k forks source link

Join - Handlebar helper #2725

Closed dieppa closed 1 month ago

dieppa commented 1 month ago

Requested issue: https://github.com/wiremock/wiremock/issues/2710

leeturner commented 1 month ago

This all looks super cool. Really like it. Very useful helper. While I was reviewing I noticed that the tests were limited to joining only with , which might be the biggest usecase for this helper to be fair. I wondered whether adding a few more variants would be good:

  @Test
  public void joinWithArrayOfInts() {
    String result = transform("{{join '*' (array 1 2 3)}}");

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

  @Test
  public void joinWithBlankSeparator() {
    String result = transform("{{join ' ' (array 'WireMock' 'Rocks')}}");

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

  @Test
  public void joinWithEmptySeparatorAndArrayOfChars() {
    String result = transform("{{join '' (array 'W' 'i' 'r' 'e' 'M' 'o' 'c' 'k' ' ' 'R' 'o' 'c' 'k' 's')}}");

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

  @Test
  public void joinWithArrayOfStringsAndStringSeparator() {
    String result = transform("{{join \" - * - \" (array 'One' 'Two' 'Three')}}");

    assertThat(result, equalToCompressingWhiteSpace("One - * - Two - * - Three"));
  }

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

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

And also, because one of the usecases we want from this is to be able to more easily generate valid json I would probably update this test just to make it output valid json:

  @Test
  public void joinWithObjectBody() {
    String result =
        transform(
            "{{#parseJson 'myThings'}}\n"
                + "[\n"
                + "  { \"id\": 1, \"name\": \"One\" },\n"
                + "  { \"id\": 2, \"name\": \"Two\" },\n"
                + "  { \"id\": 3, \"name\": \"Three\" }\n"
                + "]\n"
                + "{{/parseJson}}"
                + "[{{#join ',' myThings as |item|}}"
                + "{\n"
                + "\"name{{item.id}}\": \"{{item.name}}\"\n"
                + "}\n"
                + "{{/join}}]");

    assertThat(result, equalToCompressingWhiteSpace("[{\"name1\": \"One\"},{\"name2\": \"Two\"},{\"name3\": \"Three\"}]"));
  }
tomakehurst commented 1 month ago

Agree with @leeturner that some other variations in the tests would be good. Otherwise I'm happy with this.

dieppa commented 1 month ago

Yeah, I agree, adding some variations in the separator 👌🏼

dieppa commented 1 month ago

I added all the test for multiple separators in a unique test method. Normally I don't like this, but the code looks clear and concise 😃

tomakehurst commented 1 month ago

Can we have a better name for the PR before merging, for the sake of the release note?