<commit_before>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import os

from robot import utils
from robot.errors import DataError
from robot.common import BaseTestSuite, BaseTestCase

from rawdata import RawData, READERS
from metadata import TestSuiteMetadata, TestCaseMetadata
from keywords import KeywordList
from userkeyword import UserHandlerList


def TestSuiteData(datasources, settings, syslog):
    datasources = [ utils.normpath(path) for path in datasources ]
    if not datasources:
        raise DataError("No data sources given.")
    elif len(datasources) > 1:
        return MultiSourceSuite(datasources, settings['SuiteNames'], syslog)
    elif os.path.isdir(datasources[0]):
        return DirectorySuite(datasources[0], settings['SuiteNames'], syslog)
    else:
        return FileSuite(datasources[0], syslog)


class _BaseSuite(BaseTestSuite):

    def __init__(self, rawdata):
        name, source = self._get_name_and_source(rawdata.source)
        BaseTestSuite.__init__(self, name, source)
        metadata = TestSuiteMetadata(rawdata)   
        self.doc = metadata['Documentation']
        self.suite_setup = metadata['SuiteSetup']
        self.suite_teardown = metadata['SuiteTeardown']
        self.test_setup = metadata['TestSetup']
        self.test_teardown = metadata['TestTeardown']
        self.default_tags = metadata['DefaultTags']
        self.force_tags = metadata['ForceTags']
        self.test_timeout = metadata['TestTimeout']
        self.metadata = metadata.user_metadata 
        self.imports = metadata.imports
        self.variables = rawdata.variables
        self.user_keywords = UserHandlerList(rawdata.keywords)

    def _get_name_and_source(self, path):
        source = self._get_source(path)
        return self._get_name(source), source
        
    def _get_name(self, source):
        return utils.printable_name_from_path(source)

        
class FileSuite(_BaseSuite):
    
    def __init__(self, path, syslog):
        syslog.info("Parsing test case file '%s'" % path)
        rawdata = self._get_rawdata(path, syslog)
        _BaseSuite.__init__(self, rawdata)
        self.tests = self._process_testcases(rawdata, syslog)

    def _get_source(self, path):
        return path
        
    def _get_rawdata(self, path, syslog):
        rawdata = RawData(path, syslog)
        if rawdata.get_type() == rawdata.TESTCASE:
            return rawdata
        raise DataError("Test case file '%s' contains no test cases."  % path)

    def _process_testcases(self, rawdata, syslog):
        names = []
        tests = []
        for rawtest in rawdata.testcases:
            try:
                test = TestCase(rawtest)
            except:
                rawtest.report_invalid_syntax()
                continue
            tests.append(test)
            name = utils.normalize(test.name)
            if name in names:
                msg = "Multiple test cases with name '%s' in test suite '%s'"
                syslog.warn(msg % (test.name, self.name))
            else:
                names.append(name)
        return tests
<commit_msg>removed a warning if a suite has same test case twice (issue 133)<commit_after>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import os

from robot import utils
from robot.errors import DataError
from robot.common import BaseTestSuite, BaseTestCase

from rawdata import RawData, READERS
from metadata import TestSuiteMetadata, TestCaseMetadata
from keywords import KeywordList
from userkeyword import UserHandlerList


def TestSuiteData(datasources, settings, syslog):
    datasources = [ utils.normpath(path) for path in datasources ]
    if not datasources:
        raise DataError("No data sources given.")
    elif len(datasources) > 1:
        return MultiSourceSuite(datasources, settings['SuiteNames'], syslog)
    elif os.path.isdir(datasources[0]):
        return DirectorySuite(datasources[0], settings['SuiteNames'], syslog)
    else:
        return FileSuite(datasources[0], syslog)


class _BaseSuite(BaseTestSuite):

    def __init__(self, rawdata):
        name, source = self._get_name_and_source(rawdata.source)
        BaseTestSuite.__init__(self, name, source)
        metadata = TestSuiteMetadata(rawdata)   
        self.doc = metadata['Documentation']
        self.suite_setup = metadata['SuiteSetup']
        self.suite_teardown = metadata['SuiteTeardown']
        self.test_setup = metadata['TestSetup']
        self.test_teardown = metadata['TestTeardown']
        self.default_tags = metadata['DefaultTags']
        self.force_tags = metadata['ForceTags']
        self.test_timeout = metadata['TestTimeout']
        self.metadata = metadata.user_metadata 
        self.imports = metadata.imports
        self.variables = rawdata.variables
        self.user_keywords = UserHandlerList(rawdata.keywords)

    def _get_name_and_source(self, path):
        source = self._get_source(path)
        return self._get_name(source), source
        
    def _get_name(self, source):
        return utils.printable_name_from_path(source)

        
class FileSuite(_BaseSuite):
    
    def __init__(self, path, syslog):
        syslog.info("Parsing test case file '%s'" % path)
        rawdata = self._get_rawdata(path, syslog)
        _BaseSuite.__init__(self, rawdata)
        self.tests = self._process_testcases(rawdata)

    def _get_source(self, path):
        return path
        
    def _get_rawdata(self, path, syslog):
        rawdata = RawData(path, syslog)
        if rawdata.get_type() == rawdata.TESTCASE:
            return rawdata
        raise DataError("Test case file '%s' contains no test cases."  % path)

    def _process_testcases(self, rawdata):
        tests = []
        for rawtest in rawdata.testcases:
            try:
                tests.append(TestCase(rawtest))
            except:
                rawtest.report_invalid_syntax()
        return tests

{'fraction_non_alphanumeric': 0.08446283349978599, 'fraction_numerical': 0.0038521900413753746, 'mean_word_length': 2.9671760045274476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7456902', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1919, 'n_words': 566}