# Path: approvaltests/string_writer.py
# class StringWriter(Writer):
#     contents = ""
# 
#     def __init__(
#         self,
#         contents: str,
#         extension: str = ".txt",
#         encoding: Optional[str] = None,
#         errors: Optional[str] = None,
#         newline: Optional[str] = None,
#     ) -> None:
#         self.contents = self.sanitize_string(contents)
#         self.extension_with_dot = extension
#         self.encoding = encoding
#         self.errors = errors
#         self.newline = newline
# 
#     def sanitize_string(self, contents):
#         contents = contents or u""
#         if len(contents) == 0 or contents[-1] != "\n":
#             contents = contents + "\n"
#         return contents
# 
#     def write_received_file(self, received_file: str) -> str:
#         create_directory_if_needed(received_file)
#         with io.open(
#             received_file,
#             mode="wt",
#             encoding=self.encoding,
#             errors=self.errors,
#             newline=self.newline,
#         ) as f:
#             f.write(self.contents)
#         return received_file


﻿import os
from random import randint
from approvaltests.string_writer import StringWriter



def test_writes_file(tmpdir):
    contents = "foo" + str(randint(0, 100)) + "\n"
    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()


def test_writes_file_to_missing_directory(tmpdir):
    contents = "foo\n"
    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()


def test_new_lines_with_empty_string():

# Path: approvaltests/string_writer.py
# class StringWriter(Writer):
#     contents = ""
# 
#     def __init__(
#         self,
#         contents: str,
#         extension: str = ".txt",
#         encoding: Optional[str] = None,
#         errors: Optional[str] = None,
#         newline: Optional[str] = None,
#     ) -> None:
#         self.contents = self.sanitize_string(contents)
#         self.extension_with_dot = extension
#         self.encoding = encoding
#         self.errors = errors
#         self.newline = newline
# 
#     def sanitize_string(self, contents):
#         contents = contents or u""
#         if len(contents) == 0 or contents[-1] != "\n":
#             contents = contents + "\n"
#         return contents
# 
#     def write_received_file(self, received_file: str) -> str:
#         create_directory_if_needed(received_file)
#         with io.open(
#             received_file,
#             mode="wt",
#             encoding=self.encoding,
#             errors=self.errors,
#             newline=self.newline,
#         ) as f:
#             f.write(self.contents)
#         return received_file


# Path: tests/test_writer.py
﻿import os
from random import randint
from approvaltests.string_writer import StringWriter




def test_writes_file(tmpdir):
    contents = "foo" + str(randint(0, 100)) + "\n"
    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()


def test_writes_file_to_missing_directory(tmpdir):
    contents = "foo\n"
    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()


def test_new_lines_with_empty_string():

    sw = StringWriter("")