Esempio n. 1
0
    def test_if(self):

        self.assertEqual(BotlangSystem.run('(if #t 2 3)'), 2)
        self.assertEqual(BotlangSystem.run('(if #f 2 3)'), 3)
        self.assertEqual(BotlangSystem.run('(if (> 4 5) 100 200)'), 200)
        self.assertEqual(BotlangSystem.run('(if #t 2)'), 2)
        self.assertEqual(BotlangSystem.run('(if #f 2)'), Nil)
    def test_reverse(self):

        result = BotlangSystem.run('(reverse (list 1 2 3 4))')
        self.assertEqual(result, [4, 3, 2, 1])

        result = BotlangSystem.run('(reverse "sergio")')
        self.assertEqual(result, "oigres")
Esempio n. 3
0
    def test_reverse(self):

        result = BotlangSystem.run('(reverse (list 1 2 3 4))')
        self.assertEqual(result, [4, 3, 2, 1])

        result = BotlangSystem.run('(reverse "sergio")')
        self.assertEqual(result, "oigres")
Esempio n. 4
0
    def test_type_conversion(self):

        str_to_num = BotlangSystem.run('(num "666")')
        self.assertEqual(str_to_num, 666)

        num_to_str = BotlangSystem.run('(str 666)')
        self.assertEqual(num_to_str, "666")
Esempio n. 5
0
    def test_define(self):

        code = """
            (define x 2)
            (define y 3)
            (+ x y)
        """
        self.assertEqual(BotlangSystem.run(code), 5)

        code = """
            (define factorial
                (fun (n)
                    (if (equal? n 0)
                        1
                        (* n (factorial (- n 1)))
                    )
                )
            )
            (factorial 5)
        """
        self.assertEqual(BotlangSystem.run(code), 120)

        code = """
            [define f
                (fun (g n)
                    [fun (x) (g (+ x n))]
                )
            ]
            [define g
                (f [fun (n) (* n n)] 2)
            ]
            [define h (g 3)]
            h
        """
        self.assertEqual(BotlangSystem.run(code), 25)
    def test_base64(self):

        encoded = BotlangSystem.run('(b64-encode "hólá")')
        self.assertEqual(encoded, 'aMOzbMOh')

        decoded = BotlangSystem.run('(b64-decode "aMOzbMOh")')
        self.assertEqual(decoded, 'hólá')
Esempio n. 7
0
    def test_base64(self):

        encoded = BotlangSystem.run('(b64-encode "hólá")')
        self.assertEqual(encoded, 'aMOzbMOh')

        decoded = BotlangSystem.run('(b64-decode "aMOzbMOh")')
        self.assertEqual(decoded, 'hólá')
Esempio n. 8
0
    def test_define(self):

        code = """
            (define x 2)
            (define y 3)
            (+ x y)
        """
        self.assertEqual(BotlangSystem.run(code), 5)

        code = """
            (define factorial
                (fun (n)
                    (if (equal? n 0)
                        1
                        (* n (factorial (- n 1)))
                    )
                )
            )
            (factorial 5)
        """
        self.assertEqual(BotlangSystem.run(code), 120)

        code = """
            [define f
                (fun (g n)
                    [fun (x) (g (+ x n))]
                )
            ]
            [define g
                (f [fun (n) (* n n)] 2)
            ]
            [define h (g 3)]
            h
        """
        self.assertEqual(BotlangSystem.run(code), 25)
    def test_type_conversion(self):

        str_to_num = BotlangSystem.run('(num "666")')
        self.assertEqual(str_to_num, 666)

        num_to_str = BotlangSystem.run('(str 666)')
        self.assertEqual(num_to_str, "666")
Esempio n. 10
0
    def test_timestamp(self):

        t0 = math.floor(BotlangSystem.run('(timestamp)'))
        import time
        time.sleep(0.5)
        t1 = round(BotlangSystem.run('(timestamp)'))
        self.assertEqual(t1 - t0, 1)
Esempio n. 11
0
    def test_timestamp(self):

        t0 = math.floor(BotlangSystem.run('(timestamp)'))
        import time
        time.sleep(0.51)
        t1 = round(BotlangSystem.run('(timestamp)'))
        self.assertEqual(t1 - t0, 1)
