from datetime import datetime

from twisted.trial.unittest import TestCase

from epsilon.extime import FixedOffset, Time

from methanal.model import Value
from methanal.view import DateInput

class MockParent(object):
    def __init__(self, param):
        self.store = None
        self.param = param

        self.page = self
        self.liveFragmentChildren = []

        self.model = self

    def getParameter(self, name):
        return self.param

    def addFormChild(self, child):
        pass



class TestDateInput(TestCase):
    def setUp(self):
        self.param = Value('test')
        self.control = DateInput(parent=MockParent(self.param), name='test', timezone=FixedOffset(0, 0))

    def test_getValue(self):
        self.param.value = Time.fromDatetime(datetime(2007, 1, 1))
        self.assertTrue(isinstance(self.control.getValue(), unicode))
        self.assertEqual(self.control.getValue(), u'2007-01-01')

        self.param.value = Time.fromDatetime(datetime(542, 12, 18))
        self.assertEqual(self.control.getValue(), u'0542-12-18')

@@ -46,33 +46,24 @@
 rial
-.unittest import TestCase
+ import unittest
 %0A%0Afr
@@ -120,20 +120,24 @@
 ethanal.
-mode
+imethana
 l import
@@ -137,21 +137,28 @@
  import 
-Value
+IEnumeration
 %0Afrom me
@@ -168,20 +168,21 @@
 nal.
-view
+model
  import 
 Date
@@ -177,25 +177,49 @@
  import 
-DateInput
+Value%0Afrom methanal import view%0A%0A
 %0A%0Aclass 
@@ -415,16 +415,17 @@
 = self%0A%0A
+%0A
     def 
@@ -473,24 +473,25 @@
 self.param%0A%0A
+%0A
     def addF
@@ -535,20 +535,16 @@
 %0A%0Aclass 
-Test
 DateInpu
@@ -544,17 +544,31 @@
 ateInput
-(
+Tests(unittest.
 TestCase
@@ -649,16 +649,21 @@
 ntrol = 
+view.
 DateInpu
@@ -664,16 +664,29 @@
 teInput(
+%0A            
 parent=M
@@ -707,16 +707,28 @@
 .param),
+%0A           
  name='t
@@ -732,16 +732,28 @@
 ='test',
+%0A           
  timezon
@@ -774,16 +774,17 @@
 0, 0))%0A%0A
+%0A
     def 
@@ -1140,8 +1140,1354 @@
 12-18')%0A
+%0A%0A%0Aclass ChoiceInputTests(unittest.TestCase):%0A    def setUp(self):%0A        self.param = Value('test')%0A%0A%0A    def _createChoice(self, values):%0A        %22%22%22%0A        Create a L%7Bmethanal.view.ChoiceInput%7D from C%7Bvalues%7D, assert that%0A        L%7Bmethanal.view.ChoiceInput.value%7D provides L%7BIEnumeration%7D and%0A        calling C%7BasPairs%7D results in the same values as C%7Bvalues%7D.%0A        %22%22%22%0A        control = view.ChoiceInput(%0A            parent=MockParent(self.param),%0A            name=self.param.name,%0A            values=values)%0A        self.assertTrue(IEnumeration.providedBy(control.values))%0A        self.assertEquals(control.values.asPairs(), list(values))%0A        return control%0A%0A%0A    def test_create(self):%0A        %22%22%22%0A        Create a L%7Bmethanal.view.ChoiceInput%7D by adapting values to%0A        L%7BIEnumeration%7D.%0A        %22%22%22%0A        values = %5B%0A            (u'foo', u'Foo'),%0A            (u'bar', u'Bar')%5D%0A        self._createChoice(values)%0A%0A%0A    def test_createDeprecated(self):%0A        %22%22%22%0A        Passing values that are not adaptable to IEnumeration are converted%0A        to a C%7Blist%7D, adapted to L%7BIEnumeration%7D and a warning is emitted.%0A        %22%22%22%0A        # Not a list.%0A        values = tuple(%5B%0A            (u'foo', u'Foo'),%0A            (u'bar', u'Bar')%5D)%0A        self._createChoice(values)%0A        self.assertEquals(len(self.flushWarnings()), 1)%0A

Test that ChoiceInput adapts values to IEnumeration correctly.