# Path: zdict/utils.py
# class Color(metaclass=ColorConst):
#     _force_color = False
# 
#     @classmethod
#     def set_force_color(cls, force_color=True):
#         cls._force_color = force_color
# 
#     @classmethod
#     def format(self, s='', color='org', indent=0):
#         '''
#         :type s: str
#         :param s: message
#         :param color: predefined color name, e,g,: red, RED.
#             Using 'l' prefix for bright color, e.g.: lred, lwhite.
#             It's case-insensitive.
# 
#             If stdout isn't a tty, the color option will be ignored.
#         '''
#         colorize = self._force_color or sys.stdout.isatty()
# 
#         return '{indent}{color}{s}{org}'.format(
#             indent=' ' * indent,
#             color=getattr(self, color, '') if colorize else '',
#             s=s,
#             org=self.ORG if colorize else '',
#         )
# 
#     @classmethod
#     def print(self, *args, end='\n', **kwargs):
#         print(self.format(*args, **kwargs), end=end)
# 
# def create_zdict_db_if_not_exists():
#     if not os.path.exists(constants.DB_FILE):
#         open(constants.DB_FILE, 'a').close()
# 
# def create_zdict_dir_if_not_exists():
#     if not os.path.isdir(constants.BASE_DIR):
#         os.mkdir(constants.BASE_DIR)
# 
# def import_readline():
#     if sys.platform == 'darwin' and sys.version_info <= (3, 5):
#         import gnureadline as readline
#     else:
#         import readline
#     return readline


import sys
from zdict.utils import (Color, create_zdict_db_if_not_exists,
                         create_zdict_dir_if_not_exists,
                         import_readline)
from pytest import mark, raises
from unittest.mock import patch
def test_create_zdict_dir_if_not_exists(mkdir, isdir, constants):
    constants.BASE_DIR = '/mock'
    create_zdict_dir_if_not_exists()

    isdir.assert_called_with('/mock')
    mkdir.assert_called_with('/mock')


@patch('builtins.open')
@patch('zdict.utils.constants')
@patch('os.path.exists', return_value=False)
def test_create_zdict_db_if_not_exists(exists, constants, open):
    constants.DB_FILE = '/mock'
    create_zdict_db_if_not_exists()

    exists.assert_called_with('/mock')
    assert open.called


@mark.skipif(sys.platform.startswith('freebsd'),
             reason="gnureadline installation failed on freebsd")
def test_platform_readline():
    '''
    Check the imported readline module on different platforms
    '''
    with patch.object(sys, 'platform', new='linux'):
        readline = import_readline()
        assert readline.__name__ == 'readline'

    with patch.object(sys, 'platform', new='darwin'):

# Path: zdict/utils.py
# class Color(metaclass=ColorConst):
#     _force_color = False
# 
#     @classmethod
#     def set_force_color(cls, force_color=True):
#         cls._force_color = force_color
# 
#     @classmethod
#     def format(self, s='', color='org', indent=0):
#         '''
#         :type s: str
#         :param s: message
#         :param color: predefined color name, e,g,: red, RED.
#             Using 'l' prefix for bright color, e.g.: lred, lwhite.
#             It's case-insensitive.
# 
#             If stdout isn't a tty, the color option will be ignored.
#         '''
#         colorize = self._force_color or sys.stdout.isatty()
# 
#         return '{indent}{color}{s}{org}'.format(
#             indent=' ' * indent,
#             color=getattr(self, color, '') if colorize else '',
#             s=s,
#             org=self.ORG if colorize else '',
#         )
# 
#     @classmethod
#     def print(self, *args, end='\n', **kwargs):
#         print(self.format(*args, **kwargs), end=end)
# 
# def create_zdict_db_if_not_exists():
#     if not os.path.exists(constants.DB_FILE):
#         open(constants.DB_FILE, 'a').close()
# 
# def create_zdict_dir_if_not_exists():
#     if not os.path.isdir(constants.BASE_DIR):
#         os.mkdir(constants.BASE_DIR)
# 
# def import_readline():
#     if sys.platform == 'darwin' and sys.version_info <= (3, 5):
#         import gnureadline as readline
#     else:
#         import readline
#     return readline


# Path: zdict/tests/test_utils.py
import sys
from zdict.utils import (Color, create_zdict_db_if_not_exists,
                         create_zdict_dir_if_not_exists,
                         import_readline)
from pytest import mark, raises
from unittest.mock import patch

def test_create_zdict_dir_if_not_exists(mkdir, isdir, constants):
    constants.BASE_DIR = '/mock'
    create_zdict_dir_if_not_exists()

    isdir.assert_called_with('/mock')
    mkdir.assert_called_with('/mock')


@patch('builtins.open')
@patch('zdict.utils.constants')
@patch('os.path.exists', return_value=False)
def test_create_zdict_db_if_not_exists(exists, constants, open):
    constants.DB_FILE = '/mock'
    create_zdict_db_if_not_exists()

    exists.assert_called_with('/mock')
    assert open.called


@mark.skipif(sys.platform.startswith('freebsd'),
             reason="gnureadline installation failed on freebsd")
def test_platform_readline():
    '''
    Check the imported readline module on different platforms
    '''
    with patch.object(sys, 'platform', new='linux'):
        readline = import_readline()
        assert readline.__name__ == 'readline'

    with patch.object(sys, 'platform', new='darwin'):

        readline = import_readline()