Esempio n. 12
0
    def test_type(self):

        self.assertTrue(
            BotlangSystem.run('(list? (list 1 2 3))')
        )
        self.assertFalse(
            BotlangSystem.run('(list? "bla")')
        )
Esempio n. 13
0
    def test_type(self):

        self.assertTrue(
            BotlangSystem.run('(list? (list 1 2 3))')
        )
        self.assertFalse(
            BotlangSystem.run('(list? "bla")')
        )
Esempio n. 14
0
    def test_and(self):

        self.assertTrue(BotlangSystem.run('(and #t #t)'))
        self.assertFalse(BotlangSystem.run('(and #t #f)'))
        self.assertFalse(BotlangSystem.run('(and #f #t)'))
        self.assertFalse(BotlangSystem.run('(and #f #f)'))
        self.assertFalse(
            BotlangSystem.run('(and (not (nil? nil)) (equal? (length nil) 1))')
        )
Esempio n. 15
0
    def test_matches(self):

        self.assertTrue(
            BotlangSystem.run(
                '(match? ".*pedro.*" "hola pedro, como estas?")'
            )
        )
        self.assertFalse(
            BotlangSystem.run(
                '(match? ".*pedro.*" "hola julito, como estas?")'
            )
        )
Esempio n. 16
0
    def test_fun_name(self):

        code = """
        (defun fun6 (a) (+ a 3))
        (reflect-fun-name fun6)
        """
        self.assertEqual(BotlangSystem.run(code), 'fun6')

        code = '(reflect-fun-name (fun (a) (+ a 1)))'
        self.assertEqual(BotlangSystem.run(code), Nil)

        code = '(reflect-fun-name cos)'
        self.assertEqual(BotlangSystem.run(code), 'cos')
Esempio n. 17
0
    def test_environment(self):

        runtime = BotlangSystem()
        bindings = {
            'x': 4,
            'hola': BotlangSystem.run('(max (list 1 3 2))'),
            '+': Primitive(lambda x, y: x * y, runtime.environment)
        }
        new_env = runtime.environment.new_environment(bindings)

        self.assertEqual(BotlangSystem.run('(- 3 x)', new_env), -1)
        self.assertEqual(BotlangSystem.run('(- 10 hola)', new_env), 7)
        self.assertEqual(BotlangSystem.run('(+ 2 3)', new_env), 6)
Esempio n. 18
0
    def test_defun(self):

        code = """
            (defun hola (x y) (+ x y))
            (hola 5 3)
        """
        self.assertEqual(BotlangSystem.run(code), 8)

        code = """
            (defun oli () "oli")
            (oli)
        """
        self.assertEqual(BotlangSystem.run(code), 'oli')
Esempio n. 19
0
    def test_time_weekday(self):
        t = '(time-from-string "14 Apr 19")'
        weekday = BotlangSystem.run('(time-weekday %s)' % t)
        self.assertEqual(weekday, 6)

        weekday_iso = BotlangSystem.run('(time-weekday-iso %s)' % t)
        self.assertEqual(weekday_iso, 7)

        weekday_es = BotlangSystem.run('(time-weekday-str %s "ES")' % t)
        self.assertEqual(weekday_es, 'domingo')

        weekday_es = BotlangSystem.run('(time-weekday-str %s "EN")' % t)
        self.assertEqual(weekday_es, 'sunday')
Esempio n. 20
0
    def test_defun(self):

        code = """
            (defun hola (x y) (+ x y))
            (hola 5 3)
        """
        self.assertEqual(BotlangSystem.run(code), 8)

        code = """
            (defun oli () "oli")
            (oli)
        """
        self.assertEqual(BotlangSystem.run(code), 'oli')
Esempio n. 21
0
    def test_environment(self):

        runtime = BotlangSystem()
        bindings = {
            'x': 4,
            'hola': BotlangSystem.run('(max (list 1 3 2))'),
            '+': Primitive(lambda x, y: x * y, runtime.environment)
        }
        new_env = runtime.environment.new_environment(bindings)

        self.assertEqual(BotlangSystem.run('(- 3 x)', new_env), -1)
        self.assertEqual(BotlangSystem.run('(- 10 hola)', new_env), 7)
        self.assertEqual(BotlangSystem.run('(+ 2 3)', new_env), 6)
