|
3 | 3 | # tree_spec.rb |
4 | 4 | # |
5 | 5 | # 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> |
8 | 8 | # |
9 | 9 |
|
10 | 10 | require 'rspec' |
11 | 11 | require 'spec_helper' |
12 | 12 |
|
13 | 13 | describe Tree do |
| 14 | + class SpecializedTreeNode < Tree::TreeNode; end |
| 15 | + |
14 | 16 | shared_examples_for 'any detached node' do |
15 | 17 | it 'should not equal "Object.new"' do |
16 | 18 | expect(@tree).not_to eq(Object.new) |
|
69 | 71 |
|
70 | 72 | it_behaves_like 'any detached node' |
71 | 73 | 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 |
72 | 118 | end |
0 commit comments