Пример #1
0
    def test_sample_files(self):
        import os
        import traceback
        path = os.path.join(os.path.dirname(__file__), "inputs")
        for filename in os.listdir(path):
            if not filename.endswith('.xml'):
                continue
            f = open(os.path.join(path, filename), 'rb')
            source = f.read()
            f.close()

            from ..utils import read_encoded
            try:
                want = read_encoded(source)
            except UnicodeDecodeError:
                exc = sys.exc_info()[1]
                self.fail("%s - %s" % (exc, filename))

            from ..tokenize import iter_xml
            try:
                tokens = iter_xml(want)
                got = "".join(tokens)
            except:
                self.fail(traceback.format_exc())

            from doctest import OutputChecker
            checker = OutputChecker()

            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(f.name, want)
                diff = checker.output_difference(example, got, 0)
                self.fail("(%s) - \n%s" % (f.name, diff))
Пример #2
0
    def test_sample_files(self):
        import os
        import traceback
        path = os.path.join(os.path.dirname(__file__), "inputs")
        for filename in os.listdir(path):
            if not filename.endswith('.xml'):
                continue
            f = open(os.path.join(path, filename), 'rb')
            source = f.read()
            f.close()

            from ..utils import read_encoded
            try:
                want = read_encoded(source)
            except UnicodeDecodeError:
                exc = sys.exc_info()[1]
                self.fail("%s - %s" % (exc, filename))

            from ..tokenize import iter_xml
            try:
                tokens = iter_xml(want)
                got = "".join(tokens)
            except:
                self.fail(traceback.format_exc())

            from doctest import OutputChecker
            checker = OutputChecker()

            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(f.name, want)
                diff = checker.output_difference(
                    example, got, 0)
                self.fail("(%s) - \n%s" % (f.name, diff))
Пример #3
0
def _check_output(result, expected):
    checker = OutputChecker()
    actual = str(result)

    # FIXME!  The following is a TOTAL hack.  For some reason the
    # __str__ result for pkg_resources.Requirement gets downcased on
    # Windows.  Since INITools is the only package we're installing
    # in this file with funky case requirements, I'm forcibly
    # upcasing it.  You can also normalize everything to lowercase,
    # but then you have to remember to upcase <BLANKLINE>.  The right
    # thing to do in the end is probably to find out how to report
    # the proper fully-cased package name in our error message.
    if sys.platform == 'win32':
        actual = actual.replace('initools', 'INITools')

    # This allows our existing tests to work when run in a context
    # with distribute installed.
    actual = distribute_re.sub('', actual)

    def banner(msg):
        return '\n========== %s ==========\n' % msg

    assert checker.check_output(expected, actual, ELLIPSIS), (
        banner('EXPECTED') + expected + banner('ACTUAL') + actual +
        banner(6 * '=')
    )
Пример #4
0
def _check_output(result, expected):
    checker = OutputChecker()
    actual = str(result)

    # FIXME!  The following is a TOTAL hack.  For some reason the
    # __str__ result for pkg_resources.Requirement gets downcased on
    # Windows.  Since INITools is the only package we're installing
    # in this file with funky case requirements, I'm forcibly
    # upcasing it.  You can also normalize everything to lowercase,
    # but then you have to remember to upcase <BLANKLINE>.  The right
    # thing to do in the end is probably to find out how to report
    # the proper fully-cased package name in our error message.
    if sys.platform == 'win32':
        actual = actual.replace('initools', 'INITools')

    # This allows our existing tests to work when run in a context
    # with distribute installed.
    actual = distribute_re.sub('', actual)

    def banner(msg):
        return '\n========== {msg} ==========\n'.format(**locals())

    assert checker.check_output(expected, actual, ELLIPSIS), (
        banner('EXPECTED') + expected + banner('ACTUAL') + actual +
        banner(6 * '=')
    )
Пример #5
0
class DebugOutputChecker(OutputChecker):
    def __init__(self):
        self.output_checker = OutputChecker()

    def check_output(self, want, got, optionflags):
        return self.output_checker.check_output(want, remove_debug(got), optionflags)

    def output_difference(self, example, got, optionflags):
        return self.output_checker.output_difference(example, remove_debug(got), optionflags)
