WeeChat DevCenter

Tag - tests

Entries feed

Tuesday, October 17 2017

Automatic tests of scripting API

Automatic tests of the scripting API have been added a few days ago. For now only a small part of API functions are checked.

This is done with 3 new Python scripts in directory tests/scripts/python:

  • unparse.py: convert Python code to other languages (including Python itself)
  • testapigen.py: generate scripts in all languages to test the API
  • testapi.py scripting API tests

The script unparse.py can convert Python code to other languages: Python, Perl, Ruby, Lua, TCL, Guile, JavaScript and PHP (new plugin in version 2.0). The Python code is first parsed using AST (ast.parse), and then this tree is recursively scanned to produce code in Python or another language (note: only part of AST is supported, the minimum for WeeChat tests).

Example of code conversion in other languages:

$ ./unparse.py --language all
Enter the code to convert (Enter + ctrl+D to end)

def test_list_new():
    ptr_list = weechat.list_new()
    check(ptr_list != '')
    check(weechat.list_size(ptr_list) == 0)

python:
def test_list_new():
    ptr_list = weechat.list_new()
    check(ptr_list != '')
    check(weechat.list_size(ptr_list) == 0)

perl:
sub test_list_new
{
    $ptr_list = weechat::list_new();
    check($ptr_list ne "");
    check(weechat::list_size($ptr_list) == 0);
}

ruby:
def test_list_new
    ptr_list = weechat.list_new()
    check(ptr_list != '')
    check(weechat.list_size(ptr_list) == 0)
end

lua:
function test_list_new()
    ptr_list = weechat.list_new()
    check(ptr_list ~= '')
    check(weechat.list_size(ptr_list) == 0)
end

tcl:
proc test_list_new {} {
    set ptr_list [weechat::list_new]
    check [expr {$ptr_list ne ""}]
    check [expr {[weechat::list_size $ptr_list] == 0}]
}

guile:
(define (test_list_new)
    (let ((ptr_list (weechat:list_new)))
        (begin
            (check (string<> ptr_list ""))
            (check (= (weechat:list_size ptr_list) 0))
        )
    )
)

javascript:
function test_list_new() {
    ptr_list = weechat.list_new()
    check(ptr_list != '')
    check(weechat.list_size(ptr_list) == 0)
}

php:
function test_list_new()
{
    $ptr_list = weechat_list_new();
    check($ptr_list != "");
    check(weechat_list_size($ptr_list) == 0);
}

The script testapigen.py generates scripts in all supported languages. These scripts are loaded in WeeChat during tests, and print the results of tests, for example in Python:

