Skip to content

Instantly share code, notes, and snippets.

fun <T> Iterable<T>.foreach(action: T.() -> Unit) {
for (element in this) element.action()
}
listOf(42).foreach { inc() }
class Log {
var level: Level = Level.DEBUG
var tag: String? = null
var throwable: Throwable? = null
var message: String? = null
}
inline fun tag(crossinline tag: () -> String) {
this.tag = tag.invoke()
}
inline fun throwable(crossinline throwable: () -> Throwable) {
this.throwable = throwable.invoke()
}
inline fun level(crossinline level: () -> Level) {
this.level = level.invoke()
inline fun info(crossinline message: () -> String?) {
level { Level.INFO }
this.message = message.invoke()
}
object Logger {
@JvmStatic
inline fun log(crossinline configure: Log.() -> Unit) {
if (isWritersAvailable) {
Log().let {
configure(it)
write(it)
}
}
log {
level { WARNING }
tag { "MainActivity" }
message { "Something went wrong" }
}
log {
throwable {
UnsupportedOperationException()
}
@Test
fun `test scenario dsl example`() = scenario {
var socket: WebSocket? = null
define {
`return mock socket when call factory`()
}
act {
socket = mockWebSocketFactory.invoke()
`do something important`()
}
repositories {
jcenter()
}
dependencies {
compile "dmax.dsl:logger:0.1"
compile "dmax.dsl:logger-android:0.1@aar"
compile "dmax.dsl:logger-slf4j:0.1"
}
<dependency>
<groupId>dmax.dsl</groupId>
<artifactId>logger</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>dmax.dsl</groupId>
<artifactId>logger-android</artifactId>
<version>0.1</version>