# Path: rash/config.py
# class Configuration(object):
# 
#     """
#     RASH configuration interface.
# 
#     If you define an object named :data:`config` in the
#     :term:`configuration file`, it is going to be loaded by RASH.
#     :data:`config` must be an instance of :class:`Configuration`.
# 
#     .. glossary::
# 
#        configuration file
#          In unix-like systems, it's :file:`~/.config/rash/config.py` or
#          different place if you set :envvar:`XDG_CONFIG_HOME`.  In Mac
#          OS, it's :file:`~/Library/Application Support/RASH/config.py`.
#          Use ``rash locate config`` to locate the exact place.
# 
#     Example:
# 
#     >>> from rash.config import Configuration
#     >>> config = Configuration()
#     >>> config.isearch.query = '-u .'
# 
#     Here is a list of configuration variables you can set:
# 
#     =========================== ===========================================
#     Configuration variables
#     =========================== ===========================================
#     |record.environ|            Environment variables to record.
#     |search.alias|              Search query alias.
#     |search.kwds_adapter|       Transform keyword arguments.
#     |isearch.query|             Default isearch query.
#     |isearch.query_template|    Transform default query.
#     |isearch.base_query|        Default isearch base query.
#     =========================== ===========================================
# 
#     .. |record.environ| replace::
#        :attr:`config.record.environ <RecordConfig.environ>`
#     .. |search.alias| replace::
#        :attr:`config.search.alias <SearchConfig.alias>`
#     .. |search.kwds_adapter| replace::
#        :attr:`config.search.kwds_adapter <SearchConfig.kwds_adapter>`
#     .. |isearch.query| replace::
#        :attr:`config.isearch.query <ISearchConfig.query>`
#     .. |isearch.query_template| replace::
#        :attr:`config.isearch.query_template <ISearchConfig.query_template>`
#     .. |isearch.base_query| replace::
#        :attr:`config.isearch.base_query <ISearchConfig.base_query>`
# 
#     """
# 
#     def __init__(self):
#         self.record = RecordConfig()
#         self.search = SearchConfig()
#         self.isearch = ISearchConfig()
# 
# Path: rash/query.py
# def expand_query(config, kwds):
#     """
#     Expand `kwds` based on `config.search.query_expander`.
# 
#     :type config: .config.Configuration
#     :type kwds: dict
#     :rtype: dict
#     :return: Return `kwds`, modified in place.
# 
#     """
#     pattern = []
#     for query in kwds.pop('pattern', []):
#         expansion = config.search.alias.get(query)
#         if expansion is None:
#             pattern.append(query)
#         else:
#             parser = SafeArgumentParser()
#             search_add_arguments(parser)
#             ns = parser.parse_args(expansion)
#             for (key, value) in vars(ns).items():
#                 if isinstance(value, (list, tuple)):
#                     if not kwds.get(key):
#                         kwds[key] = value
#                     else:
#                         kwds[key].extend(value)
#                 else:
#                     kwds[key] = value
#     kwds['pattern'] = pattern
#     return config.search.kwds_adapter(kwds)
# 
# Path: rash/tests/utils.py
# class BaseTestCase(unittest.TestCase):
# 
#     if not hasattr(unittest.TestCase, 'assertIn'):
#         def assertIn(self, member, container):
#             self.assertTrue(member in container)
# 
#     if not hasattr(unittest.TestCase, 'assertNotIn'):
#         def assertNotIn(self, member, container):
#             self.assertTrue(member not in container)
# 
#     if not hasattr(unittest.TestCase, 'assertIsInstance'):
#         def assertIsInstance(self, obj, cls, msg=None):
#             self.assertTrue(isinstance(obj, cls), msg)
# 
#     if not hasattr(unittest.TestCase, 'assertSetEqual'):
#         def assertSetEqual(self, set1, set2, msg=None):
#             self.assertEqual(set1, set2, msg)


from ..config import Configuration
from ..query import expand_query
from .utils import BaseTestCase

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



class TestExpandQuery(BaseTestCase):

    def setUp(self):
        self.config = Configuration()
        self.config.search.alias['test'] = ["--include-pattern", "*test*"]
        self.config.search.alias['build'] = ["--include-pattern", "*build*"]

    def test_alias_no_query_no_expansion(self):
        kwds = expand_query(self.config, {})
        self.assertEqual(kwds.get('include_pattern', []), [])
        self.assertEqual(kwds['pattern'], [])

    def test_alias_expansion(self):
        kwds = expand_query(self.config, {'pattern': ['test']})
        self.assertEqual(kwds['include_pattern'], ['*test*'])
        self.assertEqual(kwds['pattern'], [])

    def test_alias_should_add_option(self):
        kwds = expand_query(self.config, {'include_pattern': ['*make*'],
                                          'pattern': ['test']})
        self.assertEqual(kwds['include_pattern'], ['*make*', '*test*'])
        self.assertEqual(kwds['pattern'], [])

    def test_alias_two_expansions(self):

