# Path: glottolog3/models.py
# class Doctype(Base, IdNameDescriptionMixin):
#     """
#     id -> pk
#     id -> id
#     abbr -> abbr
#     name -> name
#     """
#     abbr = Column(Unicode)
# 
#     ord = Column(Integer)
# 
#     def __str__(self):
#         return capwords(self.name.replace('_', ' '))
# 
# Path: glottolog3/util.py
# def normalize_language_explanation(chunk):
#     """
#     i) X [aaa]
#     ii) L [aaa] = "X"
#     iii) X = L [aaa]
# 
#     :return: X [aaa]
#     """
#     if '[' in chunk and not chunk.endswith(']'):
#         chunk += ']'
#     chunk = chunk.strip()
#     if '=' not in chunk:
#         return chunk
#     chunks = chunk.split('=')
#     left = '='.join(chunks[:-1]).strip()
#     right = chunks[-1].strip()
#     if right.startswith('"') and right.endswith('"') and '[' not in right and '[' in left:
#         # case ii)
#         return right[1:-1].strip() + ' [' + left.split('[', 1)[1]
#     if '[' in right and '[' not in left:
#         # case iii)
#         return left + ' [' + right.split('[', 1)[1]
#     return chunk
# 
# class ModelInstance(object):
#     def __init__(self, cls, attr='id', collection=None, alias=None):
#         self.cls = cls
#         self.attr = attr
#         self.alias = alias
#         self.collection = collection
# 
#     def serialize(self, node, appstruct):
#         if appstruct is colander.null:
#             return colander.null
#         if self.cls and not isinstance(appstruct, self.cls):
#             raise colander.Invalid(node, '%r is not a %s' % (appstruct, self.cls))
#         return getattr(appstruct, self.attr)
# 
#     def deserialize(self, node, cstruct):
#         if cstruct is colander.null:
#             return colander.null
#         value = None
#         if self.collection:
#             for obj in self.collection:
#                 if getattr(obj, self.attr) == cstruct \
#                         or (self.alias and getattr(obj, self.alias) == cstruct):
#                     value = obj
#         else:
#             value = self.cls.get(cstruct, key=self.attr, default=None) if self.cls else cstruct
#             if self.alias and value is None:
#                 value = self.cls.get(cstruct, key=self.alias, default=None) if self.cls else cstruct
#         if value is None:
#             raise colander.Invalid(node, 'no single result found')
#         return value
# 
#     def cstruct_children(self, node, cstruct):  # pragma: no cover
#         return []


import pytest
import colander
from glottolog3.models import Doctype
from glottolog3.util import normalize_language_explanation, ModelInstance



def test_normalize_language_explanation():
    #for s in [' X [aaa]', 'L [aaa] = "X"', 'X = L [aaa]']:
    #    assert normalize_language_explanation(s) == 'X [aaa]'
    assert normalize_language_explanation(' abcdefg') == 'abcdefg'


def test_ModelInstance():

# Path: glottolog3/models.py
# class Doctype(Base, IdNameDescriptionMixin):
#     """
#     id -> pk
#     id -> id
#     abbr -> abbr
#     name -> name
#     """
#     abbr = Column(Unicode)
# 
#     ord = Column(Integer)
# 
#     def __str__(self):
#         return capwords(self.name.replace('_', ' '))
# 
# Path: glottolog3/util.py
# def normalize_language_explanation(chunk):
#     """
#     i) X [aaa]
#     ii) L [aaa] = "X"
#     iii) X = L [aaa]
# 
#     :return: X [aaa]
#     """
#     if '[' in chunk and not chunk.endswith(']'):
#         chunk += ']'
#     chunk = chunk.strip()
#     if '=' not in chunk:
#         return chunk
#     chunks = chunk.split('=')
#     left = '='.join(chunks[:-1]).strip()
#     right = chunks[-1].strip()
#     if right.startswith('"') and right.endswith('"') and '[' not in right and '[' in left:
#         # case ii)
#         return right[1:-1].strip() + ' [' + left.split('[', 1)[1]
#     if '[' in right and '[' not in left:
#         # case iii)
#         return left + ' [' + right.split('[', 1)[1]
#     return chunk
# 
# class ModelInstance(object):
#     def __init__(self, cls, attr='id', collection=None, alias=None):
#         self.cls = cls
#         self.attr = attr
#         self.alias = alias
#         self.collection = collection
# 
#     def serialize(self, node, appstruct):
#         if appstruct is colander.null:
#             return colander.null
#         if self.cls and not isinstance(appstruct, self.cls):
#             raise colander.Invalid(node, '%r is not a %s' % (appstruct, self.cls))
#         return getattr(appstruct, self.attr)
# 
#     def deserialize(self, node, cstruct):
#         if cstruct is colander.null:
#             return colander.null
#         value = None
#         if self.collection:
#             for obj in self.collection:
#                 if getattr(obj, self.attr) == cstruct \
#                         or (self.alias and getattr(obj, self.alias) == cstruct):
#                     value = obj
#         else:
#             value = self.cls.get(cstruct, key=self.attr, default=None) if self.cls else cstruct
#             if self.alias and value is None:
#                 value = self.cls.get(cstruct, key=self.alias, default=None) if self.cls else cstruct
#         if value is None:
#             raise colander.Invalid(node, 'no single result found')
#         return value
# 
#     def cstruct_children(self, node, cstruct):  # pragma: no cover
#         return []


# Path: tests/test_unit.py
import pytest
import colander
from glottolog3.models import Doctype
from glottolog3.util import normalize_language_explanation, ModelInstance




def test_normalize_language_explanation():
    #for s in [' X [aaa]', 'L [aaa] = "X"', 'X = L [aaa]']:
    #    assert normalize_language_explanation(s) == 'X [aaa]'
    assert normalize_language_explanation(' abcdefg') == 'abcdefg'


def test_ModelInstance():

    mi = ModelInstance(Doctype)