Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouped assertions with and pass although one of the assertions failed #231

Closed
hasnaeFadili opened this issue Oct 23, 2020 · 1 comment
Closed

Comments

@hasnaeFadili
Copy link

I need to define custom assertions on a complexe object (I used here Pair just for demonstration), while combining this custom assertion with and the test pass although not all assertions are corrects:

here is an example to reproduce the issue:

@Test
    fun testCustomAssertions(){
        val result = 1 to "test"
        expectThat(result).and {
            isEqualTo(3 to "test")
            hasLength(4)
        }
    }
fun Assertion.Builder<Pair<Int, String>>.hasLength(expected: Int): Assertion.Builder<Pair<Int, String>> =
        assert("Has length equal to %s", expected) {
            expect {
                that(it).get { second }
                    .isNotBlank()
                    .and {
                        get { length }.isEqualTo(expected)
                    }
            }
        }
@robfletcher robfletcher added 🐛 bug Something isn't working and removed 🐛 bug Something isn't working labels Nov 3, 2020
@robfletcher
Copy link
Owner

robfletcher commented Nov 3, 2020

I think the issue here is that you should not use expect / that in that way inside a custom assertion. You're basically creating an entirely new assertion at that point.

This is pretty confusing. Strikt should prevent this, ideally at compile time (although I'm not sure how that would be feasible) but certainly at least at runtime.

If I rewrite your custom assertion like this it works as expected:

fun Assertion.Builder<Pair<Int, String>>.hasLength(expected: Int): Assertion.Builder<Pair<Int, String>> =
  compose("Has length equal to %s", expected) {
    get { second }
      .isNotBlank()
      .and {
        get { length }.isEqualTo(expected)
      }
  } then {
    if (allPassed) pass() else fail()
  }

robfletcher added a commit that referenced this issue Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants