#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest

from gi.repository import EBook
from email.utils import formataddr
from email.header import decode_header

class DataTestCase(unittest.TestCase):

    def __init__(self, name, email):
        unittest.TestCase.__init__(self, methodName='testAddressFormatting')
        self.name = name
        self.email = email

    def testAddressFormatting(self):
        contact = EBook.Contact()
        contact.set_property('name', EBook.ContactName.from_string(self.name))
        contact.set_property('email-1', self.email)

        destination = EBook.Destination()
        destination.set_contact(contact, 0)

        expected = formataddr((self.name, self.email))

        actual = ' '.join(n[0] for n in decode_header(destination.get_address()))
        self.assertMultiLineEqual(actual, expected)

    def shortDescription(self):
        return "Test case for name '%s', email '%s'" % (self.name, self.email)


def get_test_data_suite():
    test_data = ['John Doe',
                 'Jožin bažin',
                 'Jožin z bažin',
                 'Mr. Jožin bažin',
                 'Mr. Jožin z bažin',
                 'Mr. Jožin z bažin esquire']
    suite = unittest.TestSuite()
    # First run with empty email
    suite.addTests([DataTestCase(name, '') for name in test_data])
    # Then with some generic email
    suite.addTests([DataTestCase(name, 'some@email.com') for name in test_data])
    # And with unicode in email server
    suite.addTests([DataTestCase(name, 'jožin@bažin.com') for name in test_data])
    return suite


if __name__ == "__main__":
    testRunner = unittest.TextTestRunner()
    testRunner.run(get_test_data_suite())
