summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/unit/around_block_scan_spec.rb
blob: 6c940a5919b6d65321d318fe45ddaea5ce0688f7 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

require_relative "../spec_helper"

module SyntaxSuggest
  RSpec.describe AroundBlockScan do
    it "continues scan from last location even if scan is false" do
      source = <<~EOM
        print 'omg'
        print 'lol'
        print 'haha'
      EOM
      code_lines = CodeLine.from_source(source)
      block = CodeBlock.new(lines: code_lines[1])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
        .scan_neighbors_not_empty

      expect(expand.code_block.to_s).to eq(source)
      expand.scan_while { |line| false }

      expect(expand.code_block.to_s).to eq(source)
    end

    it "scan_adjacent_indent works on first or last line" do
      source_string = <<~EOM
        def foo
          if [options.output_format_tty, options.output_format_block].include?(nil)
            raise("Bad output mode '\#{v}'; each must be one of \#{lookups.output_formats.keys}.")
          end
        end
      EOM

      code_lines = code_line_array(source_string)
      block = CodeBlock.new(lines: code_lines[4])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
        .scan_adjacent_indent

      expect(expand.code_block.to_s).to eq(<<~EOM)
        def foo
          if [options.output_format_tty, options.output_format_block].include?(nil)
            raise("Bad output mode '\#{v}'; each must be one of \#{lookups.output_formats.keys}.")
          end
        end
      EOM
    end

    it "expands indentation" do
      source_string = <<~EOM
        def foo
          if [options.output_format_tty, options.output_format_block].include?(nil)
            raise("Bad output mode '\#{v}'; each must be one of \#{lookups.output_formats.keys}.")
          end
        end
      EOM

      code_lines = code_line_array(source_string)
      block = CodeBlock.new(lines: code_lines[2])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
        .stop_after_kw
        .scan_adjacent_indent

      expect(expand.code_block.to_s).to eq(<<~EOM.indent(2))
        if [options.output_format_tty, options.output_format_block].include?(nil)
          raise("Bad output mode '\#{v}'; each must be one of \#{lookups.output_formats.keys}.")
        end
      EOM
    end

    it "can stop before hitting another end" do
      source_string = <<~EOM
        def lol
        end
        def foo
          puts "lol"
        end
      EOM

      code_lines = code_line_array(source_string)
      block = CodeBlock.new(lines: code_lines[3])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
      expand.stop_after_kw
      expand.scan_while { true }

      expect(expand.code_block.to_s).to eq(<<~EOM)
        def foo
          puts "lol"
        end
      EOM
    end

    it "captures multiple empty and hidden lines" do
      source_string = <<~EOM
        def foo
          Foo.call

            puts "lol"

          end
        end
      EOM

      code_lines = code_line_array(source_string)
      block = CodeBlock.new(lines: code_lines[3])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
      expand.scan_while { true }

      expect(expand.lines.first.index).to eq(0)
      expect(expand.lines.last.index).to eq(6)
      expect(expand.code_block.to_s).to eq(source_string)
    end

    it "only takes what you ask" do
      source_string = <<~EOM
        def foo
          Foo.call

            puts "lol"

          end
        end
      EOM

      code_lines = code_line_array(source_string)
      block = CodeBlock.new(lines: code_lines[3])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
      expand.scan_while { |line| line.not_empty? }

      expect(expand.code_block.to_s).to eq(<<~EOM.indent(4))
        puts "lol"
      EOM
    end

    it "skips what you want" do
      source_string = <<~EOM
        def foo
          Foo.call

            puts "haha"
            # hide me

            puts "lol"

          end
        end
      EOM

      code_lines = code_line_array(source_string)
      code_lines[4].mark_invisible

      block = CodeBlock.new(lines: code_lines[3])
      expand = AroundBlockScan.new(code_lines: code_lines, block: block)
      expand.force_add_empty
      expand.force_add_hidden
      expand.scan_neighbors_not_empty

      expect(expand.code_block.to_s).to eq(<<~EOM.indent(4))

        puts "haha"

        puts "lol"

      EOM
    end
  end
end