|
21 | 21 | ) |
22 | 22 | end |
23 | 23 |
|
| 24 | + let(:captured_results) { [] } |
| 25 | + |
| 26 | + before do |
| 27 | + expect(Traverse).to receive(:capture).at_least(:once) {|data| captured_results.push(data) } |
| 28 | + end |
| 29 | + |
24 | 30 | it "visits the root node" do |
25 | | - root_data = nil |
26 | | - Traverse.root_only(sample_tree) do |data| |
27 | | - root_data = data |
28 | | - end |
29 | | - expect(root_data).to eq 'F' |
| 31 | + Traverse.root_only(sample_tree) |
| 32 | + expect(captured_results).to eq ['F'] |
30 | 33 | end |
31 | 34 |
|
32 | 35 | it "traverses with pre-order" do |
33 | | - visits = [] |
34 | | - Traverse.with_preorder(sample_tree) do |data| |
35 | | - visits << data |
36 | | - end |
37 | | - |
38 | | - expect(visits.count).to eq 9 |
39 | | - expect(visits).to eq %w{F B A D C E G I H} |
| 36 | + Traverse.with_preorder(sample_tree) |
| 37 | + expect(captured_results).to eq %w{F B A D C E G I H} |
40 | 38 | end |
41 | 39 |
|
42 | 40 | it "traverses with in-order" do |
43 | | - visits = [] |
44 | | - Traverse.with_inorder(sample_tree) do |data| |
45 | | - visits << data |
46 | | - end |
47 | | - |
48 | | - expect(visits.count).to eq 9 |
49 | | - expect(visits).to eq %w{A B C D E F G H I} |
| 41 | + Traverse.with_inorder(sample_tree) |
| 42 | + expect(captured_results).to eq %w{A B C D E F G H I} |
50 | 43 | end |
51 | 44 |
|
52 | 45 | it "traverses with post-order" do |
53 | | - visits = [] |
54 | | - Traverse.with_postorder(sample_tree) do |data| |
55 | | - visits << data |
56 | | - end |
57 | | - |
58 | | - expect(visits.count).to eq 9 |
59 | | - expect(visits).to eq %w{A C E D B H I G F} |
| 46 | + Traverse.with_postorder(sample_tree) |
| 47 | + expect(captured_results).to eq %w{A C E D B H I G F} |
60 | 48 | end |
61 | 49 |
|
62 | 50 | it "traverses with level-order", :pending => "Extension!" do |
63 | | - visits = [] |
64 | | - Traverse.with_levelorder(sample_tree) do |data| |
65 | | - visits << data |
66 | | - end |
67 | | - |
68 | | - expect(visits.count).to eq 9 |
69 | | - expect(visits).to eq %w{F B G A D I C E H} |
| 51 | + Traverse.with_levelorder(sample_tree) |
| 52 | + expect(captured_results).to eq %w{F B G A D I C E H} |
70 | 53 | end |
71 | 54 | end |
0 commit comments