# Path: hq/hquery/syntax_error.py
# class HquerySyntaxError(ValueError):
#     pass
# 
# Path: test/common_test_util.py
# def expected_result(contents):
#     return dedent(contents.lstrip('\n'))
# 
# Path: test/hquery/hquery_test_util.py
# def query_html_doc(html_body, hquery, preserve_space=False, wrap_body=True):
#     soup = soup_with_body(html_body) if wrap_body else make_soup(html_body)
#     raw_result = HqueryProcessor(hquery, preserve_space=preserve_space).query(soup)
#     return eliminate_blank_lines(convert_results_to_output_text(raw_result, preserve_space=preserve_space).strip())


from hq.hquery.syntax_error import HquerySyntaxError
from pytest import raises
from test.common_test_util import expected_result
from test.hquery.hquery_test_util import query_html_doc
def test_variable_declaration_and_reference_in_a_flwor():
    expected = 'bar'
    assert query_html_doc('', 'let $foo := "{0}" return $foo'.format(expected)) == expected


def test_variable_declarations_are_processed_in_order():
    hquery = 'let $hello := "hello, " let $whole-phrase := concat($hello, "world!") return $whole-phrase'
    assert query_html_doc('', hquery) == 'hello, world!'


def test_variable_is_accessible_inside_interpolated_string():
    assert query_html_doc('', 'let $foo := "bar" return `foo is $foo`') == 'foo is bar'
    assert query_html_doc('', 'let $foo := (1 to 3) return `${j:, :$foo}`') == '1, 2, 3'


def test_multiple_return_clauses_are_not_allowed():
    with raises(HquerySyntaxError):
        query_html_doc('', 'let $x := "whatever" return $x return "uh-oh"')


def test_that_no_other_clauses_are_allowed_after_a_return():
    with raises(HquerySyntaxError):
        query_html_doc('', 'let $x := "whatevs" return $x let $uh-oh := "oh no"')


def test_iteration_using_for():
    html_body = """
    <p>one</p>
    <p>two</p>
    <p>three</p>"""

# Path: hq/hquery/syntax_error.py
# class HquerySyntaxError(ValueError):
#     pass
# 
# Path: test/common_test_util.py
# def expected_result(contents):
#     return dedent(contents.lstrip('\n'))
# 
# Path: test/hquery/hquery_test_util.py
# def query_html_doc(html_body, hquery, preserve_space=False, wrap_body=True):
#     soup = soup_with_body(html_body) if wrap_body else make_soup(html_body)
#     raw_result = HqueryProcessor(hquery, preserve_space=preserve_space).query(soup)
#     return eliminate_blank_lines(convert_results_to_output_text(raw_result, preserve_space=preserve_space).strip())


# Path: test/hquery/test_flwor.py
from hq.hquery.syntax_error import HquerySyntaxError
from pytest import raises
from test.common_test_util import expected_result
from test.hquery.hquery_test_util import query_html_doc

def test_variable_declaration_and_reference_in_a_flwor():
    expected = 'bar'
    assert query_html_doc('', 'let $foo := "{0}" return $foo'.format(expected)) == expected


def test_variable_declarations_are_processed_in_order():
    hquery = 'let $hello := "hello, " let $whole-phrase := concat($hello, "world!") return $whole-phrase'
    assert query_html_doc('', hquery) == 'hello, world!'


def test_variable_is_accessible_inside_interpolated_string():
    assert query_html_doc('', 'let $foo := "bar" return `foo is $foo`') == 'foo is bar'
    assert query_html_doc('', 'let $foo := (1 to 3) return `${j:, :$foo}`') == '1, 2, 3'


def test_multiple_return_clauses_are_not_allowed():
    with raises(HquerySyntaxError):
        query_html_doc('', 'let $x := "whatever" return $x return "uh-oh"')


def test_that_no_other_clauses_are_allowed_after_a_return():
    with raises(HquerySyntaxError):
        query_html_doc('', 'let $x := "whatevs" return $x let $uh-oh := "oh no"')


def test_iteration_using_for():
    html_body = """
    <p>one</p>
    <p>two</p>
    <p>three</p>"""

    assert query_html_doc(html_body, 'for $x in //p return $x/text()') == expected_result("""