Пример #6
0
 def check_output(self, want, got, optionflags):
     want = UNICODE_LITERALS.sub('\\1', want)
     want = want.replace('FieldNotLoaded: ',
                         'workfront.meta.FieldNotLoaded: ')
     return OutputChecker.check_output(
         self, want, got, optionflags
         )
Пример #7
0
    def check_output(self, want, got, optionflags):
        if got == want:
            return True

        for pattern, repl in self.patterns:
            want = re.sub(pattern, repl, want)
            got = re.sub(pattern, repl, got)

        return OutputChecker.check_output(self, want, got, optionflags)
Пример #8
0
    def output_difference(self, example, got, optionflags):
        want = example.want

        if not want.strip():
            return OutputChecker.output_difference(
                self, example, got, optionflags)

        orig = want

        for pattern, repl in self.patterns:
            want = re.sub(pattern, repl, want)
            got = re.sub(pattern, repl, got)

        example.want = want
        result = OutputChecker.output_difference(
            self, example, got, optionflags)
        example.want = orig

        return result
Пример #9
0
def _check_output(result, expected):
    checker = OutputChecker()
    actual = str(result)

    ## FIXME!  The following is a TOTAL hack.  For some reason the
    ## __str__ result for pkg_resources.Requirement gets downcased on
    ## Windows.  Since INITools is the only package we're installing
    ## in this file with funky case requirements, I'm forcibly
    ## upcasing it.  You can also normalize everything to lowercase,
    ## but then you have to remember to upcase <BLANKLINE>.  The right
    ## thing to do in the end is probably to find out how to report
    ## the proper fully-cased package name in our error message.
    if sys.platform == "win32":
        actual = actual.replace("initools", "INITools")

    def banner(msg):
        return "\n========== %s ==========\n" % msg

    assert checker.check_output(expected, actual, ELLIPSIS), (
        banner("EXPECTED") + expected + banner("ACTUAL") + actual + banner(6 * "=")
    )
Пример #10
0
    def afterSetUp(self):
        # create an image and record its UID
        setRoles(self.portal, TEST_USER_ID, ['Manager'])

        if self.image_id not in self.portal:
            self.portal.invokeFactory('Image', id=self.image_id, title='Image')
        image = self.portal[self.image_id]
        image.setDescription('My caption')
        image.image = dummy_image()
        image.reindexObject()
        self.UID = image.UID()
        self.parser = self._makeParser(captioned_images=True,
                                       resolve_uids=True)
        assert self.parser.is_enabled()

        self.outputchecker = OutputChecker()
Пример #11
0
    def check_output(self, want, got, optionflags):
        old_want = want
        old_got = got

        def repl_dt(match_obj: Any) -> str:
            return found_items.__next__().group()

        if self.mod_name == 'time_hdr' or self.mod_name == 'README':
            # find the actual occurrences and replace in want
            for time_hdr_dt_format in [
                    "%a %b %d %Y %H:%M:%S", "%m/%d/%y %H:%M:%S"
            ]:
                match_str = get_datetime_match_string(time_hdr_dt_format)

                match_re = re.compile(match_str)
                found_items = match_re.finditer(got)
                want = match_re.sub(repl_dt, want)

            # replace elapsed time in both want and got
            match_str = 'Elapsed time: 0:00:00.[0-9| ]{6,6}'
            replacement = 'Elapsed time: 0:00:00       '
            want = re.sub(match_str, replacement, want)
            got = re.sub(match_str, replacement, got)

        if self.mod_name == 'file_catalog' or self.mod_name == 'README':
            match_str = r'\\'
            replacement = '/'
            got = re.sub(match_str, replacement, got)

            match_str = '//'
            replacement = '/'
            got = re.sub(match_str, replacement, got)

        if self.mod_name == 'diag_msg' or self.mod_name == 'README':
            for diag_msg_dt_fmt in ["%H:%M:%S.%f", "%a %b-%d %H:%M:%S"]:
                match_str = get_datetime_match_string(diag_msg_dt_fmt)

                match_re = re.compile(match_str)
                found_items = match_re.finditer(got)
                want = match_re.sub(repl_dt, want)

            match_str = "<.+?>"
            replacement = '<input>'
            got = re.sub(match_str, replacement, got)

        self.msgs.append([old_want, want, old_got, got])
        return BaseOutputChecker.check_output(self, want, got, optionflags)
    def afterSetUp(self):
        # create an image and record its UID
        self.setRoles(['Manager'])

        data = open(join(PREFIX, 'image.jpg'), 'rb').read()
        self.portal.invokeFactory('Image',
                                  id='image.jpg',
                                  title='Image',
                                  file=data)
        image = getattr(self.portal, 'image.jpg')
        image.setDescription('My caption')
        image.reindexObject()
        self.UID = image.UID()
        self.parser = self._makeParser(captioned_images=True,
                                       resolve_uids=True)
        assert self.parser.is_enabled()

        self.outputchecker = OutputChecker()
