spec/tree_spec.rb in raabro-1.0.5 vs spec/tree_spec.rb in raabro-1.1.0

- old
+ new

@@ -12,36 +12,115 @@ describe '.lookup' do it 'returns the first node with the given name' do - t = Sample::Cal.parse('4 5 6 + 1 2 3 * +') + t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false) expect( t.lookup('item').to_a(:leaves) ).to eq( [ :item, 1, 0, 1, nil, :alt, [ [ :num, 1, 0, 1, nil, :rex, '4' ] ] ] ) end + + it 'returns the first named node if the given name is nil' do + + t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false) + + expect( + t.lookup(nil).to_a(:leaves) + ).to eq( + [ :item, 1, 0, 1, nil, :alt, [ + [ :num, 1, 0, 1, nil, :rex, '4' ] + ] ] + ) + end end + describe '.sublookup' do + + it 'skips the callee node' do + + t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false) + t = t.children[0] + + expect( + t.sublookup(nil).to_a(:leaves) + ).to eq( + [ :num, 1, 0, 1, nil, :rex, '4' ] + ) + end + end + describe '.gather' do it 'returns all the nodes with a given name' do - t = Sample::Cal.parse('4 5 6 + 1 2 3 * +') + t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false) expect( t.gather('op').collect { |n| n.to_a(:leaves) } ).to eq( [ [ :op, 1, 6, 1, nil, :rex, '+' ], [ :op, 1, 14, 1, nil, :rex, '*' ], [ :op, 1, 16, 1, nil, :rex, '+' ] ] ) + end + + it 'returns all the nodes with a name if the given name is nil' do + + t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false) + + expect( + t.gather(nil).collect { |n| n.to_a(:leaves) } + ).to eq([ + [ :item, 1, 0, 1, nil, :alt, [ + [ :num, 1, 0, 1, nil, :rex, '4' ] + ] ], + [ :item, 1, 2, 1, nil, :alt, [ + [ :num, 1, 2, 1, nil, :rex, '5' ] + ] ], + [ :item, 1, 4, 1, nil, :alt, [ + [ :num, 1, 4, 1, nil, :rex, '6' ] + ] ], + [ :item, 1, 6, 1, nil, :alt, [ + [ :op, 1, 6, 1, nil, :rex, '+' ] + ] ], + [ :item, 1, 8, 1, nil, :alt, [ + [ :num, 1, 8, 1, nil, :rex, '1' ] + ] ], + [ :item, 1, 10, 1, nil, :alt, [ + [ :num, 1, 10, 1, nil, :rex, '2' ] + ] ], + [ :item, 1, 12, 1, nil, :alt, [ + [ :num, 1, 12, 1, nil, :rex, '3' ] + ] ], + [ :item, 1, 14, 1, nil, :alt, [ + [ :op, 1, 14, 1, nil, :rex, '*' ] + ] ], + [ :item, 1, 16, 1, nil, :alt, [ + [ :op, 1, 16, 1, nil, :rex, '+' ] + ] ] + ]) + end + end + + describe '.subgather' do + + it 'skips the callee node' do + + t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false) + + expect( + t.children[0].subgather(nil).collect { |n| n.to_a(:leaves) } + ).to eq([ + [ :num, 1, 0, 1, nil, :rex, '4' ] + ]) end end end