class TestSectorSelectorAutoCode(unittest.TestCase):

    def setUp(self):
        self.limitChecker = MockLimitChecker()
        self.ss = VliegTransformSelector()
        self.ss.limitCheckerFunction = self.limitChecker.isDeltaNegative
        self.pos = P(0, .1, .2, .3, .4, 5)
        self.pos_in2 = self.ss.cutPosition(
            self.ss.transformNWithoutCut(2, self.pos))
        self.pos_in3 = self.ss.cutPosition(
            self.ss.transformNWithoutCut(3, self.pos))

    def testAddautoTransformWithSectors(self):
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        self.assertEquals(self.ss.autosectors, [2, 3])
        self.assertEquals(self.ss.autotransforms, [])
        self.ss.removeAutoTransform(3)
        self.assertEquals(self.ss.autosectors, [2])

    def testAddautoTransformTransforms(self):
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('b')
        self.assertEquals(self.ss.autosectors, [])
        self.assertEquals(self.ss.autotransforms, ['a', 'b'])

    def testRemoveAutoTransformWithSectors(self):
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        self.ss.removeAutoTransform(3)
        self.assertEquals(self.ss.autosectors, [2])
        self.ss.removeAutoTransform(2)
        self.assertEquals(self.ss.autosectors, [])

    def testRemoveAutoTransformTransforms(self):
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('b')
        self.ss.removeAutoTransform('b')
        self.assertEquals(self.ss.autotransforms, ['a'])
        self.ss.removeAutoTransform('a')
        self.assertEquals(self.ss.autotransforms, [])

    def testTransformPosition(self):
        # Check that with no transforms set to autoapply, the limit
        # checker function is ignored
        ss = VliegTransformSelector()
        ss.limitCheckerFunction = self.limitChecker.isPoswithiLimits
        self.limitChecker.okay = False
        ss.transformPosition(P(0, .1, .2, .3, .4, 5))

    def testMockLimitChecker(self):
        self.assertFalse(
            self.limitChecker.isDeltaNegative(P(0, .1, .2, .3, .4, 5)))
        self.assertTrue(
            self.limitChecker.isDeltaNegative(P(0, -.1, .2, .3, .4, 5)))

    def testAutoTransformPositionWithSectors(self):
        self.ss.addAutoTransorm(2)
        print "Should print 'INFO: Autosector changed sector from 0 to 2':"
        result = self.ss.autoTransformPositionBySector(self.pos)
        self.assert_(result == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)

    def testAutoTransformPositionWithSectorChoice(self):
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        print "Should print 'WARNING: Autosector found multiple sectors...':"
        print "Should print 'INFO: Autosector changed sector from 0 to 2':"
        result = self.ss.autoTransformPositionBySector(self.pos)
        self.assert_(result == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)

    def testAutoTransformPositionWithSectorsFails(self):
        self.ss.addAutoTransorm(0)
        self.ss.addAutoTransorm(1)
        self.ss.addAutoTransorm(4)
        self.ss.addAutoTransorm(5)
        print "Should print 'INFO: Autosector changed sector from 0 to 2':"
        self.assertRaises(
            Exception, self.ss.autoTransformPositionBySector, self.pos)
        #self.ss.autoTransformPositionBySector(self.pos)
        self.assertEquals(self.ss.sector, 0)  # unchanged

    def testCreateListOfPossibleTransforms(self):
        self.ss.addAutoTransorm('a')
        self.assertEquals(self.ss.createListOfPossibleTransforms(),
                          [(), ['a', ]])
        self.ss.addAutoTransorm('b')
        self.assertEquals(self.ss.createListOfPossibleTransforms(),
                          [(), ['b', ], ['a', ], ['a', 'b']])
        self.ss.transforms = ['a', 'c']

    def testAutoTransformPositionWithTransforms(self):
        self.ss.addAutoTransorm('a')
        print "Should print 'INFO: ':"
        result = self.ss.autoTransformPositionByTransforms(self.pos)
        self.assert_(result == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)

    def testAutoTransformPositionWithTansformsChoice(self):
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('c')
        print "Should print 'WARNING:':"
        print "Should print 'INFO: ':"
        result = self.ss.autoTransformPositionByTransforms(self.pos)
        self.assert_(result == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)

    def testTransformPositionWithAutoTransforms(self):
        self.ss.addAutoTransorm('a')
        self.assert_(self.ss.transformPosition(self.pos) == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)
        print "Should not print 'INFO...'"
        self.assert_(self.ss.transformPosition(self.pos) == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)

    def testTransformPositionWithAutoTransforms2(self):
        self.ss.addAutoTransorm(2)
        self.assert_(self.ss.transformPosition(self.pos) == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)
        print "Should not print 'INFO...'"
        self.assert_(self.ss.transformPosition(self.pos) == self.pos_in2)
        self.assertEquals(self.ss.sector, 2)

    def test__repr__(self):
        self.ss.setSector(0)
        print "************************"
        print self.ss
        print "************************"
        self.ss.setTransforms('a')
        print self.ss
        print "************************"
        self.ss.setTransforms(('a', 'b', 'c'))
        print self.ss
        print "************************"

        self.ss.addAutoTransorm(1)
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        print self.ss
        print "************************"
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('c')
        print self.ss
        print "************************"
