Skip to content

Commit 54d2b39

Browse files
committed
Merge branch 'pr-77-subclasses'.
This allows subclasses of `Tree::TreeNode` to be compared.
2 parents 0abece6 + 2be7b5c commit 54d2b39

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

lib/tree.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def marshal_load(dumped_tree_array)
283283
content = Marshal.load(node_hash[:content])
284284

285285
if parent_name
286-
nodes[name] = current_node = Tree::TreeNode.new(name, content)
286+
nodes[name] = current_node = self.class.new(name, content)
287287
nodes[parent_name].add current_node
288288
else
289289
# This is the root node, hence initialize self.
@@ -920,9 +920,8 @@ def previous_sibling
920920
# this node is a 'predecessor'. Returns 'nil' if the other
921921
# object is not a 'Tree::TreeNode'.
922922
def <=>(other)
923-
return nil if other.nil? || other.class != Tree::TreeNode
924-
925-
name <=> other.name
923+
return nil if other == nil || !other.is_a?(Tree::TreeNode)
924+
self.name <=> other.name
926925
end
927926

928927
# Pretty prints the (sub)tree rooted at this node.

spec/tree_spec.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
# tree_spec.rb
44
#
55
# Author: Anupam Sengupta
6-
# Time-stamp: <2015-12-31 22:57:59 anupam>
7-
# Copyright (C) 2015 Anupam Sengupta <anupamsg@gmail.com>
6+
# Time-stamp: <2022-06-19 16:47:33 anupam>
7+
# Copyright (C) 2015-2022 Anupam Sengupta <anupamsg@gmail.com>
88
#
99

1010
require 'rspec'
1111
require 'spec_helper'
1212

1313
describe Tree do
14+
class SpecializedTreeNode < Tree::TreeNode; end
15+
1416
shared_examples_for 'any detached node' do
1517
it 'should not equal "Object.new"' do
1618
expect(@tree).not_to eq(Object.new)
@@ -69,4 +71,48 @@
6971

7072
it_behaves_like 'any detached node'
7173
end
74+
75+
describe 'comparison' do
76+
let(:node1) { Tree::TreeNode.new('a') }
77+
let(:node2) { SpecializedTreeNode.new('b') }
78+
79+
it 'allows comparison of specialized tree nodes' do
80+
expect(node1 <=> node2).to be_eql(-1)
81+
end
82+
83+
it 'does not allow comparison with nil' do
84+
expect(node1 <=> nil).to be(nil)
85+
end
86+
87+
it 'does not allow comparison with other objects' do
88+
expect(node1 <=> 'c').to be(nil)
89+
end
90+
end
91+
92+
describe 'serialization' do
93+
let(:node1) { SpecializedTreeNode.new('a') }
94+
let(:serialized_node1) { Marshal.dump(node1) }
95+
let(:node2) { Tree::TreeNode.new('b') }
96+
let(:serialized_node2) { Marshal.dump(node2) }
97+
let(:tree) do
98+
SpecializedTreeNode.new('root').tap do |root|
99+
root << node1
100+
root << node2
101+
end
102+
end
103+
let(:serialized_tree) { Marshal.dump(tree) }
104+
105+
it 'parses the serialized specialized tree node correctly (root)' do
106+
expect(Marshal.load(serialized_tree)).to be_a(SpecializedTreeNode)
107+
end
108+
109+
it 'parses the serialized specialized tree node correctly (child)' do
110+
expect(Marshal.load(serialized_tree).children.first).to \
111+
be_a(SpecializedTreeNode)
112+
end
113+
114+
it 'parses the serialized tree node correctly' do
115+
expect(Marshal.load(serialized_node2)).to be_a(Tree::TreeNode)
116+
end
117+
end
72118
end

0 commit comments

Comments
 (0)