Exemplo n.º 1
0
def test_xml_string_import():
    xml = """<topicdefntree>
      <description>Test Topics showing hierarchy and topic inheritance</description>
      <topic id="parent">
          <description>Parent with a parameter and subtopics</description>
          <listenerspec>
              <arg id="lastname">surname</arg>
              <arg id="name" optional="True">given name</arg>
            </listenerspec>
          <topic id="child">
              <description>This is the first child</description>
              <listenerspec>
                  <arg id="nick">A nickname</arg>
                </listenerspec>
            </topic>
        </topic>
    </topicdefntree>"""

    topicMgr.delTopic('parent')
    pub.clearTopicDefnProviders()
    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None

    provider = XmlTopicDefnProvider(xml)
    pub.addTopicDefnProvider(provider)
    # to force instantiation of two topic definitions that were defined in xml,
    # this time we just instantiate all of them:
    pub.instantiateAllDefinedTopics(provider)

    assert topicMgr.getTopic('parent') is not None
    assert topicMgr.getTopic('parent.child') is not None
Exemplo n.º 2
0
def test_xml_string_import():
    xml="""<topicdefntree>
      <description>Test Topics showing hierarchy and topic inheritance</description>
      <topic id="parent">
          <description>Parent with a parameter and subtopics</description>
          <listenerspec>
              <arg id="lastname">surname</arg>
              <arg id="name" optional="True">given name</arg>
            </listenerspec>
          <topic id="child">
              <description>This is the first child</description>
              <listenerspec>
                  <arg id="nick">A nickname</arg>
                </listenerspec>
            </topic>
        </topic>
    </topicdefntree>"""

    topicMgr.delTopic('parent')
    pub.clearTopicDefnProviders()
    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None

    provider = XmlTopicDefnProvider(xml)
    pub.addTopicDefnProvider( provider )
    # to force instantiation of two topic definitions that were defined in xml,
    # this time we just instantiate all of them:
    pub.instantiateAllDefinedTopics(provider)

    assert topicMgr.getTopic('parent') is not None
    assert topicMgr.getTopic('parent.child') is not None
Exemplo n.º 3
0
def test_xml_from_file():
    pub.clearTopicDefnProviders()

    provider = XmlTopicDefnProvider('xmlprovider_topics.xml', TOPIC_TREE_FROM_FILE)

    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None
    assert topicMgr.getOrCreateTopic('parent') is not None
    assert topicMgr.getOrCreateTopic('parent.child') is not None
Exemplo n.º 4
0
def test0_xml_from_file():
    pub.clearTopicDefnProviders()

    provider = XmlTopicDefnProvider('xmlprovider_topics.xml', TOPIC_TREE_FROM_FILE)

    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None
    assert topicMgr.getOrCreateTopic('parent') is not None
    assert topicMgr.getOrCreateTopic('parent.child') is not None
Exemplo n.º 5
0
def test_yaml_from_file():
    remove_all_topics()
    pub.clearTopicDefnProviders()

    provider = YamlTopicDefnProvider('yamlprovider_topics.yaml',
                                     TOPIC_TREE_FROM_FILE)
    printTreeDocs()
    print(topicMgr.getTopic('parent', True))
    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None
    assert topicMgr.getOrCreateTopic('parent') is not None
    assert topicMgr.getOrCreateTopic('parent.child') is not None
Exemplo n.º 6
0
def test_xml_import():
    pub.clearTopicDefnProviders()
    topicMgr.delTopic('parent')
    # verify pre:
    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None

    provider = XmlTopicDefnProvider('xmlprovider_topics.xml', TOPIC_TREE_FROM_FILE)
    pub.addTopicDefnProvider( provider )
    # force instantiation of two topic definitions that were defined in xml:
    pub.sendMessage('parent', lastname='')
    pub.sendMessage('parent.child', lastname='', nick='')

    # verify post:
    assert topicMgr.getTopic('parent') is not None
    assert topicMgr.getTopic('parent.child') is not None
Exemplo n.º 7
0
def test1_xml_import():
    pub.clearTopicDefnProviders()
    topicMgr.delTopic('parent')
    # verify pre:
    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None

    provider = XmlTopicDefnProvider('xmlprovider_topics.xml', TOPIC_TREE_FROM_FILE)
    pub.addTopicDefnProvider( provider )
    # force instantiation of two topic definitions that were defined in xml:
    pub.sendMessage('parent', lastname='')
    pub.sendMessage('parent.child', lastname='', nick='')

    # verify post:
    assert topicMgr.getTopic('parent') is not None
    assert topicMgr.getTopic('parent.child') is not None
Exemplo n.º 8
0
def test_yaml_string_import():
    remove_all_topics()
    str_yaml = """ALL_TOPICS:
  description: Root of all topics
  topics:
  - topic: parent
    description: Parent with a parameter and subtopics
    listenerspec:
    - arg: lastname
      description: surname
    - arg: name
      description: given name
      optional: true
    topics:
    - topic: child
      description: This is the first child
      listenerspec:
      - arg: nick
        description: A nickname
    """

    topicMgr.delTopic('parent')
    pub.clearTopicDefnProviders()
    assert topicMgr.getTopic('parent', True) is None
    assert topicMgr.getTopic('parent.child', True) is None

    provider = YamlTopicDefnProvider(str_yaml)
    pub.addTopicDefnProvider(provider)
    # to force instantiation of two topic definitions that were defined in yaml,
    # this time we just instantiate all of them:
    pub.instantiateAllDefinedTopics(provider)

    printTreeDocs()

    assert topicMgr.getTopic('parent') is not None
    assert topicMgr.getTopic('parent.child') is not None
