aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/share/nvim/site/test/autoload_test.rb
blob: aa9bb49d81d161cb6cdb70ac0b73696bb7d57a37 (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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
require_relative "./helper"
require "json"

describe "Autoloaded" do
  include WithTemporaryDirectory

  before do
    FileUtils.touch File.join(@dir, "package.json")
  end

  after do
    $vim.command("windo wincmd c")
  end

  describe "Autocommand" do
    it "must fire user autcommand \"Node\"" do
      $vim.command "au User Node let node_autocommand = 1337"
      $vim.edit File.join(@dir, "other.js")
      $vim.echo(%(g:node_autocommand)).must_equal "1337"
    end
  end

  describe "Goto file" do
    it "must define plug mapping in non-JavaScript files" do
      $vim.edit File.join(@dir, "README")
      $vim.echo(%(maparg("<Plug>NodeGotoFile", "n"))).wont_equal ""
    end

    it "must not be mapped in non-JavaScript files" do
      $vim.edit File.join(@dir, "README")
      $vim.echo(%(hasmapto("<Plug>NodeGotoFile"))).must_equal "0"
    end

    it "must edit README.txt" do
      touch File.join(@dir, "index.js"), %(// Please read README.txt)
      touch File.join(@dir, "README.txt")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$gf"
      $vim.echo(%(bufname("%"))).must_equal File.join(@dir, "README.txt")
    end

    it "must edit README before README.js" do
      touch File.join(@dir, "index.js"), "// Please read README"
      touch File.join(@dir, "README")
      touch File.join(@dir, "README.js")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$gf"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "README")
    end

    it "must edit ./README.txt relative to file" do
      touch File.join(@dir, "foo", "index.js"), %(// Please read ./README.txt)
      touch File.join(@dir, "foo", "README.txt")

      $vim.edit File.join(@dir, "foo", "index.js")
      $vim.feedkeys "$gf"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "foo", "README.txt")
    end

    it "must edit /.../README.txt" do
      touch File.join(@dir, "index.js"), %(// Read #@dir/lib/README.txt)
      touch File.join(@dir, "lib", "README.txt")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$gf"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "lib", "README.txt")
    end

    it "must edit ./other.js relative to file" do
      touch File.join(@dir, "foo", "index.js"), %(require("./other"))
      touch File.join(@dir, "foo", "other.js")

      $vim.edit File.join(@dir, "foo", "index.js")
      $vim.feedkeys "f.gf"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "foo", "other.js")
    end

    it "must edit ./index.js given ." do
      touch File.join(@dir, "other.js"), %(require("."))
      touch File.join(@dir, "index.js")

      $vim.edit File.join(@dir, "other.js")
      $vim.feedkeys "f.gf"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit ./index.js given ./" do
      touch File.join(@dir, "other.js"), %(require("./"))
      touch File.join(@dir, "index.js")

      $vim.edit File.join(@dir, "other.js")
      $vim.feedkeys "f.gf"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit ../index.js given .." do
      touch File.join(@dir, "foo", "other.js"), %(require(".."))
      touch File.join(@dir, "index.js")

      $vim.edit File.join(@dir, "foo", "other.js")
      $vim.feedkeys "f.gf"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit ../index.js given ../" do
      touch File.join(@dir, "foo", "other.js"), %(require("../"))
      touch File.join(@dir, "index.js")

      $vim.edit File.join(@dir, "foo", "other.js")
      $vim.feedkeys "f.gf"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit ./node_modules/foo/index.js given foo" do
      touch File.join(@dir, "index.js"), %(require("foo"))
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must edit ./node_modules/@scope/foo/index.js given @scope/foo" do
      touch File.join(@dir, "index.js"), %(require("@scope/foo"))
      target = File.join(@dir, "node_modules", "@scope", "foo", "index.js")
      touch target

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must edit ./foo.js given root/foo with root as symlink" do
      touch File.join(@dir, "index.js"), %(require("root/foo"))
      target = touch File.join(@dir, "foo.js")
      FileUtils.mkpath File.join(@dir, "node_modules")
      File.symlink "..", File.join(@dir, "node_modules", "root")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must relativize paths" do
      touch File.join(@dir, "index.js"), %(require("root/foo"))
      target = touch File.join(@dir, "foo.js")
      FileUtils.mkpath File.join(@dir, "node_modules")
      File.symlink "..", File.join(@dir, "node_modules", "root")

      $vim.edit File.join(@dir, "index.js")
      $vim.command %(:cd %:h)
      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal File.basename(target)
    end

    it "must not show an error when searching for nothing" do
      touch File.join(@dir, "index.js"), %("")

      $vim.edit File.join(@dir, "index.js")
      $vim.command(%(let v:errmsg = ""))
      $vim.feedkeys "gf"

      error = $vim.command("let v:errmsg").sub(/^\S+\s*/, "")
      error.must_equal ""
    end

    it "must show error when searching for a non-existent file" do
      touch File.join(@dir, "index.js"), %(require("new"))

      $vim.edit File.join(@dir, "index.js")
      $vim.command(%(let v:errmsg = ""))
      $vim.feedkeys "$hhgf"

      error = $vim.command("let v:errmsg").sub(/^\S+\s*/, "")
      error.must_equal %(E447: Can't find file "new" in path)
    end

    it "must find when filetype is JavaScript and file exists" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      touch File.join(@dir, "index.js"), %(require("foo"))
      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must find when filetype is JavaScript and file new" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      $vim.edit File.join(@dir, "index.js")
      $vim.insert %(require("foo"))
      $vim.normal
      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must find when filetype is JSON" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      # NOTE: Use an extensionless file and set ft manually to not have
      # filetype.vim set its filetype to JavaScript automatically.
      $vim.command("au BufReadPre package set ft=json")
      touch File.join(@dir, "package"), %({"dependencies": {"foo": "1.x"}})
      $vim.edit File.join(@dir, "package")
      $vim.echo("&filetype").must_equal "json"

      $vim.feedkeys "/foo\\<CR>gf"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal target
    end

    it "must find when filetype set to JavaScript after open" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      touch File.join(@dir, "index.react"), %(require("foo"))
      $vim.edit File.join(@dir, "index.react")
      $vim.echo("&filetype").must_equal ""
      $vim.command("setfiletype javascript")
      $vim.echo("&filetype").must_equal "javascript"

      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must find when filetype set to JSX after open" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      touch File.join(@dir, "index.react"), %(require("foo"))
      $vim.edit File.join(@dir, "index.react")
      $vim.echo("&filetype").must_equal ""
      $vim.command("setfiletype jsx")
      $vim.echo("&filetype").must_equal "jsx"

      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    it "must find when filetype set to JavaScript and Foo after open" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      touch File.join(@dir, "index.react"), %(require("foo"))
      $vim.edit File.join(@dir, "index.react")
      $vim.echo("&filetype").must_equal ""
      $vim.command("setfiletype javascript.foo")
      $vim.echo("&filetype").must_equal "javascript.foo"

      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end

    # Ensure the autocommand detects both orderings.
    it "must find when filetype set to Foo and JavaScript after open" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")

      touch File.join(@dir, "index.react"), %(require("foo"))
      $vim.edit File.join(@dir, "index.react")
      $vim.echo("&filetype").must_equal ""
      $vim.command("setfiletype foo.javascript")
      $vim.echo("&filetype").must_equal "foo.javascript"

      $vim.feedkeys "$hhgf"
      $vim.echo(%(bufname("%"))).must_equal target
    end
  end

  describe "Goto file with split" do
    it "must edit file in a new split" do
      touch File.join(@dir, "index.js"), %(require("./other"))
      touch File.join(@dir, "other.js")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "f.\\<C-w>f"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "other.js")
      $vim.echo(%(winnr("$"))).must_equal "2"
    end
  end

  describe "Goto file with tab" do
    it "must edit file in a new tab" do
      touch File.join(@dir, "index.js"), %(require("./other"))
      touch File.join(@dir, "other.js")

      $vim.edit File.join(@dir, "index.js")
      $vim.feedkeys "f.\\<C-w>gf"

      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "other.js")
      $vim.echo(%(tabpagenr("$"))).must_equal "2"
    end
  end

  describe "Include file search pattern" do
    it "must find matches given a require" do
      touch File.join(@dir, "index.js"), <<-end.gsub(/^\s+/, "")
        var awesome = require("foo")
        awesome()
      end

      definition = %(module.exports = function awesome() { return 1337 })
      touch File.join(@dir, "node_modules", "foo", "index.js"), definition

      $vim.edit File.join(@dir, "index.js")
      $vim.command("normal G[i").must_equal definition
    end

    it "must find matches given a relative require" do
      touch File.join(@dir, "index.js"), <<-end.gsub(/^\s+/, "")
        var awesome = require("./other")
        awesome()
      end

      definition = %(module.exports = function awesome() { return 1337 })
      touch File.join(@dir, "other.js"), definition

      $vim.edit File.join(@dir, "index.js")
      $vim.command("normal G[i").must_equal definition
    end

    it "must find matches given a relative require in another directory" do
      touch File.join(@dir, "foo", "index.js"), <<-end.gsub(/^\s+/, "")
        var awesome = require("./other")
        awesome()
      end

      definition = %(module.exports = function awesome() { return 1337 })
      touch File.join(@dir, "foo", "other.js"), definition

      $vim.edit File.join(@dir, "foo", "index.js")
      $vim.command("normal G[i").must_equal definition
    end
  end

  describe ":Nedit" do
    # NOTE: Test from a non-JavaScript file everywhere to make sure there are
    # no dependencies on JavaScript specific settings.
    FULL_COMMAND_MATCH = "2"

    it "must be available in non-JavaScript files" do
      $vim.edit File.join(@dir, "README.txt")
      $vim.echo("exists(':Nedit')").must_equal FULL_COMMAND_MATCH
    end

    it "must be available in JavaScript files" do
      $vim.edit File.join(@dir, "index.js")
      $vim.echo("exists(':Nedit')").must_equal FULL_COMMAND_MATCH
    end

    it "must edit ./README.txt" do
      touch File.join(@dir, "README.txt")
      $vim.edit File.join(@dir, "CHANGELOG.txt")
      $vim.command "Nedit ./README.txt"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "README.txt")
    end

    it "must edit ./README.txt relative to node_root" do
      touch File.join(@dir, "README.txt")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "CHANGELOG.txt")
      $vim.command "Nedit ./README.txt"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "README.txt")
    end

    it "must edit /.../README.txt" do
      touch File.join(@dir, "lib", "README.txt")
      $vim.edit File.join(@dir, "CHANGELOG.txt")
      $vim.command "Nedit #@dir/lib/README.txt"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "lib", "README.txt")
    end

    it "must edit ./other.js relative to node_root" do
      touch File.join(@dir, "other.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "CHANGELOG.txt")
      $vim.command "Nedit ./other"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "other.js")
    end

    it "must edit ./index.js given ." do
      touch File.join(@dir, "index.js")
      $vim.edit File.join(@dir, "CHANGELOG.txt")
      $vim.command "Nedit ."
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit ./index.js given . relative to node_root" do
      touch File.join(@dir, "index.js")
      Dir.mkdir File.join(@dir, "lib")
      $vim.edit File.join(@dir, "lib", "CHANGELOG.txt")
      $vim.command "Nedit ."
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit ./index.js given ./" do
      touch File.join(@dir, "index.js")
      $vim.edit File.join(@dir, "CHANGELOG.txt")
      $vim.command "Nedit ./"
      bufname = File.realpath($vim.echo(%(bufname("%"))))
      bufname.must_equal File.join(@dir, "index.js")
    end

    it "must edit /node_modules/foo/index.js given foo" do
      target = touch File.join(@dir, "node_modules", "foo", "index.js")
      $vim.edit File.join(@dir, "README.txt")
      $vim.command("Nedit foo")
      $vim.echo(%(bufname("%"))).must_equal target
    end

    describe "completion" do
      after do
        $vim.command("set wildignorecase&")
        $vim.command("set fileignorecase&")
      end

      def complete(cmd)
        cmdline = $vim.command(%(silent! normal! :#{cmd}e))
        cmdline.sub(/^:\w+\s+/, "")
      end

      public_core_modules = CORE_MODULES.select {|m| m[0] != "_" }
      private_core_modules = CORE_MODULES.select {|m| m[0] == "_" }

      it "must return files, directories, modules" do
        Dir.mkdir File.join(@dir, "node_modules")
        Dir.mkdir File.join(@dir, "node_modules", "require-guard")
        Dir.mkdir File.join(@dir, "node_modules", "export")
        Dir.mkdir File.join(@dir, "node_modules", "soul")
        touch File.join(@dir, "index.js")

        $vim.edit File.join(@dir, "README.txt")
        files = %w[export/ require-guard/ soul/ ./index.js ./package.json]
        all = public_core_modules + files
        complete("Nedit ").split.sort.must_equal all.sort
      end

      it "must return only public core modules" do
        $vim.edit File.join(@dir, "README.txt")
        modules = public_core_modules + ["./package.json"]
        complete("Nedit ").must_equal modules.join(" ")
      end

      it "must return private core modules if explicitly asked" do
        $vim.edit File.join(@dir, "README.txt")
        complete("Nedit _").must_equal private_core_modules.join(" ")
      end

      it "must return only matching modules" do
        Dir.mkdir File.join(@dir, "node_modules")
        Dir.mkdir File.join(@dir, "node_modules", "export")
        Dir.mkdir File.join(@dir, "node_modules", "soul")
        Dir.mkdir File.join(@dir, "node_modules", "soulstash")

        $vim.edit File.join(@dir, "README.txt")
        modules = "smalloc stream string_decoder sys soul/ soulstash/"
        complete("Nedit s").must_equal modules
      end

      it "must not return modules with matching bit in the middle" do
        Dir.mkdir File.join(@dir, "node_modules")
        Dir.mkdir File.join(@dir, "node_modules", "soul")
        Dir.mkdir File.join(@dir, "node_modules", "soulstash")
        Dir.mkdir File.join(@dir, "node_modules", "asoul")

        $vim.edit File.join(@dir, "README.txt")
        complete("Nedit sou").must_equal "soul/ soulstash/"
      end

      it "must return files and directories in module's directory" do
        touch File.join(@dir, "node_modules", "soul", "index.js")
        touch File.join(@dir, "node_modules", "soul", "test", "test.js")
        $vim.edit File.join(@dir, "README.txt")
        complete("Nedit soul/").must_equal "soul/index.js soul/test/"
      end

      it "must return files and directories given a double slash" do
        touch File.join(@dir, "node_modules", "soul", "index.js")
        touch File.join(@dir, "node_modules", "soul", "test", "test.js")
        $vim.edit File.join(@dir, "README.txt")
        complete("Nedit soul//").must_equal "soul//index.js soul//test/"
      end

      it "must return files case-insensitively given &fileignorecase" do
        skip if $vim.echo(%(exists("&fileignorecase"))) != "1"

        $vim.command("set fileignorecase")
        $vim.command("set nowildignorecase")

        touch File.join(@dir, "node_modules", "soul", "index.js")
        touch File.join(@dir, "node_modules", "soul", "CHANGELOG")
        $vim.edit File.join(@dir, "README.txt")
        complete("Nedit soul/chan").must_equal "soul/CHANGELOG"
      end

      it "must return files case-insensitively given only &wildignorecase" do
        skip if $vim.echo(%(exists("&wildignorecase"))) != "1"

        $vim.command("set nofileignorecase")
        $vim.command("set wildignorecase")

        touch File.join(@dir, "node_modules", "soul", "index.js")
        touch File.join(@dir, "node_modules", "soul", "CHANGELOG")
        $vim.edit File.join(@dir, "README.txt")
        complete("Nedit soul/chan").must_equal "soul/CHANGELOG"
      end
    end
  end

  describe ":Nopen" do
    it "must edit and lcd to module's directory" do
      touch File.join(@dir, "node_modules", "foo", "package.json")
      touch File.join(@dir, "node_modules", "foo", "index.js")

      $vim.edit File.join(@dir, "README.txt")
      $vim.command("vsplit")

      $vim.command("Nopen foo")
      $vim.echo(%(bufname("%"))).must_equal "index.js"
      $vim.command("pwd").must_equal File.join(@dir, "node_modules", "foo")

      $vim.command("wincmd p")
      $vim.command("pwd").must_equal Dir.pwd
    end

    it "must edit and lcd to module's root directory" do
      touch File.join(@dir, "node_modules", "foo", "package.json")
      touch File.join(@dir, "node_modules", "foo", "lib", "utils.js")

      $vim.edit File.join(@dir, "README.txt")
      $vim.command("vsplit")

      $vim.command("Nopen foo/lib/utils")
      $vim.echo(%(bufname("%"))).must_equal "lib/utils.js"
      $vim.command("pwd").must_equal File.join(@dir, "node_modules", "foo")

      $vim.command("wincmd p")
      $vim.command("pwd").must_equal Dir.pwd
    end
  end
end