import sys, os
import unittest
import ctypes
import _ctypes_test
from ctypes.wrap.cparser import find_gccxml

import imp
is_win_debug_build = False
for ext, mode, typ in imp.get_suffixes():
    if ext == "_d.pyd":
        is_win_debug_build = True

if find_gccxml():

    class Test(unittest.TestCase):
        def test(self):

            # XXX should use temporary files.

            test_dir = os.path.dirname(__file__)
            source_dir = os.path.join(test_dir, r"..\..", "source")

            from ctypes.wrap import h2xml, xml2py
            args = "-q -I%s _ctypes_test.h -o _testfile.xml" % source_dir

            h2xml.main(["testing"] + args.split())

            args = "_testfile.xml -o _testfile.py -l%s" % _ctypes_test.__file__
            xml2py.main(["testing"] + args.split())

            lines = [l.rstrip() for l in open("_testfile.py", "r").readlines()]

            global EXPECTED
            if is_win_debug_build:
                EXPECTED = [line.replace("_ctypes_test", "_ctypes_test_d") for line in EXPECTED]

            for i in xrange(len(lines)):
                if lines[i].strip().startswith("#"):
                    continue
                self.failUnlessEqual(lines[i], EXPECTED[i])

EXPECTED = r"""# generated by 'xml2py'
# flags '_ctypes_test.xml -o _ctypes_test.py -lc:\sf\ctypes_dist\build\lib.win32-2.3\_ctypes_test.pyd'
from ctypes import *



def _testfunc_i_bhilfd(b, h, i, l, f, d):
    # source/_ctypes_test.h 1
    return _testfunc_i_bhilfd._api_(b, h, i, l, f, d)
_testfunc_i_bhilfd = cdecl(c_int, '_ctypes_test', [c_char, c_short, c_int, c_long, c_float, c_double]) (_testfunc_i_bhilfd)

""".splitlines()

if __name__ == "__main__":
    unittest.main()
