spec/astrolabe/node_spec.rb in astrolabe-0.5.0 vs spec/astrolabe/node_spec.rb in astrolabe-0.5.1

- old
+ new

@@ -66,10 +66,27 @@ expect(enumerator.next.type).to eq(expected_type) end expect { enumerator.next }.to raise_error(StopIteration) end + + context 'when a node type symbol is passed' do + subject(:enumerator) { target_node.send(method_name, :send) } + + it 'enumerates only nodes matching the type' do + count = 0 + + begin + loop do + expect(enumerator.next.type).to eq(:send) + count += 1 + end + rescue StopIteration + expect(count).to be > 0 + end + end + end end end end describe '#each_ancestor' do @@ -110,49 +127,49 @@ it 'returns itself' do returned_value = target_node.each_ancestor {} expect(returned_value).to equal(target_node) end - end - include_examples 'node enumerator', :each_ancestor + context 'and a node type symbol is passed' do + it 'scans all the ancestor nodes but yields only nodes matching the type' do + yielded_types = [] - context 'when a node type symbol is passed' do - it 'scans all the ancestor nodes but yields only nodes matching the type' do - yielded_types = [] + target_node.each_ancestor(:begin) do |node| + yielded_types << node.type + end - target_node.each_ancestor(:begin) do |node| - yielded_types << node.type + expect(yielded_types).to eq([:begin]) end - - expect(yielded_types).to eq([:begin]) end - end - context 'when multiple node type symbols are passed' do - it 'scans all the ancestor nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and multiple node type symbols are passed' do + it 'scans all the ancestor nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_ancestor(:begin, :def) do |node| - yielded_types << node.type - end + target_node.each_ancestor(:begin, :def) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:def, :begin]) + expect(yielded_types).to eq([:def, :begin]) + end end - end - context 'when an array including type symbols are passed' do - it 'scans all the ancestor nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and an array including type symbols are passed' do + it 'scans all the ancestor nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_ancestor([:begin, :def]) do |node| - yielded_types << node.type - end + target_node.each_ancestor([:begin, :def]) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:def, :begin]) + expect(yielded_types).to eq([:def, :begin]) + end end end + + include_examples 'node enumerator', :each_ancestor end describe '#each_child_node' do let(:source) { <<-END } def some_method(arg_a, arg_b) @@ -182,49 +199,49 @@ it 'returns itself' do returned_value = target_node.each_child_node {} expect(returned_value).to equal(target_node) end - end - include_examples 'node enumerator', :each_child_node + context 'and a node type symbol is passed' do + it 'scans all the child nodes but yields only nodes matching the type' do + yielded_types = [] - context 'when a node type symbol is passed' do - it 'scans all the child nodes but yields only nodes matching the type' do - yielded_types = [] + target_node.each_child_node(:send) do |node| + yielded_types << node.type + end - target_node.each_child_node(:send) do |node| - yielded_types << node.type + expect(yielded_types).to eq([:send]) end - - expect(yielded_types).to eq([:send]) end - end - context 'when multiple node type symbols are passed' do - it 'scans all the child nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and multiple node type symbols are passed' do + it 'scans all the child nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_child_node(:send, :args) do |node| - yielded_types << node.type - end + target_node.each_child_node(:send, :args) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:args, :send]) + expect(yielded_types).to eq([:args, :send]) + end end - end - context 'when an array including type symbols are passed' do - it 'scans all the child nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and an array including type symbols are passed' do + it 'scans all the child nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_child_node([:send, :args]) do |node| - yielded_types << node.type - end + target_node.each_child_node([:send, :args]) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:args, :send]) + expect(yielded_types).to eq([:args, :send]) + end end end + + include_examples 'node enumerator', :each_child_node end describe '#each_descendant' do let(:source) { <<-END } class SomeClass @@ -263,49 +280,49 @@ it 'returns itself' do returned_value = target_node.each_descendant {} expect(returned_value).to equal(target_node) end - end - include_examples 'node enumerator', :each_descendant + context 'and a node type symbol is passed' do + it 'scans all the descendant nodes but yields only nodes matching the type' do + yielded_types = [] - context 'when a node type symbol is passed' do - it 'scans all the descendant nodes but yields only nodes matching the type' do - yielded_types = [] + target_node.each_descendant(:send) do |node| + yielded_types << node.type + end - target_node.each_descendant(:send) do |node| - yielded_types << node.type + expect(yielded_types).to eq([:send, :send]) end - - expect(yielded_types).to eq([:send, :send]) end - end - context 'when multiple node type symbols are passed' do - it 'scans all the descendant nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and multiple node type symbols are passed' do + it 'scans all the descendant nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_descendant(:send, :def) do |node| - yielded_types << node.type - end + target_node.each_descendant(:send, :def) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:send, :def, :send]) + expect(yielded_types).to eq([:send, :def, :send]) + end end - end - context 'when an array including type symbols are passed' do - it 'scans all the descendant nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and an array including type symbols are passed' do + it 'scans all the descendant nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_descendant([:send, :def]) do |node| - yielded_types << node.type - end + target_node.each_descendant([:send, :def]) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:send, :def, :send]) + expect(yielded_types).to eq([:send, :def, :send]) + end end end + + include_examples 'node enumerator', :each_descendant end describe '#each_node' do let(:source) { <<-END } class SomeClass @@ -344,48 +361,48 @@ it 'returns itself' do returned_value = target_node.each_node {} expect(returned_value).to equal(target_node) end - end - include_examples 'node enumerator', :each_node + context 'and a node type symbol is passed' do + it 'scans all the nodes but yields only nodes matching the type' do + yielded_types = [] - context 'when a node type symbol is passed' do - it 'scans all the nodes but yields only nodes matching the type' do - yielded_types = [] + target_node.each_node(:send) do |node| + yielded_types << node.type + end - target_node.each_node(:send) do |node| - yielded_types << node.type + expect(yielded_types).to eq([:send, :send]) end - - expect(yielded_types).to eq([:send, :send]) end - end - context 'when multiple node type symbols are passed' do - it 'scans all the nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and multiple node type symbols are passed' do + it 'scans all the nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_node(:send, :def) do |node| - yielded_types << node.type - end + target_node.each_node(:send, :def) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:send, :def, :send]) + expect(yielded_types).to eq([:send, :def, :send]) + end end - end - context 'when an array including type symbols are passed' do - it 'scans all the nodes but yields only nodes matching any of the types' do - yielded_types = [] + context 'and an array including type symbols are passed' do + it 'scans all the nodes but yields only nodes matching any of the types' do + yielded_types = [] - target_node.each_node([:send, :def]) do |node| - yielded_types << node.type - end + target_node.each_node([:send, :def]) do |node| + yielded_types << node.type + end - expect(yielded_types).to eq([:send, :def, :send]) + expect(yielded_types).to eq([:send, :def, :send]) + end end end + + include_examples 'node enumerator', :each_node end describe '#send_type?' do subject { root_node.send_type? }