Пример #13
0
    def execute(self, ext, factory, **kwargs):
        def translate(msgid, domain=None, mapping=None, context=None,
                      target_language=None, default=None):
            if default is None:
                default = str(msgid)

            if isinstance(msgid, Message):
                default = "Message"

            if mapping:
                default = re.sub(r'\${([a-z_]+)}', r'%(\1)s', default) % \
                          mapping

            if target_language is None:
                return default

            if domain is None:
                with_domain = ""
            else:
                with_domain = " with domain '%s'" % domain

            if context is None:
                with_context = ""
            else:
                with_context = ", context '%s'" % context

            stripped = default.rstrip('\n ')
            return "%s ('%s' translation into '%s'%s%s)%s" % (
                stripped, msgid, target_language, with_domain, with_context,
                default[len(stripped):]
                )

        for input_path, output_path, language in self.find_files(ext):
            # Make friendly title so we can locate the generated
            # source when debugging
            self.shortDescription = lambda: input_path

            # When input path contains the string 'implicit-i18n', we
            # enable "implicit translation".
            implicit_i18n = 'implicit-i18n' in input_path
            implicit_i18n_attrs = ("alt", "title") if implicit_i18n else ()

            enable_data_attributes = 'data-attributes' in input_path

            template = factory(
                input_path,
                keep_source=True,
                strict=False,
                implicit_i18n_translate=implicit_i18n,
                implicit_i18n_attributes=implicit_i18n_attrs,
                enable_data_attributes=enable_data_attributes,
                )

            params = kwargs.copy()
            params.update({
                'translate': translate,
                'target_language': language,
                })

            template.cook_check()

            try:
                got = template.render(**params)
            except:
                import traceback
                e = traceback.format_exc()
                self.fail("%s\n\n    Example source:\n\n%s" % (e, "\n".join(
                    ["%#03.d%s" % (lineno + 1, line and " " + line or "")
                     for (lineno, line) in
                     enumerate(template.source.split(
                         '\n'))])))

            if isinstance(got, byte_string):
                got = got.decode('utf-8')

            from doctest import OutputChecker
            checker = OutputChecker()

            if not os.path.exists(output_path):
                output = template.body
            else:
                with open(output_path, 'rb') as f:
                    output = f.read()

            from chameleon.utils import read_xml_encoding
            from chameleon.utils import detect_encoding

            if template.content_type == 'text/xml':
                encoding = read_xml_encoding(output) or \
                           template.default_encoding
            else:
                content_type, encoding = detect_encoding(
                    output, template.default_encoding)

            # Newline normalization across platforms
            want = '\n'.join(output.decode(encoding).splitlines())
            got = '\n'.join(got.splitlines())

            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(input_path, want)
                diff = checker.output_difference(
                    example, got, 0)
                self.fail("(%s) - \n%s\n\nCode:\n%s" % (
                    input_path, diff.rstrip('\n'),
                    template.source.encode('utf-8')))
Пример #14
0
 def check_output(self, want, got, optionflags):
     if sys.version_info[0] < 3:
         got = re.sub("u'(.*?)'", "'\\1'", got)
         got = re.sub('u"(.*?)"', '"\\1"', got)
     return OutputChecker.check_output(self, want, got, optionflags)
Пример #15
0
 def check_output(self, want, got, optionflags):
     want = UNICODE_LITERALS.sub('\\1', want)
     want = want.replace('FieldNotLoaded: ',
                         'workfront.meta.FieldNotLoaded: ')
     return OutputChecker.check_output(self, want, got, optionflags)