Esempio n. 22
0
    def test_nil(self):

        result = BotlangSystem.run("""
            [define value nil]
            (nil? value)
        """)
        self.assertTrue(result)

        result = BotlangSystem.run('(not-nil? nil)')
        self.assertFalse(result)

        result = BotlangSystem.run('(nil? #f)')
        self.assertFalse(result)
Esempio n. 23
0
    def test_string_operations(self):

        lower = BotlangSystem.run('(lowercase "AbCdEfgH")')
        self.assertEqual(lower, "abcdefgh")

        upper = BotlangSystem.run('(uppercase "AbCdEfgH")')
        self.assertEqual(upper, "ABCDEFGH")

        capitalized = BotlangSystem.run('(capitalize "aleluya hmno")')
        self.assertEqual(capitalized, "Aleluya hmno")

        split = BotlangSystem.run('(split "perro,gato,zapallo" ",")')
        self.assertEqual(split, ['perro', 'gato', 'zapallo'])

        join = BotlangSystem.run(
            '(join ", " (list "pollos" "pavos" "iguana"))'
        )
        self.assertEqual(join, 'pollos, pavos, iguana')

        plain = BotlangSystem.run('(plain "ÉnTérO BellákO")')
        self.assertEqual(plain, 'entero bellako')

        replaced = BotlangSystem.run('(replace "muajaja" "j" "h")')
        self.assertEqual(replaced, 'muahaha')

        trimmed = BotlangSystem.run('(trim "   hola, soy julito  ")')
        self.assertEqual(trimmed, 'hola, soy julito')
Esempio n. 24
0
    def test_and(self):

        self.assertTrue(BotlangSystem.run('(and #t #t)'))
        self.assertFalse(BotlangSystem.run('(and #t #f)'))
        self.assertFalse(BotlangSystem.run('(and #f #t)'))
        self.assertFalse(BotlangSystem.run('(and #f #f)'))
        self.assertFalse(
            BotlangSystem.run(
                '(and (not (nil? nil)) (equal? (length nil) 1))'))
        self.assertTrue(BotlangSystem.run('(and #t #t #t)'))
        self.assertFalse(BotlangSystem.run('(and #t #t #f)'))
        self.assertFalse(BotlangSystem.run('(and #t #f (/ 1 0))'))

        self.assertTrue(BotlangSystem.run('(and #t)'))
        self.assertTrue(BotlangSystem.run('(and)'))
Esempio n. 25
0
    def test_modules_resolver(self):

        resolver = BotlangSystem.bot_modules_resolver(
            BotlangSystem.base_environment()
        )
        valid_rut = BotlangSystem.run(
            '(require "bot-helpers") (validate-rut "16926695-6")',
            module_resolver=resolver
        )
        self.assertTrue(valid_rut)

        invalid_rut = BotlangSystem.run(
            '(require "bot-helpers") (validate-rut "16926695-5")',
            module_resolver=resolver
        )
        self.assertFalse(invalid_rut)
Esempio n. 26
0
    def test_nil(self):

        code = """
            [define value nil]
            (nil? value)
        """
        result = BotlangSystem.run(code)
        self.assertTrue(result)
Esempio n. 27
0
    def test_any_satisfy(self):

        self.assertTrue(
            BotlangSystem.run(
                '(any-satisfy? (fun (x) (equal? x 3)) (list 1 2 3 4))'
            )
        )
        self.assertTrue(
            BotlangSystem.run(
                '(any-satisfy? (fun (x) (equal? x 2)) (list 1 2 3 4))'
            )
        )
        self.assertFalse(
            BotlangSystem.run(
                '(any-satisfy? (fun (x) (equal? x -1)) (list 1 2 3 4))'
            )
        )
Esempio n. 28
0
    def test_any_satisfy(self):

        self.assertTrue(
            BotlangSystem.run(
                '(any-satisfy? (fun (x) (equal? x 3)) (list 1 2 3 4))'
            )
        )
        self.assertTrue(
            BotlangSystem.run(
                '(any-satisfy? (fun (x) (equal? x 2)) (list 1 2 3 4))'
            )
        )
        self.assertFalse(
            BotlangSystem.run(
                '(any-satisfy? (fun (x) (equal? x -1)) (list 1 2 3 4))'
            )
        )
