ó
#]Nc           @   s³   d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d „  Z d „  Z	 d „  Z
 d e j f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ e d k r¯ e j ƒ  n  d S(   s  Test OpenBabel executables from Python

Note: Python bindings not used

On Windows or Linux, you can run these tests at the commandline
in the build folder with:
"C:\Program Files\CMake 2.6in\ctest.exe" -C CTestTestfile.cmake
                                           -R pytest -VV

You could also "chdir" into build/test and run the test file directly:
python ../../test/testbabel.py

In both cases, the test file is run directly from the source folder,
and so you can quickly develop the tests and try them out.
iÿÿÿÿN(   t   Popent   PIPEc       
   G   s  t  |  ƒ d k r! |  \ } } n2 t  |  ƒ d k rG d |  d } } n t d ƒ ‚ | j ƒ  j ƒ  } t | d j ƒ  ƒ } | rÎ t | g | d d t d t d t d	 d ƒ} | j | j ƒ  ƒ \ } } n; t | g | d d t d t d	 d
 ƒ} | j ƒ  \ } } | j ƒ  | j ƒ  f S(   sâ   Run one of OpenBabel's executables

    With two arguments (stdin, commandline) it pipes
    the stdin through the executable.

    Example: run_exec("CC(=O)Cl", "babel -ismi -oinchi")

    Return a tuple (stdout, stderr)
    i   i   t    i    s   One or two arguments expectedt   stdint   stdoutt   stderrt   bufsizeiÿÿÿÿ(	   t   lent	   Exceptiont   encodet   splitt
   executablet   decodeR    R   t   communicate(   t   argst   textt   commandlinet   brokent   exet   pR   R   (    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   run_exec   s    
c         C   sS   d } d } t  j d k r9 d } t j j | d ƒ } n  t j j d | |  | ƒ S(   s%   Return the full path to an executableR   t   bint   win32s   .exet   Releases   ..(   t   syst   platformt   ost   patht   join(   t   namet   suffixt   folder(    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyR   8   s    c         C   s'   t  d d ƒ } | |  IJ| j ƒ  d S(   sb   Convenience function for debugging tests

    The log file (log.txt) is created in build/test
    s   log.txtt   aN(   t   opent   close(   R   t   output(    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   logA   s    
t   BaseTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sC   A base class for test classes that adds additional
    test methodsc         C   s6   t  | ƒ } |  j t j j | ƒ d | | f ƒ d  S(   Ns   '%s' executable not found at %s(   R   t
   assertTrueR   R   t   isfile(   t   selfR   t   fullpath(    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   canFindExecutableN   s    c         C   s$   |  j  t j j | ƒ d | ƒ d  S(   Ns   Cannot find the file '%s'(   R&   R   R   R'   (   R(   t   filename(    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   canFindFileS   s    c         C   s6   t  j d } t j j | d | ƒ } |  j | ƒ | S(   Ni    t   files(   R   R   R   R   R,   (   R(   R+   t   hereR)   (    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   getTestFileW   s    c         C   s£   d } | j  d ƒ } g  | D] } t j | | ƒ r | ^ q } t | ƒ d k rb |  j d ƒ n  t t j | | d ƒ d ƒ } |  j | | d | | f ƒ d S(   s'   Assert that N molecules were converted.s!   (-?\d.*) molecule(?:s?) converteds   
i    s-   Cannot find the number of molecules converteds4   Number of molecules converted is %d but should be %dN(   R
   t   ret   matchR   t   failt   intt   findallt   assertEqual(   R(   R   t   Nt   patt   linest   linet   convertedlinet   conversion_no(    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   assertConverted]   s    + (   t   __name__t
   __module__t   __doc__R*   R,   R/   R<   (    (    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyR%   J   s
   			t	   testBabelc           B   s   e  Z d  Z d „  Z RS(   s2   A series of tests relating to the Babel executablec         C   s<   |  j  d ƒ t d d ƒ \ } } |  j | j ƒ  d ƒ d  S(   Nt   babels   CC(=O)Cls   babel -ismi -oinchis   InChI=1S/C2H3ClO/c1-2(3)4/h1H3(   R*   R   R5   t   rstrip(   R(   R#   t   error(    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   testSMItoInChIl   s    (   R=   R>   R?   RD   (    (    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyR@   i   s   t   __main__(   R?   R   R0   R   t   unittestt
   subprocessR    R   R   R   R$   t   TestCaseR%   R@   R=   t   main(    (    (    s1   /Users/ghutchis/Devel/openbabel/test/testbabel.pyt   <module>   s   	 				