# Path: rash/config.py
# class Configuration(object):
# 
#     """
#     RASH configuration interface.
# 
#     If you define an object named :data:`config` in the
#     :term:`configuration file`, it is going to be loaded by RASH.
#     :data:`config` must be an instance of :class:`Configuration`.
# 
#     .. glossary::
# 
#        configuration file
#          In unix-like systems, it's :file:`~/.config/rash/config.py` or
#          different place if you set :envvar:`XDG_CONFIG_HOME`.  In Mac
#          OS, it's :file:`~/Library/Application Support/RASH/config.py`.
#          Use ``rash locate config`` to locate the exact place.
# 
#     Example:
# 
#     >>> from rash.config import Configuration
#     >>> config = Configuration()
#     >>> config.isearch.query = '-u .'
# 
#     Here is a list of configuration variables you can set:
# 
#     =========================== ===========================================
#     Configuration variables
#     =========================== ===========================================
#     |record.environ|            Environment variables to record.
#     |search.alias|              Search query alias.
#     |search.kwds_adapter|       Transform keyword arguments.
#     |isearch.query|             Default isearch query.
#     |isearch.query_template|    Transform default query.
#     |isearch.base_query|        Default isearch base query.
#     =========================== ===========================================
# 
#     .. |record.environ| replace::
#        :attr:`config.record.environ <RecordConfig.environ>`
#     .. |search.alias| replace::
#        :attr:`config.search.alias <SearchConfig.alias>`
#     .. |search.kwds_adapter| replace::
#        :attr:`config.search.kwds_adapter <SearchConfig.kwds_adapter>`
#     .. |isearch.query| replace::
#        :attr:`config.isearch.query <ISearchConfig.query>`
#     .. |isearch.query_template| replace::
#        :attr:`config.isearch.query_template <ISearchConfig.query_template>`
#     .. |isearch.base_query| replace::
#        :attr:`config.isearch.base_query <ISearchConfig.base_query>`
# 
#     """
# 
#     def __init__(self):
#         self.record = RecordConfig()
#         self.search = SearchConfig()
#         self.isearch = ISearchConfig()
# 
# Path: rash/query.py
# def expand_query(config, kwds):
#     """
#     Expand `kwds` based on `config.search.query_expander`.
# 
#     :type config: .config.Configuration
#     :type kwds: dict
#     :rtype: dict
#     :return: Return `kwds`, modified in place.
# 
#     """
#     pattern = []
#     for query in kwds.pop('pattern', []):
#         expansion = config.search.alias.get(query)
#         if expansion is None:
#             pattern.append(query)
#         else:
#             parser = SafeArgumentParser()
#             search_add_arguments(parser)
#             ns = parser.parse_args(expansion)
#             for (key, value) in vars(ns).items():
#                 if isinstance(value, (list, tuple)):
#                     if not kwds.get(key):
#                         kwds[key] = value
#                     else:
#                         kwds[key].extend(value)
#                 else:
#                     kwds[key] = value
#     kwds['pattern'] = pattern
#     return config.search.kwds_adapter(kwds)
# 
# Path: rash/tests/utils.py
# class BaseTestCase(unittest.TestCase):
# 
#     if not hasattr(unittest.TestCase, 'assertIn'):
#         def assertIn(self, member, container):
#             self.assertTrue(member in container)
# 
#     if not hasattr(unittest.TestCase, 'assertNotIn'):
#         def assertNotIn(self, member, container):
#             self.assertTrue(member not in container)
# 
#     if not hasattr(unittest.TestCase, 'assertIsInstance'):
#         def assertIsInstance(self, obj, cls, msg=None):
#             self.assertTrue(isinstance(obj, cls), msg)
# 
#     if not hasattr(unittest.TestCase, 'assertSetEqual'):
#         def assertSetEqual(self, set1, set2, msg=None):
#             self.assertEqual(set1, set2, msg)


# Path: rash/tests/test_query.py
from ..config import Configuration
from ..query import expand_query
from .utils import BaseTestCase


# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



class TestExpandQuery(BaseTestCase):

    def setUp(self):
        self.config = Configuration()
        self.config.search.alias['test'] = ["--include-pattern", "*test*"]
        self.config.search.alias['build'] = ["--include-pattern", "*build*"]

    def test_alias_no_query_no_expansion(self):
        kwds = expand_query(self.config, {})
        self.assertEqual(kwds.get('include_pattern', []), [])
        self.assertEqual(kwds['pattern'], [])

    def test_alias_expansion(self):
        kwds = expand_query(self.config, {'pattern': ['test']})
        self.assertEqual(kwds['include_pattern'], ['*test*'])
        self.assertEqual(kwds['pattern'], [])

    def test_alias_should_add_option(self):
        kwds = expand_query(self.config, {'include_pattern': ['*make*'],
                                          'pattern': ['test']})
        self.assertEqual(kwds['include_pattern'], ['*make*', '*test*'])
        self.assertEqual(kwds['pattern'], [])

    def test_alias_two_expansions(self):

        kwds = expand_query(self.config, {'pattern': ['test', 'build']})