<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
#
# Copyright © 2015 Collabora Ltd.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Unit tests for dbusapi.ast
"""


# pylint: disable=missing-docstring


from dbusapi import ast
import unittest


# pylint: disable=too-many-public-methods
class TestAstNames(unittest.TestCase):
    """Test AST node name generation."""

    def test_interface(self):
        iface = ast.Interface('SomeInterface')
        self.assertEqual(iface.format_name(), 'SomeInterface')

    def test_property(self):
        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ)
        iface = ast.Interface('SomeInterface', {}, {
            'AProperty': prop,
        })
        self.assertEqual(prop.format_name(), 'SomeInterface.AProperty')

    def test_property_unparented(self):
        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ)
        self.assertEqual(prop.format_name(), 'AProperty')

    def test_method(self):
        method = ast.Method('AMethod', [])
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(method.format_name(), 'SomeInterface.AMethod')

    def test_method_unparented(self):
        method = ast.Method('AMethod', [])
        self.assertEqual(method.format_name(), 'AMethod')

    def test_signal(self):
        signal = ast.Signal('SomeSignal', [])
        iface = ast.Interface('SomeInterface', {}, {}, {
            'SomeSignal': signal,
        })
        self.assertEqual(signal.format_name(), 'SomeInterface.SomeSignal')

    def test_signal_unparented(self):
        signal = ast.Signal('SomeSignal', [])
        self.assertEqual(signal.format_name(), 'SomeSignal')

    def test_argument(self):
        arg = ast.Argument('self', ast.Argument.DIRECTION_IN, 's')
        method = ast.Method('ParentMethod', [arg])
        self.assertEqual(arg.format_name(), '0 (‘self’)')

    def test_argument_unparented(self):
        arg = ast.Argument('self', ast.Argument.DIRECTION_IN, 's')
        self.assertEqual(arg.format_name(), '‘self’')

    def test_argument_unnamed(self):
        arg = ast.Argument(None, ast.Argument.DIRECTION_IN, 's')
        method = ast.Method('ParentMethod', [arg])
        self.assertEqual(arg.format_name(), '0')

    def test_argument_unnamed_unparented(self):
        arg = ast.Argument(None, ast.Argument.DIRECTION_IN, 's')
        self.assertEqual(arg.format_name(), 'unnamed')

    def test_annotation_interface(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        iface = ast.Interface('SomeInterface', {}, {}, {}, {
            'SomeAnnotation': annotation,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.SomeAnnotation')

    def test_annotation_property(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ, {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {}, {
            'AProperty': prop,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.AProperty.SomeAnnotation')

    def test_annotation_method(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        method = ast.Method('AMethod', [], {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.AMethod.SomeAnnotation')

    def test_annotation_signal(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        signal = ast.Signal('ASignal', [], {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {}, {}, {
            'ASignal': signal,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.ASignal.SomeAnnotation')

    def test_annotation_argument(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        arg = ast.Argument('Argument', ast.Argument.DIRECTION_IN, 's', {
            'SomeAnnotation': annotation,
        })
        method = ast.Method('AMethod', [arg])
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(annotation.format_name(),
                         '0 (‘Argument’).SomeAnnotation')

    def test_annotation_unparented(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        method = ast.Method('AMethod', [], {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.AMethod.SomeAnnotation')


# pylint: disable=too-many-public-methods
class TestAstParenting(unittest.TestCase):
    """Test AST node parenting."""

    def test_property(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        self.assertEqual(annotation.parent, None)

        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ, {
            'SomeAnnotation': annotation,
        })
        self.assertEqual(prop.interface, None)
        self.assertEqual(annotation.parent, prop)
<commit_msg>dbusapi: Fix copy–paste error in a dbusapi test<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
#
# Copyright © 2015 Collabora Ltd.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Unit tests for dbusapi.ast
"""


# pylint: disable=missing-docstring


from dbusapi import ast
import unittest


