2014-09-16 14:39:27 -04:00
|
|
|
RSpec.describe Mutant::AST do
|
|
|
|
let(:object) { described_class }
|
|
|
|
|
|
|
|
describe '.find_last_path' do
|
|
|
|
subject { object.find_last_path(root, &block) }
|
|
|
|
|
|
|
|
let(:root) { s(:root, parent) }
|
|
|
|
let(:child_a) { s(:child_a) }
|
|
|
|
let(:child_b) { s(:child_b) }
|
|
|
|
let(:parent) { s(:parent, child_a, child_b) }
|
|
|
|
|
|
|
|
def path
|
|
|
|
subject.map(&:type)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no node matches' do
|
|
|
|
let(:block) { ->(_) { false } }
|
|
|
|
|
|
|
|
it { should eql([]) }
|
|
|
|
end
|
|
|
|
|
2015-06-13 09:53:45 -04:00
|
|
|
context 'when called without block' do
|
|
|
|
let(:block) { nil }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect { subject }.to raise_error(ArgumentError, 'block expected')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-12 23:47:47 -05:00
|
|
|
context 'on non Parser::AST::Node child' do
|
|
|
|
let(:block) { ->(node) { fail if node.equal?(child_a) } }
|
|
|
|
let(:child_a) { AST::Node.new(:foo) }
|
|
|
|
|
|
|
|
it 'does not yield that node' do
|
|
|
|
expect(path).to eql([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-16 14:39:27 -04:00
|
|
|
context 'when one node matches' do
|
|
|
|
let(:block) { ->(node) { node.equal?(child_a) } }
|
|
|
|
|
|
|
|
it 'returns the full path' do
|
2015-05-31 16:44:09 -04:00
|
|
|
expect(path).to eql(%i[root parent child_a])
|
2014-09-16 14:39:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when two nodes match' do
|
|
|
|
let(:block) { ->(node) { node.equal?(child_a) || node.equal?(child_b) } }
|
|
|
|
|
|
|
|
it 'returns the last full path' do
|
2015-05-31 16:44:09 -04:00
|
|
|
expect(path).to eql(%i[root parent child_b])
|
2014-09-16 14:39:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|