1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-25 12:26:40 +02:00

tests: add tests on config functions (scripting API)

This commit is contained in:
Sébastien Helleu
2022-10-15 21:31:45 +02:00
parent ec11126246
commit a1057c9a03
5 changed files with 326 additions and 50 deletions
+220 -14
View File
@@ -37,8 +37,8 @@ def check(result, condition, lineno):
weechat.prnt('', ' TEST OK: ' + condition)
else:
weechat.prnt('',
'SCRIPT_SOURCE' + ':' + lineno + ':1: ' +
'ERROR: [' + 'SCRIPT_NAME' + '] condition is false: ' +
'{SCRIPT_SOURCE}' + ':' + lineno + ':1: ' +
'ERROR: [' + '{SCRIPT_NAME}' + '] condition is false: ' +
condition)
@@ -144,6 +144,211 @@ def test_lists():
weechat.list_free(ptr_list)
def config_reload_cb(data, config_file):
"""Config reload callback."""
return weechat.WEECHAT_RC_OK
def section_read_cb(data, config_file, section, option_name, value):
"""Section read callback."""
return weechat.WEECHAT_RC_OK
def section_write_cb(data, config_file, section_name):
"""Section write callback."""
return weechat.WEECHAT_RC_OK
def section_write_default_cb(data, config_file, section_name):
"""Section write default callback."""
return weechat.WEECHAT_RC_OK
def section_create_option_cb(data, config_file, section, option_name, value):
"""Section create option callback."""
return weechat.WEECHAT_RC_OK
def section_delete_option_cb(data, config_file, section, option):
"""Section delete option callback."""
return weechat.WEECHAT_RC_OK
def option_check_value_cb(data, option, value):
"""Option check value callback."""
return 1
def option_change_cb(data, option):
"""Option change callback."""
return 1
def option_delete_cb(data, option):
"""Option delete callback."""
return 1
def test_config():
"""Test config functions."""
# config
ptr_config = weechat.config_new(
'test_config_' + '{SCRIPT_LANGUAGE}',
'config_reload_cb', 'config_reload_data',
)
check(ptr_config != '')
# section
ptr_section = weechat.config_new_section(
ptr_config, 'section1', 0, 0,
'section_read_cb', '',
'section_write_cb', '',
'section_write_default_cb', '',
'section_create_option_cb', '',
'section_delete_option_cb', '',
)
check(ptr_section != '')
# search section
ptr_section2 = weechat.config_search_section(ptr_config, 'section1')
check(ptr_section2 == ptr_section)
# boolean option
ptr_opt_bool = weechat.config_new_option(
ptr_config, ptr_section, 'option_bool', 'boolean', 'bool option',
'', 0, 0, 'on', 'on', 0,
'option_check_value_cb', '',
'option_change_cb', '',
'option_delete_cb', '',
)
check(ptr_opt_bool != '')
check(weechat.config_boolean(ptr_opt_bool) == 1)
check(weechat.config_option_set(ptr_opt_bool, 'off', 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_set(ptr_opt_bool, 'off', 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_boolean(ptr_opt_bool) == 0)
check(weechat.config_boolean_default(ptr_opt_bool) == 1)
check(weechat.config_option_reset(ptr_opt_bool, 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_reset(ptr_opt_bool, 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_boolean(ptr_opt_bool) == 1)
# integer option
ptr_opt_int = weechat.config_new_option(
ptr_config, ptr_section, 'option_int', 'integer', 'int option',
'', 0, 256, '2', '2', 0,
'option_check_value_cb', '',
'option_change_cb', '',
'option_delete_cb', '',
)
check(ptr_opt_int != '')
check(weechat.config_integer(ptr_opt_int) == 2)
check(weechat.config_option_set(ptr_opt_int, '15', 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_set(ptr_opt_int, '15', 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_integer(ptr_opt_int) == 15)
check(weechat.config_integer_default(ptr_opt_int) == 2)
check(weechat.config_option_reset(ptr_opt_int, 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_reset(ptr_opt_int, 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_integer(ptr_opt_int) == 2)
# integer option (with string values)
ptr_opt_int_str = weechat.config_new_option(
ptr_config, ptr_section, 'option_int_str', 'integer', 'int option str',
'val1|val2|val3', 0, 0, 'val2', 'val2', 0,
'option_check_value_cb', '',
'option_change_cb', '',
'option_delete_cb', '',
)
check(ptr_opt_int_str != '')
check(weechat.config_integer(ptr_opt_int_str) == 1)
check(weechat.config_string(ptr_opt_int_str) == 'val2')
check(weechat.config_option_set(ptr_opt_int_str, 'val1', 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_set(ptr_opt_int_str, 'val1', 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_integer(ptr_opt_int_str) == 0)
check(weechat.config_string(ptr_opt_int_str) == 'val1')
check(weechat.config_integer_default(ptr_opt_int_str) == 1)
check(weechat.config_string_default(ptr_opt_int_str) == 'val2')
check(weechat.config_option_reset(ptr_opt_int_str, 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_reset(ptr_opt_int_str, 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_integer(ptr_opt_int_str) == 1)
check(weechat.config_string(ptr_opt_int_str) == 'val2')
# string option
ptr_opt_str = weechat.config_new_option(
ptr_config, ptr_section, 'option_str', 'string', 'str option',
'', 0, 0, 'value', 'value', 1,
'option_check_value_cb', '',
'option_change_cb', '',
'option_delete_cb', '',
)
check(ptr_opt_str != '')
check(weechat.config_string(ptr_opt_str) == 'value')
check(weechat.config_option_set(ptr_opt_str, 'value2', 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_set(ptr_opt_str, 'value2', 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_string(ptr_opt_str) == 'value2')
check(weechat.config_string_default(ptr_opt_str) == 'value')
check(weechat.config_option_reset(ptr_opt_str, 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_reset(ptr_opt_str, 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_string(ptr_opt_str) == 'value')
check(weechat.config_option_is_null(ptr_opt_str) == 0)
check(weechat.config_option_set_null(ptr_opt_str, 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_set_null(ptr_opt_str, 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_option_is_null(ptr_opt_str) == 1)
check(weechat.config_string(ptr_opt_str) == '')
check(weechat.config_option_unset(ptr_opt_str) == 1) # UNSET_OK_RESET
check(weechat.config_option_unset(ptr_opt_str) == 0) # UNSET_OK_NO_RESET
check(weechat.config_string(ptr_opt_str) == 'value')
check(weechat.config_option_default_is_null(ptr_opt_str) == 0)
# color option
ptr_opt_col = weechat.config_new_option(
ptr_config, ptr_section, 'option_col', 'color', 'col option',
'', 0, 0, 'lightgreen', 'lightgreen', 0,
'option_check_value_cb', '',
'option_change_cb', '',
'option_delete_cb', '',
)
check(ptr_opt_col != '')
check(weechat.config_color(ptr_opt_col) == 'lightgreen')
check(weechat.config_option_set(ptr_opt_col, 'red', 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_set(ptr_opt_col, 'red', 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_color(ptr_opt_col) == 'red')
check(weechat.config_color_default(ptr_opt_col) == 'lightgreen')
check(weechat.config_option_reset(ptr_opt_col, 1) == 2) # SET_OK_CHANGED
check(weechat.config_option_reset(ptr_opt_col, 1) == 1) # SET_OK_SAME_VALUE
check(weechat.config_color(ptr_opt_col) == 'lightgreen')
# search option
ptr_opt_bool2 = weechat.config_search_option(ptr_config, ptr_section,
'option_bool')
check(ptr_opt_bool2 == ptr_opt_bool)
# string to boolean
check(weechat.config_string_to_boolean('') == 0)
check(weechat.config_string_to_boolean('off') == 0)
check(weechat.config_string_to_boolean('0') == 0)
check(weechat.config_string_to_boolean('on') == 1)
check(weechat.config_string_to_boolean('1') == 1)
# rename option
weechat.config_option_rename(ptr_opt_bool, 'option_bool_renamed')
# read config (create it because it does not exist yet)
check(weechat.config_read(ptr_config) == 0) # CONFIG_READ_OK
# write config
check(weechat.config_write(ptr_config) == 0) # CONFIG_WRITE_OK
# reload config
check(weechat.config_reload(ptr_config) == 0) # CONFIG_READ_OK
# free option
weechat.config_option_free(ptr_opt_bool)
# free options in section
weechat.config_section_free_options(ptr_section)
# free section
weechat.config_section_free(ptr_section)
# free config
weechat.config_free(ptr_config)
# config_get
ptr_option = weechat.config_get('weechat.look.item_time_format')
check(ptr_option != '')
check(weechat.config_string(ptr_option) == '%H:%M')
# config plugin
check(weechat.config_get_plugin('option') == '')
check(weechat.config_is_set_plugin('option') == 0)
check(weechat.config_set_plugin('option', 'value') == 1) # SET_OK_SAME_VALUE
weechat.config_set_desc_plugin('option', 'description of option')
check(weechat.config_get_plugin('option') == 'value')
check(weechat.config_is_set_plugin('option') == 1)
check(weechat.config_unset_plugin('option') == 2) # UNSET_OK_REMOVED
check(weechat.config_unset_plugin('option') == -1) # UNSET_ERROR
def test_key():
"""Test key functions."""
check(
@@ -209,7 +414,7 @@ def test_display():
def completion_cb(data, completion_item, buf, completion):
"""Completion callback."""
check(data == 'completion_data')
check(completion_item == 'SCRIPT_NAME')
check(completion_item == '{SCRIPT_NAME}')
check(weechat.completion_get_string(completion, 'args') == 'w')
weechat.completion_list_add(completion, 'word_completed',
0, weechat.WEECHAT_LIST_POS_END)
@@ -226,7 +431,7 @@ def command_cb(data, buf, args):
def command_run_cb(data, buf, command):
"""Command_run callback."""
check(data == 'command_run_data')
check(command == '/cmd' + 'SCRIPT_NAME' + ' word_completed')
check(command == '/cmd' + '{SCRIPT_NAME}' + ' word_completed')
return weechat.WEECHAT_RC_OK
@@ -238,16 +443,16 @@ def timer_cb(data, remaining_calls):
def test_hooks():
"""Test function hook_command."""
# hook_completion / hook_completion_args / and hook_command
hook_cmplt = weechat.hook_completion('SCRIPT_NAME', 'description',
hook_cmplt = weechat.hook_completion('{SCRIPT_NAME}', 'description',
'completion_cb', 'completion_data')
hook_cmd = weechat.hook_command('cmd' + 'SCRIPT_NAME', 'description',
hook_cmd = weechat.hook_command('cmd' + '{SCRIPT_NAME}', 'description',
'arguments', 'description arguments',
'%(' + 'SCRIPT_NAME' + ')',
'%(' + '{SCRIPT_NAME}' + ')',
'command_cb', 'command_data')
weechat.command('', '/input insert /cmd' + 'SCRIPT_NAME' + ' w')
weechat.command('', '/input insert /cmd' + '{SCRIPT_NAME}' + ' w')
weechat.command('', '/input complete_next')
# hook_command_run
hook_cmd_run = weechat.hook_command_run('/cmd' + 'SCRIPT_NAME' + '*',
hook_cmd_run = weechat.hook_command_run('/cmd' + '{SCRIPT_NAME}' + '*',
'command_run_cb', 'command_run_data')
weechat.command('', '/input return')
weechat.unhook(hook_cmd_run)
@@ -419,11 +624,12 @@ def cmd_test_cb(data, buf, args):
"""Run all the tests."""
weechat.prnt('', '>>>')
weechat.prnt('', '>>> ------------------------------')
weechat.prnt('', '>>> Testing ' + 'SCRIPT_LANGUAGE' + ' API')
weechat.prnt('', ' > TESTS: ' + 'SCRIPT_TESTS')
weechat.prnt('', '>>> Testing ' + '{SCRIPT_LANGUAGE}' + ' API')
weechat.prnt('', ' > TESTS: ' + '{SCRIPT_TESTS}')
test_plugins()
test_strings()
test_lists()
test_config()
test_key()
test_display()
test_hooks()
@@ -436,6 +642,6 @@ def cmd_test_cb(data, buf, args):
def weechat_init():
"""Main function."""
weechat.register('SCRIPT_NAME', 'SCRIPT_AUTHOR', 'SCRIPT_VERSION',
'SCRIPT_LICENSE', 'SCRIPT_DESCRIPTION', '', '')
weechat.hook_command('SCRIPT_NAME', '', '', '', '', 'cmd_test_cb', '')
weechat.register('{SCRIPT_NAME}', '{SCRIPT_AUTHOR}', '{SCRIPT_VERSION}',
'{SCRIPT_LICENSE}', '{SCRIPT_DESCRIPTION}', '', '')
weechat.hook_command('{SCRIPT_NAME}', '', '', '', '', 'cmd_test_cb', '')
+42 -32
View File
@@ -98,56 +98,54 @@ class WeechatScript(object): # pylint: disable=too-many-instance-attributes
self.output_dir = os.path.realpath(output_dir)
self.language = language
self.extension = extension
self.script_name = 'testapi.%s' % extension
self.script_name = 'weechat_testapi.%s' % extension
self.script_path = os.path.join(self.output_dir, self.script_name)
self.comment_char = comment_char
self.weechat_module = weechat_module
self.rename_functions()
self.replace_variables()
self.update_tree()
def comment(self, string):
"""Get a commented line."""
return '%s %s' % (self.comment_char, string)
def rename_functions(self):
"""Rename some API functions in the tree."""
def update_tree(self):
"""Make changes in AST tree."""
functions = {
'prnt': 'print',
'prnt_date_tags': 'print_date_tags',
'prnt_y': 'print_y',
'prnt_y_date_tags': 'print_y_date_tags',
}
tests_count = 0
for node in ast.walk(self.tree):
if isinstance(node, ast.Call) and \
# rename some API functions
if self.language != 'python' and \
isinstance(node, ast.Call) and \
isinstance(node.func, ast.Attribute) and \
node.func.value.id == 'weechat':
node.func.attr = functions.get(node.func.attr, node.func.attr)
def replace_variables(self):
"""Replace script variables in string values."""
variables = {
'SCRIPT_SOURCE': self.source_script,
'SCRIPT_NAME': self.script_name,
'SCRIPT_PATH': self.script_path,
'SCRIPT_AUTHOR': 'Sebastien Helleu',
'SCRIPT_VERSION': '1.0',
'SCRIPT_LICENSE': 'GPL3',
'SCRIPT_DESCRIPTION': ('%s scripting API test' %
self.language.capitalize()),
'SCRIPT_LANGUAGE': self.language,
}
# count the total number of tests
tests_count = 0
for node in ast.walk(self.tree):
# count number of tests
if isinstance(node, ast.Call) and \
isinstance(node.func, ast.Name) and \
node.func.id == 'check':
tests_count += 1
variables['SCRIPT_TESTS'] = str(tests_count)
# replace script variables in string values
variables = {
'{SCRIPT_SOURCE}': self.source_script,
'{SCRIPT_NAME}': self.script_name,
'{SCRIPT_PATH}': self.script_path,
'{SCRIPT_AUTHOR}': 'Sebastien Helleu',
'{SCRIPT_VERSION}': '1.0',
'{SCRIPT_LICENSE}': 'GPL3',
'{SCRIPT_DESCRIPTION}': ('%s scripting API test' %
self.language.capitalize()),
'{SCRIPT_LANGUAGE}': self.language,
'{SCRIPT_TESTS}': str(tests_count),
}
# replace variables
for node in ast.walk(self.tree):
if isinstance(node, ast.Str) and \
node.s in variables:
if isinstance(node, ast.Str) and node.s in variables:
node.s = variables[node.s]
def write_header(self, output):
@@ -188,10 +186,6 @@ class WeechatPythonScript(WeechatScript):
super(WeechatPythonScript, self).__init__(
UnparsePython, tree, source_script, output_dir, 'python', 'py')
def rename_functions(self):
# nothing to rename in Python
pass
def write_header(self, output):
output.write('# -*- coding: utf-8 -*-\n')
super(WeechatPythonScript, self).write_header(output)
@@ -224,12 +218,16 @@ class WeechatRubyScript(WeechatScript):
super(WeechatRubyScript, self).__init__(
UnparseRuby, tree, source_script, output_dir, 'ruby', 'rb')
def rename_functions(self):
super(WeechatRubyScript, self).rename_functions()
def update_tree(self):
super(WeechatRubyScript, self).update_tree()
for node in ast.walk(self.tree):
if isinstance(node, ast.Attribute) and \
node.value.id == 'weechat':
node.value.id = 'Weechat'
if isinstance(node, ast.Call) \
and isinstance(node.func, ast.Attribute) \
and node.func.attr == 'config_new_option':
node.args = node.args[:11] + [ast.List(node.args[11:])]
class WeechatLuaScript(WeechatScript):
@@ -265,6 +263,18 @@ class WeechatGuileScript(WeechatScript):
UnparseGuile, tree, source_script, output_dir, 'guile', 'scm',
comment_char=';')
def update_tree(self):
super(WeechatGuileScript, self).update_tree()
functions_with_list = (
'config_new_section',
'config_new_option',
)
for node in ast.walk(self.tree):
if isinstance(node, ast.Call) \
and isinstance(node.func, ast.Attribute) \
and node.func.attr in functions_with_list:
node.args = [ast.Call('list', node.args)]
def write_footer(self, output):
output.write('\n'
'(weechat_init)\n')
+57 -1
View File
@@ -267,6 +267,14 @@ class UnparsePython(object):
# ignore import
pass
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'[',
self.make_list(node.elts),
']',
)
def _ast_module(self, node):
"""Add an AST Module in output."""
self.add(node.body)
@@ -478,6 +486,14 @@ class UnparsePerl(UnparsePython):
'}',
)
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'(',
self.make_list(node.elts),
')',
)
def _ast_pass(self, node):
"""Add an AST Pass in output."""
pass
@@ -588,6 +604,14 @@ class UnparseRuby(UnparsePython):
'end',
)
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'Array[',
self.make_list(node.elts),
']',
)
def _ast_pass(self, node):
"""Add an AST Pass in output."""
pass
@@ -683,6 +707,14 @@ class UnparseLua(UnparsePython):
'end',
)
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'{',
self.make_list(node.elts),
'}',
)
def _ast_pass(self, node):
"""Add an AST Pass in output."""
pass
@@ -704,7 +736,7 @@ class UnparseTcl(UnparsePython):
def _ast_assign(self, node):
"""Add an AST Assign in output."""
exclude_types = (ast.Dict, ast.Str, ast.Subscript)
exclude_types = (ast.Dict, ast.List, ast.Str, ast.Subscript)
self.add(
self.fill,
'set ',
@@ -836,6 +868,14 @@ class UnparseTcl(UnparsePython):
'}',
)
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'[',
self.make_list(node.elts, sep=' '),
']',
)
def _ast_pass(self, node):
"""Add an AST Pass in output."""
pass
@@ -1040,6 +1080,14 @@ class UnparseGuile(UnparsePython):
)
self.add(self.fill, ')')
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'\'(',
self.make_list(node.elts, sep=' '),
')',
)
def _ast_pass(self, node):
"""Add an AST Pass in output."""
pass
@@ -1228,6 +1276,14 @@ class UnparsePhp(UnparsePython):
'}',
)
def _ast_list(self, node):
"""Add an AST List in output."""
self.add(
'array(',
self.make_list(node.elts),
')',
)
def _ast_if(self, node):
"""Add an AST If in output."""
self.add(