# pylint: disable=too-many-public-methods
class TestAstNames(unittest.TestCase):
    """Test AST node name generation."""

    def test_interface(self):
        iface = ast.Interface('SomeInterface')
        self.assertEqual(iface.format_name(), 'SomeInterface')

    def test_property(self):
        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ)
        iface = ast.Interface('SomeInterface', {}, {
            'AProperty': prop,
        })
        self.assertEqual(prop.format_name(), 'SomeInterface.AProperty')

    def test_property_unparented(self):
        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ)
        self.assertEqual(prop.format_name(), 'AProperty')

    def test_method(self):
        method = ast.Method('AMethod', [])
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(method.format_name(), 'SomeInterface.AMethod')

    def test_method_unparented(self):
        method = ast.Method('AMethod', [])
        self.assertEqual(method.format_name(), 'AMethod')

    def test_signal(self):
        signal = ast.Signal('SomeSignal', [])
        iface = ast.Interface('SomeInterface', {}, {}, {
            'SomeSignal': signal,
        })
        self.assertEqual(signal.format_name(), 'SomeInterface.SomeSignal')

    def test_signal_unparented(self):
        signal = ast.Signal('SomeSignal', [])
        self.assertEqual(signal.format_name(), 'SomeSignal')

    def test_argument(self):
        arg = ast.Argument('self', ast.Argument.DIRECTION_IN, 's')
        method = ast.Method('ParentMethod', [arg])
        self.assertEqual(arg.format_name(), '0 (‘self’)')

    def test_argument_unparented(self):
        arg = ast.Argument('self', ast.Argument.DIRECTION_IN, 's')
        self.assertEqual(arg.format_name(), '‘self’')

    def test_argument_unnamed(self):
        arg = ast.Argument(None, ast.Argument.DIRECTION_IN, 's')
        method = ast.Method('ParentMethod', [arg])
        self.assertEqual(arg.format_name(), '0')

    def test_argument_unnamed_unparented(self):
        arg = ast.Argument(None, ast.Argument.DIRECTION_IN, 's')
        self.assertEqual(arg.format_name(), 'unnamed')

    def test_annotation_interface(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        iface = ast.Interface('SomeInterface', {}, {}, {}, {
            'SomeAnnotation': annotation,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.SomeAnnotation')

    def test_annotation_property(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ, {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {}, {
            'AProperty': prop,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.AProperty.SomeAnnotation')

    def test_annotation_method(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        method = ast.Method('AMethod', [], {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.AMethod.SomeAnnotation')

    def test_annotation_signal(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        signal = ast.Signal('ASignal', [], {
            'SomeAnnotation': annotation,
        })
        iface = ast.Interface('SomeInterface', {}, {}, {
            'ASignal': signal,
        })
        self.assertEqual(annotation.format_name(),
                         'SomeInterface.ASignal.SomeAnnotation')

    def test_annotation_argument(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        arg = ast.Argument('Argument', ast.Argument.DIRECTION_IN, 's', {
            'SomeAnnotation': annotation,
        })
        method = ast.Method('AMethod', [arg])
        iface = ast.Interface('SomeInterface', {
            'AMethod': method,
        })
        self.assertEqual(annotation.format_name(),
                         '0 (‘Argument’).SomeAnnotation')

    def test_annotation_unparented(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        self.assertEqual(annotation.format_name(), 'SomeAnnotation')


# pylint: disable=too-many-public-methods
class TestAstParenting(unittest.TestCase):
    """Test AST node parenting."""

    def test_property(self):
        annotation = ast.Annotation('SomeAnnotation', 'value')
        self.assertEqual(annotation.parent, None)

        prop = ast.Property('AProperty', 's', ast.Property.ACCESS_READ, {
            'SomeAnnotation': annotation,
        })
        self.assertEqual(prop.interface, None)
        self.assertEqual(annotation.parent, prop)

{'fraction_non_alphanumeric': 0.1256253908692933, 'fraction_numerical': 0.0017198248905565979, 'mean_word_length': 2.953337453646477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7287495', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3386, 'n_words': 825}