net.twisterrob.gradle icon indicating copy to clipboard operation
net.twisterrob.gradle copied to clipboard

Shorten test constants

Open TWiStErRob opened this issue 4 years ago • 1 comments
trafficstars

And maybe introduce trimIndent.

@Language(lang)
val x = """
...
"""
val x = lang("""
...
""").trimIndent()

Could bring it even farther

val x = text(/*indent = false,*/ xml = """
""")

This wouldn't work with default/overloaded methods, but it works if there's an extra param:

object Xml
object Java
fun text(xml: String, indent: Boolean = false, type: Xml = Xml)
fun text(java: String, indent: Boolean = false, type: Java = Java)

This way the named parameter will choose the overload.

TWiStErRob avatar Nov 22 '21 19:11 TWiStErRob

Based on https://stackoverflow.com/a/37394267/253468

class Xml private constructor()
class Java private constructor()

fun text(@Language("xml") xml: String, vararg type: Xml, indent: Boolean = false) {}
fun text(@Language("java") java: String, vararg type: Java, indent: Boolean = false) {}

fun f() {
	text(java = "class X {}")
	text(xml = "<foo></bar>")
	text(xml = "<foo></bar>", indent = false)
	text("<foo></bar>", false) // fails -> good
}

but not sure why I thought calling the fun text is a good idea; this feels much cleaner right now:

java("class X {}")
java("class X {}", indent = false)

TWiStErRob avatar Dec 17 '21 23:12 TWiStErRob