class TestSectorSelectorAutoCode(object):
    def setup_method(self):
        self.limitChecker = MockLimitChecker()
        self.ss = VliegTransformSelector()
        self.ss.limitCheckerFunction = self.limitChecker.isDeltaNegative
        self.pos = P(0, .1, .2, .3, .4, 5)
        self.pos_in2 = self.ss.cutPosition(
            self.ss.transformNWithoutCut(2, self.pos))
        self.pos_in3 = self.ss.cutPosition(
            self.ss.transformNWithoutCut(3, self.pos))

    def testAddautoTransformWithSectors(self):
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        assert (self.ss.autosectors, [2, 3])
        assert (self.ss.autotransforms, [])
        self.ss.removeAutoTransform(3)
        assert (self.ss.autosectors, [2])

    def testAddautoTransformTransforms(self):
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('b')
        assert (self.ss.autosectors, [])
        assert (self.ss.autotransforms, ['a', 'b'])

    def testRemoveAutoTransformWithSectors(self):
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        self.ss.removeAutoTransform(3)
        assert (self.ss.autosectors, [2])
        self.ss.removeAutoTransform(2)
        assert (self.ss.autosectors, [])

    def testRemoveAutoTransformTransforms(self):
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('b')
        self.ss.removeAutoTransform('b')
        assert (self.ss.autotransforms, ['a'])
        self.ss.removeAutoTransform('a')
        assert (self.ss.autotransforms, [])

    def testTransformPosition(self):
        # Check that with no transforms set to autoapply, the limit
        # checker function is ignored
        ss = VliegTransformSelector()
        ss.limitCheckerFunction = self.limitChecker.isPoswithiLimits
        self.limitChecker.okay = False
        ss.transformPosition(P(0, .1, .2, .3, .4, 5))

    def testMockLimitChecker(self):
        assert not self.limitChecker.isDeltaNegative(P(0, .1, .2, .3, .4, 5))
        assert self.limitChecker.isDeltaNegative(P(0, -.1, .2, .3, .4, 5))

    def testAutoTransformPositionWithSectors(self):
        self.ss.addAutoTransorm(2)
        print "Should print 'INFO: Autosector changed sector from 0 to 2':"
        result = self.ss.autoTransformPositionBySector(self.pos)
        assert (result == self.pos_in2)
        assert (self.ss.sector, 2)

    def testAutoTransformPositionWithSectorChoice(self):
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        print "Should print 'WARNING: Autosector found multiple sectors...':"
        print "Should print 'INFO: Autosector changed sector from 0 to 2':"
        result = self.ss.autoTransformPositionBySector(self.pos)
        assert (result == self.pos_in2)
        assert (self.ss.sector, 2)

    def testAutoTransformPositionWithSectorsFails(self):
        self.ss.addAutoTransorm(0)
        self.ss.addAutoTransorm(1)
        self.ss.addAutoTransorm(4)
        self.ss.addAutoTransorm(5)
        print "Should print 'INFO: Autosector changed sector from 0 to 2':"
        with pytest.raises(Exception):
            self.ss.autoTransformPositionBySector(self.pos)
        #self.ss.autoTransformPositionBySector(self.pos)
        assert (self.ss.sector, 0)  # unchanged

    def testCreateListOfPossibleTransforms(self):
        self.ss.addAutoTransorm('a')
        assert (self.ss.createListOfPossibleTransforms(), [(), [
            'a',
        ]])
        self.ss.addAutoTransorm('b')
        assert (self.ss.createListOfPossibleTransforms(), [(), [
            'b',
        ], [
            'a',
        ], ['a', 'b']])
        self.ss.transforms = ['a', 'c']

    def testAutoTransformPositionWithTransforms(self):
        self.ss.addAutoTransorm('a')
        print "Should print 'INFO: ':"
        result = self.ss.autoTransformPositionByTransforms(self.pos)
        assert (result == self.pos_in2)
        assert (self.ss.sector, 2)

    def testAutoTransformPositionWithTansformsChoice(self):
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('c')
        print "Should print 'WARNING:':"
        print "Should print 'INFO: ':"
        result = self.ss.autoTransformPositionByTransforms(self.pos)
        assert (result == self.pos_in2)
        assert (self.ss.sector, 2)

    def testTransformPositionWithAutoTransforms(self):
        self.ss.addAutoTransorm('a')
        assert (self.ss.transformPosition(self.pos) == self.pos_in2)
        assert (self.ss.sector, 2)
        print "Should not print 'INFO...'"
        assert (self.ss.transformPosition(self.pos) == self.pos_in2)
        assert (self.ss.sector, 2)

    def testTransformPositionWithAutoTransforms2(self):
        self.ss.addAutoTransorm(2)
        assert (self.ss.transformPosition(self.pos) == self.pos_in2)
        assert (self.ss.sector, 2)
        print "Should not print 'INFO...'"
        assert (self.ss.transformPosition(self.pos) == self.pos_in2)
        assert (self.ss.sector, 2)

    def test__repr__(self):
        self.ss.setSector(0)
        print "************************"
        print self.ss
        print "************************"
        self.ss.setTransforms('a')
        print self.ss
        print "************************"
        self.ss.setTransforms(('a', 'b', 'c'))
        print self.ss
        print "************************"

        self.ss.addAutoTransorm(1)
        self.ss.addAutoTransorm(2)
        self.ss.addAutoTransorm(3)
        print self.ss
        print "************************"
        self.ss.addAutoTransorm('a')
        self.ss.addAutoTransorm('c')
        print self.ss
        print "************************"
