import sys

if sys.hexversion < 0x02070000:
    import unittest2 as unittest
else:
    import unittest

from datetime import date

import qual

class CalendarTest(unittest.TestCase):
    def check_valid_date(self, year, month, day):
        d = self.calendar.date(year, month, day)
        self.assertIsNotNone(d)

    def check_invalid_date(self, year, month, day):
        self.assertRaises(Exception, lambda : self.calendar(year, month, day))

class TestProlepticGregorianCalendar(CalendarTest):
    def setUp(self):
        self.calendar = qual.ProlepticGregorianCalendar()

    def test_leap_year_from_before_1582(self):
        """Pope Gregory introduced the calendar in 1582"""
        self.check_valid_date(1200, 2, 29)

    def test_day_missed_out_in_British_calendar_change(self):
        """This date never happened in English law:
           It was missed when changing from the Julian to
           Gregorian. This test proves that we are not
           using a historical British calendar."""
        self.check_valid_date(1752, 9, 3)

    def test_Julian_leap_day_is_not_a_valid_date(self):
        """This day /was/ a leap day contemporaneously,
           but is not a valid date of the Gregorian calendar."""
        self.check_invalid_date(1300, 2, 29)

    def test_calendar_reference(self):
        """Can we retrieve the calendar from the date"""
        d = self.calendar.date(2010, 8, 1)
        self.assertEqual(d.calendar, qual.ProlepticGregorianCalendar)

class TestJulianCalendar(CalendarTest):
    def setUp(self):
        self.calendar = qual.JulianCalendar()

    def test_Julian_to_Gregorian_conversion(self):
        """ This date is the first day the Gregorian calendar was used
            in Catholic countries which adopted the new calendar immediately.
            We should be able to convert it from Julian to Gregorian."""
        d = self.calendar.date(1582, 10, 5)
        c = qual.ProlepticGregorianCalendar()
        d2 = c.date(1582, 10, 15)
        self.assertEqual(d.convert_to(c), d2)

    def test_Gregorian_to_Julian_conversion(self):
        """Convert the first date of the Gregorian calendar to Julian."""
        gregorian_calendar = qual.ProlepticGregorianCalendar()
        gregorian_date = gregorian_calendar.date(1582, 10, 15)
        julian_date = self.calendar.date(1582, 10, 5)
        self.assertEqual(gregorian_date.convert_to(self.calendar), julian_date)


@@ -1576,32 +1576,209 @@
 lianCalendar()%0A%0A
+class JulianGregorianConversion(unittest.TestCase):%0A    def setUp(self):%0A        self.gregorian = qual.ProlepticGregorianCalendar()%0A        self.julian = qual.JulianCalendar()%0A%0A
     def test_Jul
@@ -2047,32 +2047,30 @@
    d = self.
-calendar
+julian
 .date(1582, 
@@ -2088,60 +2088,27 @@
     
-c = qual.ProlepticGregorianCalendar()%0A        d2 = c
+d2 = self.gregorian
 .dat
@@ -2161,17 +2161,30 @@
 vert_to(
-c
+self.gregorian
 ), d2)%0A%0A
@@ -2330,96 +2330,29 @@
 ian_
-calendar = qual.ProlepticGregorianCalendar()%0A        gregorian_date = gregorian_calendar
+date = self.gregorian
 .dat
@@ -2390,32 +2390,30 @@
 date = self.
-calendar
+julian
 .date(1582, 
@@ -2475,24 +2475,22 @@
 to(self.
-calendar
+julian
 ), julia

Move conversion between Julian and GRegorian into a new class.