
98
|
第
5
章
products.filter { it.onSale }
.map { it.name }
.joinToString(separator = ", ")
.ifEmpty { "none" }
➋
➊
默认为空集合。
➋
默认为空字符串。
无论哪种情况,非销售产品的集合都将返回字符串“none”。参见示例 5-16 中的测试。
示例 5-16:测试产品
class IfEmptyOrBlankKtTest {
private val overthruster = Product("Oscillation Overthruster", 1_000_000.0)
private val fluxcapacitor = Product("Flux Capacitor", 299_999.95, onSale = true)
private val tpsReportCoverSheet = Product("TPS Report Cover Sheet", 0.25)
@Test
fun productsOnSale() {
val products = listOf(overthruster, fluxcapacitor, tpsReportCoverSheet)
assertAll( "On sale products",
{ assertEquals("Flux Capacitor",
onSaleProducts_ifEmptyCollection(products)) ...