Esempio n. 29
0
    def test_primitive_values(self):

        self.assertTrue(BotlangSystem.run('#t'))
        self.assertTrue(BotlangSystem.run('true'))
        self.assertFalse(BotlangSystem.run('#f'))
        self.assertFalse(BotlangSystem.run('false'))
        self.assertEqual(BotlangSystem.run('2'), 2)
        self.assertEqual(BotlangSystem.run('3.14'), 3.14)
        self.assertEqual(BotlangSystem.run('"hola"'), "hola")
        self.assertEqual(BotlangSystem.run('"\u2063"'), u'\u2063')
Esempio n. 30
0
    def test_time_to_string(self):
        t = 975553200  # 30 nov 00
        result = BotlangSystem.run(
            '(time-to-string {} "%d %b %y %H:%M")'.format(t))
        self.assertEqual('30 Nov 00 03:00', result)

        result = BotlangSystem.run(
            '(time-to-string {} "%d %b %y %H:%M" "America/Santiago")'.format(
                t))
        self.assertEqual('30 Nov 00 00:00', result)

        t = time_from_string('2019-04-16T01')
        result = BotlangSystem.run('(time-to-string {} "%d %H:%M")'.format(t))
        self.assertEqual(result, '16 01:00')

        result = BotlangSystem.run(
            '(time-to-string {} "%d %H:%M" "America/Santiago")'.format(t))
        self.assertEqual(result, '15 21:00')
Esempio n. 31
0
    def test_begin(self):

        code = """
            (begin
                (define x 1)
                (define y 2)
                (+ x y)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 3)
Esempio n. 32
0
    def test_or(self):

        self.assertTrue(BotlangSystem.run('(or #t #t)'))
        self.assertTrue(BotlangSystem.run('(or #t #f)'))
        self.assertTrue(BotlangSystem.run('(or #f #t)'))
        self.assertFalse(BotlangSystem.run('(or #f #f)'))

        self.assertTrue(BotlangSystem.run('(or #f #f #t)'))
        self.assertTrue(BotlangSystem.run('(or #f #t (/ 1 0))'))

        self.assertTrue(BotlangSystem.run('(or #t)'))
        self.assertFalse(BotlangSystem.run('(or)'))
Esempio n. 33
0
    def test_begin(self):

        code = """
            (begin
                (define x 1)
                (define y 2)
                (+ x y)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 3)
Esempio n. 34
0
    def test_time_add_seconds(self):
        time_plus = BotlangSystem.run("""
            (define init_time 100)
            (time-delta-seconds init_time 30)
        """)
        self.assertEqual(time_plus, 100 + 30)

        time_less = BotlangSystem.run("""
              (define init_time 100)
              (time-delta-seconds init_time -30)
          """)
        self.assertEqual(time_less, 100 - 30)

        time_now = time.time()
        time_plus30 = time_now + 30
        result = BotlangSystem.run("""
            (define init_time """ + str(time_now) + """ )
            (time-delta-seconds init_time 30)
            """)
        self.assertEqual(time_plus30, result)
Esempio n. 35
0
    def test_time_add_minutes(self):
        time_plus = BotlangSystem.run("""
            (define init_time 120)
            (time-delta-minutes init_time 30)
        """)
        self.assertEqual(time_plus, 120 + 30 * 60)

        time_less = BotlangSystem.run("""
            (define init_time 120)
            (time-delta-minutes init_time -30)
        """)
        self.assertEqual(time_less, 120 - 30 * 60)

        time_now = time.time()
        time_plus30 = time_now + 30 * 60
        result = BotlangSystem.run("""
            (define init_time """ + str(time_now) + """ )
            (time-delta-minutes init_time 30)
            """)
        self.assertEqual(time_plus30, result)
Esempio n. 36
0
    def test_time_add_days(self):
        time_plus = BotlangSystem.run("""
            (define init_time 150)
            (time-delta-days init_time 30)
        """)
        self.assertEqual(time_plus, 150 + 30 * 60 * 60 * 24)

        time_less = BotlangSystem.run("""
            (define init_time 150)
            (time-delta-days init_time -30)
        """)
        self.assertEqual(time_less, 150 - 30 * 60 * 60 * 24)

        time_now = time.time()
        time_plus30 = time_now + 30 * 60 * 60 * 24
        result = BotlangSystem.run("""
            (define init_time """ + str(time_now) + """ )
            (time-delta-days init_time 30)
            """)
        self.assertEqual(time_plus30, result)
