Skip to content

Commit 3858262

Browse files
committed
Fixed the <=> operator, and added Rspec tests.
This is for addressing bug report #50. Also, Added retrieval of the parent node in the example code.
1 parent 851498e commit 3858262

12 files changed

Lines changed: 155 additions & 20 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ group :development, :test do
77
gem "rake", "~> 10.4"
88
gem "test-unit", "~> 3.0"
99
gem "coveralls", ">= 0.7", :require => false, :platforms => :mri_21
10+
gem "rspec", ">= 3.4"
1011
end
1112

1213
# Local Variables:

Gemfile.lock

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,65 @@
11
PATH
22
remote: .
33
specs:
4-
rubytree (0.9.6)
4+
rubytree (0.9.7)
55
json (~> 1.8)
66
structured_warnings (~> 0.2)
77

88
GEM
99
remote: https://rubygems.org/
1010
specs:
11-
coveralls (0.8.2)
11+
coveralls (0.8.10)
1212
json (~> 1.8)
1313
rest-client (>= 1.6.8, < 2)
14-
simplecov (~> 0.10.0)
14+
simplecov (~> 0.11.0)
1515
term-ansicolor (~> 1.3)
1616
thor (~> 0.19.1)
17+
tins (~> 1.6.0)
18+
diff-lcs (1.2.5)
1719
docile (1.1.5)
18-
domain_name (0.5.24)
20+
domain_name (0.5.25)
1921
unf (>= 0.0.5, < 1.0.0)
2022
http-cookie (1.0.2)
2123
domain_name (~> 0.5)
2224
json (1.8.3)
23-
mime-types (2.6.1)
24-
netrc (0.10.3)
25-
power_assert (0.2.3)
25+
mime-types (2.99)
26+
netrc (0.11.0)
27+
power_assert (0.2.6)
2628
rake (10.4.2)
27-
rdoc (4.2.0)
29+
rdoc (4.2.1)
30+
json (~> 1.4)
2831
rest-client (1.8.0)
2932
http-cookie (>= 1.0.2, < 2.0)
3033
mime-types (>= 1.16, < 3.0)
3134
netrc (~> 0.7)
35+
rspec (3.4.0)
36+
rspec-core (~> 3.4.0)
37+
rspec-expectations (~> 3.4.0)
38+
rspec-mocks (~> 3.4.0)
39+
rspec-core (3.4.1)
40+
rspec-support (~> 3.4.0)
41+
rspec-expectations (3.4.0)
42+
diff-lcs (>= 1.2.0, < 2.0)
43+
rspec-support (~> 3.4.0)
44+
rspec-mocks (3.4.0)
45+
diff-lcs (>= 1.2.0, < 2.0)
46+
rspec-support (~> 3.4.0)
47+
rspec-support (3.4.1)
3248
rtags (0.97)
3349
rtagstask (0.0.4)
3450
rtags (> 0.0.0)
35-
simplecov (0.10.0)
51+
simplecov (0.11.1)
3652
docile (~> 1.1.0)
3753
json (~> 1.8)
3854
simplecov-html (~> 0.10.0)
3955
simplecov-html (0.10.0)
4056
structured_warnings (0.2.0)
4157
term-ansicolor (1.3.2)
4258
tins (~> 1.0)
43-
test-unit (3.1.2)
59+
test-unit (3.1.5)
4460
power_assert
4561
thor (0.19.1)
46-
tins (1.5.4)
62+
tins (1.6.0)
4763
unf (0.1.4)
4864
unf_ext
4965
unf_ext (0.0.7.1)
@@ -57,6 +73,7 @@ DEPENDENCIES
5773
coveralls (>= 0.7)
5874
rake (~> 10.4)
5975
rdoc (~> 4.2)
76+
rspec (>= 3.4)
6077
rtagstask (~> 0.0)
6178
rubytree!
6279
test-unit (~> 3.0)

History.rdoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
= History of Changes
22

