1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
require 'rspec/support/source'
module RSpec::Support
RSpec.describe Source, :if => RSpec::Support::RubyFeatures.ripper_supported? do
subject(:source) do
Source.new(source_string)
end
let(:source_string) { <<-END.gsub(/^ +\|/, '') }
|2.times do
| puts :foo
|end
END
# [:program,
# [[:method_add_block,
# [:call, [:@int, "2", [1, 0]], :".", [:@ident, "times", [1, 2]]],
# [:do_block,
# nil,
# [[:command,
# [:@ident, "puts", [2, 2]],
# [:args_add_block,
# [[:symbol_literal, [:symbol, [:@ident, "foo", [2, 8]]]]],
# false]]]]]]]
describe '.from_file', :isolated_directory do
subject(:source) do
Source.from_file(path)
end
let(:path) do
'source.rb'
end
before do
File.open(path, 'w') { |file| file.write(source_string) }
end
it 'returns a Source with the absolute path' do
expect(source.lines.first).to eq('2.times do')
expect(source.path).not_to eq(path)
expect(source.path).to end_with(path)
end
it 'continues to work if File.read is stubbed' do
allow(::File).to receive(:read).and_raise
expect(source.lines.first).to eq('2.times do')
end
end
describe '#lines' do
it 'returns an array of lines without linefeed' do
expect(source.lines).to eq([
'2.times do',
' puts :foo',
'end'
])
end
it 'returns an array of lines no matter the encoding' do
source_string << "\xAE"
encoded_string = source_string.force_encoding('US-ASCII')
expect(Source.new(encoded_string).lines).to eq([
'2.times do',
' puts :foo',
'end',
'?',
])
end
end
describe '#ast' do
it 'returns a root node' do
expect(source.ast).to have_attributes(:type => :program)
end
end
describe '#tokens' do
it 'returns an array of tokens' do
expect(source.tokens).to all be_a(Source::Token)
end
end
describe '#nodes_by_line_number' do
it 'returns a hash containing nodes for each line number' do
expect(source.nodes_by_line_number).to match(
1 =>
if RUBY_VERSION >= '2.6.0'
[
an_object_having_attributes(:type => :@int),
an_object_having_attributes(:type => :@period),
an_object_having_attributes(:type => :@ident)
]
else
[
an_object_having_attributes(:type => :@int),
an_object_having_attributes(:type => :@ident)
]
end,
2 => [
an_object_having_attributes(:type => :@ident),
an_object_having_attributes(:type => :@ident)
]
)
expect(source.nodes_by_line_number[0]).to be_empty
end
end
describe '#tokens_by_line_number' do
it 'returns a hash containing tokens for each line number' do
expect(source.tokens_by_line_number).to match(
1 => [
an_object_having_attributes(:type => :on_int),
an_object_having_attributes(:type => :on_period),
an_object_having_attributes(:type => :on_ident),
an_object_having_attributes(:type => :on_sp),
an_object_having_attributes(:type => :on_kw),
an_object_having_attributes(:type => :on_ignored_nl)
],
2 => [
an_object_having_attributes(:type => :on_sp),
an_object_having_attributes(:type => :on_ident),
an_object_having_attributes(:type => :on_sp),
an_object_having_attributes(:type => :on_symbeg),
an_object_having_attributes(:type => :on_ident),
an_object_having_attributes(:type => :on_nl)
],
3 => [
an_object_having_attributes(:type => :on_kw),
an_object_having_attributes(:type => :on_nl)
]
)
expect(source.tokens_by_line_number[0]).to be_empty
end
end
describe '#inspect' do
it 'returns a string including class name and file path' do
expect(source.inspect).to start_with('#<RSpec::Support::Source (string)>')
end
end
end
end
|