# Path: spec/unit/fixtures.py
# class Internal(object):
#     _itdb = None
# 
#     def __init__(self, foo=None):
#         self.data = foo
# 
#     def __iter__(self):
#         return iter(self.data)
# 
#     def __getitem__(self, name):
#         return self.data.__getitem__(name)
# 
#     def __setitem__(self, name, value):
#         return self.data.__setitem__(name, value)
# 
#     def get(self, name, default=None):
#         return self.data.get(name, default)
# 
#     def ipod_filename(self):
#         return self.data.get('filename_locale', 'filename.mp3')
# 
#     @property
#     def Playlists(self):
#         return []
# 
# def patch_gpod_module():
#     module = mock()
# 
#     module.Track = None
#     module.Database = None
# 
#     import sys
#     sys.modules['gpod'] = module
# 
#     return module
# 
# Path: ipodio/track.py
# class Track(object):
#     def __init__(self, track, hasher=Hasher()):
#         self.__track = track
#         self._hasher = hasher
# 
#     @classmethod
#     def create(cls, filename, internal_class=gpod.Track):
#         return cls(internal_class(filename))
# 
#     def compute_hash(self):
#         return self._hasher.hash(self.filename)
# 
#     def update_hash(self):
#         self.hash = self.compute_hash()
# 
#     @property
#     def _userdata(self):
#         if not self.__track['userdata']:
#             self.__track['userdata'] = {}
#         return self.__track['userdata']
# 
#     def _get_trackdata(self, name):
#         try:
#             return self.__track.__getitem__(name)
#         except KeyError:
#             return None
# 
#     def _get_unicode_trackdata(self, name):
#         return unicode(self._get_trackdata(name) or '')
# 
#     @property
#     def hash(self):
#         return self._userdata.get('mp3hash')
# 
#     @hash.setter
#     def hash(self, hash):
#         self._userdata['mp3hash'] = hash
# 
#     @property
#     def internal(self):
#         return self.__track
# 
#     @property
#     def extension(self):
#         filetype = self._get_trackdata('filetype')
# 
#         if filetype:
#             if 'MPEG' in filetype:
#                 return u'mp3'
#             elif 'AAC' in filetype:
#                 return u'm4a'
#             elif 'AIFF' in filetype:
#                 return u'aiff'
# 
#         return self.filename.rsplit('.', 1)[1]
# 
#     @property
#     def filename(self):
#         return unicode(self.__track.ipod_filename()
#                        or self._userdata.get('filename_locale'))
# 
#     @property
#     def filename_from_tags(self):
#         filename = u'{number}_{title}_{album}_{artist}'.format(
#             number=self.number or 0, title=self.title,
#             album=self.album, artist=self.artist)
# 
#         return "{filename}.{extension}".format(
#             filename=_helpers.clean_filename(filename),
#             extension=self.extension)
# 
#     @property
#     def number(self):
#         return self._get_trackdata('track_nr')
# 
#     @property
#     def title(self):
#         return self._get_unicode_trackdata('title')
# 
#     @property
#     def album(self):
#         return self._get_unicode_trackdata('album')
# 
#     @property
#     def artist(self):
#         return self._get_unicode_trackdata('artist')
# 
#     def __str__(self):
#         number = u"{}. ".format(self.number) if self.number else u""
# 
#         return "{number}'{title}' by: '{artist}'".format(
#             number=number, title=self.title, artist=self.artist)


from spec.unit.fixtures import Internal, patch_gpod_module
from ipodio.track import Track
from expects import expect
from mockito import mock, spy, when, verify, any
from mamba import describe, context, before

    with context('the filename_from_tags property'):
        def should_be_a_composed_name_from_track_tag_info():
            expect(_.track.filename_from_tags).to.be.equal(
                '2_The_Title__The_Artist.mp3')

        def should_be_an_almost_ISO9660_compliant_filename():
            almost_iso9660 = r'^\w+\.\w+$'

            expect(_.track.filename_from_tags).to.match(almost_iso9660)

    with context('when printed'):
        def should_output_track_attributes():
            expect(str(_.track)).to.be.equal("2. 'The Title' by: 'The Artist'")

    @before.each
    def fixture():
        _.internal_class = Internal
        _.hash = '204939024023840234'

        _.hasher = mock()
        when(_.hasher).hash(any(unicode)).thenReturn(_.hash)

        _.track_data = {
            'userdata': {},
            'track_nr': 2,
            'title': 'The Title',
            'album': None,
            'artist': 'The Artist'
        }

