# Path: format_sql/styler.py
# class Liner:
# 
#     def __init__(self):
#         self.line = []
#         self.lines = []
# 
#     def add_to_line(self, val):
#         self.line.append('%s' % val)
# 
#     def add_line(self, val):
#         self.add_to_line(val)
#         self.end_line()
# 
#     def add_empty_lines(self, count=1):
#         self.end_line()
#         for _ in range(count):
#             self.lines.append('')
# 
#     def end_line(self):
#         line = ''.join(self.line)
#         if line:
#             self.lines.append(line)
#         self.line = []
# 
#     def add_to_last_line(self, val):
#         line = self.lines.pop()
#         self.add_to_line(line)
#         self.add_to_line(val)
#         self.end_line()
# 
# def _style_func(func, liner, end_line=True):
#     liner.add_to_line(func.name.upper())
#     liner.add_to_line('(')
# 
#     for i, arg in enumerate(func.args):
#         if isinstance(arg, (Identifier, Str, Number)):
#             liner.add_to_line(arg)
#         elif isinstance(arg, Func):
#             _style_func(arg, liner, end_line=False)
# 
#         if i + 1 < len(func.args):
#             liner.add_to_line(', ')
# 
#     liner.add_to_line(')')
#     if func.as_ and func.alias:
#         liner.add_to_line(' AS ')
#         liner.add_to_line(func.alias)
# 
#     if end_line:
#         liner.end_line()
# 
# def style(statements, indent=0, keyword_upper=True, liner=None):
#     if not liner:
#         liner = Liner()
# 
#     structures = {
#         From: _style_from,
#         GroupBy: _style_group_by,
#         Having: _style_having,
#         Insert: _style_insert,
#         Limit: _style_limit,
#         OrderBy: _style_order_by,
#         Select: _style_select,
#         Semicolon: _style_semicolon,
#         Where: _style_where,
#     }
# 
#     for i, statement in enumerate(statements):
#         func = structures[statement.__class__]
#         try:
#             func(statement, liner=liner, indent=indent)
#         except IndexError:
#             raise InvalidSQL()
# 
#         if isinstance(statement, Semicolon) and len(statements) > i + 1:
#             # statements holds multiple separate statements
#             liner.add_empty_lines(count=2)
# 
#     liner.end_line()
#     return liner.lines


from format_sql.styler import Liner, _style_func, style



def assert_func_style(statements1, styled2):
    liner = Liner()
    _style_func(statements1[0], liner)
    assert styled2 == liner.lines


def assert_style(statements1, styled2):
    assert style(statements1) == styled2


def test_from_1(from_1):
    assert_style(from_1.statements, from_1.style)


def test_from_2(from_2):
    assert_style(from_2.statements, from_2.style)


def test_from_3(from_3):
    assert_style(from_3.statements, from_3.style)


def test_from_4(from_4):
    assert_style(from_4.statements, from_4.style)


def test_from_5(from_5):

# Path: format_sql/styler.py
# class Liner:
# 
#     def __init__(self):
#         self.line = []
#         self.lines = []
# 
#     def add_to_line(self, val):
#         self.line.append('%s' % val)
# 
#     def add_line(self, val):
#         self.add_to_line(val)
#         self.end_line()
# 
#     def add_empty_lines(self, count=1):
#         self.end_line()
#         for _ in range(count):
#             self.lines.append('')
# 
#     def end_line(self):
#         line = ''.join(self.line)
#         if line:
#             self.lines.append(line)
#         self.line = []
# 
#     def add_to_last_line(self, val):
#         line = self.lines.pop()
#         self.add_to_line(line)
#         self.add_to_line(val)
#         self.end_line()
# 
# def _style_func(func, liner, end_line=True):
#     liner.add_to_line(func.name.upper())
#     liner.add_to_line('(')
# 
#     for i, arg in enumerate(func.args):
#         if isinstance(arg, (Identifier, Str, Number)):
#             liner.add_to_line(arg)
#         elif isinstance(arg, Func):
#             _style_func(arg, liner, end_line=False)
# 
#         if i + 1 < len(func.args):
#             liner.add_to_line(', ')
# 
#     liner.add_to_line(')')
#     if func.as_ and func.alias:
#         liner.add_to_line(' AS ')
#         liner.add_to_line(func.alias)
# 
#     if end_line:
#         liner.end_line()
# 
# def style(statements, indent=0, keyword_upper=True, liner=None):
#     if not liner:
#         liner = Liner()
# 
#     structures = {
#         From: _style_from,
#         GroupBy: _style_group_by,
#         Having: _style_having,
#         Insert: _style_insert,
#         Limit: _style_limit,
#         OrderBy: _style_order_by,
#         Select: _style_select,
#         Semicolon: _style_semicolon,
#         Where: _style_where,
#     }
# 
#     for i, statement in enumerate(statements):
#         func = structures[statement.__class__]
#         try:
#             func(statement, liner=liner, indent=indent)
#         except IndexError:
#             raise InvalidSQL()
# 
#         if isinstance(statement, Semicolon) and len(statements) > i + 1:
#             # statements holds multiple separate statements
#             liner.add_empty_lines(count=2)
# 
#     liner.end_line()
#     return liner.lines


# Path: tests/test_styler.py
from format_sql.styler import Liner, _style_func, style




def assert_func_style(statements1, styled2):
    liner = Liner()
    _style_func(statements1[0], liner)
    assert styled2 == liner.lines


def assert_style(statements1, styled2):
    assert style(statements1) == styled2


def test_from_1(from_1):
    assert_style(from_1.statements, from_1.style)


def test_from_2(from_2):
    assert_style(from_2.statements, from_2.style)


def test_from_3(from_3):
    assert_style(from_3.statements, from_3.style)


def test_from_4(from_4):
    assert_style(from_4.statements, from_4.style)


def test_from_5(from_5):

    assert_style(from_5.statements, from_5.style)