https://kotlinlang.org logo
#atrium
Title
# atrium
r

robstoll

10/24/2020, 4:23 PM
The name of the function is not the best as you proved. In case someone has a better name then please share your idea
j

jdornieden

10/25/2020, 11:43 AM
If I understand this function correctly, it’s only needed to handle nulls when using assertion creator lambdas. Instead of renaming: What do you think about adding a “toBeNull()” and extend “expectLambda()” to accept Expect<T?>. Null wouldn’t be a special case anymore and you could just use:
_expect_("calling myNullableFun with ...") *{*
*   *_mapOf_(
     
Int.MIN_VALUE _to expectLambda_<String> *{* _contains_("min") *}*,
     
-1 _to expectLambda_ *{* toBeNull() *}*,
     
0 _to expectLambda_ *{* toBeNull() *}*,
     
1 _to expectLambda_ *{* _toBe_("1") *}*,
     
2 _to expectLambda_ *{* _endsWith_("2") *}*,
     
Int.MAX_VALUE _to expectLambda_ *{* _toBe_("max") *}*
*   *)._forEach_ *{* (arg, assertionCreatorOrNull) *->*
*     *_feature_ *{* f(::myNullableFun, arg) *}*
   
}
}
r

robstoll

10/25/2020, 12:43 PM
As info, you can already use
toBe(null)
How are you going to use the assertionCreatorOrNull in your example, you missed to show it? Actually,
expectLambda
should already accept
T
without any upper bound which is the same as
T: Any?
If that's not the case, then we should fix it
j

jdornieden

10/25/2020, 1:07 PM
Yeah, you’re right. That was a copy & paste error:
Copy code
expect("calling myNullableFun with ...") {
   mapOf(
     Int.MIN_VALUE to expectLambda<String> { contains("min") },
     -1 to expectLambda { toBeNull() },
     0 to expectLambda { toBeNull() },
     1 to expectLambda { toBe("1") },
     2 to expectLambda { endsWith("2") },
     Int.MAX_VALUE to expectLambda { toBe("max") }
   ).forEach { (arg, assertionCreator) ->
     feature( { f(::myNullableFun, arg) }, assertionCreator)
   }
}
Basically I wondered if it is necessary to have a special treatment for null or if it was possible to combine the two Data Driven Testing examples (Readme) into one simpler example.
The current signature of expectLambda is “fun <T> expectLambda(noinline assertionCreator: Expect<T>.() -> Unit)“, so it’s not possible to use it with nullable types at the moment. I think the same applies to toBe.
r

robstoll

10/25/2020, 1:16 PM
It is,
T
means
T: Any?
, you should be able to write:
Copy code
expectLambda<Int?> { toBe(null) }
No?
j

jdornieden

10/25/2020, 1:48 PM
Yes, you are right. I forget to change the type to String? in the example...🙄 Here is the already working example:
Copy code
expect("calling myNullableFun with ...") {
   mapOf(
     Int.MIN_VALUE to expectLambda<String?> { notToBeNull().contains("min") },
     -1 to expectLambda { toBe(null) },
     0 to expectLambda { toBe(null) },
     1 to expectLambda { toBe("1") },
     2 to expectLambda { notToBeNull().endsWith("2") },
     Int.MAX_VALUE to expectLambda { toBe("max") }
   ).forEach { (arg, assertionCreator) ->
     feature( { f(::myNullableFun, arg) }, assertionCreator)
   }
}
3 Views