@@ -75,6 +75,10 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
75
75
val tupleFlatten = createStringBuilder(packageName)
76
76
.append(" import kotlin.jvm.JvmName\n\n " )
77
77
78
+ val varargToList = StringBuilder (dontModifyNotice)
79
+ .append(" @file:Suppress(\" MethodOverloading\" , \" FunctionName\" )\n " )
80
+ .append(" package " ).append(packageName).append(" \n\n " )
81
+
78
82
val toVararg = createStringBuilder(packageName)
79
83
.append(" import kotlin.jvm.JvmName\n\n " )
80
84
@@ -327,6 +331,32 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
327
331
}
328
332
}
329
333
334
+ varargToList.append(
335
+ """
336
+ |/**
337
+ | * Adds the given [arg] and the [otherArgs] into a new [List] and returns it.
338
+ | *
339
+ | * This function is intended for API functions which expect `x: T, vararg otherX: T` and want to pass the arguments
340
+ | * to another function which expects only one argument of `List<T>`.
341
+ | *
342
+ | * @return a [List] containing [arg] and [otherArgs].
343
+ | */
344
+ |fun <T> varargToList(arg: T, otherArgs: Array<out T>): List<T> {
345
+ | val list = ArrayList<T>(otherArgs.size + 1)
346
+ | list.add(arg)
347
+ | list.addAll(otherArgs)
348
+ | return list
349
+ |}
350
+ |
351
+ |/**
352
+ | * Delegates to [varargToList] -- adds `this` and the [otherArgs] into a new [List] and returns it.
353
+ | */
354
+ |@Suppress("NOTHING_TO_INLINE")
355
+ |inline infix fun <T> T.glue(otherArgs: Array<out T>): List<T> = varargToList(this, otherArgs)
356
+ |
357
+ """ .trimMargin()
358
+ ).appendLine()
359
+
330
360
listOf (" Array" , " Iterable" , " Sequence" ).forEach { receiver ->
331
361
toVararg.append(
332
362
"""
@@ -345,6 +375,37 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
345
375
}
346
376
347
377
primitiveTypes.forEach { (type, arrayType) ->
378
+ varargToList.append(
379
+ """
380
+ |/**
381
+ | * Adds the given [arg] and the [otherArgs] into a new [List] and returns it.
382
+ | *
383
+ | * This function is intended for API functions which expect `x: $type , vararg otherX: $type ` and want to pass
384
+ | * the arguments to another function which expects only one argument of `List<$type >`.
385
+ | *
386
+ | * @return a [List] containing [arg] and [otherArgs].
387
+ | *
388
+ | * @since 3.1.0
389
+ | */
390
+ |fun varargToList(arg: $type , otherArgs: $arrayType ): List<$type > {
391
+ | val list = ArrayList<$type >(otherArgs.size + 1)
392
+ | list.add(arg)
393
+ | list.addAll(otherArgs.asList())
394
+ | return list
395
+ |}
396
+ |
397
+ |/**
398
+ | * Delegates to [varargToList] -- adds `this` and the [otherArgs] into a new [List] and returns it.
399
+ | *
400
+ | * @since 3.1.0
401
+ | */
402
+ |@Suppress("NOTHING_TO_INLINE")
403
+ |inline infix fun $type .glue(otherArgs: $arrayType ): List<$type > = varargToList(this, otherArgs)
404
+
405
+ """ .trimMargin()
406
+ ).appendLine()
407
+
408
+
348
409
listOf (" Iterable" , " Array" ).forEach { receiver ->
349
410
toVararg.append(
350
411
"""
@@ -410,6 +471,9 @@ val generate: TaskProvider<Task> = tasks.register("generate") {
410
471
val tupleFlattenFile = packageDir.resolve(" tupleFlatten.kt" )
411
472
tupleFlattenFile.writeText(tupleFlatten.toString())
412
473
474
+ val varargToListFile = packageDir.resolve(" varargToList.kt" )
475
+ varargToListFile.writeText(varargToList.toString())
476
+
413
477
val toVarargFile = packageDir.resolve(" toVararg.kt" )
414
478
toVarargFile.writeText(toVararg.toString())
415
479
}
@@ -451,23 +515,31 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
451
515
val tupleFactoryTest = createStringBuilder(packageName)
452
516
.appendTest(" TupleFactoryTest" )
453
517
518
+ val varargToListTest = createStringBuilder(packageName)
519
+ .appendTest(" VarargToListTest" )
520
+
454
521
val toVarargTest = createStringBuilder(packageName)
455
522
.appendTest(" ToVarargTest" )
456
523
457
- toVarargTest .append(
524
+ varargToListTest .append(
458
525
"""
459
- | fun expectString(s: String, vararg others: String) {}
460
- | fun expectBoolean(first: Boolean, vararg others: Boolean) {}
461
- | fun expectByte(first: Byte, vararg others: Byte) {}
462
- | fun expectChar(first: Char, vararg others: Char) {}
463
- | fun expectShort(first: Short, vararg others: Short) {}
464
- | fun expectInt(first: Int, vararg others: Int) {}
465
- | fun expectLong(first: Long, vararg others: Long) {}
466
- | fun expectFloat(first: Float, vararg others: Float) {}
467
- | fun expectDouble(first: Double, vararg others: Double) {}
468
- |
469
- """ .trimMargin()
470
- ).appendLine()
526
+ | @Test
527
+ | fun varArgToList_array_of_strings() {
528
+ | val arr = arrayOf("a", "b")
529
+ | val list = varargToList("c", arr)
530
+ |
531
+ | expect(list).toContainExactly("c", "a", "b")
532
+ | }
533
+ |
534
+ | @Test
535
+ | fun glue_array_of_strings() {
536
+ | val arr = arrayOf("a", "b")
537
+ | val list = "c" glue arr
538
+ |
539
+ | expect(list).toContainExactly("c", "a", "b")
540
+ | }
541
+ """ .trimMargin()
542
+ ).appendLine().appendLine()
471
543
472
544
listOf (
473
545
" Array" to " arrayOf" ,
@@ -506,6 +578,26 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
506
578
" Double" -> " 1.0" to " 2.0, 3.0"
507
579
else -> throw IllegalStateException (" not all primitiveTypes cases covered: $type " )
508
580
}
581
+ varargToListTest.append(
582
+ """
583
+ | @Test
584
+ | fun varArgToList_$arrayType () {
585
+ | val arr = ${arrayType} Of($value2 )
586
+ | val list = varargToList($value1 , arr)
587
+ |
588
+ | expect(list).toContainExactly($value1 , $value2 )
589
+ | }
590
+ |
591
+ | @Test
592
+ | fun glue_$arrayType () {
593
+ | val arr = ${arrayType} Of($value2 )
594
+ | val list = $value1 glue arr
595
+ |
596
+ | expect(list).toContainExactly($value1 , $value2 )
597
+ | }
598
+ """ .trimMargin()
599
+ ).appendLine().appendLine()
600
+
509
601
toVarargTest.append(
510
602
"""
511
603
| @Test
@@ -517,8 +609,8 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
517
609
| expect$type (first, *rest)
518
610
|
519
611
| expect(pair) {
520
- | this.first.toEqual($value1 )
521
- | second.asList().toContainExactly($value2 )
612
+ | this.first.toEqual($value1 )
613
+ | second.asList().toContainExactly($value2 )
522
614
| }
523
615
| }
524
616
""" .trimMargin()
@@ -841,6 +933,11 @@ val generateTest: TaskProvider<Task> = tasks.register("generateTest") {
841
933
val factoryTestFile = packageDir.resolve(" TupleFactoryTest.kt" )
842
934
factoryTestFile.writeText(tupleFactoryTest.toString())
843
935
936
+
937
+ varargToListTest.append(" }" )
938
+ val varargToListTestFile = packageDir.resolve(" VarargToListTest.kt" )
939
+ varargToListTestFile.writeText(varargToListTest.toString())
940
+
844
941
toVarargTest.append(" }" )
845
942
val toVarargTestFile = packageDir.resolve(" ToVarargTest.kt" )
846
943
toVarargTestFile.writeText(toVarargTest.toString())
0 commit comments