Esempio n. 37
0
    def test_compression(self):

        text = """
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
        veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
        commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
        velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident, sunt in culpa qui officia deserunt
        mollit anim id est laborum.
        """

        self.assertEqual(len(text), 511)
        compressed = BotlangSystem.run('(bz2-compress "{0}")'.format(text))
        self.assertEqual(len(compressed), 420)

        decompressed = BotlangSystem.run(
            '(bz2-decompress "{0}")'.format(compressed)
        )
        self.assertEqual(len(decompressed), len(text))
Esempio n. 38
0
    def test_compression(self):

        text = """
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
        veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
        commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
        velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident, sunt in culpa qui officia deserunt
        mollit anim id est laborum.
        """

        self.assertEqual(len(text), 511)
        compressed = BotlangSystem.run('(bz2-compress "{0}")'.format(text))
        self.assertEqual(len(compressed), 420)

        decompressed = BotlangSystem.run(
            '(bz2-decompress "{0}")'.format(compressed)
        )
        self.assertEqual(len(decompressed), len(text))
Esempio n. 39
0
    def test_primitive_application(self):

        self.assertTrue(BotlangSystem.run('(not #f)'))
        self.assertEqual(BotlangSystem.run('(sqrt 4)'), 2)
        self.assertEqual(BotlangSystem.run('(* 5 (/ 10 2))'), 25)
        self.assertEqual(BotlangSystem.run('(list 3 4 5 2 1)'), [3, 4, 5, 2, 1])
        self.assertEqual(BotlangSystem.run('(max (list 3 4 5 2 1))'), 5)
        self.assertEqual(BotlangSystem.run('(min (list 3 4 5 2 1))'), 1)
        self.assertEqual(BotlangSystem.run('(map abs (list 1 -2 3))'), [1, 2, 3])
        self.assertEqual(
            BotlangSystem.run('(append "Asd \\"" "qwerty" "\\". sumthin")'),
            'Asd "qwerty". sumthin'
        )
Esempio n. 40
0
    def test_local_definitions(self):

        code = """
            (local
                (
                    (f (fun (x y) (* x y)))
                    (x (* 2 3))
                )
                (f x 2)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 12)
Esempio n. 41
0
    def test_local_definitions(self):

        code = """
            (local
                (
                    (f (fun (x y) (* x y)))
                    (x (* 2 3))
                )
                (f x 2)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 12)
Esempio n. 42
0
    def test_nesting(self):

        code = """
            (if (> (* 0.3 (+ 10 23)) 10)
                esto-no-sera-evaluado-asi-que-pico
                (and
                     (= (head (map abs (list -1 -2 -3))) 1)
                     (<= 11 (/ 100 6))
                )
             )
        """
        self.assertTrue(BotlangSystem.run(code))
Esempio n. 43
0
    def test_nesting(self):

        code = """
            (if (> (* 0.3 (+ 10 23)) 10)
                esto-no-sera-evaluado-asi-que-pico
                (and
                     (= (head (map abs (list -1 -2 -3))) 1)
                     (<= 11 (/ 100 6))
                )
             )
        """
        self.assertTrue(BotlangSystem.run(code))
Esempio n. 44
0
    def test_primitive_application(self):

        self.assertTrue(BotlangSystem.run('(not #f)'))
        self.assertEqual(BotlangSystem.run('(sqrt 4)'), 2)
        self.assertEqual(BotlangSystem.run('(* 5 (/ 10 2))'), 25)
        self.assertEqual(BotlangSystem.run('(list 3 4 5 2 1)'),
                         [3, 4, 5, 2, 1])
        self.assertEqual(BotlangSystem.run('(max (list 3 4 5 2 1))'), 5)
        self.assertEqual(BotlangSystem.run('(min (list 3 4 5 2 1))'), 1)
        self.assertEqual(BotlangSystem.run('(map abs (list 1 -2 3))'),
                         [1, 2, 3])
        self.assertEqual(
            BotlangSystem.run('(append "Asd \\"" "qwerty" "\\". sumthin")'),
            'Asd "qwerty". sumthin')