Пример #16
0
    def execute(self, ext, factory, **kwargs):
        from chameleon.utils import DebuggingOutputStream

        def translate(msgid, domain=None, mapping=None, context=None,
                      target_language=None, default=None):
            if default is None:
                default = str(msgid)

            if isinstance(msgid, Message):
                default = "Message"

            if mapping:
                default = re.sub(r'\${([a-z_]+)}', r'%(\1)s', default) % \
                          mapping

            if target_language is None:
                return default

            if domain is None:
                with_domain = ""
            else:
                with_domain = " with domain '%s'" % domain

            stripped = default.rstrip('\n ')
            return "%s ('%s' translation into '%s'%s)%s" % (
                stripped, msgid, target_language, with_domain,
                default[len(stripped):]
                )

        for name, source, want, language in self.find_files(ext):
            if language is not None:
                name += '-' + language

            # Make friendly title so we can locate the generated
            # source when debugging
            title = os.path.basename(name).\
                    replace('-', '_').\
                    replace('.', '_')

            self.shortDescription = lambda: name
            template = factory(
                source,
                keep_source=True,
                output_stream_factory=DebuggingOutputStream,

                # The ``_digest_`` method is internal to the template
                # class; we provide a custom function that lets us
                # choose the filename for the generated Python module
                _digest = lambda body, title=title: title,
                )

            params = kwargs.copy()
            params.update({
                'translate': translate,
                'target_language': language,
                })

            try:
                got = template.render(**params)
            except:
                import traceback
                e = traceback.format_exc()
                self.fail("%s\n\n    Example source:\n\n%s" % (e, "\n".join(
                    ["%#03.d%s" % (lineno + 1, line and " " + line or "")
                     for (lineno, line) in
                     enumerate(template.source.split(
                         '\n'))])))

            from doctest import OutputChecker
            checker = OutputChecker()
            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(name, want)
                diff = checker.output_difference(
                    example, got, 0)
                self.fail("(%s) - \n%s\n\nCode:\n%s" % (
                    name, diff.rstrip('\n'), template.source.encode('utf-8')))
Пример #17
0
 def check_output(self, want, got, optionflags):
     return BaseOutputChecker.check_output(self, want, self._decode(got),
                                           optionflags)
Пример #18
0
    def run_tests(self, ext, factory, **kwargs):
        from chameleon.utils import DebuggingOutputStream

        def translate(msgid, domain=None, mapping=None, context=None,
                      target_language=None, default=None):
            if default is None:
                default = str(msgid)

            if isinstance(msgid, Message):
                default = "Message"

            if mapping:
                default = re.sub(r'\${([a-z_]+)}', r'%(\1)s', default) % \
                          mapping

            if target_language is None:
                return default

            if domain is None:
                with_domain = ""
            else:
                with_domain = " with domain '%s'" % domain

            stripped = default.rstrip('\n ')
            return "%s ('%s' translation into '%s'%s)%s" % (
                stripped, msgid, target_language, with_domain,
                default[len(stripped):]
                )

        for name, source, want, language in self.find_files(ext):
            if language is not None:
                name += '-' + language

            self.shortDescription = lambda: name
            template = factory(
                source,
                keep_source=True,
                output_stream_factory=DebuggingOutputStream,
                )

            params = kwargs.copy()
            params.update({
                'translate': translate,
                'target_language': language,
                })

            try:
                got = template.render(**params)
            except:
                import traceback
                e = traceback.format_exc()
                self.fail("%s\n\n    Example source:\n\n%s" % (e, "\n".join(
                    ["%#03.d%s" % (lineno + 1, line and " " + line or "")
                     for (lineno, line) in
                     enumerate(template.source.split(
                         '\n'))])))

            from doctest import OutputChecker
            checker = OutputChecker()
            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(name, want)
                diff = checker.output_difference(
                    example, got, 0)
                self.fail("(%s) - \n%s\n\nCode:\n%s" % (
                    name, diff.rstrip('\n'), template.source.encode('utf-8')))
