Skip to content
New issue

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

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

Already on GitHub? # to your account

provide VarArgFormatter #351

Open
jangalinski opened this issue Jun 21, 2018 · 9 comments
Open

provide VarArgFormatter #351

jangalinski opened this issue Jun 21, 2018 · 9 comments

Comments

@jangalinski
Copy link
Contributor

When using given().something_with_varargs_params_$("foo","bar") I want to have nice formatting of the vararg params, currently its something like "something with varargs params String@12345"

@janschaefer
Copy link
Contributor

janschaefer commented Jun 22, 2018

You can use the @Table annotation to format Arrays and varargs. See http://jgiven.org/userguide/#_tables_as_parameters

@zambrovski
Copy link

Sure, @Table is nice, but a table breaks the page flow of the test description. For short lists, a comma-separated list is adequate too.

@janschaefer
Copy link
Contributor

Ok, I understand and I agree. But then this would be just an alternative implementation to format any lists, right? So maybe it should not only format varargs, but also any kind of Iterables. It should then also be named differently, IMHO.

@janschaefer
Copy link
Contributor

What about @CommaSeparated? Maybe even with an attribute that defines the separator? @CommaSeparated( sep = ';');

janschaefer pushed a commit that referenced this issue Jul 28, 2018
@janschaefer
Copy link
Contributor

I just tried to reproduce this issue and JGiven already formats varargs parameters as a comma-separated list. See linked commit.

@zambrovski
Copy link

zambrovski commented Jul 28, 2018

I see. This seems to be because of Kotlin we are using where a varags seems to be treated as an array. My y proposed formatter was actually an array formatter. By the way, I still owe you a Kotlin example...

@janschaefer
Copy link
Contributor

I see. Still even Java arrays are formatted as a comma-separated list. Is Kotlin using some other array type as Java? A Kotlin example would help me tracking down the issue :-)

janschaefer pushed a commit that referenced this issue Jul 28, 2018
@janschaefer
Copy link
Contributor

Did you test your PR with Kotlin? Because your code uses instanceof Object[]. I assume that this will not work for the Kotlin Array type.

@zambrovski
Copy link

My actual implementation in Kotlin was:

import com.tngtech.jgiven.annotation.Format
import com.tngtech.jgiven.format.ArgumentFormatter
import com.tngtech.jgiven.format.PrintfFormatter

/**
 * Argument formatter dealing with varargs and displaying them as a comma-separated list of quoted strings.
 * Delegates on the Printf-Formatter for actual formatting.
 */
open class VarargsFormatter : ArgumentFormatter<Any> {

  private val printfFormatter = PrintfFormatter()

  override fun format(argumentToFormat: Any?, vararg formatterArguments: String?): String = if (argumentToFormat is Array<*>) {
    argumentToFormat.joinToString(", ") { printfFormatter.format(it, *formatterArguments) }
  } else {
    printfFormatter.format(argumentToFormat, *formatterArguments)
  }
}

/**
 * Annotation using the Vargarg Argument formatter.
 */
@MustBeDocumented
@Format(value = VarargsFormatter::class, args = ["\"%s\""])
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.FIELD)
annotation class QuotedVarargs

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

No branches or pull requests

3 participants