예제 #1
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)
예제 #2
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)
예제 #3
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")
예제 #4
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")
예제 #5
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")
예제 #6
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á')
예제 #7
0
    def test_get_node(self):

        bot_code = """
        (define node1 (bot-node (context)
            (node-result
                context
                "Nodo 1"
                end-node
            )
        ))
        (define node2 1)
        (bot-node (context)
            ((reflect-get-node (input-message)) context)
        )
        """
        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'node1')
        self.assertEqual(result.message, 'Nodo 1')

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'node3')
        self.assertTrue("name 'node3' is not defined" in str(cm.exception))

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'node2')
        self.assertTrue("'node2' is not a bot node" in str(cm.exception))
예제 #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)
예제 #9
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)
예제 #10
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á')
예제 #11
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")
예제 #12
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)
예제 #13
0
    def test_get_node(self):

        bot_code = """
        (define node1 (bot-node (context)
            (node-result
                context
                "Nodo 1"
                end-node
            )
        ))
        (define node2 1)
        (bot-node (context)
            ((reflect-get-node (input-message)) context)
        )
        """
        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'node1')
        self.assertEqual(result.message, 'Nodo 1')

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'node3')
        self.assertTrue("name 'node3' is not defined" in str(cm.exception))

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'node2')
        self.assertTrue("'node2' is not a bot node" in str(cm.exception))
예제 #14
0
    def test_type(self):

        self.assertTrue(
            BotlangSystem.run('(list? (list 1 2 3))')
        )
        self.assertFalse(
            BotlangSystem.run('(list? "bla")')
        )
예제 #15
0
    def test_type(self):

        self.assertTrue(
            BotlangSystem.run('(list? (list 1 2 3))')
        )
        self.assertFalse(
            BotlangSystem.run('(list? "bla")')
        )
예제 #16
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))')
        )
예제 #17
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?")'
            )
        )
예제 #18
0
    def test_cond(self):

        test_code = """
            (cond
                [(equal? sup dawg) "1"]
                [(< sup dawg) 2]
                [else (+ 1 2)]
            )
        """

        environment = BotlangSystem.base_environment().update(
            {'sup': 3, 'dawg': 4}
        )
        self.assertEqual(
            BotlangSystem(environment).eval(test_code),
            2
        )

        environment = BotlangSystem.base_environment().update(
            {'sup': 4, 'dawg': 4}
        )
        self.assertEqual(
            BotlangSystem(environment).eval(test_code),
            '1'
        )

        environment = BotlangSystem.base_environment().update(
            {'sup': 5, 'dawg': 4}
        )
        self.assertEqual(
            BotlangSystem(environment).eval(test_code),
            3
        )

        another_test_code = """
            (define dict (make-dict (list)))
            (cond
                [(equal? sup 1) (put! dict "sup" 1)]
                [(< sup dawg) (put! dict "dawg" 2)]
            )
            dict
        """
        environment = BotlangSystem.base_environment().update(
            {'sup': 1, 'dawg': 2}
        )
        self.assertEqual(
            BotlangSystem(environment).eval(another_test_code).get('sup'),
            1
        )
        self.assertIsNone(
            BotlangSystem(environment).eval(another_test_code).get('dawg')
        )
예제 #19
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)
예제 #20
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)
예제 #21
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')
예제 #22
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')
예제 #23
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)
예제 #24
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')
예제 #25
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')
예제 #26
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')
예제 #27
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)'))
예제 #28
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)
예제 #29
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')
예제 #30
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))'
            )
        )
예제 #31
0
    def test_nil(self):

        code = """
            [define value nil]
            (nil? value)
        """
        result = BotlangSystem.run(code)
        self.assertTrue(result)
예제 #32
0
    def test_bot_node(self):

        code = """
            (bot-node (data)
                (node-result
                    data
                    "Holi, soy Botcito"
                    (terminal-node "HOLA")
                )
            )
        """
        node_result = BotlangSystem(BotlangSystem.base_environment()).eval_bot(
            code, 'mensaje inicial')
        self.assertTrue(isinstance(node_result, BotResultValue))
        self.assertTrue(isinstance(node_result.data, dict))
        self.assertEqual(node_result.message, 'Holi, soy Botcito')
        self.assertEqual(node_result.bot_state, 'HOLA')
예제 #33
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))'
            )
        )
예제 #34
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')
예제 #35
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')
예제 #36
0
    def test_input_message(self):

        code = """
        (define node2 (bot-node (context message)
            (node-result
                context
                (append "node2: " message)
                end-node
            )
        ))
        (bot-node (context message)
            (node2 context (append "node1: " message))
        )
        """
        result = BotlangSystem.bot_instance().eval_bot(code, 'Hola')
        self.assertEqual(result.message, 'node2: node1: Hola')

        result = BotlangSystem.bot_instance().eval_bot(code, 'Hola', 'node2')
        self.assertEqual(result.message, 'node2: Hola')