class TestVliegTransformSelector(unittest.TestCase):

    def setUp(self):
        self.limitChecker = MockLimitChecker()
        self.ss = VliegTransformSelector()
        self.ss.limitCheckerFunction = self.limitChecker.isPoswithiLimits

    def test__init__(self):
        self.assertEquals(self.ss.sector, 0)

    def testCutPosition(self):
        d = .1
        tocut = P(-180., 180., 180. - d, 180. + d, -180. + d, -180. - d)
        expected = P(-180., 180., 180. - d, -180. + d, -180. + d, 180. - d)
        self.assert_(self.ss.cutPosition(tocut) == expected)

    def testTransformNWithoutCut(self):
        pos = P(1, 2, 3, 4, 5, 6)
        self.assert_(self.ss.transformNWithoutCut(0, pos) == pos)

    def testTransformPosition(self):
        pos = P(0 - 360, .1 + 360, .2 - 360, .3 + 360, .4, 5)
        res = self.ss.transformPosition(pos)
        des = P(0, .1, .2, .3, .4, 5)
        self.assert_(res == des, '%s!=\n%s' % (res, des))

    def testSetSector(self):
        self.ss.setSector(4)
        self.assertEquals(self.ss.sector, 4)
        self.assertEquals(self.ss.transforms, ['b', 'c'])

    def testSetTransforms(self):
        self.ss.setTransforms(('c', 'b'))
        self.assertEquals(self.ss.sector, 4)
        self.assertEquals(self.ss.transforms, ['b', 'c'])

    def testAddAutoTransormWithBadInput(self):
        self.assertEquals(self.ss.autosectors, [])
        self.assertEquals(self.ss.autotransforms, [])
        self.assertRaises(ValueError, self.ss.addAutoTransorm, 'not transform')
        self.assertRaises(ValueError, self.ss.addAutoTransorm, 9999)
        self.assertRaises(ValueError, self.ss.addAutoTransorm, [])

    def testAddAutoTransormWithTransforms(self):
        self.ss.autosectors = [1, 2, 3, 4, 5]
        self.ss.addAutoTransorm('a')
        print "Should now print a warning..."
        self.ss.addAutoTransorm('a')  # twice
        self.ss.addAutoTransorm('b')
        self.assertEquals(self.ss.autosectors, [])
        self.assertEquals(self.ss.autotransforms, ['a', 'b'])

    def testAddAutoTransormWithSectors(self):
        self.ss.autotransforms = ['a', 'c']
        self.ss.addAutoTransorm(2)
        print "Should now print a warning..."
        self.ss.addAutoTransorm(2)  # twice
        self.ss.addAutoTransorm(3)
        self.assertEquals(self.ss.autosectors, [2, 3])
        self.assertEquals(self.ss.autotransforms, [])

    def testis_position_within_limits(self):
        self.assertEquals(self.ss.is_position_within_limits(None), True)
        self.limitChecker.okay = False
        self.assertEquals(self.ss.is_position_within_limits(None), False)

    def test__repr__(self):
        self.ss.setSector(0)
        print "************************"
        print self.ss
        print "************************"
        self.ss.setTransforms('a')
        print self.ss
        print "************************"
        self.ss.setTransforms(('a', 'b', 'c'))
        print self.ss
        print "************************"
