highlight_spec.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear, nvim = helpers.clear, helpers.nvim
  3. local Screen = require('test.functional.ui.screen')
  4. local eq, eval = helpers.eq, helpers.eval
  5. local command = helpers.command
  6. local meths = helpers.meths
  7. describe('API: highlight',function()
  8. local expected_rgb = {
  9. background = Screen.colors.Yellow,
  10. foreground = Screen.colors.Red,
  11. special = Screen.colors.Blue,
  12. bold = true,
  13. }
  14. local expected_cterm = {
  15. background = 10,
  16. underline = true,
  17. }
  18. local expected_rgb2 = {
  19. background = Screen.colors.Yellow,
  20. foreground = Screen.colors.Red,
  21. special = Screen.colors.Blue,
  22. bold = true,
  23. italic = true,
  24. reverse = true,
  25. undercurl = true,
  26. underline = true,
  27. }
  28. before_each(function()
  29. clear()
  30. command("hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold")
  31. end)
  32. it("nvim_get_hl_by_id", function()
  33. local hl_id = eval("hlID('NewHighlight')")
  34. eq(expected_cterm, nvim("get_hl_by_id", hl_id, false))
  35. hl_id = eval("hlID('NewHighlight')")
  36. -- Test valid id.
  37. eq(expected_rgb, nvim("get_hl_by_id", hl_id, true))
  38. -- Test invalid id.
  39. local err, emsg = pcall(meths.get_hl_by_id, 30000, false)
  40. eq(false, err)
  41. eq('Invalid highlight id: 30000', string.match(emsg, 'Invalid.*'))
  42. -- Test all highlight properties.
  43. command('hi NewHighlight gui=underline,bold,undercurl,italic,reverse')
  44. eq(expected_rgb2, nvim("get_hl_by_id", hl_id, true))
  45. -- Test nil argument.
  46. err, emsg = pcall(meths.get_hl_by_id, { nil }, false)
  47. eq(false, err)
  48. eq('Wrong type for argument 1, expecting Integer',
  49. string.match(emsg, 'Wrong.*'))
  50. -- Test 0 argument.
  51. err, emsg = pcall(meths.get_hl_by_id, 0, false)
  52. eq(false, err)
  53. eq('Invalid highlight id: 0',
  54. string.match(emsg, 'Invalid.*'))
  55. -- Test -1 argument.
  56. err, emsg = pcall(meths.get_hl_by_id, -1, false)
  57. eq(false, err)
  58. eq('Invalid highlight id: -1',
  59. string.match(emsg, 'Invalid.*'))
  60. end)
  61. it("nvim_get_hl_by_name", function()
  62. local expected_normal = { background = Screen.colors.Yellow,
  63. foreground = Screen.colors.Red }
  64. -- Test `Normal` default values.
  65. eq({}, nvim("get_hl_by_name", 'Normal', true))
  66. eq(expected_cterm, nvim("get_hl_by_name", 'NewHighlight', false))
  67. eq(expected_rgb, nvim("get_hl_by_name", 'NewHighlight', true))
  68. -- Test `Normal` modified values.
  69. command('hi Normal guifg=red guibg=yellow')
  70. eq(expected_normal, nvim("get_hl_by_name", 'Normal', true))
  71. -- Test invalid name.
  72. local err, emsg = pcall(meths.get_hl_by_name , 'unknown_highlight', false)
  73. eq(false, err)
  74. eq('Invalid highlight name: unknown_highlight',
  75. string.match(emsg, 'Invalid.*'))
  76. -- Test nil argument.
  77. err, emsg = pcall(meths.get_hl_by_name , { nil }, false)
  78. eq(false, err)
  79. eq('Wrong type for argument 1, expecting String',
  80. string.match(emsg, 'Wrong.*'))
  81. -- Test empty string argument.
  82. err, emsg = pcall(meths.get_hl_by_name , '', false)
  83. eq(false, err)
  84. eq('Invalid highlight name: ',
  85. string.match(emsg, 'Invalid.*'))
  86. -- Test "standout" attribute. #8054
  87. eq({ underline = true, },
  88. meths.get_hl_by_name('cursorline', 0));
  89. command('hi CursorLine cterm=standout,underline term=standout,underline gui=standout,underline')
  90. command('set cursorline')
  91. eq({ underline = true, standout = true, },
  92. meths.get_hl_by_name('cursorline', 0));
  93. end)
  94. end)