import os.path
import sys

import perf
from perf import _utils as utils
from perf import tests
from perf.tests import unittest


class MiscTests(unittest.TestCase):
    def test_version_tuple(self):
        self.assertIsInstance(perf.VERSION, tuple)
        self.assertTrue(all(isinstance(part, int) for part in perf.VERSION),
                        perf.VERSION)

    def test_version_str(self):
        self.assertIsInstance(perf.__version__, str)
        self.assertEqual(perf.__version__,
                         '.'.join(str(part) for part in perf.VERSION))

    def test_format_metadata(self):
        self.assertEqual(perf.format_metadata('loops', 2 ** 24),
                         '2^24')

    def test_python_implementation(self):
        name = perf.python_implementation()
        self.assertIsInstance(name, str)
        self.assertRegex(name, '^[a-z]+$')

    def test_python_has_jit(self):
        jit = perf.python_has_jit()
        self.assertIsInstance(jit, bool)

    @unittest.skipUnless(hasattr(os, 'symlink'), 'need os.symlink')
    def test_abs_executable(self):
        with tests.temporary_file() as tmpname:
            tmpname = os.path.realpath(tmpname)

            try:
                os.symlink(sys.executable, tmpname)
            except (OSError, NotImplementedError):
                self.skipTest("os.symlink() failed")

            self.assertEqual(utils.abs_executable(tmpname),
                             tmpname)

    def test_parse_run_list(self):
        parse_run_list = utils.parse_run_list

        with self.assertRaises(ValueError):
            parse_run_list('')
        with self.assertRaises(ValueError):
            parse_run_list('0')
        self.assertEqual(parse_run_list('1'),
                         [0])
        self.assertEqual(parse_run_list('1-2,5-6'),
                         [0, 1, 4, 5])
        self.assertEqual(parse_run_list('1,3,7'),
                         [0, 2, 6])

        # tolerate spaces
        self.assertEqual(parse_run_list(' 1 , 2 '),
                         [0, 1])

        # errors
        self.assertRaises(ValueError, parse_run_list, 'x')
        self.assertRaises(ValueError, parse_run_list, '1,')

    def test_setup_version(self):
        import setup
        self.assertEqual(perf.__version__, setup.VERSION)

    def test_doc_version(self):
        doc_path = os.path.join(os.path.dirname(__file__), '..', '..', 'doc')
        doc_path = os.path.realpath(doc_path)

        old_path = sys.path[:]
        try:
            sys.path.insert(0, doc_path)
            import conf
            self.assertEqual(perf.__version__, conf.version)
            self.assertEqual(perf.__version__, conf.release)
        finally:
            sys.path[:] = old_path


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