Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to pass newlines as a parameter (dupe of #30) #146

Open
coopstah13 opened this issue Dec 6, 2017 · 4 comments
Open

Unable to pass newlines as a parameter (dupe of #30) #146

coopstah13 opened this issue Dec 6, 2017 · 4 comments

Comments

@coopstah13
Copy link

According to issue #30 the workaround for passing in newlines is to use a class provider. I'm using class providers nearly exclusively and my trailing newline is still trimmed.

    @SuppressWarnings("unused")
    public static Object[] provideInvalidValues() {
        List<String> invalidValues = new ArrayList<>();

        invalidValues.addAll(BlankStringProvider.BLANK_STRINGS);

        invalidValues.add("1a"); // starts with a number
        invalidValues.add("a-"); // contains a hyphen
        invalidValues.add("a\n"); // contains a new line
        invalidValues.add("a\r"); // contains a carriage return
        invalidValues.add("a\nb"); // contains a new line
        invalidValues.add("a\rb"); // contains a carriage return
        invalidValues.add("a 1"); // contains a space
        invalidValues.add("a∫ı"); // contains funky characters
        invalidValues.add("aü"); // also contains funky characters

        return invalidValues.toArray();
    }

In this example a\n and a\r are provided in the test as just a. I don't really care about how the "test readability" looks, I just want to test the data I want to test.

@coopstah13
Copy link
Author

I did some debugging, I've traced the issue down to the InvokedParameterisedMethod class, in the constructor if the value of params is a string, then it is passed through the Utils.splitAtCommaOrPipe method, which is trimming the string

@stef2georg
Copy link

To workaround this I am using a simple String wrapper:

public final class Text {

  private final String text;

  private Text(final String text) {
    this.text = text;
  }

  public static Text of(final String text) {
    return new Text(text);
  }

  @Override
  public String toString() {
    return text;
  }

}

Then you can use it in your provider method:

public static Object[] provideInvalidValues() {
  List<String> invalidValues = new ArrayList<>();

  invalidValues.add(Text.of(" "));

  return invalidValues.toArray();
}

@aleskiewicz
Copy link

@coopstah13 you should use 2-dimensional array as result of provideInvalidValues. Something like:

  @SuppressWarnings("unused")
  public static Object[][] provideInvalidValues() {
    return new Object[]{new String[]{"a\n", "a\r"}};
  }

should be enough for your case.

@coopstah13
Copy link
Author

I'm using the wrapper class workaround. I get the 2d array works, my largest issue is that it's easy to do it wrong and you won't even know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants