1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 21:06:38 +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
+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')