Skip to content
This repository
tree: 13d8cc6a8b
Fetching contributors…

Cannot retrieve contributors at this time

file 104 lines (88 sloc) 2.936 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
require File.dirname(__FILE__) + '/spec_helper'
require 'ostruct'

module InheritanceSpecs
  class Grandpa
    extend Machinist::Machinable
    attr_accessor :name, :age
  end
  
  class Dad < Grandpa
    extend Machinist::Machinable
    attr_accessor :name, :age
  end

  class Son < Dad
    extend Machinist::Machinable
    attr_accessor :name, :age
  end
end

describe Machinist::Blueprint do

  describe "explicit inheritance" do
    it "inherits attributes from the parent blueprint" do
      parent_blueprint = Machinist::Blueprint.new(OpenStruct) do
        name { "Fred" }
        age { 97 }
      end

      child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do
        name { "Bill" }
      end

      child = child_blueprint.make
      child.name.should == "Bill"
      child.age.should == 97
    end

    it "takes the serial number from the parent" do
      parent_blueprint = Machinist::Blueprint.new(OpenStruct) do
        parent_serial { sn }
      end

      child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do
        child_serial { sn }
      end

      parent_blueprint.make.parent_serial.should == "0001"
      child_blueprint.make.child_serial.should == "0002"
      parent_blueprint.make.parent_serial.should == "0003"
    end
  end

  describe "class inheritance" do
    before(:each) do
      [InheritanceSpecs::Grandpa, InheritanceSpecs::Dad, InheritanceSpecs::Son].each(&:clear_blueprints!)
    end

    it "inherits blueprinted attributes from the parent class" do
      InheritanceSpecs::Dad.blueprint do
        name { "Fred" }
      end
      InheritanceSpecs::Son.blueprint { }
      InheritanceSpecs::Son.make.name.should == "Fred"
    end

    it "overrides blueprinted attributes in the child class" do
      InheritanceSpecs::Dad.blueprint do
        name { "Fred" }
      end
      InheritanceSpecs::Son.blueprint do
        name { "George" }
      end
      InheritanceSpecs::Dad.make.name.should == "Fred"
      InheritanceSpecs::Son.make.name.should == "George"
    end

    it "inherits from blueprinted attributes in ancestor class" do
      InheritanceSpecs::Grandpa.blueprint do
        name { "Fred" }
      end
      InheritanceSpecs::Son.blueprint { }
      InheritanceSpecs::Grandpa.make.name.should == "Fred"
      lambda { InheritanceSpecs::Dad.make }.should raise_error(RuntimeError)
      InheritanceSpecs::Son.make.name.should == "Fred"
    end

    it "follows inheritance for named blueprints correctly" do
      InheritanceSpecs::Dad.blueprint do
        name { "John" }
        age { 56 }
      end
      InheritanceSpecs::Dad.blueprint(:special) do
        name { "Paul" }
      end
      InheritanceSpecs::Son.blueprint(:special) do
        age { 37 }
      end
      InheritanceSpecs::Son.make(:special).name.should == "John"
      InheritanceSpecs::Son.make(:special).age.should == 37
    end
  end

end
Something went wrong with that request. Please try again.