grappa-py / grappa

Behavior-oriented, expressive, human-friendly Python assertion library for the 21st century
http://grappa.rtfd.io
MIT License
134 stars 15 forks source link

Combinating composition and conditional operators does not work #41

Open jcassee opened 6 years ago

jcassee commented 6 years ago

Not sure if this is expected to work, but I had hoped to combine composition and conditional operators:

from grappa import *

{1: {2: 3}} | should.have.key(1) > should.have.key(2) > should.equal(3)

{1: {2: 3}} | should.have.key(1) > should.all(should.have.key(2) > should.equal(3))
AssertionError: Oops! Something went wrong!

  The following assertion was not satisfied
    subject "Empty" should equal 3

  What we expected
    a value that is equal to "3"

  Difference comparison
    > - Empty
    > + 3

  Where
    File "/home/joost/Ontwikkeling/GoAbout/pacioli/grappa-test.py", line 7, in <module>

     1|   #!/usr/bin/env python
     2|   
     3|   from grappa import *
     4|   
     5|   {1: {2: 3}} | should.have.key(1) > should.have.key(2) > should.equal(3)
     6|   
     7| > {1: {2: 3}} | should.have.key(1) > should.any(should.have.key(2) > should.equal(3))
     8|   
h2non commented 6 years ago

Because of the operator precedence in Python and some inherent complexity with lazy evaluation in such case, it's not currently possible using > yield test subject operator in conditional composition. I think I can find a way to make it work, but I can't spend time on that right now.

The recommended style here that actually works is using the yield chain operator instead:

{1: {2: 3}} | should.have.key(1) > should.any(
   should.have.key(2).that.should.be.equal(3),
   should.have.key(2).that.should.be.empty)
jcassee commented 6 years ago

Thanks, Tomás, I can use that.