sorellabs / claire

[Unmaintained: please use jsverify instead] A property-based testing library for clearly specifying code invariants and behaviour.
MIT License
79 stars 4 forks source link

Satisfy clause context should be the invocation context of function returned by asTest() #25

Closed adambaker closed 11 years ago

adambaker commented 11 years ago

Here's how I work around this limitation currently

describe 'ActivityLog', ->
  beforeEach ->
    @log = @ActivityLog.new()

  describe 'update', ->
    it 'makes the date the start of the provided date', ->
      claire.forAll(ActivityAttr).satisfy( (attrs) =>
        @log.update attrs
        dateMo = moment(@log.get('date'))
        dateMo.valueOf() == moment(moment(attrs.date)).startOf('day').valueOf()
      ).asTest()()

I should be able to say:

describe 'ActivityLog', ->
  beforeEach ->
    @log = @ActivityLog.new()

  describe 'update', ->
    it 'makes the date the start of the provided date', claire.forAll(ActivityAttr).satisfy( (attrs) ->
        @log.update attrs
        dateMo = moment(@log.get('date'))
        dateMo.valueOf() == moment(moment(attrs.date)).startOf('day').valueOf()
      ).asTest()
robotlolita commented 11 years ago

Hm, currently the satisfy function runs in the context of the property, but I guess it does make sense to have the asTest function run in the context that's passed to it. This should be an easy fix by cloning the property object in the as-test function and binding the invariant to the this object passed to the returned function. I'll work on this tomorrow.

robotlolita commented 11 years ago

Could you check out 0.4.1 and see if it works for you? :3

adambaker commented 11 years ago

great success, thanks.