Пример #19
0
    def execute(self, ext, factory, **kwargs):
        def translate(msgid,
                      domain=None,
                      mapping=None,
                      context=None,
                      target_language=None,
                      default=None):
            if default is None:
                default = str(msgid)

            if isinstance(msgid, Message):
                default = "Message"

            if mapping:
                default = re.sub(r'\${([a-z_]+)}', r'%(\1)s', default) % \
                          mapping

            if target_language is None:
                return default

            if domain is None:
                with_domain = ""
            else:
                with_domain = " with domain '%s'" % domain

            if context is None:
                with_context = ""
            else:
                with_context = ", context '%s'" % context

            stripped = default.rstrip('\n ')
            return "%s ('%s' translation into '%s'%s%s)%s" % (
                stripped, msgid, target_language, with_domain, with_context,
                default[len(stripped):])

        for input_path, output_path, language in self.find_files(ext):
            # Make friendly title so we can locate the generated
            # source when debugging
            self.shortDescription = lambda: input_path

            # When input path contains the string 'implicit-i18n', we
            # enable "implicit translation".
            implicit_i18n = 'implicit-i18n' in input_path
            implicit_i18n_attrs = ("alt", "title") if implicit_i18n else ()

            enable_data_attributes = 'data-attributes' in input_path

            template = factory(
                input_path,
                keep_source=True,
                strict=False,
                implicit_i18n_translate=implicit_i18n,
                implicit_i18n_attributes=implicit_i18n_attrs,
                enable_data_attributes=enable_data_attributes,
            )

            params = kwargs.copy()
            params.update({
                'translate': translate,
                'target_language': language,
            })

            template.cook_check()

            try:
                got = template.render(**params)
            except:
                import traceback
                e = traceback.format_exc()
                self.fail("%s\n\n    Example source:\n\n%s" % (e, "\n".join([
                    "%#03.d%s" % (lineno + 1, line and " " + line or "")
                    for (lineno,
                         line) in enumerate(template.source.split('\n'))
                ])))

            if isinstance(got, byte_string):
                got = got.decode('utf-8')

            from doctest import OutputChecker
            checker = OutputChecker()

            if not os.path.exists(output_path):
                output = template.body
            else:
                with open(output_path, 'rb') as f:
                    output = f.read()

            from chameleon.utils import read_xml_encoding
            from chameleon.utils import detect_encoding

            if template.content_type == 'text/xml':
                encoding = read_xml_encoding(output) or \
                           template.default_encoding
            else:
                content_type, encoding = detect_encoding(
                    output, template.default_encoding)

            # Newline normalization across platforms
            want = '\n'.join(output.decode(encoding).splitlines())
            got = '\n'.join(got.splitlines())

            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(input_path, want)
                diff = checker.output_difference(example, got, 0)
                self.fail("(%s) - \n%s\n\nCode:\n%s" %
                          (input_path, diff.rstrip('\n'),
                           template.source.encode('utf-8')))
Пример #20
0
 def check_output(self, want, got, optionflags):
     if sys.version_info[0] > 2:
         want = re.sub("u'(.*?)'", "'\\1'", want)
         want = re.sub("u'(.*?)'", "'\\1'", want)
     return OutputChecker.check_output(self, want, got, optionflags)
Пример #21
0
 def __init__(self):
     self.output_checker = OutputChecker()