Esempio n. 45
0
    def test_lists(self):

        self.assertEqual(BotlangSystem.run('(list 1 2 3 4)'), [1, 2, 3, 4])
        self.assertEqual(BotlangSystem.run('\'(1 2 3 4)'), [1, 2, 3, 4])
        self.assertEqual(BotlangSystem.run('(list "1" "2")'), ['1', '2'])
        self.assertEqual(BotlangSystem.run('\'("1" "2")'), ['1', '2'])
        self.assertEqual(BotlangSystem.run('\'(hola chao)'), ['hola', 'chao'])
        self.assertEqual(BotlangSystem.run('(list "hola" "chao")'),
                         ['hola', 'chao'])

        a_list = BotlangSystem.run('(cons (list 2 3) 1)')
        self.assertEqual(a_list, [[2, 3], 1])
        another_list = BotlangSystem.run('(cons 1 (list 2 3))')
        self.assertEqual(another_list, [1, 2, 3])
Esempio n. 46
0
    def test_primitive_values(self):

        self.assertTrue(BotlangSystem.run('#t'))
        self.assertFalse(BotlangSystem.run('#f'))
        self.assertEqual(BotlangSystem.run('2'), 2)
        self.assertEqual(BotlangSystem.run('3.14'), 3.14)
        self.assertEqual(BotlangSystem.run('"hola"'), "hola")
        self.assertEqual(BotlangSystem.run('"\u2063"'), u'\u2063')
Esempio n. 47
0
    def test_module(self):

        module_resolver = ModuleResolver(BotlangSystem.base_environment())
        module = BotlangSystem.run("""
        (module "my-module"
            [define say-cats
                (function () "cats")
            ]
            [define say-i-like
                (function () "i like")
            ]
            [define say-sentence
                (function () (append (say-i-like) " " (say-cats)))
            ]

            (provide
                say-sentence
                say-cats
            )
        )
        """, module_resolver=module_resolver)
        self.assertEqual(module.name, 'my-module')

        bindings = module.get_bindings(
            Evaluator(module_resolver=module_resolver)
        )
        self.assertEqual(len(bindings.items()), 2)
        self.assertFalse(bindings.get('say-sentence') is None)
        self.assertFalse(bindings.get('say-cats') is None)
        self.assertTrue(bindings.get('say-i-like') is None)

        code = """
        (require "my-module")
        (say-sentence)
        """
        result = BotlangSystem.run(code, module_resolver=module_resolver)
        self.assertEqual(result, "i like cats")
Esempio n. 48
0
    def test_first_order_functions(self):

        code = """
            (begin
                (define f
                    (fun (n)
                        (fun (x) (+ n x))
                    )
                )
                (define g (f 3))

                (+ (g 3) (g 2))
            )
        """
        self.assertEqual(BotlangSystem.run(code), 11)
Esempio n. 49
0
    def test_first_order_functions(self):

        code = """
            (begin
                (define f
                    (fun (n)
                        (fun (x) (+ n x))
                    )
                )
                (define g (f 3))

                (+ (g 3) (g 2))
            )
        """
        self.assertEqual(BotlangSystem.run(code), 11)
Esempio n. 50
0
    def test_recursion(self):

        code = """
            (local
                ((factorial
                    (fun (n)
                        (if (equal? n 1)
                            n
                            (* n (factorial (- n 1)))
                        )
                    )
                ))
                (factorial 5)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 120)
Esempio n. 51
0
    def test_recursion(self):

        code = """
            (local
                ((factorial
                    (fun (n)
                        (if (equal? n 1)
                            n
                            (* n (factorial (- n 1)))
                        )
                    )
                ))
                (factorial 5)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 120)
Esempio n. 52
0
    def test_external_modules(self):

        external_module = ExternalModule(
            'cool-module',
            {
                'moo': lambda: 'moo',
                'meow': lambda: 'mew'
            }
        )
        environment = BotlangSystem.base_environment()
        resolver = ModuleResolver(environment)
        resolver.add_module(external_module)
        meow = BotlangSystem.run(
            '(require "cool-module") (meow)',
            module_resolver=resolver
        )
        self.assertEqual(meow, 'mew')
