require 'spec_helper'

describe Rtime::Executor do
  let :executor do
    Rtime::Executor.new
  end

  describe 'execute' do
    it 'needs arguments to execute' do
      expect { executor.execute }.to raise_error ArgumentError
    end

    it 'times the executed process' do
      expect(executor.timer).to receive(:time).and_call_original
      executor.execute 'true'
    end

    it 'returns a filled Rtime::Result' do
      result = executor.execute('true', name: 'the name')
      expect(result).to be_a Rtime::Result
      expect(result.start).to be_a Time
      expect(result.pid).to respond_to :to_int
      expect(result.duration).to be_a Float
      expect(result.exitstatus).to eq 0
      expect(result.name).to eq 'the name'
    end
  end

  describe 'measure' do
    it 'needs a name argument to execute' do
      expect { executor.measure do end }.to raise_error ArgumentError
    end

    it 'needs a block to call' do
      expect { executor.measure('foo') }.to raise_error ArgumentError
    end

    it 'measures times of a block' do
      executed = false
      result = executor.measure('the name') do
        executed = true
      end
      expect(executed).to eq true
      expect(result).to be_a Rtime::Result
      expect(result.start).to be_a Time
      expect(result.pid).to eq $$
      expect(result.duration).to be_a Float
      expect(result.exitstatus).to eq 0
      expect(result.name).to eq 'the name'
    end
  end
end
