[liblouis-liblouisxml] An example of python tests

  • From: Michael Whapples <mwhapples@xxxxxxx>
  • To: liblouis-liblouisxml@xxxxxxxxxxxxx
  • Date: Fri, 19 Feb 2010 15:22:42 +0000

Hello,

I have started on some tests for the python bindings, but I have to say looking at what I have written I don't see it being pleasing within docstrings (I think they may get far too long). I attach example.txt which is what I have written which would do for louis.translate (for thorough testing there is still more I would say which should be done, eg. what happens if we pass a negative value for mode or cursorPos?).

I am thinking it may be better to write a separate "tutorial" type document containing all the doctests if we do go the doctest route (I don't know whether it would be possible to put the doctests in the main liblouis documentation or whether it should be a python specific document written in RST and simply link between the two.

Also there was question about whether this could be integrated with the make system, yes we can exit with different values. The script run_test.py is a basic script to run the doctests in example.txt (place them both in the python directory of your liblouis distribution). The script run_test.py exits with the exit value being the number of failed tests. It should be just as possible to write such a script if doctests were in the docstrings.

Do others have any views on this and which way they would prefer?

Michael Whapples
translate(tran_tables, inbuf [, typeform] [, cursorPos] [,mode]) -> unicode, 
list of integers, list of integers, integer

Translates inbuf to Braille and provides position information.
The parameter tran_tables is a list containing the names of translation 
tables to be used and inbuf is the text to be translated. The function
returns the Braille, a list of input positions for each position in the 
output, a list of positions in the output for each position in the input and 
the cursor position. This means a basic call would be like:

>>> translate(['en-us-g2.ctb'], 'Hello world')
(u',hello _w', [0, 0, 1, 2, 3, 4, 5, 6, 6], [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7], 
1)

Should we want to specify any emphasis on characters in inbuf we can use the 
typeform parameter. If we want information about which cells in the output
is an 8-dot Braille cell then we use a list for typeform and this will be
modified accordingly.

>>> l = [computer_braille, computer_braille, computer_braille, 
>>> computer_braille, computer_braille]
>>> translate(['en-us-g2.ctb'], 'Hello', typeform=l, mode=comp8Dots)
(u'Hello', [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 0)
>>> l
['8', '0', '0', '0', '0']

If you use anything other than a list (eg. a tuple) for typeform, then it 
will not be updated.

>>> t = (computer_braille, computer_braille, computer_braille, 
>>> computer_braille, computer_braille)
>>> translate(['en-us-g2.ctb'], 'Hello', typeform=t, mode=comp8Dots)
(u'Hello', [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 0)
>>> t
(8, 8, 8, 8, 8)

If we want to know where a cursor should be positioned after translation 
then we should give the index of the cursor in inbuf as the cursorPos parameter.

>>> translate(["en-us-g2.ctb"], "Hello", cursorPos=2)
(u',hello', [0, 0, 1, 2, 3, 4], [1, 2, 3, 4, 5], 3)

We can use the mode parameter to alter how the translation is done.

>>> translate(["en-us-g2.ctb"], 'world', mode=noContractions)
(u'world', [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 0)

import sys
import doctest

import louis

res = doctest.testfile('example.txt', globs={'translate': louis.translate, 
'computer_braille': louis.computer_braille, 'comp8Dots': louis.comp8Dots, 
'noContractions': louis.noContractions})
sys.exit(res[0])

Other related posts: