File: source_spec.rb

package info (click to toggle)
ruby-rspec 3.5.0c3e0m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • ctags: 4,788
  • sloc: ruby: 62,572; sh: 785; makefile: 100
file content (131 lines) | stat: -rw-r--r-- 3,868 bytes parent folder | download
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
require 'rspec/core/source'

module RSpec::Core
  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
    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 => [
            an_object_having_attributes(:type => :@int),
            an_object_having_attributes(:type => :@ident)
          ],
          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::Core::Source (string)>')
      end
    end
  end
end