403Webshell
Server IP : 15.235.198.142  /  Your IP : 216.73.216.190
Web Server : Apache/2.4.58 (Ubuntu)
System : Linux ballsack 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 8.3.6
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /lib/python3/dist-packages/twisted/trial/test/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /lib/python3/dist-packages/twisted/trial/test/test_matchers.py
"""
Tests for L{twisted.trial.test.matchers}.
"""
from hamcrest import anything, assert_that, contains_string, equal_to, not_
from hamcrest.core.core.allof import AllOf
from hamcrest.core.string_description import StringDescription
from hypothesis import given
from hypothesis.strategies import just, sampled_from, text

from twisted.python.filepath import FilePath
from twisted.trial.unittest import SynchronousTestCase
from .matchers import fileContents


class FileContentsTests(SynchronousTestCase):
    """
    Tests for L{fileContents}.
    """

    @given(text(), just("utf-8"))
    def test_matches(self, contents: str, encoding: str) -> None:
        """
        L{fileContents} matches a L{IFilePath} that refers to a file that
        contains a string that is matched by the parameterized matcher.

        :param contents: The text string to place in the file and match
            against.

        :param encoding: The text encoding to use to encode C{contents} when
            writing to the file.
        """
        p = FilePath(self.mktemp())
        p.setContent(contents.encode(encoding))

        description = StringDescription()
        assert_that(
            fileContents(equal_to(contents)).matches(p, description), equal_to(True)
        )
        assert_that(str(description), equal_to(""))

    @given(
        just("some text, it doesn't matter what"),
        sampled_from(["ascii", "latin-1", "utf-8"]),
    )
    def test_mismatches(self, contents: str, encoding: str) -> None:
        """
        L{fileContents} does not match an L{IFilePath} that refers to a
        file that contains a string that is not matched by the parameterized
        matcher.

        :param contents: The text string to place in the file and match
            against.

        :param encoding: The text encoding to use to encode C{contents} when
            writing to the file.
        """
        p = FilePath(self.mktemp())
        p.setContent(contents.encode(encoding))

        description = StringDescription()
        assert_that(
            fileContents(not_(anything())).matches(p, description), equal_to(False)
        )
        assert_that(str(description), equal_to(f"was <{p}>"))

    def test_ioerror(self) -> None:
        """
        L{fileContents} reports details of any I/O error encountered while
        attempting to match.
        """
        p = FilePath(self.mktemp())

        description = StringDescription()
        assert_that(fileContents(anything()).matches(p, description), equal_to(False))
        assert_that(
            str(description),
            # It must contain at least ...
            AllOf(
                # the name of the matcher.
                contains_string("fileContents"),
                # the name of the exception raised.
                contains_string("FileNotFoundError"),
                # the repr (so weird values are escaped) of the path being
                # matched against.
                contains_string(repr(p.path)),
            ),
        )

Youez - 2016 - github.com/yon3zu
LinuXploit