Esempio n. 53
0
    def test_lists(self):

        self.assertEqual(BotlangSystem.run('(list 1 2 3 4)'), [1, 2, 3, 4])
        self.assertEqual(BotlangSystem.run('\'(1 2 3 4)'), [1, 2, 3, 4])
        self.assertEqual(BotlangSystem.run('(list "1" "2")'), ['1', '2'])
        self.assertEqual(BotlangSystem.run('\'("1" "2")'), ['1', '2'])
        self.assertEqual(
            BotlangSystem.run('\'(hola chao)'),
            ['hola', 'chao']
        )
        self.assertEqual(
            BotlangSystem.run('(list "hola" "chao")'),
            ['hola', 'chao']
        )

        a_list = BotlangSystem.run('(cons (list 2 3) 1)')
        self.assertEqual(a_list, [[2, 3], 1])
        another_list = BotlangSystem.run('(cons 1 (list 2 3))')
        self.assertEqual(another_list, [1, 2, 3])
Esempio n. 54
0
    def test_sort(self):

        sorted_lists = BotlangSystem.run("""
            [define num-list (list 5 3 0 4 9 1)]
            [define asc-nums (sort (function (a b) (< a b)) num-list)]
            [define desc-nums (sort (function (a b) (> a b)) num-list)]

            [define objs-list
                (list
                    (list "holi" 1)
                    (list "shao" 4)
                    (list "bla" -3)
                    (list "lala" -8)
                )
            ]
            [define asc-objs
                (sort [function (a b) (< (get a 1) (get b 1))] objs-list)
            ]
            [define desc-objs
                (sort [function (a b) (> (get a 1) (get b 1))] objs-list)
            ]

            (make-dict
                (list
                    (list "asc-nums" asc-nums)
                    (list "desc-nums" desc-nums)
                    (list "asc-objs" asc-objs)
                    (list "desc-objs" desc-objs)
                )
            )
        """)
        self.assertEqual(sorted_lists['asc-nums'], [0, 1, 3, 4, 5, 9])
        self.assertEqual(sorted_lists['desc-nums'], [9, 5, 4, 3, 1, 0])
        self.assertEqual(
            sorted_lists['asc-objs'],
            [["lala", -8], ["bla", -3], ["holi", 1], ["shao", 4]]
        )
        self.assertEqual(
            sorted_lists['desc-objs'],
            [["shao", 4], ["holi", 1], ["bla", -3], ["lala", -8]]
        )
Esempio n. 55
0
    def test_sort(self):

        sorted_lists = BotlangSystem.run("""
            [define num-list (list 5 3 0 4 9 1)]
            [define asc-nums (sort (function (a b) (< a b)) num-list)]
            [define desc-nums (sort (function (a b) (> a b)) num-list)]

            [define objs-list
                (list
                    (list "holi" 1)
                    (list "shao" 4)
                    (list "bla" -3)
                    (list "lala" -8)
                )
            ]
            [define asc-objs
                (sort [function (a b) (< (get a 1) (get b 1))] objs-list)
            ]
            [define desc-objs
                (sort [function (a b) (> (get a 1) (get b 1))] objs-list)
            ]

            (make-dict
                (list
                    (list "asc-nums" asc-nums)
                    (list "desc-nums" desc-nums)
                    (list "asc-objs" asc-objs)
                    (list "desc-objs" desc-objs)
                )
            )
        """)
        self.assertEqual(sorted_lists['asc-nums'], [0, 1, 3, 4, 5, 9])
        self.assertEqual(sorted_lists['desc-nums'], [9, 5, 4, 3, 1, 0])
        self.assertEqual(
            sorted_lists['asc-objs'],
            [["lala", -8], ["bla", -3], ["holi", 1], ["shao", 4]]
        )
        self.assertEqual(
            sorted_lists['desc-objs'],
            [["shao", 4], ["holi", 1], ["bla", -3], ["lala", -8]]
        )
Esempio n. 56
0
    def test_time_part_extractors(self):
        t = '(time-from-string "14 Apr 19 18:30:12")'
        weekday = BotlangSystem.run('(time-year %s)' % t)
        self.assertEqual(weekday, 2019)

        weekday = BotlangSystem.run('(time-month %s)' % t)
        self.assertEqual(weekday, 4)

        weekday = BotlangSystem.run('(time-day %s)' % t)
        self.assertEqual(weekday, 14)

        weekday = BotlangSystem.run('(time-hour %s)' % t)
        self.assertEqual(weekday, 18)

        weekday = BotlangSystem.run('(time-hour %s "America/Santiago")' % t)
        self.assertEqual(weekday, 14)

        weekday = BotlangSystem.run('(time-minute %s)' % t)
        self.assertEqual(weekday, 30)

        weekday = BotlangSystem.run('(time-second %s)' % t)
        self.assertEqual(weekday, 12)
