Beispiel #1
0
    def test_cond_branches_evaluation(self):

        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
        })
        result = BotlangSystem(environment).eval(test_code)
        self.assertEqual(result.get('sup'), 1)
        self.assertIsNone(result.get('dawg'))

        environment = BotlangSystem.base_environment().update({
            'sup': 0,
            'dawg': 1
        })
        result = BotlangSystem(environment).eval(test_code)
        self.assertIsNone(result.get('sup'))
        self.assertEqual(result.get('dawg'), 2)

        environment = BotlangSystem.base_environment().update({
            'sup': 0,
            'dawg': -1
        })
        result = BotlangSystem(environment).eval(test_code)
        self.assertIsNone(result.get('sup'))
        self.assertIsNone(result.get('dawg'))
Beispiel #2
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)
    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')
        )
    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)
    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')
Beispiel #6
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')
    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')
    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")