# Path: ledgerbil/colorable.py
# class Colorable:
# 
#     START_CODE = "\033"
#     END_CODE = f"{START_CODE}[0m"
# 
#     BRIGHT_OFFSET = 60
# 
#     COLORS = {
#         "black": 30,
#         "gray": 30,
#         "grey": 30,
#         "red": 31,
#         "green": 32,
#         "yellow": 33,
#         "blue": 34,
#         "magenta": 35,
#         "purple": 35,
#         "cyan": 36,
#         "white": 37,
#     }
# 
#     ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
# 
#     def __init__(self, color, value, fmt="", bright=False):
# 
#         if color not in self.COLORS:
#             raise UnsupportedColorError(
#                 f"I don't know what to do with this color: {color}"
#             )
# 
#         self.my_color = color
#         self.value = value
#         self.bright = bright
#         self.format_string = fmt
# 
#     def __repr__(self):
#         return (
#             f"Colorable('{self.my_color}', '{self.value}', "
#             f"fmt='{self.format_string}', bright={self.bright})"
#         )
# 
#     def __str__(self):
#         start = self.ansi_sequence(self.COLORS[self.my_color], bright=self.bright)
#         ansi_str = f"{start}{self.value:{self.format_string}}{self.END_CODE}"
#         return ansi_str
# 
#     def __len__(self):
#         return len(self.value)
# 
#     def __eq__(self, other):
#         return str(self) == str(other)
# 
#     def ansi_sequence(self, code, bright=False):
#         offset = 60 if bright else 0
#         color = code + offset
#         return f"{self.START_CODE}[0;{color}m"
# 
#     def plain(self):
#         return self.value
# 
#     @staticmethod
#     def get_plain_string(ansi_string):
#         return Colorable.ansi_escape.sub("", ansi_string)
# 
# Path: ledgerbil/tests/helpers.py
# class OutputFileTester(OutputFileTesterBase):
#     def __init__(self, testfile):
#         super().__init__()
#         self.testfile = testfile
# 
#     def save_out_file(self, data):
#         with open(self.get_out_filename(self.testfile), "w", encoding="utf-8") as afile:
#             afile.write(self.remove_color(data))
# 
#     def assert_out_equals_expected(self):
#         assert filecmp.cmp(
#             self.get_out_filename(self.testfile),
#             self.get_expected_filename(self.testfile),
#         ), f"test filename out: {self.get_out_filename(self.testfile)}"


import sys
import pytest
from datetime import date
from textwrap import dedent
from unittest import mock
from ... import settings, settings_getter
from ...colorable import Colorable
from ...tests.helpers import OutputFileTester
from .. import grid
    expected = dedent(
        '''\
        1\t2\t3\t4\t5
        a\tb\t"""c"""\td e f\t"g\th"
        \t4\t\t6\tglurg
        '''
    )
    csv_report = grid.get_csv_report(rows, tabs=True)
    assert csv_report == expected


def test_get_flat_report():
    rows = [
        ["lemon", "lime", grid.TOTAL_HEADER, ""],
        [2.65, 500.1, 502.75, "expenses: widgets"],
        [17.37, 28.19, 45.56, "expenses: car: gas"],
        [6.5, 0, 6.5, "expenses: car: maintenance"],
        [0, -10123.55, -10123.55, "expenses: unicorns"],
        [26.52, -9595.26, -9568.74, grid.EMPTY_VALUE],
    ]
    report = grid.get_flat_report(rows)
    helper = OutputFileTester("test_grid_flat_report")
    helper.save_out_file(report)
    helper.assert_out_equals_expected()


def test_get_flat_report_networth():
    rows = [["lemon", "lime", ""], [200.11, 300.44, "net worth"]]
    report = grid.get_flat_report(rows, networth=True)
    expected = "         lemon          lime\n      $ 200.11      $ 300.44  net worth\n"

