Atrium: Result.isFailure

Created on 10 Oct 2019  ·  10Comments  ·  Source: robstoll/atrium

Platform (jvm, js, android): jvm
Extension (none, kotlin 1.3, jdk8): kotlin 1.3

Code related feature

expect(Result.success(1)).isFailure<IllegalArgumentException>()
expect(Result.success(1)).isFailure<IllegalArgumentException> {
  messageContains("hello world")
}

//instead of

expect(Result.success(1)).feature { f(it::exceptionOrNull) }.isA<IllegalArgumentException>()
expect(Result.success(1)).feature({ f(it::exceptionOrNull) }.isA<IllegalArgumentException> {
    isGreaterThan(1)  
    isLessThan(5)
}

However in reporting we want to see for

expect(Result.success(1)).isFailure<IllegalArgumentException>()

the following

expect: Success(1)
◆ ▶ exception: ❗❗ is not a Failure
  >> is instance of type: IllegalArgumentException

And for

expect(Result.success(1)).isFailure<IllegalArgumentException> {
  messageContains("hello world")
}

the following

expect: Success(1)
◆ ▶ exception: ❗❗ is not a Failure
  >> is instance of type: IllegalArgumentException
  >> message:
     >> contains: "hello world"

And for

expect(Result.failiure<Int>(UnsupportedOpertionException("NOP")))
  .isFailure<IllegalArgumentException> { messageContains("hello world") }

the following

expect: Failure(java.lang.UnsupportedOpertionException: NOP)
◆ ▶ exception: UnsupportedOpertionException: NOP
  >> is instance of type: IllegalArgumentException
  >> message:
     >> contains: "hello world"

And for

expect(Result.failiure<Int>(UnsupportedOpertionException("NOP")))
  .isFailure<IllegalArgumentException> { messageContains("hello world") }

the following

expect: Failure(java.lang.IllegalArgumentException: NOP)
◆ ▶ exception: IllegalArgumentException: NOP
  >> message:
     >> contains: \"hello world\"

Following the things you need to do:

lib

  • implement _isFailure in resultAssertions returning ChangedSubjectPostStep<Throwable?, TExpected> where TExpected: Throwable by two steps (see fun0Assertions.kt -> isThrowing):

    1. extract exceptionOrNull manually (use exception as representation) with a custom representation in case it is null (via withOptions)



      • add IS_NOT_FAILURE("❗❗ is not a failure") to DescriptionResultAssertions for en_GB


      • add IS_NOT_FAILURE("❗❗ ist kein Fehler") to DescriptionResultAssertions for de_CH



    2. down-cast it to the corresponding TExpected (pass expectedType: KClass<TExpected> as second argument) - use also the ThrowableThrownFailureHandler as for fun0Assertions.kt -> isThrowing

domain

  • extend ResultAssertions with a function isFailure which returns ChangedSubjectPostStep<Throwable?, TExpected> where TExpected: Throwable (see isSuccess as a guideline)
  • modify ResultAssertionsBuilder, delegate to resultAssertions (see isSuccess as a guideline)
  • delegate implementation to robstoll-lib in ResultAssertionsImpl (see isSuccess as a guideline)

api

  • provide a val which returns Expect (see anyAssertions.kt -> isA as a guideline)
  • provide a fun which expects an assertionCreator-lambda and returns Expect (see anyAssertions.kt -> isA and fun0Assertions.kt -> toThrow as a guideline)
  • add @since 0.9.0 (adopt to next release version) to KDOC of val and fun
  • add isFailure to ResultFeatureAssertionsSpec in specs-common (you can reuse the corresponding describe and context, see TODOs -- no need to cover the nullable case within the specs) and extend it in atrium-api-fluent-en_GB-common/src/test
  • add isFailure to the ambiguityTest in ResultFeatureAssertionsSpec of atrium-api-fluent-en_GB-common and show that isFailure also compiles for a nullable Result type (e.g. Result<Int?>)

Your first contribution?

  • Write a comment I'll work on this if you would like to take this issue over.
    This way we get the chance to revise the description in case things have changed in the meantime,
    we might give you additional hints and we can assign the task to you, so that others do not start as well.
  • See Your first code contribution for guidelines.
  • Do not hesitate to ask questions here or to contact us via Atrium's slack channel if you need help
    (Invite yourself in case you do not have an account yet).
good first issue help wanted

All 10 comments

I will work on this

Please only one at a time so that other people taking part in hacktoberfest have a chance to deliver as well. Be fast with #203 and you can pick up this one as well :wink:

i will work on it.

@Anubhav007 do you need help? Un-assigning you so that others can take over

I could work on this

@robstoll, in the instructions, you mention that isFailure() should return ChangedSubjectPostStep but then you also say that its implementation should use ExpectImpl.feature.extractor... which returns an ExtractedFeaturePostStep.
Does this mean that the method's implementation should, first, use ExpectImpl.feature.extractor... to extract the exception from the Result, and then, use ExpectImpl.changeSubject... to assert the specified exception is indeed the one that was thrown?

@johnGachihi I probably have updated the description to use feature.extractor without changing the return type. Updated it now. I'll merge part of https://github.com/robstoll/atrium/pull/270 later this evening in order that you don't have to repeat this work. Have a look at it in case you want extra hints, it's very similar to your issue.

@johnGachihi please rebase on master, I have merged Result.isSuccess (see 6ef99c01 for details) and made some cleanup and added some TODOs for you.

I was actually wrong in my last comment, the domain will return ChangedSubjectPostStep as we need to narrow the subject down to the specific exception type (kind of what you expected).

I have updated the description again and I have also changed the steps in the description. It makes probably more sense to first implement the lib and then build on the upper layers based on it.

Thanks. The description makes better sense now.

implemented with 163071b

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robstoll picture robstoll  ·  3Comments

robstoll picture robstoll  ·  5Comments

robstoll picture robstoll  ·  6Comments

robstoll picture robstoll  ·  6Comments

robstoll picture robstoll  ·  3Comments