>>> Running command: /script load -q ./tmp_weechat_test/testapi/testapi.py
>>> Running command: /testapi.py
>>>
>>> ------------------------------
>>> Testing python API
  > TESTS: 57
  > test_plugins
      TEST OK: weechat.plugin_get_name('') == 'core'
      TEST OK: weechat.plugin_get_name(weechat.buffer_get_pointer(weechat.buffer_search_main(), 'plugin')) == 'core'
  > test_strings
      TEST OK: weechat.charset_set('iso-8859-15') == 1
      TEST OK: weechat.charset_set('') == 1
      TEST OK: weechat.iconv_to_internal('iso-8859-15', 'abc') == 'abc'
      TEST OK: weechat.iconv_from_internal('iso-8859-15', 'abcd') == 'abcd'
      TEST OK: weechat.gettext('abcdef') == 'abcdef'
      TEST OK: weechat.ngettext('file', 'files', 1) == 'file'
      TEST OK: weechat.ngettext('file', 'files', 2) == 'files'
      TEST OK: weechat.strlen_screen('abcd') == 4
      TEST OK: weechat.string_match('abcdef', 'abc*', 0) == 1
      TEST OK: weechat.string_eval_path_home('test ${abc}', {}, {'abc': '123'}, {}) == 'test 123'
      TEST OK: weechat.string_mask_to_regex('test*mask') == 'test.*mask'
      TEST OK: weechat.string_has_highlight('my test string', 'test,word2') == 1
      TEST OK: weechat.string_has_highlight_regex('my test string', 'test|word2') == 1
      TEST OK: weechat.string_remove_color('test', '?') == 'test'
      TEST OK: weechat.string_is_command_char('/test') == 1
      TEST OK: weechat.string_is_command_char('test') == 0
      TEST OK: weechat.string_input_for_buffer('test') == 'test'
      TEST OK: weechat.string_input_for_buffer('/test') == ''
      TEST OK: weechat.string_input_for_buffer('//test') == '/test'
      TEST OK: weechat.string_eval_expression('100 > 50', {}, {}, {'type': 'condition'}) == '1'
      TEST OK: weechat.string_eval_expression('${buffer.full_name}', {}, {}, {}) == 'core.weechat'
  > test_lists
      TEST OK: ptr_list != ''
      TEST OK: weechat.list_size(ptr_list) == 0
      TEST OK: weechat.list_size(ptr_list) == 1
      TEST OK: weechat.list_size(ptr_list) == 2
      TEST OK: weechat.list_search(ptr_list, 'abc') == item_abc
      TEST OK: weechat.list_search(ptr_list, 'def') == item_def
      TEST OK: weechat.list_search(ptr_list, 'ghi') == ''
      TEST OK: weechat.list_search_pos(ptr_list, 'abc') == 0
      TEST OK: weechat.list_search_pos(ptr_list, 'def') == 1
      TEST OK: weechat.list_search_pos(ptr_list, 'ghi') == -1
      TEST OK: weechat.list_casesearch(ptr_list, 'abc') == item_abc
      TEST OK: weechat.list_casesearch(ptr_list, 'def') == item_def
      TEST OK: weechat.list_casesearch(ptr_list, 'ghi') == ''
      TEST OK: weechat.list_casesearch(ptr_list, 'ABC') == item_abc
      TEST OK: weechat.list_casesearch(ptr_list, 'DEF') == item_def
      TEST OK: weechat.list_casesearch(ptr_list, 'GHI') == ''
      TEST OK: weechat.list_casesearch_pos(ptr_list, 'abc') == 0
      TEST OK: weechat.list_casesearch_pos(ptr_list, 'def') == 1
      TEST OK: weechat.list_casesearch_pos(ptr_list, 'ghi') == -1
      TEST OK: weechat.list_casesearch_pos(ptr_list, 'ABC') == 0
      TEST OK: weechat.list_casesearch_pos(ptr_list, 'DEF') == 1
      TEST OK: weechat.list_casesearch_pos(ptr_list, 'GHI') == -1
      TEST OK: weechat.list_get(ptr_list, 0) == item_abc
      TEST OK: weechat.list_get(ptr_list, 1) == item_def
      TEST OK: weechat.list_get(ptr_list, 2) == ''
      TEST OK: weechat.list_string(item_def) == 'def2'
      TEST OK: weechat.list_next(item_abc) == item_def
      TEST OK: weechat.list_next(item_def) == ''
      TEST OK: weechat.list_prev(item_abc) == ''
      TEST OK: weechat.list_prev(item_def) == item_abc
      TEST OK: weechat.list_size(ptr_list) == 1
      TEST OK: weechat.list_get(ptr_list, 0) == item_def
      TEST OK: weechat.list_get(ptr_list, 1) == ''
      TEST OK: weechat.list_size(ptr_list) == 0
  > TESTS END

>>> Tests python: 57 tests, 57 OK, 0 errors, 0 unexpected messages, 9 ms

Tuesday, February 5 2013

Test script for relay protocol

A test script called testproto.py has been added in QWeeChat to test the relay protocol with WeeChat. This script runs in terminal and does not require Qt.

Script is located in path src/qweechat/weechat/testproto.py of QWeeChat repository.

Screenshot (click for full size):

testproto.png