# Path: ledgerbil/colorable.py
# class Colorable:
# 
#     START_CODE = "\033"
#     END_CODE = f"{START_CODE}[0m"
# 
#     BRIGHT_OFFSET = 60
# 
#     COLORS = {
#         "black": 30,
#         "gray": 30,
#         "grey": 30,
#         "red": 31,
#         "green": 32,
#         "yellow": 33,
#         "blue": 34,
#         "magenta": 35,
#         "purple": 35,
#         "cyan": 36,
#         "white": 37,
#     }
# 
#     ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
# 
#     def __init__(self, color, value, fmt="", bright=False):
# 
#         if color not in self.COLORS:
#             raise UnsupportedColorError(
#                 f"I don't know what to do with this color: {color}"
#             )
# 
#         self.my_color = color
#         self.value = value
#         self.bright = bright
#         self.format_string = fmt
# 
#     def __repr__(self):
#         return (
#             f"Colorable('{self.my_color}', '{self.value}', "
#             f"fmt='{self.format_string}', bright={self.bright})"
#         )
# 
#     def __str__(self):
#         start = self.ansi_sequence(self.COLORS[self.my_color], bright=self.bright)
#         ansi_str = f"{start}{self.value:{self.format_string}}{self.END_CODE}"
#         return ansi_str
# 
#     def __len__(self):
#         return len(self.value)
# 
#     def __eq__(self, other):
#         return str(self) == str(other)
# 
#     def ansi_sequence(self, code, bright=False):
#         offset = 60 if bright else 0
#         color = code + offset
#         return f"{self.START_CODE}[0;{color}m"
# 
#     def plain(self):
#         return self.value
# 
#     @staticmethod
#     def get_plain_string(ansi_string):
#         return Colorable.ansi_escape.sub("", ansi_string)
# 
# Path: ledgerbil/tests/helpers.py
# class OutputFileTester(OutputFileTesterBase):
#     def __init__(self, testfile):
#         super().__init__()
#         self.testfile = testfile
# 
#     def save_out_file(self, data):
#         with open(self.get_out_filename(self.testfile), "w", encoding="utf-8") as afile:
#             afile.write(self.remove_color(data))
# 
#     def assert_out_equals_expected(self):
#         assert filecmp.cmp(
#             self.get_out_filename(self.testfile),
#             self.get_expected_filename(self.testfile),
#         ), f"test filename out: {self.get_out_filename(self.testfile)}"


# Path: ledgerbil/ledgershell/tests/test_grid.py
import sys
import pytest
from datetime import date
from textwrap import dedent
from unittest import mock
from ... import settings, settings_getter
from ...colorable import Colorable
from ...tests.helpers import OutputFileTester
from .. import grid

    expected = dedent(
        '''\
        1\t2\t3\t4\t5
        a\tb\t"""c"""\td e f\t"g\th"
        \t4\t\t6\tglurg
        '''
    )
    csv_report = grid.get_csv_report(rows, tabs=True)
    assert csv_report == expected


def test_get_flat_report():
    rows = [
        ["lemon", "lime", grid.TOTAL_HEADER, ""],
        [2.65, 500.1, 502.75, "expenses: widgets"],
        [17.37, 28.19, 45.56, "expenses: car: gas"],
        [6.5, 0, 6.5, "expenses: car: maintenance"],
        [0, -10123.55, -10123.55, "expenses: unicorns"],
        [26.52, -9595.26, -9568.74, grid.EMPTY_VALUE],
    ]
    report = grid.get_flat_report(rows)
    helper = OutputFileTester("test_grid_flat_report")
    helper.save_out_file(report)
    helper.assert_out_equals_expected()


def test_get_flat_report_networth():
    rows = [["lemon", "lime", ""], [200.11, 300.44, "net worth"]]
    report = grid.get_flat_report(rows, networth=True)
    expected = "         lemon          lime\n      $ 200.11      $ 300.44  net worth\n"

    assert Colorable.get_plain_string(report) == expected