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

Fix #351: Provide a Varargs formatter and @VarargsQuoted annotation #352

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tngtech.jgiven.annotation;

import com.tngtech.jgiven.format.PrintfFormatter;
import com.tngtech.jgiven.format.VarargsFormatter;

import java.lang.annotation.*;

/**
* Varargs step parameters annotated with this annotation will be put into quotes (" ") in reports.
*
*/
@Documented
@Format( value = VarargsFormatter.class, args = "\"%s\"" )
@Retention( RetentionPolicy.RUNTIME )
@Target( { ElementType.PARAMETER, ElementType.ANNOTATION_TYPE, ElementType.FIELD } )
public @interface VarargsQuoted {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.tngtech.jgiven.format;

/**
* Argument formatter for varargs delegating to {@link PrintfFormatter}
*/
public class VarargsFormatter implements ArgumentFormatter<Object> {

private final PrintfFormatter formatter = new PrintfFormatter();
private final String delimiter;

public VarargsFormatter() {
this(", ");
}

public VarargsFormatter(final String delimiter) {
this.delimiter = delimiter;
}

@Override
public String format(final Object argumentToFormat, final String... formatterArguments) {
if (argumentToFormat instanceof Object[]) {
final StringBuilder buffer = new StringBuilder();
final Object[] elements = (Object[]) argumentToFormat;
for (int i = 0; i < elements.length; i++) {
buffer.append(formatter.format(elements[i], formatterArguments));
if (i < elements.length - 1) {
buffer.append(delimiter);
}
}
return buffer.toString();
}
return formatter.format(argumentToFormat, formatterArguments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.tngtech.jgiven.format;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.assertj.core.api.Java6Assertions.assertThat;

public class VarargsFormatterTest {

@Rule
public ExpectedException thrown = ExpectedException.none();
private VarargsFormatter formatter;


@Before
public void setup() {
this.formatter = new VarargsFormatter();
}

@Test
public void testVarargsFormatSingle() {
assertThat(formatter.format("value", "\"%s\"")).isEqualTo("\"value\"");
}

@Test
public void testVarargsFormatSingleVarargs() {
String[] varargs = {"value"};
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"value\"");
}

@Test
public void testVarargsFormatMultiple() {
Object[] varargs = {"value1", 1L};
assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"value1\", \"1\"");
}

@Test
public void testVarargsFormatNoFormatArgs() {
String[] varargs = {"value"};

thrown.expect(ArrayIndexOutOfBoundsException.class);
formatter.format(varargs);
}

@Test
public void testVarargsFormatNull() {
String[] varargs = null;

assertThat(formatter.format(varargs, "\"%s\"")).isEqualTo("\"null\"");
}

}