Esempio n. 57
0
    def test_dictionaries(self):

        computed_dict = BotlangSystem.run("""
        (make-dict '[
                (holi "chao")
                (doge "wow")
                (such "much")
            ]
        )
        """)
        expected_dict = OrderedDict([
            ('holi', 'chao'),
            ('doge', 'wow'),
            ('such', 'much')
        ])
        self.assertEqual(computed_dict, expected_dict)

        computed_dict = BotlangSystem.run("""
        (make-dict (list
                '(holi "chao")
                '(doge "wow")
                '(such "much")
            )
        )
        """)
        self.assertEqual(computed_dict, expected_dict)

        computed_dict = BotlangSystem.run("""
        (make-dict (list
                (list 'holi "chao")
                (list 'doge "wow")
                (list 'such "much")
            )
        )
        """)
        self.assertEqual(computed_dict, expected_dict)

        dict_keys = BotlangSystem.run("""
        (keys
            (make-dict (list
                    (list 'holi "chao")
                    (list 'doge "wow")
                    (list 'such "much")
                )
            )
        )
        """)
        self.assertTrue(isinstance(dict_keys, list))
        self.assertEqual(dict_keys, list(expected_dict.keys()))

        dict_values = BotlangSystem.run("""
        (values
            (make-dict (list
                    (list 'holi "chao")
                    (list 'doge "wow")
                    (list 'such "much")
                )
            )
        )
        """)
        self.assertTrue(isinstance(dict_values, list))
        self.assertEqual(dict_values, list(expected_dict.values()))

        dict_associations = BotlangSystem.run("""
        (associations
            (make-dict (list
                    (list 'holi "chao")
                    (list 'doge "wow")
                    (list 'such "much")
                )
            )
        )
        """)
        self.assertTrue(isinstance(dict_associations, list))
        self.assertEqual(dict_associations, list(expected_dict.items()))

        immutable_dict = BotlangSystem.run("""
        (define my-dict (make-dict (list)))
        (put my-dict "datum" 10)
        """)
        self.assertEqual(len(immutable_dict.values()), 1)
        self.assertEqual(immutable_dict['datum'], 10)

        mutable_dict = BotlangSystem.run("""
        (define my-dict (make-dict (list)))
        (put! my-dict "datum1" 4)
        (put! my-dict "datum2" 5)
        my-dict
        """)
        self.assertEqual(len(mutable_dict.values()), 2)
        self.assertEqual(mutable_dict['datum1'], 4)
        self.assertEqual(mutable_dict['datum2'], 5)

        mutable_dict = BotlangSystem.run("""
        (define my-dict (make-dict (list)))
        (put! my-dict "datum1" 4)
        (put! my-dict "datum2" 5)
        (remove! my-dict "datum2")
        my-dict
        """)
        self.assertEqual(len(mutable_dict.values()), 1)
        self.assertEqual(mutable_dict['datum1'], 4)
Esempio n. 58
0
    def test_closures(self):

        self.assertTrue(BotlangSystem.run('((fun (x) x) #t)'))
        self.assertEqual(BotlangSystem.run(
            '((fun (x y) (+ (* x x) (* y y))) 3 4)'
        ), 25)
Esempio n. 59
0
    def test_if(self):

        self.assertEqual(BotlangSystem.run('(if #t 2 3)'), 2)
        self.assertEqual(BotlangSystem.run('(if #f 2 3)'), 3)
        self.assertEqual(BotlangSystem.run('(if (> 4 5) 100 200)'), 200)
Esempio n. 60
0
    def test_or(self):

        self.assertTrue(BotlangSystem.run('(or #t #t)'))
        self.assertTrue(BotlangSystem.run('(or #t #f)'))
        self.assertTrue(BotlangSystem.run('(or #f #t)'))
        self.assertFalse(BotlangSystem.run('(or #f #f)'))