from unittest import TestCase

class TestParse(TestCase):

    def test_tokens(self):
        from numeric.parse import gen_tokens
        from numeric.parse import NumberFormatException
        list_tokens = lambda *args, **kwargs: list(gen_tokens(*args, **kwargs))
        # ints
        self.assertListEqual(list_tokens('4'), ['4'])
        self.assertListEqual(list_tokens('44'), ['44'])
        self.assertListEqual(list_tokens('444'), ['444'])
        # decimals
        self.assertListEqual(list_tokens('4.'), ['4.'])
        self.assertListEqual(list_tokens('44.'), ['44.'])
        self.assertListEqual(list_tokens('444.'), ['444.'])
        self.assertListEqual(list_tokens('4.0'), ['4.0'])
        self.assertListEqual(list_tokens('44.0'), ['44.0'])
        self.assertListEqual(list_tokens('444.0'), ['444.0'])
        self.assertListEqual(list_tokens('.1'), ['.1'])
        self.assertListEqual(list_tokens('.12'), ['.12'])
        # too many decimals
        self.assertRaises(NumberFormatException, list_tokens, '4.4.')
        self.assertRaises(NumberFormatException, list_tokens, '4.44.')
        self.assertRaises(NumberFormatException, list_tokens, '4.44.3')
        self.assertRaises(NumberFormatException, list_tokens, '44.44.3')
        self.assertRaises(NumberFormatException, list_tokens, '..')
        # multi value
        self.assertListEqual(list_tokens('4+4'), ['4', '+', '4'])
        self.assertListEqual(list_tokens('4 +4'), ['4', '+', '4'])
        self.assertListEqual(list_tokens('4+ 4'), ['4', '+', '4'])
        self.assertListEqual(list_tokens('4 + 4'), ['4', '+', '4'])
        self.assertListEqual(list_tokens('4*4'), ['4', '*', '4'])
        self.assertListEqual(list_tokens('4 *4'), ['4', '*', '4'])
        self.assertListEqual(list_tokens('4* 4'), ['4', '*', '4'])
        self.assertListEqual(list_tokens('4 * 4'), ['4', '*', '4'])
        self.assertListEqual(list_tokens('4/4'), ['4', '/', '4'])
        self.assertListEqual(list_tokens('4 /4'), ['4', '/', '4'])
        self.assertListEqual(list_tokens('4/ 4'), ['4', '/', '4'])
        self.assertListEqual(list_tokens('4 / 4'), ['4', '/', '4'])
        self.assertListEqual(list_tokens('4-4'), ['4', '-', '4'])
        self.assertListEqual(list_tokens('4 -4'), ['4', '-', '4'])
        self.assertListEqual(list_tokens('4- 4'), ['4', '-', '4'])
        self.assertListEqual(list_tokens('4 - 4'), ['4', '-', '4'])
        self.assertListEqual(list_tokens('4^4'), ['4', '^', '4'])
        self.assertListEqual(list_tokens('4 ^4'), ['4', '^', '4'])
        self.assertListEqual(list_tokens('4^ 4'), ['4', '^', '4'])
        self.assertListEqual(list_tokens('4 ^ 4'), ['4', '^', '4'])
        # variable
        self.assertListEqual(list_tokens('a+a'), ['a', '+', 'a'])
        self.assertListEqual(list_tokens('a +a'), ['a', '+', 'a'])
        self.assertListEqual(list_tokens('a+ a'), ['a', '+', 'a'])
        self.assertListEqual(list_tokens('a + a'), ['a', '+', 'a'])
        self.assertListEqual(list_tokens('a*a'), ['a', '*', 'a'])
        self.assertListEqual(list_tokens('a *a'), ['a', '*', 'a'])
        self.assertListEqual(list_tokens('a* a'), ['a', '*', 'a'])
        self.assertListEqual(list_tokens('a * a'), ['a', '*', 'a'])
        self.assertListEqual(list_tokens('a/a'), ['a', '/', 'a'])
        self.assertListEqual(list_tokens('a /a'), ['a', '/', 'a'])
        self.assertListEqual(list_tokens('a/ a'), ['a', '/', 'a'])
        self.assertListEqual(list_tokens('a / a'), ['a', '/', 'a'])
        self.assertListEqual(list_tokens('a-a'), ['a', '-', 'a'])
        self.assertListEqual(list_tokens('a -a'), ['a', '-', 'a'])
        self.assertListEqual(list_tokens('a- a'), ['a', '-', 'a'])
        self.assertListEqual(list_tokens('a - a'), ['a', '-', 'a'])
        self.assertListEqual(list_tokens('a^a'), ['a', '^', 'a'])
        self.assertListEqual(list_tokens('a ^a'), ['a', '^', 'a'])
        self.assertListEqual(list_tokens('a^ a'), ['a', '^', 'a'])
        self.assertListEqual(list_tokens('a ^ a'), ['a', '^', 'a'])
        # combo left first
        self.assertListEqual(list_tokens('4+a'), ['4', '+', 'a'])
        self.assertListEqual(list_tokens('4 +a'), ['4', '+', 'a'])
        self.assertListEqual(list_tokens('4+ a'), ['4', '+', 'a'])
        self.assertListEqual(list_tokens('4 + a'), ['4', '+', 'a'])
        self.assertListEqual(list_tokens('4*a'), ['4', '*', 'a'])
        self.assertListEqual(list_tokens('4 *a'), ['4', '*', 'a'])
        self.assertListEqual(list_tokens('4* a'), ['4', '*', 'a'])
        self.assertListEqual(list_tokens('4 * a'), ['4', '*', 'a'])
        self.assertListEqual(list_tokens('4/a'), ['4', '/', 'a'])
        self.assertListEqual(list_tokens('4 /a'), ['4', '/', 'a'])
        self.assertListEqual(list_tokens('4/ a'), ['4', '/', 'a'])
        self.assertListEqual(list_tokens('4 / a'), ['4', '/', 'a'])
        self.assertListEqual(list_tokens('4-a'), ['4', '-', 'a'])
        self.assertListEqual(list_tokens('4 -a'), ['4', '-', 'a'])
        self.assertListEqual(list_tokens('4- a'), ['4', '-', 'a'])
        self.assertListEqual(list_tokens('4 - a'), ['4', '-', 'a'])
        self.assertListEqual(list_tokens('4^a'), ['4', '^', 'a'])
        self.assertListEqual(list_tokens('4 ^a'), ['4', '^', 'a'])
        self.assertListEqual(list_tokens('4^ a'), ['4', '^', 'a'])
        self.assertListEqual(list_tokens('4 ^ a'), ['4', '^', 'a'])
        # combo right first
        self.assertListEqual(list_tokens('b+2'), ['b', '+', '2'])
        self.assertListEqual(list_tokens('b +2'), ['b', '+', '2'])
        self.assertListEqual(list_tokens('b+ 2'), ['b', '+', '2'])
        self.assertListEqual(list_tokens('b + 2'), ['b', '+', '2'])
        self.assertListEqual(list_tokens('b*2'), ['b', '*', '2'])
        self.assertListEqual(list_tokens('b *2'), ['b', '*', '2'])
        self.assertListEqual(list_tokens('b* 2'), ['b', '*', '2'])
        self.assertListEqual(list_tokens('b * 2'), ['b', '*', '2'])
        self.assertListEqual(list_tokens('b/2'), ['b', '/', '2'])
        self.assertListEqual(list_tokens('b /2'), ['b', '/', '2'])
        self.assertListEqual(list_tokens('b/ 2'), ['b', '/', '2'])
        self.assertListEqual(list_tokens('b / 2'), ['b', '/', '2'])
        self.assertListEqual(list_tokens('b-2'), ['b', '-', '2'])
        self.assertListEqual(list_tokens('b -2'), ['b', '-', '2'])
        self.assertListEqual(list_tokens('b- 2'), ['b', '-', '2'])
        self.assertListEqual(list_tokens('b - 2'), ['b', '-', '2'])
        self.assertListEqual(list_tokens('b^2'), ['b', '^', '2'])
        self.assertListEqual(list_tokens('b ^2'), ['b', '^', '2'])
        self.assertListEqual(list_tokens('b^ 2'), ['b', '^', '2'])
        self.assertListEqual(list_tokens('b ^ 2'), ['b', '^', '2'])
        # with spaces
        self.assertListEqual(list_tokens(' b+2 '), ['b', '+', '2'])
        self.assertListEqual(list_tokens(' b +2 '), ['b', '+', '2'])
        self.assertListEqual(list_tokens(' b+ 2 '), ['b', '+', '2'])
        self.assertListEqual(list_tokens(' b + 2 '), ['b', '+', '2'])
        self.assertListEqual(list_tokens(' b*2 '), ['b', '*', '2'])
        self.assertListEqual(list_tokens(' b *2 '), ['b', '*', '2'])
        self.assertListEqual(list_tokens(' b* 2 '), ['b', '*', '2'])
        self.assertListEqual(list_tokens(' b * 2 '), ['b', '*', '2'])
        self.assertListEqual(list_tokens(' b/2 '), ['b', '/', '2'])
        self.assertListEqual(list_tokens(' b /2 '), ['b', '/', '2'])
        self.assertListEqual(list_tokens(' b/ 2 '), ['b', '/', '2'])
        self.assertListEqual(list_tokens(' b / 2 '), ['b', '/', '2'])
        self.assertListEqual(list_tokens(' b-2 '), ['b', '-', '2'])
        self.assertListEqual(list_tokens(' b -2 '), ['b', '-', '2'])
        self.assertListEqual(list_tokens(' b- 2 '), ['b', '-', '2'])
        self.assertListEqual(list_tokens(' b - 2 '), ['b', '-', '2'])
        self.assertListEqual(list_tokens(' b^2 '), ['b', '^', '2'])
        self.assertListEqual(list_tokens(' b ^2 '), ['b', '^', '2'])
        self.assertListEqual(list_tokens(' b^ 2 '), ['b', '^', '2'])
        self.assertListEqual(list_tokens(' b ^ 2 '), ['b', '^', '2'])

