Sha256: 24aca62cec81bb32f20fc58b8f4f95efcb7fcb22140a7ec1c0bde5d07a65326a

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require 'spec_helper'


module Test
  module Hunt
    class Logger; end

    class Target
      include ::Scorpion::Object

      depend_on do
        logger Logger, public: true
        sailor Logger, public: true, lazy: true
      end
    end

  end
end

describe Scorpion::Hunt do
  
  let( :scorpion ){ double Scorpion }
  let( :hunt ){ Scorpion::Hunt.new scorpion, String, nil }

  describe "#fetch_by_traits" do
    it "changes context" do
      expect( scorpion ).to receive( :execute ) do |hunt|
        expect( hunt.contract ).to eq Regexp
      end

      hunt.fetch_by_traits Regexp, nil
    end

    it "restores context" do
      expect( scorpion ).to receive( :execute )

      hunt.fetch_by_traits Numeric, nil
      expect( hunt.contract ).to eq String
    end

    it "finds matching argument in parent" do
      hunt.arguments << "Hello"

      expect( hunt.fetch String ).to eq "Hello"
    end

    it "finds matching argument in grandparent" do
      hunt = Scorpion::Hunt.new scorpion, String, nil, "Hello"
      hunt.send :push, Regexp, nil, [], nil

      expect( scorpion ).to receive( :execute ) do |hunt|
        next if hunt.contract == String

        expect( hunt.fetch String ).to eq "Hello"
      end.at_least(:once)

      hunt.fetch Numeric
    end
  end

  describe "#inject" do
    let( :target )  do
      Test::Hunt::Target.new
    end

    before( :each ) do
      allow( scorpion ).to receive( :execute ) do |hunt|
        Test::Hunt::Logger.new if hunt.contract == Test::Hunt::Logger
      end
    end

    it "injects attributes" do
      hunt.inject target

      expect( target.logger? ).to be_truthy
      expect( target.logger  ).to be_a Test::Hunt::Logger
    end

    it "does not overwrite existing attributes" do
      logger = Test::Hunt::Logger.new
      target.logger = logger
      hunt.inject target

      expect( target.logger ).to be logger
    end

    it "does not fetch lazy attributes" do
      hunt.inject target
      expect( target.sailor? ).to be_falsy
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scorpion-ioc-0.4.0 spec/lib/scorpion/hunt_spec.rb