test: nvim_get_hl_by_name/by_id #7082

- test all properties
- test failure modes
This commit is contained in:
Justin M. Keyes 2017-10-08 20:23:11 +02:00
parent 04187a1c74
commit 52517321d1
3 changed files with 65 additions and 22 deletions

View File

@ -33,7 +33,6 @@
#include "nvim/syntax.h" #include "nvim/syntax.h"
#include "nvim/getchar.h" #include "nvim/getchar.h"
#include "nvim/os/input.h" #include "nvim/os/input.h"
#include "nvim/ui.h"
#define LINE_BUFFER_SIZE 4096 #define LINE_BUFFER_SIZE 4096

View File

@ -7127,8 +7127,7 @@ syn_list_header(int did_header, int outlen, int id)
/// Set the attribute numbers for a highlight group. /// Set the attribute numbers for a highlight group.
/// Called after one of the attributes has changed. /// Called after one of the attributes has changed.
/// @param idx corrected highlight index /// @param idx corrected highlight index
static void static void set_hl_attr(int idx)
set_hl_attr(int idx)
{ {
attrentry_T at_en = ATTRENTRY_INIT; attrentry_T at_en = ATTRENTRY_INIT;
struct hl_group *sgp = HL_TABLE() + idx; struct hl_group *sgp = HL_TABLE() + idx;
@ -8241,9 +8240,7 @@ RgbValue name_to_color(const uint8_t *name)
return -1; return -1;
} }
/// Retrieves attribute description from its id /// Gets highlight description for id `attr_id` as a map.
///
/// @param attr_id attribute id
Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err) Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err)
{ {
HlAttrs attrs = HLATTRS_INIT; HlAttrs attrs = HLATTRS_INIT;
@ -8256,7 +8253,7 @@ Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err)
attrentry_T *aep = syn_cterm_attr2entry((int)attr_id); attrentry_T *aep = syn_cterm_attr2entry((int)attr_id);
if (!aep) { if (!aep) {
api_set_error(err, kErrorTypeException, api_set_error(err, kErrorTypeException,
"Invalid attribute id %d", attr_id); "Invalid attribute id: %d", attr_id);
return dic; return dic;
} }

View File

@ -6,15 +6,26 @@ local command = helpers.command
local meths = helpers.meths local meths = helpers.meths
describe('highlight api',function() describe('highlight api',function()
local expected_rgb = { background = Screen.colors.Yellow, local expected_rgb = {
foreground = Screen.colors.Red, background = Screen.colors.Yellow,
special = Screen.colors.Blue, foreground = Screen.colors.Red,
bold = true, special = Screen.colors.Blue,
} bold = true,
}
local expected_cterm = { background = 10, local expected_cterm = {
underline = true, background = 10,
} underline = true,
}
local expected_rgb2 = {
background = Screen.colors.Yellow,
foreground = Screen.colors.Red,
special = Screen.colors.Blue,
bold = true,
italic = true,
reverse = true,
undercurl = true,
underline = true,
}
before_each(function() before_each(function()
clear() clear()
@ -26,31 +37,67 @@ describe('highlight api',function()
eq(expected_cterm, nvim("get_hl_by_id", hl_id, false)) eq(expected_cterm, nvim("get_hl_by_id", hl_id, false))
hl_id = eval("hlID('NewHighlight')") hl_id = eval("hlID('NewHighlight')")
-- Test valid id.
eq(expected_rgb, nvim("get_hl_by_id", hl_id, true)) eq(expected_rgb, nvim("get_hl_by_id", hl_id, true))
local expected_error = 'Invalid highlight id' -- Test invalid id.
-- Assume there is no hl id 30000.
local err, emsg = pcall(meths.get_hl_by_id, 30000, false) local err, emsg = pcall(meths.get_hl_by_id, 30000, false)
eq(false, err) eq(false, err)
eq(expected_error, string.match(emsg, expected_error)) eq('Invalid highlight id: 30000', string.match(emsg, 'Invalid.*'))
-- Test all highlight properties.
command('hi NewHighlight gui=underline,bold,undercurl,italic,reverse')
eq(expected_rgb2, nvim("get_hl_by_id", hl_id, true))
-- Test nil argument.
err, emsg = pcall(meths.get_hl_by_id, { nil }, false)
eq(false, err)
eq('Wrong type for argument 1, expecting Integer',
string.match(emsg, 'Wrong.*'))
-- Test 0 argument.
err, emsg = pcall(meths.get_hl_by_id, 0, false)
eq(false, err)
eq('Invalid highlight id: 0',
string.match(emsg, 'Invalid.*'))
-- Test -1 argument.
err, emsg = pcall(meths.get_hl_by_id, -1, false)
eq(false, err)
eq('Invalid highlight id: -1',
string.match(emsg, 'Invalid.*'))
end) end)
it("nvim_get_hl_by_name", function() it("nvim_get_hl_by_name", function()
local expected_normal = { background = Screen.colors.Yellow, local expected_normal = { background = Screen.colors.Yellow,
foreground = Screen.colors.Red } foreground = Screen.colors.Red }
-- Test "Normal" hl defaults. -- Test `Normal` default values.
eq({}, nvim("get_hl_by_name", 'Normal', true)) eq({}, nvim("get_hl_by_name", 'Normal', true))
eq(expected_cterm, nvim("get_hl_by_name", 'NewHighlight', false)) eq(expected_cterm, nvim("get_hl_by_name", 'NewHighlight', false))
eq(expected_rgb, nvim("get_hl_by_name", 'NewHighlight', true)) eq(expected_rgb, nvim("get_hl_by_name", 'NewHighlight', true))
-- Test `Normal` modified values.
command('hi Normal guifg=red guibg=yellow') command('hi Normal guifg=red guibg=yellow')
eq(expected_normal, nvim("get_hl_by_name", 'Normal', true)) eq(expected_normal, nvim("get_hl_by_name", 'Normal', true))
local expected_error = 'Invalid highlight name' -- Test invalid name.
local err, emsg = pcall(meths.get_hl_by_name , 'unknown_highlight', false) local err, emsg = pcall(meths.get_hl_by_name , 'unknown_highlight', false)
eq(false, err) eq(false, err)
eq(expected_error, string.match(emsg, expected_error)) eq('Invalid highlight name: unknown_highlight',
string.match(emsg, 'Invalid.*'))
-- Test nil argument.
err, emsg = pcall(meths.get_hl_by_name , { nil }, false)
eq(false, err)
eq('Wrong type for argument 1, expecting String',
string.match(emsg, 'Wrong.*'))
-- Test empty string argument.
err, emsg = pcall(meths.get_hl_by_name , '', false)
eq(false, err)
eq('Invalid highlight name: ',
string.match(emsg, 'Invalid.*'))
end) end)
end) end)