summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/unit/code_search_spec.rb
blob: 502de14d7f3547e32e9c5359a172d3c7afb5005f (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# frozen_string_literal: true

require_relative "../spec_helper"

module SyntaxSuggest
  RSpec.describe CodeSearch do
    it "rexe regression" do
      lines = fixtures_dir.join("rexe.rb.txt").read.lines
      lines.delete_at(148 - 1)
      source = lines.join

      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join.strip).to eq(<<~EOM.strip)
        class Lookups
      EOM
    end

    it "squished do regression" do
      source = <<~EOM
        def call
          trydo

            @options = CommandLineParser.new.parse

            options.requires.each { |r| require!(r) }
            load_global_config_if_exists
            options.loads.each { |file| load(file) }

            @user_source_code = ARGV.join(' ')
            @user_source_code = 'self' if @user_source_code == ''

            @callable = create_callable

            init_rexe_context
            init_parser_and_formatters

            # This is where the user's source code will be executed; the action will in turn call `execute`.
            lookup_action(options.input_mode).call unless options.noop

            output_log_entry
          end # one
        end # two
      EOM

      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        trydo
        end # one
      EOM
    end

    it "regression test ambiguous end" do
      source = <<~EOM
        def call          # 0
            print "lol"   # 1
          end # one       # 2
        end # two         # 3
      EOM

      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM)
        end # two         # 3
      EOM
    end

    it "regression dog test" do
      source = <<~EOM
        class Dog
          def bark
            puts "woof"
        end
      EOM
      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM)
        class Dog
      EOM
      expect(search.invalid_blocks.first.lines.length).to eq(4)
    end

    it "handles mismatched |" do
      source = <<~EOM
        class Blerg
          Foo.call do |a
          end # one

          puts lol
          class Foo
          end # two
        end # three
      EOM
      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        Foo.call do |a
        end # one
      EOM
    end

    it "handles mismatched }" do
      source = <<~EOM
        class Blerg
          Foo.call do {

          puts lol
          class Foo
          end # two
        end # three
      EOM
      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        Foo.call do {
      EOM
    end

    it "handles no spaces between blocks and trailing slash" do
      source = <<~'EOM'
        require "rails_helper"
        RSpec.describe Foo, type: :model do
          describe "#bar" do
            context "context" do
              it "foos the bar with a foo and then bazes the foo with a bar to"\
                "fooify the barred bar" do
                travel_to DateTime.new(2020, 10, 1, 10, 0, 0) do
                  foo = build(:foo)
                end
              end
            end
          end
          describe "#baz?" do
            context "baz has barred the foo" do
              it "returns true" do # <== HERE
            end
          end
        end
      EOM

      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join.strip).to eq('it "returns true" do # <== HERE')
    end

    it "handles no spaces between blocks" do
      source = <<~EOM
        context "foo bar" do
          it "bars the foo" do
            travel_to DateTime.new(2020, 10, 1, 10, 0, 0) do
            end
          end
        end
        context "test" do
          it "should" do
        end
      EOM
      search = CodeSearch.new(source)
      search.call

      expect(search.invalid_blocks.join.strip).to eq('it "should" do')
    end

    it "records debugging steps to a directory" do
      Dir.mktmpdir do |dir|
        dir = Pathname(dir)
        search = CodeSearch.new(<<~EOM, record_dir: dir)
          class OH
            def hello
            def hai
            end
          end
        EOM
        search.call

        expect(search.record_dir.entries.map(&:to_s)).to include("1-add-1-(3__4).txt")
        expect(search.record_dir.join("1-add-1-(3__4).txt").read).to include(<<~EOM)
            1  class OH
            2    def hello
          > 3    def hai
          > 4    end
            5  end
        EOM
      end
    end

    it "def with missing end" do
      search = CodeSearch.new(<<~EOM)
        class OH
          def hello

          def hai
            puts "lol"
          end
        end
      EOM
      search.call

      expect(search.invalid_blocks.join.strip).to eq("def hello")

      search = CodeSearch.new(<<~EOM)
        class OH
          def hello

          def hai
          end
        end
      EOM
      search.call

      expect(search.invalid_blocks.join.strip).to eq("def hello")

      search = CodeSearch.new(<<~EOM)
        class OH
          def hello
          def hai
          end
        end
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        def hello
      EOM
    end

    describe "real world cases" do
      it "finds hanging def in this project" do
        source_string = fixtures_dir.join("this_project_extra_def.rb.txt").read
        search = CodeSearch.new(source_string)
        search.call

        document = DisplayCodeWithLineNumbers.new(
          lines: search.code_lines.select(&:visible?),
          terminal: false,
          highlight_lines: search.invalid_blocks.flat_map(&:lines)
        ).call

        expect(document).to include(<<~EOM)
          > 36      def filename
        EOM
      end

      it "Format Code blocks real world example" do
        search = CodeSearch.new(<<~EOM)
          require 'rails_helper'

          RSpec.describe AclassNameHere, type: :worker do
            describe "thing" do
              context "when" do
                let(:thing) { stuff }
                let(:another_thing) { moarstuff }
                subject { foo.new.perform(foo.id, true) }

                it "stuff" do
                  subject

                  expect(foo.foo.foo).to eq(true)
                end
              end
            end # line 16 accidental end, but valid block

              context "stuff" do
                let(:thing) { create(:foo, foo: stuff) }
                let(:another_thing) { create(:stuff) }

                subject { described_class.new.perform(foo.id, false) }

                it "more stuff" do
                  subject

                  expect(foo.foo.foo).to eq(false)
                end
              end
            end # mismatched due to 16
          end
        EOM
        search.call

        document = DisplayCodeWithLineNumbers.new(
          lines: search.code_lines.select(&:visible?),
          terminal: false,
          highlight_lines: search.invalid_blocks.flat_map(&:lines)
        ).call

        expect(document).to include(<<~EOM)
             1  require 'rails_helper'
             2
             3  RSpec.describe AclassNameHere, type: :worker do
          >  4    describe "thing" do
          > 16    end # line 16 accidental end, but valid block
          > 30    end # mismatched due to 16
            31  end
        EOM
      end
    end

    # For code that's not perfectly formatted, we ideally want to do our best
    # These examples represent the results that exist today, but I would like to improve upon them
    describe "needs improvement" do
      describe "mis-matched-indentation" do
        it "extra space before end" do
          search = CodeSearch.new(<<~EOM)
            Foo.call
              def foo
                puts "lol"
                puts "lol"
               end # one
            end # two
          EOM
          search.call

          expect(search.invalid_blocks.join).to eq(<<~EOM)
            Foo.call
            end # two
          EOM
        end

        it "stacked ends 2" do
          search = CodeSearch.new(<<~EOM)
            def cat
              blerg
            end

            Foo.call do
            end # one
            end # two

            def dog
            end
          EOM
          search.call

          expect(search.invalid_blocks.join).to eq(<<~EOM)
            Foo.call do
            end # one
            end # two

          EOM
        end

        it "stacked ends " do
          search = CodeSearch.new(<<~EOM)
            Foo.call
              def foo
                puts "lol"
                puts "lol"
            end
            end
          EOM
          search.call

          expect(search.invalid_blocks.join).to eq(<<~EOM)
            Foo.call
            end
          EOM
        end

        it "missing space before end" do
          search = CodeSearch.new(<<~EOM)
            Foo.call

              def foo
                puts "lol"
                puts "lol"
             end
            end
          EOM
          search.call

          # expand-1 and expand-2 seem to be broken?
          expect(search.invalid_blocks.join).to eq(<<~EOM)
            Foo.call
            end
          EOM
        end
      end
    end

    it "returns syntax error in outer block without inner block" do
      search = CodeSearch.new(<<~EOM)
        Foo.call
          def foo
            puts "lol"
            puts "lol"
          end # one
        end # two
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM)
        Foo.call
        end # two
      EOM
    end

    it "doesn't just return an empty `end`" do
      search = CodeSearch.new(<<~EOM)
        Foo.call
        end
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM)
        Foo.call
        end
      EOM
    end

    it "finds multiple syntax errors" do
      search = CodeSearch.new(<<~EOM)
        describe "hi" do
          Foo.call
          end
        end

        it "blerg" do
          Bar.call
          end
        end
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        Foo.call
        end
        Bar.call
        end
      EOM
    end

    it "finds a typo def" do
      search = CodeSearch.new(<<~EOM)
        defzfoo
          puts "lol"
        end
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM)
        defzfoo
        end
      EOM
    end

    it "finds a mis-matched def" do
      search = CodeSearch.new(<<~EOM)
        def foo
          def blerg
        end
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        def blerg
      EOM
    end

    it "finds a naked end" do
      search = CodeSearch.new(<<~EOM)
        def foo
          end # one
        end # two
      EOM
      search.call

      expect(search.invalid_blocks.join).to eq(<<~EOM.indent(2))
        end # one
      EOM
    end

    it "returns when no invalid blocks are found" do
      search = CodeSearch.new(<<~EOM)
        def foo
          puts 'lol'
        end
      EOM
      search.call

      expect(search.invalid_blocks).to eq([])
    end

    it "expands frontier by eliminating valid lines" do
      search = CodeSearch.new(<<~EOM)
        def foo
          puts 'lol'
        end
      EOM
      search.create_blocks_from_untracked_lines

      expect(search.code_lines.join).to eq(<<~EOM)
        def foo
        end
      EOM
    end
  end
end