<commit_before>motion_require '../spec_helper'

class MockObservable
  attr_accessor :an_attribute
end

describe "NSRunloop aware Bacon" do
  describe "concerning `wait' with a fixed time" do
    it "allows the user to postpone execution of a block for n seconds, which will halt any further execution of specs" do
      started_at_1 = started_at_2 = started_at_3 = Time.now
      #number_of_specs_before = Bacon::Counter[:specifications]

      wait 0.5
      (Time.now - started_at_1).should.be.close(0.5, 0.5)

      wait 1 do
        (Time.now - started_at_2).should.be.close(1.5, 0.5)
        wait 1.5 do
          (Time.now - started_at_3).should.be.close(3, 0.5)
          #Bacon::Counter[:specifications].should == number_of_specs_before
        end
      end
    end
  end

  describe "concerning `wait' without a fixed time" do
    def delegateCallbackMethod
      @delegateCallbackCalled = true
      resume
    end

    it "allows the user to postpone execution of a block until Context#resume is called, from for instance a delegate callback" do
      performSelector('delegateCallbackMethod', withObject:nil, afterDelay:0.1)
      @delegateCallbackCalled.should == nil
      wait do
        @delegateCallbackCalled.should == true
      end
    end

    def delegateCallbackTookTooLongMethod
      raise "Oh noes, I must never be called!"
    end

    ## This spec adds a failure to the ErrorLog!
    # it "has a default timeout of 1 second after which the spec will fail and further scheduled calls to the Context are cancelled" do
    #   expect_spec_to_fail!
    #   performSelector('delegateCallbackTookTooLongMethod', withObject:nil, afterDelay:1.2)
    #   wait do
    #     # we must never arrive here, because the default timeout of 1 second will have passed
    #     raise "Oh noes, we shouldn't have arrived in this postponed block!"
    #   end
    # end

    ## This spec adds a failure to the ErrorLog!
    # it "takes an explicit timeout" do
    #   expect_spec_to_fail!
    #   performSelector('delegateCallbackTookTooLongMethod', withObject:nil, afterDelay:0.8)
    #   wait_max 0.3 do
    #     # we must never arrive here, because the default timeout of 1 second will have passed
    #     raise "Oh noes, we shouldn't have arrived in this postponed block!"
    #   end
    # end
  end

  describe "concerning `wait_for_change'" do
    before { @observable = MockObservable.new }

    def triggerChange
      @observable.an_attribute = 'changed'
    end

    it "resumes the postponed block once an observed value changes" do
      performSelector('triggerChange', withObject:nil, afterDelay:0)
      value = nil
      wait_for_change @observable, 'an_attribute' do
        value = @observable.an_attribute
      end
      value.should == 'changed'
    end

    ## This spec adds a failure to the ErrorLog!
    # it "has a default timeout of 1 second" do
    #   expect_spec_to_fail!
    #   wait_for_change(@observable, 'an_attribute') do
    #     raise "Oh noes, I must never be called!"
    #   end
    #   performSelector('triggerChange', withObject:nil, afterDelay:1.1)
    #   wait 1.2 do
    #     # we must never arrive here, because the default timeout of 1 second will have passed<commit_msg>wait_for_change only pauses the given block<commit_after>motion_require '../spec_helper'

class MockObservable
  attr_accessor :an_attribute
end

describe "NSRunloop aware Bacon" do
  describe "concerning `wait' with a fixed time" do
    it "allows the user to postpone execution of a block for n seconds, which will halt any further execution of specs" do
      started_at_1 = started_at_2 = started_at_3 = Time.now
      #number_of_specs_before = Bacon::Counter[:specifications]

      wait 0.5
      (Time.now - started_at_1).should.be.close(0.5, 0.5)

      wait 1 do
        (Time.now - started_at_2).should.be.close(1.5, 0.5)
        wait 1.5 do
          (Time.now - started_at_3).should.be.close(3, 0.5)
          #Bacon::Counter[:specifications].should == number_of_specs_before
        end
      end
    end
  end

  describe "concerning `wait' without a fixed time" do
    def delegateCallbackMethod
      @delegateCallbackCalled = true
      resume
    end

    def delegateCallbackTookTooLongMethod
      raise "Oh noes, I must never be called!"
    end

    it "allows the user to postpone execution of a block until Context#resume is called, from for instance a delegate callback" do
      performSelector('delegateCallbackMethod', withObject:nil, afterDelay:0.1)
      @delegateCallbackCalled.should == nil
      wait { @delegateCallbackCalled.should == true }
    end

    ## This spec adds a failure to the ErrorLog!
    # it "has a default timeout of 1 second after which the spec will fail and further scheduled calls to the Context are cancelled" do
    #   expect_spec_to_fail!
    #   performSelector('delegateCallbackTookTooLongMethod', withObject:nil, afterDelay:1.2)
    #   wait do
    #     # we must never arrive here, because the default timeout of 1 second will have passed
    #     raise "Oh noes, we shouldn't have arrived in this postponed block!"
    #   end
    # end

    ## This spec adds a failure to the ErrorLog!
    # it "takes an explicit timeout" do
    #   expect_spec_to_fail!
    #   performSelector('delegateCallbackTookTooLongMethod', withObject:nil, afterDelay:0.8)
    #   wait_max 0.3 do
    #     # we must never arrive here, because the default timeout of 1 second will have passed
    #     raise "Oh noes, we shouldn't have arrived in this postponed block!"
    #   end
    # end
  end

  describe "concerning `wait_for_change'" do
    before { @observable = MockObservable.new }

    def triggerChange
      @observable.an_attribute = 'changed'
    end

    it "resumes the postponed block once an observed value changes" do
      value = nil
      performSelector('triggerChange', withObject:nil, afterDelay:0)
      wait_for_change @observable, 'an_attribute' do
        @observable.an_attribute.should == 'changed'
      end
    end

    ## This spec adds a failure to the ErrorLog!
    # it "has a default timeout of 1 second" do
    #   expect_spec_to_fail!
    #   wait_for_change(@observable, 'an_attribute') do
    #     raise "Oh noes, I must never be called!"
    #   end
    #   performSelector('triggerChange', withObject:nil, afterDelay:1.1)
    #   wait 1.2 do
    #     # we must never arrive here, because the default timeout of 1 second will have passed
{'fraction_non_alphanumeric': 0.07442364227139099, 'fraction_numerical': 0.012378152560730311, 'mean_word_length': 2.9438682123245883, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4872795', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1869, 'n_words': 666}