# Path: fretwork/midi/DataTypeConverters.py
# def readBew(value):
#     """
#     Reads string as big endian word, (asserts len(value) in [1,2,4])
# 
#     .. doctest::
# 
#         >>> readBew('aáâã')
#         0
#         >>> readBew('aá')
#         0
#     """
#     try:
#         return unpack('>%s' % {1:'B', 2:'H', 4:'L'}[len(value)], value)[0]
#     except KeyError:
#         return 0
# 
# Path: fretwork/midi/DataTypeConverters.py
# def readVar(value):
#     """
#     Converts varlength format to integer. Just pass it 0 or more chars that
#     might be a varlen and it will only use the relevant chars.
#     use varLen(readVar(value)) to see how many bytes the integer value takes.
#     asserts len(value) >= 0
# 
#     .. doctest::
# 
#         >>> readVar('@')
#         1081408
#         >>> readVar('áâãa')
#         295821045191137
#     """
#     sum = 0
#     for byte in unpack('%sB' % len(value), value):
#         sum = (sum << 7) + (byte & 0x7F)
#         if not 0x80 & byte: break # stop after last byte
#     return sum
# 
# Path: fretwork/midi/DataTypeConverters.py
# def toBytes(value):
#     "Turns a string into a list of byte values"
#     return unpack('%sB' % len(value), value)
# 
# Path: fretwork/midi/DataTypeConverters.py
# def varLen(value):
#     """
#     Returns the the number of bytes an integer will be when
#     converted to varlength
#     """
#     if value <= 127:
#         return 1
#     elif value <= 16383:
#         return 2
#     elif value <= 2097151:
#         return 3
#     else:
#         return 4


import sys
import unittest
from fretwork.midi.DataTypeConverters import readBew
from fretwork.midi.DataTypeConverters import readVar
from fretwork.midi.DataTypeConverters import toBytes
from fretwork.midi.DataTypeConverters import varLen
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.




class DataTypeConvertersTest(unittest.TestCase):

    def test_toBytes_type(self):
        if sys.version_info.major == 2:
            value = bytes('áâãa')
        else:
            value = bytes('áâãa', 'utf-8')
        result = toBytes(value)
        self.assertIs(type(result), tuple)

    def test_readBew_keyerror(self):
        if sys.version_info.major == 2:
            value = bytes('aà')
        else:
            value = bytes('aà', 'utf-8')
        self.assertIs(readBew(value), 0)

    def test_readBew_type(self):
        value = b'a'

# Path: fretwork/midi/DataTypeConverters.py
# def readBew(value):
#     """
#     Reads string as big endian word, (asserts len(value) in [1,2,4])
# 
#     .. doctest::
# 
#         >>> readBew('aáâã')
#         0
#         >>> readBew('aá')
#         0
#     """
#     try:
#         return unpack('>%s' % {1:'B', 2:'H', 4:'L'}[len(value)], value)[0]
#     except KeyError:
#         return 0
# 
# Path: fretwork/midi/DataTypeConverters.py
# def readVar(value):
#     """
#     Converts varlength format to integer. Just pass it 0 or more chars that
#     might be a varlen and it will only use the relevant chars.
#     use varLen(readVar(value)) to see how many bytes the integer value takes.
#     asserts len(value) >= 0
# 
#     .. doctest::
# 
#         >>> readVar('@')
#         1081408
#         >>> readVar('áâãa')
#         295821045191137
#     """
#     sum = 0
#     for byte in unpack('%sB' % len(value), value):
#         sum = (sum << 7) + (byte & 0x7F)
#         if not 0x80 & byte: break # stop after last byte
#     return sum
# 
# Path: fretwork/midi/DataTypeConverters.py
# def toBytes(value):
#     "Turns a string into a list of byte values"
#     return unpack('%sB' % len(value), value)
# 
# Path: fretwork/midi/DataTypeConverters.py
# def varLen(value):
#     """
#     Returns the the number of bytes an integer will be when
#     converted to varlength
#     """
#     if value <= 127:
#         return 1
#     elif value <= 16383:
#         return 2
#     elif value <= 2097151:
#         return 3
#     else:
#         return 4


# Path: tests/midi/test_datatypeconverters.py
import sys
import unittest
from fretwork.midi.DataTypeConverters import readBew
from fretwork.midi.DataTypeConverters import readVar
from fretwork.midi.DataTypeConverters import toBytes
from fretwork.midi.DataTypeConverters import varLen

# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.




class DataTypeConvertersTest(unittest.TestCase):

    def test_toBytes_type(self):
        if sys.version_info.major == 2:
            value = bytes('áâãa')
        else:
            value = bytes('áâãa', 'utf-8')
        result = toBytes(value)
        self.assertIs(type(result), tuple)

    def test_readBew_keyerror(self):
        if sys.version_info.major == 2:
            value = bytes('aà')
        else:
            value = bytes('aà', 'utf-8')
        self.assertIs(readBew(value), 0)

    def test_readBew_type(self):
        value = b'a'

        result = readBew(value)