Hi. I'm having a strange issue with custom strikt...
# strikt
a
Hi. I'm having a strange issue with custom strikt exctensions: I have the following extension that checks for params on a
Intent
for android:
Copy code
inline fun <reified T : Parcelable> Assertion.Builder<Intent>.hasParams(
  paramsName: String,
  noinline block: Assertion.Builder<T>.() -> Unit = {},
): Assertion.Builder<Intent> {
  return assert("has params %s", expected = paramsName) {
    val params = it.extras?.getParcelable<T>(paramsName)
    if (params != null) {
      expectThat(params).isA<T>().and(block)
    } else {
      fail()
    }
  }
}
When I use this extension, whatever check I put on following lines are marked ass successfull, but those should fail:
Copy code
@Test
  fun testHasParamsSuccess() {
    val intent = Intent(getApplicationContext(), SelectCountryActivity::class.java)
      .putExtra("param1", TestClass(id = "ABCD"))

    expectThat(intent)
      .hasParams<TestClass>("param1") { get { id }.isEqualTo("ABCD") }
      .assertThat("Simple") { false } // Here assertThat { false } must fail
  }
What I'm doing wrong with the
hasParams
extension?
r
You should’t use
expectThat
inside — that creates an entire new assertion chain rather than contributing to the one you’re working on. Gimme a sec & I’ll see if I can work out the correct implementation
I think you can just implement the
hasParams
assertion as
Copy code
get { extras.getParcelable<T>(paramsName) }.isNotNull().isA<T>().and(block)
a
and add
return this
at the end?
r
yep
This is the 2nd time this has come up recently. I think I need to improve the docs on this. I wonder if I can even make it fail with a clear message if you do that.
a
that would be awesome
r
It is confusing
a
thanks, your code works very well! I've even made a test to cover the "false positive" case
👍 1
r
Thanks. Glad it’s useful for you
a
BTW @robfletcher, would a "strikt-android-intents" fit in your base code?, as a separate module of course, but provided a side of the libraries?
r
maybe. What’s the “intents” part of that? (I know nothing about Android dev)
a
uhmmm, Intents are responsible of launch different parts of the android app, or even between apps
whatever ,a module
strik-android
could be a good addition
r
oh, ok, I thought you maybe meant intents in the sense of some kind of alternative approach to doing assertions or something
yes, an Android library would be 100% super-useful to have in Strikt
I’ve just added new modules as I’ve personally needed them at work. The
strikt-arrow
and
strikt-gradle
modules were community contributions, tho.
a
Ok, will try to