How to use the approvaltests.string_writer.StringWriter function in approvaltests

To help you get started, we’ve selected a few approvaltests examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github approvals / ApprovalTests.Python / approvaltests / approvals.py View on Github external
newline: An optional string that controls how universal newlines work when comparing data.
            It can be None, '', '\n', '\r', and '\r\n'. If None (the default) universal newlines are
            enabled and any '\n' characters are translated to the system default line separator
            given by os.linesep. If newline is '', no translation takes place. If newline is any of
            the other legal values, any '\n' characters written are translated to the given string.
            
    Raises:
        ApprovalException: If the verification fails because the given string does not match the
            approved string.
        
        ValueError: If data cannot be encoded using the specified encoding when errors is set to
            None or 'strict'.
    """
    reporter = get_reporter(reporter)
    approver = FileApprover()
    writer = StringWriter(data, encoding=encoding, errors=errors, newline=newline)
    error = approver.verify(namer, writer, reporter)
    if error is not None:
        raise ApprovalException(error)
github approvals / ApprovalTests.Python / tests / test_writer.py View on Github external
def test_writes_file(tmpdir):
    contents = "foo" + str(randint(0, 100))
    sw = StringWriter(contents)
    filename = os.path.join(str(tmpdir), 'stuff.txt')
    sw.write_received_file(filename)

    with open(filename, 'r') as received:
        assert contents == received.read()
github approvals / ApprovalTests.Python / tests / test_fileapprover.py View on Github external
def test_returns_error_when_files_are_different(self):
        namer = get_default_namer()
        writer = StringWriter("b")
        reporter = ReporterForTesting()
        approver = FileApprover()
        error = approver.verify(namer, writer, reporter)
        self.assertEqual("Approval Mismatch", error)
github approvals / ApprovalTests.Python / tests / test_fileapprover.py View on Github external
def test_returns_none_when_files_are_same_files(self):
        namer = get_default_namer()
        writer = StringWriter("b")
        reporter = GenericDiffReporterFactory().get_first_working()
        approver = FileApprover()
        error = approver.verify(namer, writer, reporter)
        self.assertEqual(None, error)
github approvals / ApprovalTests.Python / tests / test_writer.py View on Github external
def test_writes_file_to_missing_directory(tmpdir):
    contents = "foo"
    sw = StringWriter(contents)
    filename = os.path.join(str(tmpdir), 'non_existent_folder', './stuff.txt')
    sw.write_received_file(filename)

    with open(filename, 'r') as received:
        assert contents == received.read()