# Path: lib/util.py
# def format_satoshis(x, is_diff=False, num_zeros = 0, decimal_point = 8, whitespaces=False):
#     from decimal import Decimal
#     s = Decimal(x)
#     sign, digits, exp = s.as_tuple()
#     digits = map(str, digits)
#     while len(digits) < decimal_point + 1:
#         digits.insert(0,'0')
#     digits.insert(-decimal_point,'.')
#     s = ''.join(digits).rstrip('0')
#     if sign:
#         s = '-' + s
#     elif is_diff:
#         s = "+" + s
# 
#     p = s.find('.')
#     s += "0"*( 1 + num_zeros - ( len(s) - p ))
#     if whitespaces:
#         s += " "*( 1 + decimal_point - ( len(s) - p ))
#         s = " "*( 13 - decimal_point - ( p )) + s
#     return s
# 
# def parse_URI(uri):
#     import urlparse
#     import bitcoin
#     from decimal import Decimal
# 
#     if ':' not in uri:
#         assert bitcoin.is_address(uri)
#         return uri, None, None, None, None
# 
#     u = urlparse.urlparse(uri)
#     assert u.scheme == 'myriadcoin'
# 
#     address = u.path
#     valid_address = bitcoin.is_address(address)
# 
#     pq = urlparse.parse_qs(u.query)
#     
#     for k, v in pq.items():
#         if len(v)!=1:
#             raise Exception('Duplicate Key', k)
# 
#     amount = label = message = request_url = ''
#     if 'amount' in pq:
#         am = pq['amount'][0]
#         m = re.match('([0-9\.]+)X([0-9])', am)
#         if m:
#             k = int(m.group(2)) - 8
#             amount = Decimal(m.group(1)) * pow(  Decimal(10) , k)
#         else:
#             amount = Decimal(am) * 100000000
#     if 'message' in pq:
#         message = pq['message'][0]
#     if 'label' in pq:
#         label = pq['label'][0]
#     if 'r' in pq:
#         request_url = pq['r'][0]
#         
#     if request_url != '':
#         return address, amount, label, message, request_url
# 
#     assert valid_address
# 
#     return address, amount, label, message, request_url


import unittest
from lib.util import format_satoshis, parse_URI

class TestUtil(unittest.TestCase):

    def test_format_satoshis(self):
        result = format_satoshis(1234)
        expected = "0.00001234"
        self.assertEqual(expected, result)

    def test_format_satoshis_diff_positive(self):
        result = format_satoshis(1234, is_diff=True)
        expected = "+0.00001234"
        self.assertEqual(expected, result)

    def test_format_satoshis_diff_negative(self):
        result = format_satoshis(-1234, is_diff=True)
        expected = "-0.00001234"
        self.assertEqual(expected, result)

    def _do_test_parse_URI(self, uri, expected_address, expected_amount, expected_label, expected_message, expected_request_url):

# Path: lib/util.py
# def format_satoshis(x, is_diff=False, num_zeros = 0, decimal_point = 8, whitespaces=False):
#     from decimal import Decimal
#     s = Decimal(x)
#     sign, digits, exp = s.as_tuple()
#     digits = map(str, digits)
#     while len(digits) < decimal_point + 1:
#         digits.insert(0,'0')
#     digits.insert(-decimal_point,'.')
#     s = ''.join(digits).rstrip('0')
#     if sign:
#         s = '-' + s
#     elif is_diff:
#         s = "+" + s
# 
#     p = s.find('.')
#     s += "0"*( 1 + num_zeros - ( len(s) - p ))
#     if whitespaces:
#         s += " "*( 1 + decimal_point - ( len(s) - p ))
#         s = " "*( 13 - decimal_point - ( p )) + s
#     return s
# 
# def parse_URI(uri):
#     import urlparse
#     import bitcoin
#     from decimal import Decimal
# 
#     if ':' not in uri:
#         assert bitcoin.is_address(uri)
#         return uri, None, None, None, None
# 
#     u = urlparse.urlparse(uri)
#     assert u.scheme == 'myriadcoin'
# 
#     address = u.path
#     valid_address = bitcoin.is_address(address)
# 
#     pq = urlparse.parse_qs(u.query)
#     
#     for k, v in pq.items():
#         if len(v)!=1:
#             raise Exception('Duplicate Key', k)
# 
#     amount = label = message = request_url = ''
#     if 'amount' in pq:
#         am = pq['amount'][0]
#         m = re.match('([0-9\.]+)X([0-9])', am)
#         if m:
#             k = int(m.group(2)) - 8
#             amount = Decimal(m.group(1)) * pow(  Decimal(10) , k)
#         else:
#             amount = Decimal(am) * 100000000
#     if 'message' in pq:
#         message = pq['message'][0]
#     if 'label' in pq:
#         label = pq['label'][0]
#     if 'r' in pq:
#         request_url = pq['r'][0]
#         
#     if request_url != '':
#         return address, amount, label, message, request_url
# 
#     assert valid_address
# 
#     return address, amount, label, message, request_url


# Path: lib/tests/test_util.py
import unittest
from lib.util import format_satoshis, parse_URI


class TestUtil(unittest.TestCase):

    def test_format_satoshis(self):
        result = format_satoshis(1234)
        expected = "0.00001234"
        self.assertEqual(expected, result)

    def test_format_satoshis_diff_positive(self):
        result = format_satoshis(1234, is_diff=True)
        expected = "+0.00001234"
        self.assertEqual(expected, result)

    def test_format_satoshis_diff_negative(self):
        result = format_satoshis(-1234, is_diff=True)
        expected = "-0.00001234"
        self.assertEqual(expected, result)

    def _do_test_parse_URI(self, uri, expected_address, expected_amount, expected_label, expected_message, expected_request_url):

        address, amount, label, message, request_url = parse_URI(uri)