Exemplo n.º 9
0
def test_string_prov_export():
    clear_topic_tree()

    importStr = '''
        """Tree docs, can be anything you want."""

        class root_topic_1:
            """Root topic 1."""

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """
                    pass

        class root_topic_2:
            """Root topic 2."""

        '''
    pub.clearTopicDefnProviders()
    provider = pub.addTopicDefnProvider(importStr, pub.TOPIC_TREE_FROM_STRING)
    treeDoc = provider.getTreeDoc()
    assert treeDoc == """Tree docs, can be anything you want."""
    root = topicMgr.getOrCreateTopic('root_topic_1.subtopic_1')
    assert root is not None
    assert topicMgr.getOrCreateTopic('root_topic_2').hasMDS()

    # few sanity checks
    def sub_1(arg1, arg2=None):
        pass

    assert root.hasMDS()
    assert pub.isValid(sub_1, 'root_topic_1.subtopic_1')

    # export tree
    exported = pub.exportTopicTreeSpec(rootTopic='root_topic_1',
                                       moduleDoc=treeDoc)
    # print(exported)

    expectExport = '''\
        # Automatically generated by TopicTreeSpecPrinter(**kwargs).
        # The kwargs were:
        # - fileObj: StringIO
        # - footer: '# End of topic tree definition. Note that application may l...'
        # - indentStep: 4
        # - treeDoc: 'Tree docs, can be anything you want....'
        # - width: 70


        """
        Tree docs, can be anything you want.
        """


        class root_topic_1:
            """
            Root topic 1.
            """

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """


        # End of topic tree definition. Note that application may load
        # more than one definitions provider.
        '''

    # check there are no differences
    from difflib import context_diff, ndiff
    diffs = ndiff(dedent(expectExport).splitlines(), exported.splitlines())
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert diffs == ['- ', '+         ']

    # now for module:
    provider = pub.addTopicDefnProvider('test4_prov_module_expect')
    pub.instantiateAllDefinedTopics(provider)
    modDoc = provider.getTreeDoc()
    assert modDoc.startswith('\nTree docs, can be anything you')
    pub.exportTopicTreeSpec('test4_prov_module_actual',
                            rootTopic='root_topic_1b',
                            moduleDoc=treeDoc)
    lines1 = open('test4_prov_module_actual.py', 'r').readlines()
    lines2 = open('test4_prov_module_expect.py', 'r').readlines()
    diffs = ndiff(lines1, lines2)
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert not list(diffs) or list(diffs) == [
        '- # - fileObj: TextIOWrapper\n', '+ # - fileObj: file\n'
    ]
    Path('test4_prov_module_actual.py').unlink()
Exemplo n.º 10
0
def test_string_prov_export():
    clear_topic_tree()

    importStr = '''
        """Tree docs, can be anything you want."""

        class root_topic_1:
            """Root topic 1."""

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """
                    pass

        class root_topic_2:
            """Root topic 2."""

        '''
    pub.clearTopicDefnProviders()
    provider = pub.addTopicDefnProvider(importStr, pub.TOPIC_TREE_FROM_STRING)
    treeDoc = provider.getTreeDoc()
    assert treeDoc == """Tree docs, can be anything you want."""
    root = topicMgr.getOrCreateTopic('root_topic_1.subtopic_1')
    assert root is not None
    assert topicMgr.getOrCreateTopic('root_topic_2').hasMDS()

    # few sanity checks
    def sub_1(arg1, arg2=None): pass

    assert root.hasMDS()
    assert pub.isValid(sub_1, 'root_topic_1.subtopic_1')

    # export tree
    exported = pub.exportTopicTreeSpec(rootTopic='root_topic_1', moduleDoc=treeDoc)
    # print(exported)

    expectExport = '''\
        # Automatically generated by TopicTreeSpecPrinter(**kwargs).
        # The kwargs were:
        # - fileObj: StringIO
        # - footer: '# End of topic tree definition. Note that application may l...'
        # - indentStep: 4
        # - treeDoc: 'Tree docs, can be anything you want....'
        # - width: 70


        """
        Tree docs, can be anything you want.
        """


        class root_topic_1:
            """
            Root topic 1.
            """

            class subtopic_1:
                """
                Sub topic 1 of root topic. Docs rely on one
                blank line for topic doc, and indentation for
                each argument doc.
                """

                def msgDataSpec(arg1, arg2=None):
                    """
                    - arg1: some multiline doc
                        for arg1
                    - arg2: some multiline doc
                        for arg2
                    """


        # End of topic tree definition. Note that application may load
        # more than one definitions provider.
        '''

    # check there are no differences
    from difflib import context_diff, ndiff
    diffs = ndiff(dedent(expectExport).splitlines(), exported.splitlines())
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert diffs == ['- ', '+         ']

    # now for module:
    provider = pub.addTopicDefnProvider('test4_prov_module_expect')
    pub.instantiateAllDefinedTopics(provider)
    modDoc = provider.getTreeDoc()
    assert modDoc.startswith('\nTree docs, can be anything you')
    pub.exportTopicTreeSpec('test4_prov_module_actual',
                            rootTopic='root_topic_1b', moduleDoc=treeDoc)
    lines1 = open('test4_prov_module_actual.py', 'r').readlines()
    lines2 = open('test4_prov_module_expect.py', 'r').readlines()
    diffs = ndiff(lines1, lines2)
    diffs = [d for d in diffs if not d.startswith(' ')]
    assert not list(diffs) or list(diffs) == ['- # - fileObj: TextIOWrapper\n', '+ # - fileObj: file\n']
    Path('test4_prov_module_actual.py').unlink()