# Path: spec/unit/fixtures.py
# class Internal(object):
#     _itdb = None
# 
#     def __init__(self, foo=None):
#         self.data = foo
# 
#     def __iter__(self):
#         return iter(self.data)
# 
#     def __getitem__(self, name):
#         return self.data.__getitem__(name)
# 
#     def __setitem__(self, name, value):
#         return self.data.__setitem__(name, value)
# 
#     def get(self, name, default=None):
#         return self.data.get(name, default)
# 
#     def ipod_filename(self):
#         return self.data.get('filename_locale', 'filename.mp3')
# 
#     @property
#     def Playlists(self):
#         return []
# 
# def patch_gpod_module():
#     module = mock()
# 
#     module.Track = None
#     module.Database = None
# 
#     import sys
#     sys.modules['gpod'] = module
# 
#     return module
# 
# Path: ipodio/track.py
# class Track(object):
#     def __init__(self, track, hasher=Hasher()):
#         self.__track = track
#         self._hasher = hasher
# 
#     @classmethod
#     def create(cls, filename, internal_class=gpod.Track):
#         return cls(internal_class(filename))
# 
#     def compute_hash(self):
#         return self._hasher.hash(self.filename)
# 
#     def update_hash(self):
#         self.hash = self.compute_hash()
# 
#     @property
#     def _userdata(self):
#         if not self.__track['userdata']:
#             self.__track['userdata'] = {}
#         return self.__track['userdata']
# 
#     def _get_trackdata(self, name):
#         try:
#             return self.__track.__getitem__(name)
#         except KeyError:
#             return None
# 
#     def _get_unicode_trackdata(self, name):
#         return unicode(self._get_trackdata(name) or '')
# 
#     @property
#     def hash(self):
#         return self._userdata.get('mp3hash')
# 
#     @hash.setter
#     def hash(self, hash):
#         self._userdata['mp3hash'] = hash
# 
#     @property
#     def internal(self):
#         return self.__track
# 
#     @property
#     def extension(self):
#         filetype = self._get_trackdata('filetype')
# 
#         if filetype:
#             if 'MPEG' in filetype:
#                 return u'mp3'
#             elif 'AAC' in filetype:
#                 return u'm4a'
#             elif 'AIFF' in filetype:
#                 return u'aiff'
# 
#         return self.filename.rsplit('.', 1)[1]
# 
#     @property
#     def filename(self):
#         return unicode(self.__track.ipod_filename()
#                        or self._userdata.get('filename_locale'))
# 
#     @property
#     def filename_from_tags(self):
#         filename = u'{number}_{title}_{album}_{artist}'.format(
#             number=self.number or 0, title=self.title,
#             album=self.album, artist=self.artist)
# 
#         return "{filename}.{extension}".format(
#             filename=_helpers.clean_filename(filename),
#             extension=self.extension)
# 
#     @property
#     def number(self):
#         return self._get_trackdata('track_nr')
# 
#     @property
#     def title(self):
#         return self._get_unicode_trackdata('title')
# 
#     @property
#     def album(self):
#         return self._get_unicode_trackdata('album')
# 
#     @property
#     def artist(self):
#         return self._get_unicode_trackdata('artist')
# 
#     def __str__(self):
#         number = u"{}. ".format(self.number) if self.number else u""
# 
#         return "{number}'{title}' by: '{artist}'".format(
#             number=number, title=self.title, artist=self.artist)


# Path: spec/unit/track_spec.py
from spec.unit.fixtures import Internal, patch_gpod_module
from ipodio.track import Track
from expects import expect
from mockito import mock, spy, when, verify, any
from mamba import describe, context, before


    with context('the filename_from_tags property'):
        def should_be_a_composed_name_from_track_tag_info():
            expect(_.track.filename_from_tags).to.be.equal(
                '2_The_Title__The_Artist.mp3')

        def should_be_an_almost_ISO9660_compliant_filename():
            almost_iso9660 = r'^\w+\.\w+$'

            expect(_.track.filename_from_tags).to.match(almost_iso9660)

    with context('when printed'):
        def should_output_track_attributes():
            expect(str(_.track)).to.be.equal("2. 'The Title' by: 'The Artist'")

    @before.each
    def fixture():
        _.internal_class = Internal
        _.hash = '204939024023840234'

        _.hasher = mock()
        when(_.hasher).hash(any(unicode)).thenReturn(_.hash)

        _.track_data = {
            'userdata': {},
            'track_nr': 2,
            'title': 'The Title',
            'album': None,
            'artist': 'The Artist'
        }

        _.track = Track(Internal(_.track_data), hasher=_.hasher)