class AgentMLBasicTestCase(unittest.TestCase): """ Simple test case utilizing the AgentML demonstration code """ def setUp(self, **kwargs): """ Set up the Unit Test """ self.aml = AgentML(log_level=logging.WARN) self.aml.add_condition('foo_bar', FooBarType) self.aml.load_directory(os.path.join(os.path.dirname(self.aml.script_path), 'tests', 'lang')) self.username = "******" self.success = 'Success!' self.failure = 'Failure!'
class AgentMlLanguage(LanguageAbstract): def __init__(self): self._aml = AgentML() def get_reply(self, message, client, groups=None): return self._aml.get_reply(client, message, groups) def load_file(self, file_path): self._aml.load_file(file_path) def load_directory(self, dir_path): self._aml.load_directory(dir_path) def file_extensions(self): return ['aml']
class AgentMLLanguage(LanguageInterface): def __init__(self): self._log = logging.getLogger('firefly.language.aml') self.aml = AgentML() super(AgentMLLanguage, self).__init__() def get_reply(self, message, client='localhost', groups=None): message = unicode(message, 'utf-8') try: return self.aml.get_reply(client, message, groups) except errors.AgentMLError as e: self._log.info(e.message) def load_file(self, file_path): self._log.debug('Loading file: %s', file_path) self.aml.load_file(file_path) def load_directory(self, dir_path): self._log.debug('Loading directory: %s', dir_path) self.aml.load_directory(dir_path)
def demo(): aml = AgentML(logging.ERROR) aml.load_directory('demo/lang') aml.interpreter()
class AgentMLTestCase(unittest.TestCase): """ Base class for all AgentML test cases """ def setUp(self, **kwargs): """ Set up the Unit Test """ self.aml = AgentML(log_level=logging.WARN) self.aml.add_condition('foo_bar', FooBarType) self.aml.load_directory(os.path.join(os.path.dirname(self.aml.script_path), 'tests', 'lang')) self.username = "******" self.success = 'Success!' self.failure = 'Failure!' def tearDown(self): pass def get_reply(self, message, expected, groups=None): """ Test that the user's message gets the expected response :param message: The message to send :type message: str :param expected: The expected response :type expected: str, None or List :param groups: The trigger groups to search, defaults to only matching non-grouped triggers :type groups: set or AnyGroup """ reply = self.aml.get_reply(self.username, message, groups) if isinstance(expected, list): self.assertIn(reply, expected) else: self.assertEqual(reply, expected) def user_var(self, var, expected): """ Test the value of a user variable :param var: The name of the variable :type var: str :param expected: The expected value of the variable :type expected: str or None """ try: value = self.aml.get_var(var, self.username) except VarNotDefinedError: value = None self.assertEqual(value, expected) def global_var(self, var, expected): """ Test the value of a global variable :param var: The name of the variable :type var: str :param expected: The expected value of the variable :type expected: str or None """ try: value = self.aml.get_var(var) except VarNotDefinedError: value = None self.assertEqual(value, expected) def topic(self, expected): """ Test the topic the user is currently in :param expected: The name of the topic :type expected: str or None """ user = self.aml.get_user(self.username) self.assertEqual(user.topic, expected) def chance(self, message, max_tries=250): """ Test a chance statement using brute force :param message: The message triggering a chance statement :type message: str :param max_tries: The maximum number of attempts to make before giving up :type max_tries: int """ responses = set() attempts = 0 expected = {'Success!', None} while responses != expected: responses.add(self.aml.get_reply(self.username, message)) attempts += 1 if attempts >= max_tries: break self.assertEqual(responses, expected)