Пример #22
0
    def check_output(self, want: str, got: str, optionFlags: int) -> bool:
        """Method to fix up the doctest examples.

        Args:
            want: the expected output specified in the example
            got: the output collected by doctest when the example is run
            optionFlags : provides some customization options - see
                            doctest documentation for details

        Returns:
            True if the example want matches the got, False if not

        """
        new_got = got
        # print(want)
        # print(got)
        # handle starting case
        if (('Starting' in want) and ('Starting' in new_got)):
            DT_idx_want_start = want.find('Starting')
            DT_idx_got_start = new_got.find('Starting')
            DT_idx_want_stop = want.find('*', DT_idx_want_start)
            DT_idx_got_stop = new_got.find('*', DT_idx_got_start)

            new_got = new_got[0:DT_idx_got_start] \
                + want[DT_idx_want_start:DT_idx_want_stop] \
                + new_got[DT_idx_got_stop:]

        # handle ending case
        if (('Ending' in want) and ('Ending' in new_got)):
            DT_idx_want_start = want.find('Ending')
            DT_idx_got_start = new_got.find('Ending')
            DT_idx_want_stop = want.find('*', DT_idx_want_start)
            DT_idx_got_stop = new_got.find('*', DT_idx_got_start)

            new_got = new_got[0:DT_idx_got_start] \
                + want[DT_idx_want_start:DT_idx_want_stop] \
                + new_got[DT_idx_got_stop:]

        # handle elapsed time case
        if (('Elapsed time:' in want) and ('Elapsed time:' in new_got)):
            DT_idx_want_start = want.find('Elapsed time:')
            DT_idx_got_start = new_got.find('Elapsed time:')
            DT_idx_want_stop = want.find('*', DT_idx_want_start)
            DT_idx_got_stop = new_got.find('*', DT_idx_got_start)

            new_got = new_got[0:DT_idx_got_start] \
                + want[DT_idx_want_start:DT_idx_want_stop] \
                + new_got[DT_idx_got_stop:]

        if 'this is a diagnostic message' in want:
            start_of_msg_want = want.find('this is a diagnostic message')
            start_of_msg_got = got.find('this is a diagnostic message')
            new_got = want[0:start_of_msg_want] + new_got[start_of_msg_got:]

        # if 'diagnostic info' in want:
        #     start_of_msg_want = want.find('diagnostic info')
        #     start_of_msg_got = got.find('diagnostic info')
        #     new_got = want[0:start_of_msg_want] + new_got[start_of_msg_got:]

        if 'CallerInfo' in want:
            start_cls_want = want.find('cls_name=')
            start_cls_got = got.find('cls_name=')
            new_got = want[0:start_cls_want] + new_got[start_cls_got:]

        gfcs_suffix = 'get_formatted_call_sequence['
        dm_suffix = 'diag_msg['
        if gfcs_suffix in got or dm_suffix in got:
            brkt1_num = '0'
            brkt2_num = '0'
            brkt3_num = '0'
            brkt1 = got.find('[')
            if brkt1 > 0:
                r_brkt1 = got.find(']', brkt1)
                brkt1_num = got[brkt1 + 1:r_brkt1]
                brkt2 = got.find('[', brkt1 + 1)
                if brkt2 > 0:
                    r_brkt2 = got.find(']', brkt2)
                    brkt2_num = got[brkt2 + 1:r_brkt2]
                    brkt3 = got.find('[', brkt2 + 1)
                    if brkt3 > 0:
                        r_brkt3 = got.find(']', brkt3)
                        brkt3_num = got[brkt3 + 1:r_brkt3]

            prefix = '<doctest scottbrian_utils.diag_msg.'
            suffix = gfcs_suffix if gfcs_suffix in got else dm_suffix
            old1 = prefix + suffix + brkt1_num + ']>'
            old2 = prefix + suffix + brkt2_num + ']>'
            old3 = prefix + suffix + brkt3_num + ']>'

            new_text = '<input>'
            if old1 in new_got:
                new_got = new_got.replace(old1, new_text)
            if old2 in new_got:
                new_got = new_got.replace(old2, new_text)
            if old3 in new_got:
                new_got = new_got.replace(old3, new_text)

            # handle time stamp
            if dm_suffix in got:  # if diag_msg
                lt_sign_idx = want.find('<')
                if lt_sign_idx > 0:
                    new_got = want[0:lt_sign_idx] + new_got[lt_sign_idx:]

        return OutputChecker.check_output(self, want, new_got, optionFlags)
Пример #23
0
 def check_output(self, want, got, optionflags):
     if optionflags & IGNORE_RESULT:
         return True
     return OutputChecker.check_output(self, want, got, optionflags)
