Daniel Pitts
04/09/2024, 4:17 PM@ParameterizedTest(name = "{0} radians is {1}")
@CsvSource(
"NORTH, 3π/2",
"EAST, 0",
"SOUTH, π/2",
"WEST, π",
)
fun radians(direction: Direction, @ConvertWith(NPiOverMConverter::class) expected: Double) {
assertEquals(expected, direction.radians)
}
companion object {
private val pattern = Regex("""(\d+)?(π)?(?:/(\d+))?""")
class NPiOverMConverter : ArgumentConverter {
override fun convert(value: Any?, context: ParameterContext): Any {
if (value !is String) {
throw IllegalArgumentException("Invalid value: $value")
}
val (n, pi, m) = requireNotNull(pattern.matchEntire(value)) {
"Invalid value: $value"
}.destructured
val nValue = n.toDoubleOrNull() ?: 1.0
val mValue = m.toDoubleOrNull() ?: 1.0
val piValue = if (pi == "π") Math.PI else 1.0
return nValue * piValue / mValue
}
}
}
Luke
04/09/2024, 8:10 PMArgumentConverter
into a generic type so that the convert
function takes and return a specific type?Daniel Pitts
04/09/2024, 8:51 PMCLOVIS
04/10/2024, 5:04 AMradians
function only asserts its inputs. Unless the system-under-test is the argument converter? But that's strange, it looks like machinery, not actual code.Daniel Pitts
04/10/2024, 3:53 PMDirection
enum. I'm validating the radians value is calculated properly.Daniel Pitts
04/10/2024, 3:54 PMDirection
class.CLOVIS
04/15/2024, 7:41 AMparameterize {
val data by parameterOf(
Direction.NORTH to (3 * PI / 2),
Direction.EAST to (0.0),
Direction.SOUTH to (PI / 2),
Direction.WEST to (PI),
)
val (direction, expected) = data
test("$direction radians is $expected") {
assertEquals(expected, direction.radians)
}
}
As you can see, there is no processing whatsoever. The test just tests the data.
But honestly, even this seems overkill. If I were in your situation, I may not even use any parametrization at all and just write four tests, they're one-liners anyway.
Also, I'm suspicious of using assertEquals
on Double
, especially when PI
is involved.