def test_register_language(): """ Test both style of language registration. """ clear_language_registrations() register_language('test-lang', pattern='*.test', description='test-lang description', metamodel=mymetamodel_callable) language = language_description('test-lang') assert type(language) is LanguageDesc assert language.name == 'test-lang' assert language.pattern == '*.test' assert language.description == 'test-lang description' assert language.metamodel == mymetamodel_callable clear_language_registrations() register_language(LanguageDesc('test-lang', pattern='*.test', description='test-lang description', metamodel=mymetamodel_callable)) language = language_description('test-lang') assert type(language) is LanguageDesc assert language.name == 'test-lang' assert language.pattern == '*.test' assert language.description == 'test-lang description' assert language.metamodel == mymetamodel_callable
def get_language_desc(language_name: str, file_name: Optional[str] = None) -> LanguageDesc: """Returns a language info by a given language name or file extension. Args: language_name: textX language name (id) file_name: file path of active document Returns: A language info (`LanguageDesc`) Raises: MultipleLanguagesError: If there are multiple registered metamodels that can parse a language LanguageNotRegistered: If there are no registered metamodels that can parse a language """ lang_desc = None try: lang_desc = language_description(language_name) except TextXRegistrationError: # When client does not set language id/name, i.e. extension is not # installed, then try to load by file extension lang_descs = languages_for_file(file_name) lang_descs_len = len(lang_descs) if lang_descs_len > 1: raise MultipleLanguagesError(file_name) lang_desc = lang_descs.pop() if lang_descs_len == 1 else None if lang_desc is None: raise LanguageNotRegistered(file_name) return lang_desc
def test_language_for_file(): """ Test providing language description for a given file name or pattern. """ clear_language_registrations() tx_lang = language_description('textx') assert tx_lang is language_for_file('test.tx') assert tx_lang is language_for_file('*.tx')
def test_declaratively_registered_languages_always_available(): """ Declaratively registered languages will be re-registered at the first API call. textX language is declaratively registered and thus is always accessible. """ clear_language_registrations() tx_lang = language_description('textx') assert tx_lang.name == 'textX' assert tx_lang.pattern == '*.tx' assert tx_lang.project_name == 'textX'
def test_asking_for_unregistered_language_raises_error(): """ """ with pytest.raises(TextXRegistrationError, match='.*not registered.*'): language_description('unexisting')