summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/api/highlight_spec.lua
blob: a9723b59cb1c225130b6cff2e46d5ec9d48181fc (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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local clear, eq, neq = n.clear, t.eq, t.neq
local command = n.command
local exec_capture = n.exec_capture
local api = n.api
local fn = n.fn
local pcall_err = t.pcall_err
local ok = t.ok
local assert_alive = n.assert_alive

describe('API: set highlight', function()
  local highlight_color = {
    fg = tonumber('0xff0000'),
    bg = tonumber('0x0032aa'),
    ctermfg = 8,
    ctermbg = 15,
  }
  local highlight1 = {
    bg = highlight_color.bg,
    fg = highlight_color.fg,
    bold = true,
    italic = true,
  }
  local highlight2_config = {
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    underline = true,
    reverse = true,
  }
  local highlight2_result = {
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    underline = true,
    reverse = true,
  }
  local highlight3_config = {
    bg = highlight_color.bg,
    fg = highlight_color.fg,
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    bold = true,
    italic = true,
    reverse = true,
    underdashed = true,
    strikethrough = true,
    altfont = true,
    dim = true,
    blink = true,
    conceal = true,
    overline = true,
    cterm = {
      italic = true,
      reverse = true,
      strikethrough = true,
      altfont = true,
      dim = true,
      blink = true,
      conceal = true,
      overline = true,
      nocombine = true,
    },
  }
  local highlight3_result_gui = {
    bg = highlight_color.bg,
    fg = highlight_color.fg,
    bold = true,
    italic = true,
    reverse = true,
    underdashed = true,
    strikethrough = true,
    altfont = true,
    dim = true,
    blink = true,
    conceal = true,
    overline = true,
  }
  local highlight3_result_cterm = {
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    italic = true,
    reverse = true,
    strikethrough = true,
    altfont = true,
    dim = true,
    blink = true,
    conceal = true,
    overline = true,
    nocombine = true,
  }

  local function get_ns()
    local ns = api.nvim_create_namespace('Test_set_hl')
    api.nvim_set_hl_ns(ns)
    return ns
  end

  ---@param expect table<string, any>
  ---@param result table<string, any>
  ---@param cterm? boolean
  local function match(expect, result, cterm)
    for k, v in pairs(expect) do
      eq(v, cterm and result.cterm[k] or result[k])
    end
  end

  before_each(clear)

  it('validation', function()
    eq(
      "Invalid 'blend': out of range",
      pcall_err(api.nvim_set_hl, 0, 'Test_hl3', { fg = '#FF00FF', blend = 999 })
    )
    eq(
      "Invalid 'blend': expected Integer, got Array",
      pcall_err(api.nvim_set_hl, 0, 'Test_hl3', { fg = '#FF00FF', blend = {} })
    )
    -- 'url' is rejected. #38162
    eq("Invalid key: 'url'", pcall_err(api.nvim_set_hl, 0, 'Test', { url = 'https://example.com' }))
    assert_alive()
  end)

  it('can set gui highlight', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight1)
    match(highlight1, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('can set cterm highlight', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight2_config)
    match(highlight2_result, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('can set empty cterm attr', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', { cterm = {} })
    eq({}, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('cterm attr defaults to gui attr', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight1)
    match({
      bold = true,
      italic = true,
    }, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('can overwrite attr for cterm #test', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight3_config)
    match(highlight3_result_gui, api.nvim_get_hl(ns, { name = 'Test_hl' }))
    match(highlight3_result_cterm, api.nvim_get_hl(ns, { name = 'Test_hl' }), true)
  end)

  it('only allows one underline attribute #22371', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', {
      underdouble = true,
      underdotted = true,
      cterm = {
        underline = true,
        undercurl = true,
      },
    })
    local result = api.nvim_get_hl(ns, { name = 'Test_hl' })
    match({ undercurl = true }, result, true)
    match({ underdotted = true }, result)
  end)

  it('can set all underline cterm attributes #31385', function()
    local ns = get_ns()
    local attrs = { 'underline', 'undercurl', 'underdouble', 'underdotted', 'underdashed' }
    for _, attr in ipairs(attrs) do
      api.nvim_set_hl(ns, 'Test_' .. attr, { cterm = { [attr] = true } })
      match({ [attr] = true }, api.nvim_get_hl(ns, { name = 'Test_' .. attr }), true)
    end
  end)

  it('can set a highlight in the global namespace', function()
    api.nvim_set_hl(0, 'Test_hl', highlight2_config)
    eq(
      'Test_hl        xxx cterm=underline,reverse ctermfg=8 ctermbg=15 gui=underline,reverse',
      exec_capture('highlight Test_hl')
    )

    api.nvim_set_hl(0, 'Test_hl', { background = highlight_color.bg })
    eq('Test_hl        xxx guibg=#0032aa', exec_capture('highlight Test_hl'))

    api.nvim_set_hl(0, 'Test_hl2', highlight3_config)
    eq(
      'Test_hl2       xxx cterm=italic,reverse,strikethrough,altfont,dim,blink,conceal,overline,nocombine ctermfg=8 ctermbg=15 gui=bold,underdashed,italic,reverse,strikethrough,altfont,dim,blink,conceal,overline guifg=#ff0000 guibg=#0032aa',
      exec_capture('highlight Test_hl2')
    )

    -- Colors are stored with the name they are defined, but
    -- with canonical casing
    api.nvim_set_hl(0, 'Test_hl3', { bg = 'reD', fg = 'bLue' })
    eq('Test_hl3       xxx guifg=Blue guibg=Red', exec_capture('highlight Test_hl3'))
  end)

  it('can modify a highlight in the global namespace', function()
    api.nvim_set_hl(0, 'Test_hl3', { bg = 'red', fg = 'blue' })
    eq('Test_hl3       xxx guifg=Blue guibg=Red', exec_capture('highlight Test_hl3'))

    api.nvim_set_hl(0, 'Test_hl3', { bg = 'red' })
    eq('Test_hl3       xxx guibg=Red', exec_capture('highlight Test_hl3'))

    api.nvim_set_hl(0, 'Test_hl3', { ctermbg = 9, ctermfg = 12 })
    eq('Test_hl3       xxx ctermfg=12 ctermbg=9', exec_capture('highlight Test_hl3'))

    api.nvim_set_hl(0, 'Test_hl3', { ctermbg = 'red', ctermfg = 'blue' })
    eq('Test_hl3       xxx ctermfg=12 ctermbg=9', exec_capture('highlight Test_hl3'))

    api.nvim_set_hl(0, 'Test_hl3', { ctermbg = 9 })
    eq('Test_hl3       xxx ctermbg=9', exec_capture('highlight Test_hl3'))

    eq(
      "Invalid highlight color: 'redd'",
      pcall_err(api.nvim_set_hl, 0, 'Test_hl3', { fg = 'redd' })
    )

    eq(
      "Invalid highlight color: 'bleu'",
      pcall_err(api.nvim_set_hl, 0, 'Test_hl3', { ctermfg = 'bleu' })
    )

    api.nvim_set_hl(0, 'Test_hl3', { fg = '#FF00FF' })
    eq('Test_hl3       xxx guifg=#ff00ff', exec_capture('highlight Test_hl3'))

    eq(
      "Invalid highlight color: '#FF00FF'",
      pcall_err(api.nvim_set_hl, 0, 'Test_hl3', { ctermfg = '#FF00FF' })
    )

    for _, fg_val in ipairs { nil, 'NONE', 'nOnE', '', -1 } do
      api.nvim_set_hl(0, 'Test_hl3', { fg = fg_val })
      eq('Test_hl3       xxx cleared', exec_capture('highlight Test_hl3'))
    end

    api.nvim_set_hl(0, 'Test_hl3', { fg = '#FF00FF', blend = 50 })
    eq('Test_hl3       xxx guifg=#ff00ff blend=50', exec_capture('highlight Test_hl3'))
  end)

  it("correctly sets 'Normal' internal properties", function()
    -- Normal has some special handling internally. #18024
    api.nvim_set_hl(0, 'Normal', { fg = '#000083', bg = '#0000F3' })
    eq({ fg = 131, bg = 243 }, api.nvim_get_hl(0, { name = 'Normal' }))
  end)

  it('does not segfault on invalid group name #20009', function()
    eq(
      'Vim:E5248: Invalid character in group name',
      pcall_err(api.nvim_set_hl, 0, 'foo bar', { bold = true })
    )
    assert_alive()
  end)

  it('can be silenced if there are too many groups #38930', function()
    local n_groups = vim.tbl_count(api.nvim_get_hl(0, {}))
    local has_fail = false
    for i = n_groups + 1, 20000 do
      local _, msg = pcall(api.nvim_set_hl, 0, 'New_' .. i, { fg = '#000000' })
      local is_fail = type(msg) == 'string'
        and msg:find('Too many highlight and syntax groups$') ~= nil
      has_fail = has_fail or is_fail
    end
    eq('', exec_capture('messages'))
    eq(true, has_fail)
  end)

  it('update=true sets only specified keys', function()
    api.nvim_set_hl(0, 'TestGroup', { fg = '#ff0000', bg = '#0000ff', bold = true })
    api.nvim_set_hl(0, 'TestGroup', { bg = '#00ff00', update = true })
    local hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(tonumber('0xff0000'), hl.fg)
    eq(tonumber('0x00ff00'), hl.bg)
    eq(true, hl.bold)

    api.nvim_set_hl(0, 'TestGroup', { bold = false, update = true })
    hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(nil, hl.bold)
    eq(tonumber('0xff0000'), hl.fg)

    api.nvim_set_hl(0, 'TestGroup', { italic = true })

    hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(nil, hl.fg)
    eq(nil, hl.bg)
    eq(true, hl.italic)

    local ns = api.nvim_create_namespace('test')
    api.nvim_set_hl(ns, 'TestGroup', { fg = '#ff0000', italic = true })
    api.nvim_set_hl(ns, 'TestGroup', { fg = '#00ff00', update = true })
    hl = api.nvim_get_hl(ns, { name = 'TestGroup' })
    eq(tonumber('0x00ff00'), hl.fg)
    eq(true, hl.italic)

    api.nvim_set_hl(0, 'NamedColor', { fg = 'red', bg = 'blue' })
    api.nvim_set_hl(0, 'LinkedGroup', { link = 'NamedColor' })
    api.nvim_set_hl(0, 'LinkedGroup', { bold = true, fg = 'green', update = true })
    hl = api.nvim_get_hl(0, { name = 'LinkedGroup' })
    eq(nil, hl.link)
    eq(true, hl.bold)
    eq(
      'LinkedGroup    xxx cterm=bold gui=bold guifg=Green guibg=Blue',
      n.exec_capture('hi LinkedGroup')
    )
    api.nvim_set_hl(0, 'LinkedGroup', { bg = '#121314', update = true })
    eq(
      'LinkedGroup    xxx cterm=bold gui=bold guifg=Green guibg=#121314',
      n.exec_capture('hi LinkedGroup')
    )

    -- underline style flags: false must not corrupt other styles
    local unders = { 'underline', 'undercurl', 'underdouble', 'underdotted', 'underdashed' }
    for _, a in ipairs(unders) do
      for _, b in ipairs(unders) do
        if a ~= b then
          api.nvim_set_hl(0, 'TestGroup', { [a] = true, [b] = false })
          hl = api.nvim_get_hl(0, { name = 'TestGroup' })
          eq(true, hl[a])
          eq(nil, hl[b])
        end
      end
    end
    api.nvim_set_hl(0, 'TestGroup', { underdouble = true, fg = '#ff0000', bold = true })
    api.nvim_set_hl(0, 'TestGroup', { fg = '#00ff00', update = true })
    hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(true, hl.underdouble)
    eq(true, hl.bold)
    eq(65280, hl.fg)

    api.nvim_set_hl(0, 'TestGroup', { underdashed = true, update = true })
    hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(true, hl.underdashed)
    eq(nil, hl.underdouble)

    api.nvim_set_hl(0, 'TestGroup', { underdouble = true, bold = true })
    api.nvim_set_hl(0, 'TestGroup', { underdashed = false, update = true })
    hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(true, hl.underdouble)
    api.nvim_set_hl(0, 'TestGroup', { underdouble = false, update = true })
    hl = api.nvim_get_hl(0, { name = 'TestGroup' })
    eq(nil, hl.underdouble)
    eq(true, hl.bold)
  end)

  it('can set font', function()
    local ns = api.nvim_create_namespace('test_font')
    api.nvim_set_hl(ns, 'TestFont', { fg = '#ff0000', font = 'Courier New 10' })
    local hl = api.nvim_get_hl(ns, { name = 'TestFont' })
    eq('Courier New 10', hl.font)
    eq(16711680, hl.fg)

    api.nvim_set_hl(ns, 'TestFont', { font = 'Monaco' })
    hl = api.nvim_get_hl(ns, { name = 'TestFont' })
    eq('Monaco', hl.font)

    -- Clear font with "NONE"
    api.nvim_set_hl(ns, 'TestFont', { font = 'NONE' })
    hl = api.nvim_get_hl(ns, { name = 'TestFont' })
    eq(nil, hl.font)

    -- global namespace
    api.nvim_set_hl(0, 'TestFontGlobal', { bg = '#00ff00', font = 'JetBrains Mono' })
    hl = api.nvim_get_hl(0, { name = 'TestFontGlobal' })
    eq('JetBrains Mono', hl.font)
    eq(65280, hl.bg)
  end)
end)

describe('API: get highlight', function()
  local highlight_color = {
    fg = tonumber('0xff0000'),
    bg = tonumber('0x0032aa'),
    ctermfg = 8,
    ctermbg = 15,
  }
  local highlight1 = {
    bg = highlight_color.bg,
    fg = highlight_color.fg,
    bold = true,
    italic = true,
    cterm = { bold = true, italic = true },
  }
  local highlight2 = {
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    underline = true,
    reverse = true,
    cterm = { underline = true, reverse = true },
  }
  local highlight3_config = {
    bg = highlight_color.bg,
    fg = highlight_color.fg,
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    bold = true,
    italic = true,
    reverse = true,
    underdashed = true,
    strikethrough = true,
    altfont = true,
    dim = true,
    blink = true,
    conceal = true,
    overline = true,
    cterm = {
      italic = true,
      reverse = true,
      strikethrough = true,
      altfont = true,
      dim = true,
      blink = true,
      conceal = true,
      overline = true,
      nocombine = true,
    },
  }
  local highlight3_result = {
    bg = highlight_color.bg,
    fg = highlight_color.fg,
    ctermbg = highlight_color.ctermbg,
    ctermfg = highlight_color.ctermfg,
    bold = true,
    italic = true,
    reverse = true,
    underdashed = true,
    strikethrough = true,
    altfont = true,
    dim = true,
    blink = true,
    conceal = true,
    overline = true,
    cterm = {
      italic = true,
      nocombine = true,
      reverse = true,
      strikethrough = true,
      altfont = true,
      dim = true,
      blink = true,
      conceal = true,
      overline = true,
    },
  }

  local function get_ns()
    -- Test namespace filtering behavior
    local ns2 = api.nvim_create_namespace('Another_namespace')
    api.nvim_set_hl(ns2, 'Test_hl', { ctermfg = 23 })
    api.nvim_set_hl(ns2, 'Test_another_hl', { link = 'Test_hl' })
    api.nvim_set_hl(ns2, 'Test_hl_link', { link = 'Test_another_hl' })
    api.nvim_set_hl(ns2, 'Test_another_hl_link', { link = 'Test_hl_link' })

    local ns = api.nvim_create_namespace('Test_set_hl')
    api.nvim_set_hl_ns(ns)

    return ns
  end

  before_each(clear)

  it('validation', function()
    eq(
      "Invalid 'name': expected String, got Integer",
      pcall_err(api.nvim_get_hl, 0, { name = 177 })
    )
    eq('Highlight id out of bounds', pcall_err(api.nvim_get_hl, 0, { name = 'Test set hl' }))
  end)

  it('nvim_get_hl with create flag', function()
    eq({}, api.nvim_get_hl(0, { name = 'Foo', create = false }))
    eq(0, fn.hlexists('Foo'))
    api.nvim_get_hl(0, { name = 'Bar', create = true })
    eq(1, fn.hlexists('Bar'))
    api.nvim_get_hl(0, { name = 'FooBar' })
    eq(1, fn.hlexists('FooBar'))
  end)

  it('can get all highlights in current namespace', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', { bg = '#B4BEFE' })
    api.nvim_set_hl(ns, 'Test_hl_link', { link = 'Test_hl' })
    eq({
      Test_hl = {
        bg = 11845374,
      },
      Test_hl_link = {
        link = 'Test_hl',
      },
    }, api.nvim_get_hl(ns, {}))
  end)

  it('can get gui highlight', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight1)
    eq(highlight1, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('can get cterm highlight', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight2)
    eq(highlight2, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('can get empty cterm attr', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', { cterm = {} })
    eq({}, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('cterm attr defaults to gui attr', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight1)
    eq(highlight1, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('can overwrite attr for cterm', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', highlight3_config)
    eq(highlight3_result, api.nvim_get_hl(ns, { name = 'Test_hl' }))
  end)

  it('only allows one underline attribute #22371', function()
    local ns = get_ns()
    api.nvim_set_hl(ns, 'Test_hl', {
      underdouble = true,
      underdotted = true,
      cterm = {
        underline = true,
        undercurl = true,
      },
    })
    eq(
      { underdotted = true, cterm = { undercurl = true } },
      api.nvim_get_hl(ns, { name = 'Test_hl' })
    )
  end)

  it('can get a highlight in the global namespace', function()
    api.nvim_set_hl(0, 'Test_hl', highlight2)
    eq(highlight2, api.nvim_get_hl(0, { name = 'Test_hl' }))

    api.nvim_set_hl(0, 'Test_hl', { background = highlight_color.bg })
    eq({
      bg = 12970,
    }, api.nvim_get_hl(0, { name = 'Test_hl' }))

    api.nvim_set_hl(0, 'Test_hl2', highlight3_config)
    eq(highlight3_result, api.nvim_get_hl(0, { name = 'Test_hl2' }))

    -- Colors are stored with the name they are defined, but
    -- with canonical casing
    api.nvim_set_hl(0, 'Test_hl3', { bg = 'reD', fg = 'bLue' })
    eq({
      bg = 16711680,
      fg = 255,
    }, api.nvim_get_hl(0, { name = 'Test_hl3' }))
  end)

  it('nvim_get_hl by id', function()
    local hl_id = api.nvim_get_hl_id_by_name('NewHighlight')

    command(
      'hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold'
    )
    eq({
      fg = 16711680,
      bg = 16776960,
      sp = 255,
      bold = true,
      ctermbg = 10,
      cterm = { underline = true },
    }, api.nvim_get_hl(0, { id = hl_id }))

    -- Test 0 argument
    eq('Highlight id out of bounds', pcall_err(api.nvim_get_hl, 0, { id = 0 }))

    eq(
      "Invalid 'id': expected Integer, got String",
      pcall_err(api.nvim_get_hl, 0, { id = 'Test_set_hl' })
    )

    -- Test all highlight properties.
    command(
      'hi NewHighlight gui=underline,bold,italic,reverse,strikethrough,altfont,dim,blink,conceal,overline,nocombine'
    )
    eq({
      fg = 16711680,
      bg = 16776960,
      sp = 255,
      altfont = true,
      blink = true,
      bold = true,
      conceal = true,
      dim = true,
      italic = true,
      nocombine = true,
      overline = true,
      reverse = true,
      strikethrough = true,
      underline = true,
      ctermbg = 10,
      cterm = { underline = true },
    }, api.nvim_get_hl(0, { id = hl_id }))

    -- Test undercurl
    command('hi NewHighlight gui=undercurl')
    eq({
      fg = 16711680,
      bg = 16776960,
      sp = 255,
      undercurl = true,
      ctermbg = 10,
      cterm = { underline = true },
    }, api.nvim_get_hl(0, { id = hl_id }))
  end)

  it('can correctly detect links', function()
    command('hi String guifg=#a6e3a1 ctermfg=NONE')
    command('hi link @string string')
    command('hi link @string.cpp @string')
    eq({ fg = 10937249 }, api.nvim_get_hl(0, { name = 'String' }))
    eq({ link = 'String' }, api.nvim_get_hl(0, { name = '@string' }))
    eq({ fg = 10937249 }, api.nvim_get_hl(0, { name = '@string.cpp', link = false }))
  end)

  it('can get all attributes for a linked group', function()
    command('hi Bar guifg=red')
    command('hi Foo guifg=#00ff00 gui=bold,underline')
    command('hi! link Foo Bar')
    eq(
      { link = 'Bar', fg = tonumber('00ff00', 16), bold = true, underline = true },
      api.nvim_get_hl(0, { name = 'Foo', link = true })
    )
  end)

  it('can set link as well as other attributes', function()
    command('hi Bar guifg=red')
    local hl = { link = 'Bar', fg = tonumber('00ff00', 16), bold = true, cterm = { bold = true } }
    api.nvim_set_hl(0, 'Foo', hl)
    eq(hl, api.nvim_get_hl(0, { name = 'Foo', link = true }))
  end)

  it('link_global resolves in global namespace', function()
    local ns = api.nvim_create_namespace('hl_test')
    api.nvim_set_hl(0, 'GlTarget', { fg = '#ff0000' })
    api.nvim_set_hl(ns, 'GlSource', { link_global = fn.hlID('GlTarget') })
    eq({ link = 'GlTarget' }, api.nvim_get_hl(ns, { name = 'GlSource' }))

    -- when both link and link_global are given, link_global wins
    api.nvim_set_hl(0, 'OtherTarget', { fg = '#00ff00' })
    api.nvim_set_hl(
      ns,
      'GlSource',
      { link = fn.hlID('OtherTarget'), link_global = fn.hlID('GlTarget') }
    )
    eq({ link = 'GlTarget' }, api.nvim_get_hl(ns, { name = 'GlSource' }))
  end)

  it("doesn't contain unset groups", function()
    local id = api.nvim_get_hl_id_by_name '@foobar.hubbabubba'
    ok(id > 0)

    local data = api.nvim_get_hl(0, {})
    eq(nil, data['@foobar.hubbabubba'])
    eq(nil, data['@foobar'])

    command 'hi @foobar.hubbabubba gui=bold'
    data = api.nvim_get_hl(0, {})
    eq({ bold = true }, data['@foobar.hubbabubba'])
    eq(nil, data['@foobar'])

    -- @foobar.hubbabubba was explicitly cleared and thus shows up
    -- but @foobar was never touched, and thus doesn't
    command 'hi clear @foobar.hubbabubba'
    data = api.nvim_get_hl(0, {})
    eq({}, data['@foobar.hubbabubba'])
    eq(nil, data['@foobar'])
  end)

  it('should return default flag', function()
    api.nvim_set_hl(0, 'Tried', { fg = '#00ff00', default = true })
    eq({ fg = tonumber('00ff00', 16), default = true }, api.nvim_get_hl(0, { name = 'Tried' }))
  end)

  it('should not output empty gui and cterm #23474', function()
    api.nvim_set_hl(0, 'Foo', { default = true })
    api.nvim_set_hl(0, 'Bar', { default = true, fg = '#ffffff' })
    api.nvim_set_hl(0, 'FooBar', { default = true, fg = '#ffffff', cterm = { bold = true } })
    api.nvim_set_hl(
      0,
      'FooBarA',
      { default = true, fg = '#ffffff', cterm = { bold = true, italic = true } }
    )

    eq('Foo            xxx cleared', exec_capture('highlight Foo'))
    eq({ default = true }, api.nvim_get_hl(0, { name = 'Foo' }))
    eq('Bar            xxx guifg=#ffffff', exec_capture('highlight Bar'))
    eq('FooBar         xxx cterm=bold guifg=#ffffff', exec_capture('highlight FooBar'))
    eq('FooBarA        xxx cterm=bold,italic guifg=#ffffff', exec_capture('highlight FooBarA'))
  end)

  it('can override exist highlight group by force #20323', function()
    local white = tonumber('ffffff', 16)
    local green = tonumber('00ff00', 16)
    api.nvim_set_hl(0, 'Foo', { fg = white })
    api.nvim_set_hl(0, 'Foo', { fg = green, force = true })
    eq({ fg = green }, api.nvim_get_hl(0, { name = 'Foo' }))
    api.nvim_set_hl(0, 'Bar', { link = 'Comment', default = true })
    api.nvim_set_hl(0, 'Bar', { link = 'Foo', default = true, force = true })
    eq({ link = 'Foo', default = true }, api.nvim_get_hl(0, { name = 'Bar' }))
  end)

  it('round-trips fg_indexed/bg_indexed through nvim_get_hl', function()
    api.nvim_set_hl(0, 'Test_idx', {
      fg = '#cc0000',
      bg = '#0000cc',
      ctermfg = 1,
      ctermbg = 4,
      fg_indexed = true,
      bg_indexed = true,
    })
    local hl = api.nvim_get_hl(0, { name = 'Test_idx' })
    eq(true, hl.fg_indexed)
    eq(true, hl.bg_indexed)
    eq(tonumber('0xcc0000'), hl.fg)
    eq(tonumber('0x0000cc'), hl.bg)
    eq(1, hl.ctermfg)
    eq(4, hl.ctermbg)

    api.nvim_set_hl(0, 'Test_idx', { fg_indexed = false, update = true })
    eq(nil, api.nvim_get_hl(0, { name = 'Test_idx' }).fg_indexed)
    eq(true, api.nvim_get_hl(0, { name = 'Test_idx' }).bg_indexed)
    eq(tonumber('0xcc0000'), api.nvim_get_hl(0, { name = 'Test_idx' }).fg)
  end)
end)

describe('API: set/get highlight namespace', function()
  before_each(clear)

  it('set/get highlight namespace', function()
    eq(0, api.nvim_get_hl_ns({}))
    local ns = api.nvim_create_namespace('')
    api.nvim_set_hl_ns(ns)
    eq(ns, api.nvim_get_hl_ns({}))
  end)

  it('set/get window highlight namespace', function()
    eq(-1, api.nvim_get_hl_ns({ winid = 0 }))
    local ns = api.nvim_create_namespace('')
    api.nvim_win_set_hl_ns(0, ns)
    eq(ns, api.nvim_get_hl_ns({ winid = 0 }))
  end)

  it('setting namespace takes priority over &winhighlight', function()
    local win = api.nvim_get_current_win()
    eq(-1, api.nvim_get_hl_ns({ winid = win }))
    command('set winhighlight=Visual:Search')
    local winhl_ns = api.nvim_get_hl_ns({ winid = win })
    neq(0, winhl_ns)
    eq('Search', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
    n.insert('foobar')
    local ns = api.nvim_create_namespace('')
    neq(winhl_ns, ns)
    api.nvim_win_set_hl_ns(win, ns)
    eq(ns, api.nvim_get_hl_ns({ winid = win }))
    command('enew') -- Switching buffer keeps namespace. #30904
    eq(ns, api.nvim_get_hl_ns({ winid = win }))
    command('set winhighlight=')
    eq(ns, api.nvim_get_hl_ns({ winid = win }))
    -- Setting 'winhighlight' changes its namespace even when not using it.
    command('set winhighlight=Visual:IncSearch')
    eq('IncSearch', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
    eq(ns, api.nvim_get_hl_ns({ winid = win }))
    -- Setting 'winhighlight' works with global namespace. #37865
    api.nvim_win_set_hl_ns(win, 0)
    eq(0, api.nvim_get_hl_ns({ winid = win }))
    command('set winhighlight=Visual:IncSearch')
    eq('IncSearch', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
    eq(winhl_ns, api.nvim_get_hl_ns({ winid = win }))
    command('set winhighlight=')
    eq(0, api.nvim_get_hl_ns({ winid = win }))
    -- 'winhighlight' keeps the same namespace.
    api.nvim_win_set_hl_ns(win, winhl_ns)
    eq(winhl_ns, api.nvim_get_hl_ns({ winid = win }))
    command('set winhighlight=Visual:Substitute')
    eq('Substitute', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
    eq(winhl_ns, api.nvim_get_hl_ns({ winid = win }))
  end)
end)