#!/usr/bin/env python3

import unittest

from fract4d_compiler import preprocessor


class Test(unittest.TestCase):
    def testEmpty(self):
        pp = preprocessor.T('')

    def testIfdef(self):
        pp = preprocessor.T('''
        $IFDEF wibble
        >>><<<
        $ENDIF
        wobble
        ''')

        self.assertEqual(pp.out(), '''



        wobble
        ''')

    def testUndef(self):
        pp = preprocessor.T('''
        $DEFINE foo
        $UNDEF foo
        $IFDEF foo
        poo
        $ENDIF
        ''')
        self.assertEqual(pp.out(), '''





        ''')

    def testContinuationFix(self):
        pp = preprocessor.T('''
        z = 1.0\\
        5
        ''')

        self.assertEqual(pp.out(), '''
        z = 1.05

        ''')

    def testContinuation2(self):
        pp = preprocessor.T('''
; The normal library that comes with Ultra Fractal includes much more advanced routines.\\

; Most of the routines includes something called "Iteration trap".
''')
        self.assertEqual(pp.out(), '''
; The normal library that comes with Ultra Fractal includes much more advanced routines.

; Most of the routines includes something called "Iteration trap".
''')

    def testBadUndef(self):
        pp = preprocessor.T('''
        $UNDEF foo
        ''')
        self.assertEqual(pp.out(), '''

        ''')

        try:
            pp = preprocessor.T('''
            $UNDEF
            ''')
            self.fail("should've raised an exception")
        except preprocessor.Error as err:
            self.assertEqual(str(err), "2: $UNDEF without variable")

    def testIfWithoutEndif(self):
        try:
            pp = preprocessor.T('''
            $IFDEF foople
            ''')
            self.fail("Should have raised an exception")
        except preprocessor.Error as err:
            self.assertEqual(str(err), "2: $IFDEF without $ENDIF")

    def testEndifWithoutIf(self):
        try:
            pp = preprocessor.T('''
            $IFDEF foogle

            $IFDEF bargle
            $ENDIF
            $ENDIF
            $ENDIF
            ''')
            self.fail("Should have raised an exception")
        except preprocessor.Error as err:
            self.assertEqual(str(err), "7: $ENDIF without $IFDEF")

    def testIfdefWithoutVar(self):
        try:
            pp = preprocessor.T('''
            $IFDEF
            foo
            $ENDIF
            ''')
            self.fail("Should have raised an exception")
        except preprocessor.Error as err:
            self.assertEqual(str(err), "2: $IFDEF without variable")

    def testDefine(self):
        pp = preprocessor.T('$define foo\nbar')

        self.assertEqual(pp.out(), "\nbar")

    def testDefineWithoutVar(self):
        try:
            pp = preprocessor.T('$define !!!\n')
            self.fail("Should have raised an exception")
        except preprocessor.Error as err:
            self.assertEqual(str(err), "1: $DEFINE without variable")

    def testDefineWorks(self):
        pp = preprocessor.T('''
        $define wibble
        $IFDEF wibble
        >>><<<
        $ENDIF
        wobble
        ''')

        self.assertEqual(pp.out(), '''


        >>><<<

        wobble
        ''')

    def testNestedIfdef(self):
        pp = preprocessor.T('''
        $define bar
        $IFDEF foo
           foo
           $IFDEF bar
           foobar
           $ENDIF
        $ENDIF
        $IFDEF bar
           bar
           $IFDEF foo
           barfoo
           $ENDIF
        $ENDIF
        ''')

        self.assertEqual(pp.out(), '''








           bar




        ''')

    def testNestedDefine(self):
        pp = preprocessor.T('''
        $IFDEF foo
           $deFine bar
           $define
        $ENDIF
        $IFDEF bar
           foobar
        $ENDIF
        ''')

        self.assertEqual(pp.out(), '''







        ''')

    def testElse(self):
        pp = preprocessor.T('''
        $define bar
        $IFDEF foo
           foo
        $ELSE
           not foo
           $ifdef bar
               notfoo,bar
           $else
               notfoo,notbar
           $endif
        $ENDIF
        ''')

        self.assertEqual(pp.out(), '''




           not foo

               notfoo,bar




        ''')

    def testCompressed(self):
        pp = preprocessor.T('''
        ExampleFractal2 {
::+1Vo1in2ln5SPutNQA47Lw+fQQnaRb8KNjlfkCdIpN9QRD6hm7BctotZjeVR6dX3f9doeSKR
  J7jFo5k18SDnZ48pF5YF7gil++HfwzTJUp8Y/P9GLrMl/rNaAfvXFJqzxRhg3Zu40ZV9PTZX
  5VyYwriLLSvoEF5xQQgOOHq4JClM2/XYZCeu3nX59bF5c5Pt9pdPR2g+P+Qt71v1Dsyan9/d
  tIvQfviS2Bh6aMsxLjXdinVkwjlUU55P+QGrsUkfqxVeuiXF/ugVbj2uD2Euf3OA3GFEEu5p
  gVBb3Gsb/ac7217BczOIaj2rM2p84wggNY4uVr3FteLC72TqOWUldJl9+GjeToj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