require '../spec_helper'

describe "Freakset Acceptance", ->
  
  beforeEach ->
    @freakset = new Freakset()
    @set = null


  it "should compile a Simple Rescued Freakset", ->
    errFn = jasmine.createSpy("ErrFn")
  
    @freakset.rescue "rescue:main", errFn, (innerSet) ->
    
      innerSet.step "step1", (set) ->
        set.store['steps'].push("step1")
        set.commit()
  
      innerSet.step "step2", ->
        throw 42
  
  
    @set = @freakset.compile { steps: [] }
    
    waitsFor -> !@set.isRunning()
    
    runs ->
      expect(@set.store['steps']).toContain("step1")
      expect(errFn).toHaveBeenCalled()
   
  
  it "should compile a Nested Rescued Freakset", ->

    errFn = jasmine.createSpy("ErrFn")
    innerErrFn = jasmine.createSpy("InnerErrFn")
  
    @freakset.rescue "rescue:main", errFn, (innerSet) =>
  
      innerSet.step "step1", (set) ->
        set.store['steps'].push("step1")
        set.commit()
       
      innerSet.rescue "rescue:inner", innerErrFn, (innerInnerSet) =>
      
        innerInnerSet.step "step1.1", (set) ->
          set.store['steps'].push("step1.1")
          set.commit()
  
        innerInnerSet.step "step1.2", ->
          throw 23
  
      innerSet.step "step2", (set) ->
        set.store['steps'].push("step2")
        set.commit()
  
  
    @set = @freakset.compile { steps: [] }
  
    waitsFor -> !@set.isRunning()
  
    runs -> 
      expect(@set.store['steps']).toContain("step1")
      expect(@set.store['steps']).toContain("step1.1")
      expect(@set.store['steps']).toContain("step2")
      expect(innerErrFn).toHaveBeenCalled()
      expect(errFn).not.toHaveBeenCalled()
  
   
  it "should compile a Deep Nested Rescued Freakset", ->

    errFn = jasmine.createSpy("ErrFn")
    innerErrFn = jasmine.createSpy("InnerErrFn")
    innerInnerErrFn = jasmine.createSpy("InnerInnerErrFn")
  
    @freakset.rescue "rescue:main", errFn, (innerSet) =>
  
      innerSet.step "step1", (set) ->
        set.store['steps'].push("step1")
        set.commit()
  
      innerSet.rescue "rescue:inner", innerErrFn, (innerInnerSet) =>
  
        innerInnerSet.step "step1.1", (set) ->
          set.store['steps'].push("step1.1")
          set.commit()
        
        innerInnerSet.rescue "rescue:inner:inner", innerInnerErrFn, (innerInnerInnerSet) =>
          innerInnerInnerSet.step "step1.1.1", ->
            throw 82
          
        innerInnerSet.step "step1.2", (set) ->
          set.store['steps'].push("step1.2")
          set.commit()
  
      innerSet.step "step2", (set) ->
        set.store['steps'].push("step2")
        set.commit()
  
  
    @set = @freakset.compile { steps: [] }
  
    waitsFor -> !@set.isRunning()
  
    runs -> 
      expect(@set.store['steps']).toContain("step1")
      expect(@set.store['steps']).toContain("step1.1")
      expect(@set.store['steps']).toContain("step1.2")
      expect(@set.store['steps']).toContain("step2")
      expect(innerInnerErrFn).toHaveBeenCalled()
      expect(innerErrFn).not.toHaveBeenCalled()
      expect(errFn).not.toHaveBeenCalled()