class TestVliegTransformSelector(object):
    def setup_method(self):
        self.limitChecker = MockLimitChecker()
        self.ss = VliegTransformSelector()
        self.ss.limitCheckerFunction = self.limitChecker.isPoswithiLimits

    def test__init__(self):
        assert (self.ss.sector, 0)

    def testCutPosition(self):
        d = .1
        tocut = P(-180., 180., 180. - d, 180. + d, -180. + d, -180. - d)
        expected = P(-180., 180., 180. - d, -180. + d, -180. + d, 180. - d)
        assert (self.ss.cutPosition(tocut) == expected)

    def testTransformNWithoutCut(self):
        pos = P(1, 2, 3, 4, 5, 6)
        assert (self.ss.transformNWithoutCut(0, pos) == pos)

    def testTransformPosition(self):
        pos = P(0 - 360, .1 + 360, .2 - 360, .3 + 360, .4, 5)
        res = self.ss.transformPosition(pos)
        des = P(0, .1, .2, .3, .4, 5)
        assert (res == des, '%s!=\n%s' % (res, des))

    def testSetSector(self):
        self.ss.setSector(4)
        assert (self.ss.sector, 4)
        assert (self.ss.transforms, ['b', 'c'])

    def testSetTransforms(self):
        self.ss.setTransforms(('c', 'b'))
        assert (self.ss.sector, 4)
        assert (self.ss.transforms, ['b', 'c'])

    def testAddAutoTransormWithBadInput(self):
        assert (self.ss.autosectors, [])
        assert (self.ss.autotransforms, [])
        with pytest.raises(ValueError):
            self.ss.addAutoTransorm('not transform')
        with pytest.raises(ValueError):
            self.ss.addAutoTransorm(9999)
        with pytest.raises(ValueError):
            self.ss.addAutoTransorm([])

    def testAddAutoTransormWithTransforms(self):
        self.ss.autosectors = [1, 2, 3, 4, 5]
        self.ss.addAutoTransorm('a')
        print "Should now print a warning..."
        self.ss.addAutoTransorm('a')  # twice
        self.ss.addAutoTransorm('b')
        assert (self.ss.autosectors, [])
        assert (self.ss.autotransforms, ['a', 'b'])

    def testAddAutoTransormWithSectors(self):
        self.ss.autotransforms = ['a', 'c']
        self.ss.addAutoTransorm(2)
        print "Should now print a warning..."
        self.ss.addAutoTransorm(2)  # twice
        self.ss.addAutoTransorm(3)
        assert (self.ss.autosectors, [2, 3])
        assert (self.ss.autotransforms, [])

    def testis_position_within_limits(self):
        assert (self.ss.is_position_within_limits(None), True)
        self.limitChecker.okay = False
        assert (self.ss.is_position_within_limits(None), False)

    def test__repr__(self):
        self.ss.setSector(0)
        print "************************"
        print self.ss
        print "************************"
        self.ss.setTransforms('a')
        print self.ss
        print "************************"
        self.ss.setTransforms(('a', 'b', 'c'))
        print self.ss
        print "************************"