require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') include Fathom describe Node do it "should take an options hash to instantiate" do lambda{Node.new(:any_value => true)}.should_not raise_error end it "should take an optional name parameter" do node = Node.new(:name => "My Name") node.name.should eql("My Name") end it "should take an optional distribution parameter" do node = Node.new(:distribution => Fathom::Distributions::Gaussian) node.distribution.should eql(Fathom::Distributions::Gaussian) end it "should default to the standard distribution" do node = Node.new node.distribution.should eql(Fathom::Distributions::Gaussian) end it "should be able to take a symbol and find the appropriate distribution" do node = Node.new(:distribution => :gaussian) node.distribution.should eql(Fathom::Distributions::Gaussian) end it "should take an optional description" do node = Node.new(:description => "some description") node.description.should eql('some description') end it "should take an optional values parameter" do node = Node.new(:values => [1,2,3]) node.values.should eql([1,2,3]) end it "should provide name_sym, if a name is defined" do node = Node.new(:name => "Some name") node.name_sym.should eql(:some_name) end it "should change dashes to underscores for name_sym" do node = Node.new(:name => "Some-name") node.name_sym.should eql(:some_name) end it "should take an optional parents list" do n1 = Node.new n2 = Node.new(:parents => [n1]) n2.parents.should eql([n1]) end it "should allow a single node as a parent or parents and turn it into an array" do n1 = Node.new n2 = Node.new(:parents => n1) n2.parents.should eql([n1]) n2 = Node.new(:parent => n1) n2.parents.should eql([n1]) end it "should create an empty list of parents by default" do n1 = Node.new n1.parents.should eql([]) end it "should be able to add a parent" do n1, n2 = Node.new, Node.new n3 = Node.new(:parent => n2) n3.add_parent(n1) n3.parents.should eql([n2, n1]) end it "should register the receiving node when adding a parent" do n1 = Node.new n1.should_receive(:register_child).and_return(true) n2 = Node.new(:parent => n1) n1 = Node.new n1.should_receive(:register_child).and_return(true) n2 = Node.new n2.add_parent(n1) end it "should not allow a parent to be registered if it is not a child of the other node" do n1 = Node.new n2 = Node.new lambda{n2.register_parent(n1)}.should raise_error end it "should take an optional children list" do n1 = Node.new n2 = Node.new(:children => [n1]) n2.children.should eql([n1]) end it "should allow a single node as a child or children and turn it into an array" do n1 = Node.new n2 = Node.new(:children => n1) n2.children.should eql([n1]) n2 = Node.new(:child => n1) n2.children.should eql([n1]) end it "should create an empty list of children by default" do n1 = Node.new n1.children.should eql([]) end it "should register the receiving node when adding a child" do n1 = Node.new n1.should_receive(:register_parent).and_return(true) n2 = Node.new(:child => n1) n1 = Node.new n1.should_receive(:register_parent).and_return(true) n2 = Node.new n2.add_child(n1) end it "should not allow a child to be registered if it is not a parent of the other node" do n1 = Node.new n2 = Node.new lambda{n2.register_child(n1)}.should raise_error end it "should define an accessor method for the child node when added" do n1 = Node.new(:name => 'n1') n2 = Node.new :name => 'n2', :child => n1 n2.n1.should eql(n1) n1.should_not respond_to(:n1) end it "should define an accessor method for the parent node when added" do n1 = Node.new(:name => 'n1') n2 = Node.new :parent => n1 n2.n1.should eql(n1) n1.should_not respond_to(:n1) end it "should have defined an accessor method for the added child to the parent" do n1 = Node.new(:name => 'n1') n2 = Node.new :name => 'n2', :child => n1 n1.n2.should eql(n2) end it "should have defined an accessor method for the added parent to the child" do n1 = Node.new(:name => 'n1') n2 = Node.new :name => 'n2', :parent => n1 n1.n2.should eql(n2) end end