3+
=== 0.9.7 / 2015-12-31
4+
5+
* Released 0.9.6. This is a minor bug-fix release.
6+
7+
* This release allows the {Tree::TreeNode#print_tree} method to be used on
8+
non-root nodes. Thanks to {https://github.com/ojab Ojab}.
9+
10+
* The spaceship operator ('<=>') now returns 'nil' if the object being compared
11+
to is itself 'nil' or not another {Tree::TreeNode}.
12+
13+
=== 0.9.6 / 2015-05-30
14+
15+
* Released 0.9.6, which is same as 0.9.5, except for an update to the Gem release date.
16+
317
=== 0.9.5 / 2015-05-30
418

519
* Released 0.9.5.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ This example can also be found at
129129
* [Bundler][] for creating the stable build environment
130130
* [Rake][] for building the package
131131
* [Yard][] for the documentation
132+
* [RSpec][] for additional Ruby Spec test files
132133

133134
## INSTALL: ##
134135

@@ -260,6 +261,6 @@ A big thanks to the following contributors for helping improve **RubyTree**:
260261
[rt_site]: http://rubytree.anupamsg.me "RubyTree Site"
261262
[structured_warnings]: http://github.com/schmidt/structured_warnings "structured_warnings"
262263
[tree_data_structure]: http://en.wikipedia.org/wiki/Tree_data_structure "Tree Data Structure"
263-
264+
[RSpec]: https://relishapp.com/rspec/
264265

265266
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/evolve75/rubytree/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ task :default do
4545
Rake::Task["test:coverage"].invoke
4646
else
4747
Rake::Task["test:unit"].invoke
48+
Rake::Task["spec"].invoke
4849
end
4950
end
5051

@@ -133,6 +134,17 @@ namespace :test do # ................................ Test related
133134

134135
end
135136

137+
begin # ................................ rspec tests
138+
require 'rspec/core/rake_task'
139+
140+
RSpec::Core::RakeTask.new(:spec) do |t|
141+
t.fail_on_error = false
142+
t.rspec_opts = ["--color", "--format doc"]
143+
end
144+
rescue LoadError
145+
# Cannot load rspec.
146+
end
147+
136148
namespace :tag do # ................................ Emacs Tags
137149
begin
138150
require 'rtagstask'

examples/example_basic.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# example_basic.rb:: Basic usage of the tree library.
44
#
55
# Author: Anupam Sengupta
6-
# Time-stamp: <2013-12-28 12:14:20 anupam>
7-
# Copyright (C) 2013 Anupam Sengupta <anupamsg@gmail.com>
6+
# Time-stamp: <2015-12-31 22:17:30 anupam>
7+
# Copyright (C) 2013, 2015 Anupam Sengupta <anupamsg@gmail.com>
88
#
99
# The following example implements this tree structure:
1010
#
@@ -46,6 +46,9 @@
4646
# ..... Lets retrieve immediate children of the root node as an array.
4747
children_of_root = root_node.children
4848

49+
# ..... Retrieve the parent of a node.
50+
parent = child1.parent
51+
4952
# ..... This is a depth-first and L-to-R pre-ordered traversal.
5053
root_node.each { |node| node.content.reverse }
5154

lib/tree.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,9 +922,10 @@ def previous_sibling
922922
# @param [Tree::TreeNode] other The other node to compare against.
923923
#
924924
# @return [Integer] +1 if this node is a 'successor', 0 if equal and -1 if
925-
# this node is a 'predecessor'.
925+
# this node is a 'predecessor'. Returns 'nil' if the other
926+
# object is not a 'Tree::TreeNode'.
926927
def <=>(other)
927-
return +1 if other == nil
928+
return nil if other == nil || other.class != Tree::TreeNode
928929
self.name <=> other.name
929930
end
930931

lib/tree/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@
3737
#
3838
module Tree
3939
# Rubytree Package Version
40-
VERSION = '0.9.6'
40+
VERSION = '0.9.7'
4141
end

rubytree.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require "tree/version"
1212

1313
Gem::Specification.new do |s|
1414
s.name = 'rubytree'
15-
s.date = '2015-05-30'
15+
s.date = '2015-12-31'
1616
s.version = Tree::VERSION
1717
s.license = 'BSD'
1818

@@ -54,6 +54,7 @@ Gem::Specification.new do |s|
5454
s.files = Dir['lib/**/*.rb'] # The actual code
5555
s.files += Dir['[A-Z]*'] # Various documentation files
5656
s.files += Dir['test/**/*.rb'] # Test cases
57+
s.files += Dir['spec/**/*.rb'] # Rspec Test cases
5758
s.files += Dir['examples/**/*.rb'] # Examples
5859

5960
s.files += ['.gemtest'] # Support for gem-test
@@ -74,6 +75,7 @@ Gem::Specification.new do |s|
7475
s.add_development_dependency 'rdoc' , '~> 4.2'
7576
s.add_development_dependency 'yard' , '~> 0.8'
7677
s.add_development_dependency 'rtagstask' , '~> 0.0'
78+
s.add_development_dependency 'rspec' , '~> 3.4'
7779

7880
s.post_install_message = <<-EOF
7981
========================================================================

spec/spec_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env ruby
2+
#
3+
# spec_helper.rb
4+
#
5+
# Author: Anupam Sengupta
6+
# Time-stamp: <2015-12-31 21:00:36 anupam>
7+
# Copyright (C) 2015 Anupam Sengupta <anupamsg@gmail.com>
8+
#
9+
10+
require "tree"

0 commit comments

Comments
 (0)