local U=require("\112\108\046\099\108\097\115\115")local e=require("\105\110\118\111\107\097\116\105\111\110\046\108\097\110\103\046\100\101\108\101\103\097\116\105\111\110")describe("\100\101\108\101\103\097\116\105\111\110",function()describe("\046\100\101\108\101\103\097\116\101",function()local Q=U()function Q.start(U)return"\101\110\103\105\110\101 \115\116\097\114\116\101\100"end function Q.shutdown(U)return"\101\110\103\105\110\101 \115\116\111\112\112\101\100"end local R=U()function R._init(e,U)e.engine=U end e.delegate(R,"\101\110\103\105\110\101",{"\115\116\097\114\116","\115\104\117\116\100\111\119\110"})local k,A local C,o before_each(function()k=Q()A=R(k)C=spy.on(k,"\115\116\097\114\116")o=spy.on(k,"\115\104\117\116\100\111\119\110")end)after_each(function()C:revert()o:revert()end)it("\099\114\101\097\116\101\115 \100\101\108\101\103\097\116\101\100 \109\101\116\104\111\100\115 \105\110 \116\104\101 \116\097\114\103\101\116 \099\108\097\115\115 \111\098\106\101\099\116",function()assert.is_function(R.start)assert.is_function(R.shutdown)assert.is_function(A.start)assert.is_function(A.shutdown)end)it("\099\114\101\097\116\101\115 \109\101\116\104\111\100\115 \116\104\097\116 \100\101\108\101\103\097\116\101 \116\111 \116\104\101 \110\097\109\101\100 \097\116\116\114\105\098\117\116\101",function()assert.equal("\101\110\103\105\110\101 \115\116\097\114\116\101\100",A:start())assert.equal("\101\110\103\105\110\101 \115\116\111\112\112\101\100",A:shutdown());(assert.spy(C)).was.called_with(k);(assert.spy(o)).was.called_with(k)end)end)end)
local class = require("pl.class")
local delegation = require("invokation.lang.delegation")

describe("delegation", function()
  describe(".delegate", function()
    local Engine = class()

    function Engine:start()
      return "engine started"
    end

    function Engine:shutdown()
      return "engine stopped"
    end

    local Car = class()

    function Car:_init(e)
      self.engine = e
    end

    delegation.delegate(Car, "engine", { "start", "shutdown" })

    local engine, car
    local spyStart, spyShutdown

    before_each(function()
      engine = Engine()
      car = Car(engine)
      spyStart = spy.on(engine, "start")
      spyShutdown = spy.on(engine, "shutdown")
    end)

    after_each(function()
      spyStart:revert()
      spyShutdown:revert()
    end)

    it("creates delegated methods in the target class object", function()
      assert.is_function(Car.start)
      assert.is_function(Car.shutdown)
      assert.is_function(car.start)
      assert.is_function(car.shutdown)
    end)

    it("creates methods that delegate to the named attribute", function()
      assert.equal("engine started", car:start())
      assert.equal("engine stopped", car:shutdown())

      assert.spy(spyStart).was.called_with(engine)
      assert.spy(spyShutdown).was.called_with(engine)
    end)
  end)
end)
