TestUtils.kt (1340B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2020 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.common 18 19 import kotlin.random.Random 20 import org.junit.Assert.assertTrue 21 import org.junit.Assert.fail 22 23 private val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9') 24 fun getRandomString(minLength: Int = 1, maxLength: Int = Random.nextInt(0, 1337)) = 25 (minLength..maxLength) 26 .map { Random.nextInt(0, charPool.size) } 27 .map(charPool::get) 28 .joinToString("") 29 30 inline fun <reified T : Throwable> assertThrows( 31 msg: String? = null, 32 function: () -> Any 33 ) { 34 try { 35 function.invoke() 36 fail(msg) 37 } catch (e: Exception) { 38 assertTrue(e is T) 39 } 40 }