Skip to content

Commit

Permalink
No more yield
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbert committed Sep 23, 2014
1 parent 72598a1 commit c812a61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
12 changes: 8 additions & 4 deletions lib/traverse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ def initialize(data, left, right)

class Traverse

def self.capture(data)
puts "Captured: #{data}"
end

def self.root_only(tree)
yield tree.data
capture(tree.data)
end

def self.with_preorder(node, &block)
def self.with_preorder(node)
# TODO
end

def self.with_inorder(node, &block)
def self.with_inorder(node)
# TODO
end

def self.with_postorder(node, &block)
def self.with_postorder(node)
# TODO
end

Expand Down
49 changes: 16 additions & 33 deletions spec/traverse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,34 @@
)
end

let(:captured_results) { [] }

before do
expect(Traverse).to receive(:capture).at_least(:once) {|data| captured_results.push(data) }
end

it "visits the root node" do
root_data = nil
Traverse.root_only(sample_tree) do |data|
root_data = data
end
expect(root_data).to eq 'F'
Traverse.root_only(sample_tree)
expect(captured_results).to eq ['F']
end

it "traverses with pre-order" do
visits = []
Traverse.with_preorder(sample_tree) do |data|
visits << data
end

expect(visits.count).to eq 9
expect(visits).to eq %w{F B A D C E G I H}
Traverse.with_preorder(sample_tree)
expect(captured_results).to eq %w{F B A D C E G I H}
end

it "traverses with in-order" do
visits = []
Traverse.with_inorder(sample_tree) do |data|
visits << data
end

expect(visits.count).to eq 9
expect(visits).to eq %w{A B C D E F G H I}
Traverse.with_inorder(sample_tree)
expect(captured_results).to eq %w{A B C D E F G H I}
end

it "traverses with post-order" do
visits = []
Traverse.with_postorder(sample_tree) do |data|
visits << data
end

expect(visits.count).to eq 9
expect(visits).to eq %w{A C E D B H I G F}
Traverse.with_postorder(sample_tree)
expect(captured_results).to eq %w{A C E D B H I G F}
end

it "traverses with level-order", :pending => "Extension!" do
visits = []
Traverse.with_levelorder(sample_tree) do |data|
visits << data
end

expect(visits.count).to eq 9
expect(visits).to eq %w{F B G A D I C E H}
Traverse.with_levelorder(sample_tree)
expect(captured_results).to eq %w{F B G A D I C E H}
end
end

0 comments on commit c812a61

Please sign in to comment.