# coding=utf8


import unittest

from borax import choices


class Demo1Field(choices.ConstChoices):
    A = 1, 'VER'
    B = 2, 'STE'
    c = 3, 'SSS'
    D = 5
    _E = 6, 'ES'


class FieldChoiceTestCase(unittest.TestCase):
    def test_get_value(self):
        self.assertEqual(1, Demo1Field.A)
        self.assertEqual(2, Demo1Field.B)
        self.assertEqual(5, Demo1Field.D)

    def test_is_valid(self):
        self.assertTrue(Demo1Field.is_valid(1))
        self.assertTrue(Demo1Field.is_valid(2))
        self.assertTrue(Demo1Field.is_valid(3))
        self.assertFalse(Demo1Field.is_valid(4))
        self.assertTrue(Demo1Field.is_valid(5))
        self.assertFalse(Demo1Field.is_valid(6))

    def test_get_display(self):
        self.assertEqual('VER', Demo1Field.get_value_display(1))
        self.assertIsNone(Demo1Field.get_value_display(4))
        self.assertEqual(5, Demo1Field.get_value_display(5))


class GenderChoices(choices.ConstChoices):
    male = choices.Item('male', 'Male')
    female = choices.Item('female', 'Female')
    unknown = choices.Item('unknown', 'Unknown')


class ChoicesItemTestCase(unittest.TestCase):
    def test_get_value(self):
        self.assertEqual('male', GenderChoices.male)

    def test_is_valid(self):
        self.assertTrue(GenderChoices.is_valid('male'))
        self.assertTrue(GenderChoices.is_valid('female'))
        self.assertTrue(GenderChoices.is_valid('unknown'))
        self.assertFalse(GenderChoices.is_valid('Unknown'))

    def test_get_display(self):
        self.assertEqual('Male', GenderChoices.get_value_display('male'))
        self.assertEqual('Female', GenderChoices.get_value_display('female'))
        self.assertIsNone(Demo1Field.get_value_display(4))

    def test_choices(self):
        self.assertListEqual([('male', 'Male'), ('female', 'Female'), ('unknown', 'Unknown')],
                             GenderChoices.choices)


class OffsetChoices(choices.ConstChoices):
    up = choices.Item((0, -1), 'Up')
    down = choices.Item((0, 1), 'Down')
    left = choices.Item((-1, 0), 'Left')
    right = choices.Item((0, 1), 'Right')


class OffsetChoicesTestCase(unittest.TestCase):
    def test_get_value(self):
        self.assertEqual((0, -1), OffsetChoices.up)

    def test_is_valid(self):
        self.assertTrue(OffsetChoices.is_valid((0, 1)))
        self.assertTrue(OffsetChoices.is_valid((-1, 0)))
        self.assertFalse(OffsetChoices.is_valid((0, 0)))
        self.assertFalse(OffsetChoices.is_valid(0))

    def test_get_display(self):
        self.assertEqual('Up', OffsetChoices.get_value_display((0, -1)))
        self.assertEqual('Left', OffsetChoices.get_value_display((-1, 0)))
        self.assertIsNone(OffsetChoices.get_value_display((0, 0)))