예제 #37
0
    def test_begin(self):

        code = """
            (begin
                (define x 1)
                (define y 2)
                (+ x y)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 3)
예제 #38
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)'))
예제 #39
0
    def test_begin(self):

        code = """
            (begin
                (define x 1)
                (define y 2)
                (+ x y)
            )
        """
        self.assertEqual(BotlangSystem.run(code), 3)
예제 #40
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)
예제 #41
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))
예제 #42
0
 def eval(self, code_string):
     try:
         ast_seq = Parser.parse(code_string, 'REPL')
         return BotlangSystem.interpret(
             ast_seq,
             Evaluator(),
             self.dsl.environment
         )
     except Exception as e:
         name = e.__class__.__name__
         return '{0}: {1}'.format(name, e.message)
예제 #43
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'
        )
예제 #44
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)
예제 #45
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)
예제 #46
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))
예제 #47
0
    def test_bot_recursion(self):

        code = """
            [define get-count
                (function (data)
                    [define c (get-or-nil data "counter")]
                    (if (nil? c) 0 c)
                )
            ]
            [define node1
                (bot-node (data)
                    [define count (+ (get-count data) 1)]
                    (node-result
                        (put data "counter" count)
                        count
                        node1
                    )
                )
            ]
            node1
        """
        r1 = BotlangSystem.bot_instance().eval_bot(code, '')
        self.assertEqual(r1.message, 1)

        r2 = BotlangSystem.bot_instance().eval_bot(
            code, '', r1.next_node, r1.data
        )
        self.assertEqual(r2.message, 2)

        r3 = BotlangSystem.bot_instance().eval_bot(
            code, '', r2.next_node, r2.data
        )
        self.assertEqual(r3.message, 3)

        r = r3
        for i in range(0, 10):
            r = BotlangSystem.bot_instance().eval_bot(
                code, '', r.next_node, r.data
            )

        self.assertEqual(r.message, 13)
예제 #48
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)
예제 #49
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))
예제 #50
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)
예제 #51
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))
예제 #52
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])
예제 #53
0
    def test_cond_results(self):

        test_code = """
            (cond
                [(equal? sup dawg) "1"]
                [(< sup dawg) 2]
                [else (+ 1 2)]
            )
        """

        environment = BotlangSystem.base_environment().update({
            'sup': 3,
            'dawg': 4
        })
        self.assertEqual(BotlangSystem(environment).eval(test_code), 2)

        environment = BotlangSystem.base_environment().update({
            'sup': 4,
            'dawg': 4
        })
        self.assertEqual(BotlangSystem(environment).eval(test_code), '1')

        environment = BotlangSystem.base_environment().update({
            'sup': 5,
            'dawg': 4
        })
        self.assertEqual(BotlangSystem(environment).eval(test_code), 3)
예제 #54
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')
예제 #55
0
    def test_get_value(self):

        bot_code = """
        (define val1 1)
        (define val2 "hola")
        (define val3 (fun (x) x))
        (bot-node (ctx msg)
            (node-result ctx (reflect-get msg) end-node)
        )
        """
        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'val1')
        self.assertEqual(result.message, 1)

        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'val2')
        self.assertEqual(result.message, 'hola')

        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'val3')
        self.assertTrue(isinstance(result.message, FunVal))

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'val4')
        self.assertTrue("name 'val4' is not defined" in str(cm.exception))
예제 #56
0
    def test_get_value(self):

        bot_code = """
        (define val1 1)
        (define val2 "hola")
        (define val3 (fun (x) x))
        (bot-node (ctx msg)
            (node-result ctx (reflect-get msg) end-node)
        )
        """
        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'val1')
        self.assertEqual(result.message, 1)

        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'val2')
        self.assertEqual(result.message, 'hola')

        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'val3')
        self.assertTrue(isinstance(result.message, FunVal))

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'val4')
        self.assertTrue("name 'val4' is not defined" in str(cm.exception))
예제 #57
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')
예제 #58
0
    def test_get_function(self):

        bot_code = """
        (define fun1 (function (x) (* x x)))
        (define fun2 4)
        (bot-node (context message)
            (node-result
                context
                ((reflect-get-fun message) 3)
                end-node
            )
        )
        """
        result = BotlangSystem.bot_instance().eval_bot(bot_code, 'fun1')
        self.assertEqual(result.message, 9)

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'fun3')
        self.assertTrue("name 'fun3' is not defined" in str(cm.exception))

        with self.assertRaises(BotlangErrorException) as cm:
            BotlangSystem.bot_instance().eval_bot(bot_code, 'fun2')
        self.assertTrue("'fun2' is not a function" in str(cm.exception))
예제 #59
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")
예제 #60
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)