Пример #24
0
    def execute(self, ext, factory, **kwargs):
        def translate(msgid, domain=None, mapping=None, context=None, target_language=None, default=None):
            if default is None:
                default = str(msgid)

            if isinstance(msgid, Message):
                default = "Message"

            if mapping:
                default = re.sub(r"\${([a-z_]+)}", r"%(\1)s", default) % mapping

            if target_language is None:
                return default

            if domain is None:
                with_domain = ""
            else:
                with_domain = " with domain '%s'" % domain

            stripped = default.rstrip("\n ")
            return "%s ('%s' translation into '%s'%s)%s" % (
                stripped,
                msgid,
                target_language,
                with_domain,
                default[len(stripped) :],
            )

        for input_path, output_path, language in self.find_files(ext):
            # Make friendly title so we can locate the generated
            # source when debugging
            self.shortDescription = lambda: input_path

            # Very implicitly enable implicit translation based on
            # a string included in the input path:
            implicit_i18n = "implicit-i18n" in input_path
            implicit_i18n_attrs = ("alt", "title") if implicit_i18n else ()

            template = factory(
                input_path,
                keep_source=True,
                strict=False,
                implicit_i18n_translate=implicit_i18n,
                implicit_i18n_attributes=implicit_i18n_attrs,
            )

            params = kwargs.copy()
            params.update({"translate": translate, "target_language": language})

            template.cook_check()

            try:
                got = template.render(**params)
            except:
                import traceback

                e = traceback.format_exc()
                self.fail(
                    "%s\n\n    Example source:\n\n%s"
                    % (
                        e,
                        "\n".join(
                            [
                                "%#03.d%s" % (lineno + 1, line and " " + line or "")
                                for (lineno, line) in enumerate(template.source.split("\n"))
                            ]
                        ),
                    )
                )

            if isinstance(got, byte_string):
                got = got.decode("utf-8")

            from doctest import OutputChecker

            checker = OutputChecker()

            if not os.path.exists(output_path):
                output = template.body
            else:
                with open(output_path, "rb") as f:
                    output = f.read()

            from chameleon.utils import read_xml_encoding
            from chameleon.utils import detect_encoding

            if template.content_type == "text/xml":
                encoding = read_xml_encoding(output) or template.default_encoding
            else:
                content_type, encoding = detect_encoding(output, template.default_encoding)

            want = output.decode(encoding)

            if checker.check_output(want, got, 0) is False:
                from doctest import Example

                example = Example(input_path, want)
                diff = checker.output_difference(example, got, 0)
                self.fail("(%s) - \n%s\n\nCode:\n%s" % (input_path, diff.rstrip("\n"), template.source.encode("utf-8")))
Пример #25
0
    def test_sample_files(self):
        import os
        import traceback
        path = os.path.join(os.path.dirname(__file__), "inputs")
        for filename in os.listdir(path):
            if not filename.endswith('.html'):
                continue

            with open(os.path.join(path, filename), 'rb') as f:
                source = f.read()

            from ..utils import read_encoded
            try:
                want = read_encoded(source)
            except UnicodeDecodeError:
                exc = sys.exc_info()[1]
                self.fail("%s - %s" % (exc, filename))

            from ..tokenize import iter_xml
            from ..parser import ElementParser
            try:
                tokens = iter_xml(want)
                parser = ElementParser(tokens, {
                    'xmlns': XMLNS_NS,
                    'xml': XML_NS,
                    'py': PY_NS,
                    })
                elements = tuple(parser)
            except:
                self.fail(traceback.format_exc())

            output = []

            def render(kind, args):
                if kind == 'element':
                    # start tag
                    tag, end, children = args
                    output.append("%(prefix)s%(name)s" % tag)

                    for attr in tag['attrs']:
                        output.append(
                            "%(space)s%(name)s%(eq)s%(quote)s%(value)s%(quote)s" % \
                            attr
                            )

                    output.append("%(suffix)s" % tag)

                    # children
                    for item in children:
                        render(*item)

                    # end tag
                    output.append(
                        "%(prefix)s%(name)s%(space)s%(suffix)s" % end
                        )
                elif kind == 'text':
                    text = args[0]
                    output.append(text)
                elif kind == 'start_tag':
                    node = args[0]
                    output.append(
                        "%(prefix)s%(name)s%(space)s%(suffix)s" % node
                        )
                else:
                    raise RuntimeError("Not implemented: %s." % kind)

            for kind, args in elements:
                render(kind, args)

            got = "".join(output)

            from doctest import OutputChecker
            checker = OutputChecker()

            if checker.check_output(want, got, 0) is False:
                from doctest import Example
                example = Example(f.name, want)
                diff = checker.output_difference(
                    example, got, 0)
                self.fail("(%s) - \n%s" % (f.name, diff))
Пример #26
0
 def output_difference(self, example, got, optionflags):
     return BaseOutputChecker.output_difference(self, example,
                                                self._decode(got),
                                                optionflags)
 def check_output(self, want, got, optionflags):
     if optionflags & IGNORE_RESULT:
         return True
     return OutputChecker.check_output(self, want, got, optionflags)
Пример #28
0
 def check_output(self, want, got, optionflags):
     if got:
         got = re.sub("u'(.*?)'", "'\\1'", got)
         got = re.sub('u"(.*?)"', '"\\1"', got)
     return OutputChecker.check_output(self, want, got, optionflags)