spec/node_spec.rb in SgfParser-2.0.0 vs spec/node_spec.rb in SgfParser-3.0.0
- old
+ new
@@ -1,90 +1,196 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'spec_helper'
-describe "SGF::Node" do
+RSpec.describe SGF::Node do
- before :each do
- @node = SGF::Node.new
- end
+ let(:node) { SGF::Node.new }
+ subject { node }
- it "should be a valid node" do
- @node.class.should == SGF::Node
- @node.properties.should == {}
- @node.parent.should == nil
- @node.children.should == []
- end
+ context 'inspect' do
+ subject { node.inspect }
+ it { is_expected.to match(/#{node.object_id}/) }
+ it { is_expected.to match(/SGF::Node/) }
+ it { is_expected.to match(/Has no parent/) }
- it "should store properties" do
- @node.add_properties "PB" => "Dosaku"
- @node.properties.should == {"PB" => "Dosaku"}
- end
+ it "should give you a relatively useful inspect" do
+ node.add_properties({C: "Oh hi", PB: "Dosaku", AE: "[dd][gh]"})
+ is_expected.to match(/3 Properties/)
- it "should link to a parent" do
- parent = SGF::Node.new
- @node.parent = parent
- @node.parent.should == parent
- end
+ node.add_children SGF::Node.new, SGF::Node.new
+ expect(node.inspect).to match(/2 Children/)
- it "should link to children" do
- child1 = SGF::Node.new
- child2 = SGF::Node.new
- child3 = SGF::Node.new
- @node.add_children child1, child2, child3
- @node.children.should == [child1, child2, child3]
- end
+ node.parent = SGF::Node.new
+ expect(node.inspect).to match(/Has a parent/)
+ end
- it "should link to children, who should get new parents" do
- child1 = SGF::Node.new
- child2 = SGF::Node.new
- child3 = SGF::Node.new
- @node.add_children child1, child2, child3
- @node.children.each { |child| child.parent.should == @node }
end
- it "should allow concatenation of properties" do
- @node.add_properties "TC" => "Hello,"
- @node.add_properties "TC" => " world!"
- @node.properties["TC"].should == "Hello, world!"
+ it "should properly show a string version of the node" do
+ subject.add_properties({"C" => "Oh hi]", "PB" => "Dosaku"})
+ expect(subject.to_s).to eq ";C[Oh hi\\]]\nPB[Dosaku]"
end
- it "should give you the properties based on method given" do
- @node.add_properties "PW" => "The Tick"
- @node.add_properties "PB" => "Batmanuel"
- @node.pw.should == "The Tick"
- @node.pb.should == "Batmanuel"
+ it "should properly show a string version of the node if identities are symbols" do
+ subject.add_properties({C: "Oh hi]", PB: "Dosaku"})
+ expect(subject.to_s).to eq ";C[Oh hi\\]]\nPB[Dosaku]"
end
- it "should allow you to change a property completely" do
- @node.add_properties "RE" => "This is made up"
- @node.properties["RE"] = "This is also made up"
- @node.re.should == "This is also made up"
- @node.re = "And that too"
- @node.re.should == "And that too"
- end
+ context "Heredity" do
- it "should implement [] as a shortcut to read properties" do
- @node.add_properties "PB" => "Dosaku"
- @node["PB"].should == "Dosaku"
- @node[:PB].should == "Dosaku"
+ let(:parent) { SGF::Node.new }
+ let(:child1) { SGF::Node.new }
+ let(:child3) { SGF::Node.new }
+ let(:child2) { SGF::Node.new }
+
+ it "should link to a parent" do
+ subject.parent = parent
+ expect(subject.parent).to eq parent
+ end
+
+ it "should link to children" do
+ node.add_children child1, child2, child3
+ expect(node.children).to eq [child1, child2, child3]
+ end
+
+ it "should link to children, who should get new parents" do
+ node.add_children child1, child2, child3
+ node.children.each { |child| expect(child.parent).to eq node }
+ end
+
+ it "should not be the child of many nodes" do
+ parent2 = SGF::Node.new
+ parent.add_children node
+ parent2.add_children node
+ expect(node.parent).to eq(parent2)
+ expect(parent2.children).to include(node)
+ expect(parent.children).to_not include(node)
+ end
+
+ it "should become a child of its new parent" do
+ node.parent = parent
+ expect(parent.children).to include node
+ end
+
end
- it "should give you a relatively useful inspect" do
- @node.inspect.should match /#{@node.object_id}/
- @node.inspect.should match /SGF::Node/
+ context "Properties" do
- @node.add_properties({"C" => "Oh hi", "PB" => "Dosaku", "AE" => "[dd][gh]"})
- @node.inspect.should match /3 Properties/
+ it "should store properties" do
+ node.add_properties PB: "Dosaku"
+ expect(node.properties).to eq({"PB" => "Dosaku"})
+ end
- @node.add_children SGF::Node.new, SGF::Node.new
- @node.inspect.should match /2 Children/
+ it "should allow concatenation of properties" do
+ node.add_properties "TC" => "Hello,"
+ node.add_properties "TC" => " world!"
+ expect(node.properties["TC"]).to eq "Hello, world!"
+ end
- @node.inspect.should match /Has no parent/
- @node.parent = SGF::Node.new
- @node.inspect.should match /Has a parent/
+ it "should give you the properties based on method given" do
+ node.add_properties "PW" => "The Tick"
+ node.add_properties "PB" => "Batmanuel"
+ expect(node.pw).to eq "The Tick"
+ expect(node.pb).to eq "Batmanuel"
+ end
+
+ it "should allow you to change a property completely" do
+ node.add_properties "RE" => "This is made up"
+ node.properties["RE"] = "This is also made up"
+ expect(node.re).to eq "This is also made up"
+ node.re = "And that too"
+ expect(node.re).to eq "And that too"
+ node[:RE] = 'kokolegorille'
+ expect(node[:RE]).to eq 'kokolegorille'
+ end
+
+ it "should implement [] as a shortcut to read properties" do
+ node.add_properties "PB" => "Dosaku"
+ expect(node["PB"]).to eq "Dosaku"
+ expect(node[:PB]).to eq "Dosaku"
+ end
+
end
- it "should properly show a string version of the node" do
- @node.add_properties({"C" => "Oh hi]", "PB" => "Dosaku"})
- @node.to_str.should == ";C[Oh hi\\]]\nPB[Dosaku]"
+ context "Node depth" do
+
+ it "should get a node depth number one more by the parent when attached to a parent" do
+ child = SGF::Node.new
+ node.add_children child
+ expect(node.depth).to eq 0
+ expect(child.depth).to eq 1
+ end
+
+ it "should properly set depth if a parent is passed to initializer" do
+ node2 = SGF::Node.new parent: node
+ expect(node2.parent).to eq node
+ expect(node2.depth).to eq 1
+ end
+
+ it "should properly update depth when parentage changes" do
+ link1 = SGF::Node.new
+ link2 = SGF::Node.new
+ link2.parent = link1
+ link1.parent = node
+ expect(node.parent).to be_nil
+ expect(link1.parent).to eq node
+ expect(link2.parent).to eq link1
+ expect(node.depth).to eq 0
+ expect(link1.depth).to eq 1
+ expect(link2.depth).to eq 2
+ end
+
+ it "should properly update depth if parent is set to nil / parent is removed" do
+ parent = SGF::Node.new
+ child = SGF::Node.new
+
+ node.add_children child
+ node.parent = parent
+ expect(node.depth).to eq 1
+ expect(child.depth).to eq 2
+
+ node.parent = nil
+ expect(node.depth).to eq 0
+ expect(child.depth).to eq 1
+
+ node.parent = parent
+ expect(node.depth).to eq 1
+ expect(child.depth).to eq 2
+
+ node.remove_parent
+ expect(node.depth).to eq 0
+ expect(child.depth).to eq 1
+ end
+
+ it "should properly update depth when childhood changes" do
+ link1 = SGF::Node.new
+ link2 = SGF::Node.new
+ link3 = SGF::Node.new
+ link1.add_children link2
+ link2.add_children link3
+ node.add_children link1
+ node.add_children link3
+ expect(node.depth).to eq 0
+ expect(link1.depth).to eq 1
+ expect(link2.depth).to eq 2
+ expect(link3.depth).to eq 1
+ end
end
-end
\ No newline at end of file
+ context "self-consistency" do
+ it "should only track changes from its current parent" do
+ link1 = SGF::Node.new
+ link2 = SGF::Node.new
+
+ link3 = SGF::Node.new
+ link1.add_children link2
+ node.parent = link2
+ expect(node.depth).to eq 2
+
+ link1.depth = 2
+ expect(node.depth).to eq 4
+
+ node.parent = link3
+ link1.depth = 6
+ expect(node.depth).to eq 1
+ end
+ end
+end