| 1 |
from typing import cast, Collection, Optional |
| 2 |
|
| 3 |
from graphql.language.block_string import ( |
| 4 |
is_printable_as_block_string, |
| 5 |
dedent_block_string_lines, |
| 6 |
print_block_string, |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
def join_lines(*args: str) -> str: |
| 11 |
return "\n".join(args) |
| 12 |
|
| 13 |
|
| 14 |
def describe_dedent_block_string_lines(): |
| 15 |
def _assert_dedent(lines: Collection[str], expected: Collection[str]) -> None: |
| 16 |
assert dedent_block_string_lines(lines) == expected |
| 17 |
|
| 18 |
def handles_empty_string(): |
| 19 |
_assert_dedent([""], []) |
| 20 |
|
| 21 |
def does_not_dedent_first_line(): |
| 22 |
_assert_dedent([" a"], [" a"]) |
| 23 |
_assert_dedent([" a", " b"], [" a", "b"]) |
| 24 |
|
| 25 |
def removes_minimal_indentation_length(): |
| 26 |
_assert_dedent(["", " a", " b"], ["a", " b"]) |
| 27 |
_assert_dedent(["", " a", " b"], [" a", "b"]) |
| 28 |
_assert_dedent(["", " a", " b", "c"], [" a", " b", "c"]) |
| 29 |
|
| 30 |
def dedent_both_tab_and_space_as_single_character(): |
| 31 |
_assert_dedent(["", "\ta", " b"], ["a", " b"]) |
| 32 |
_assert_dedent(["", "\t a", " b"], ["a", " b"]) |
| 33 |
_assert_dedent(["", " \t a", " b"], ["a", " b"]) |
| 34 |
|
| 35 |
def removes_uniform_indentation_from_a_string(): |
| 36 |
lines = ["", " Hello,", " World!", "", " Yours,", " GraphQL."] |
| 37 |
_assert_dedent(lines, ["Hello,", " World!", "", "Yours,", " GraphQL."]) |
| 38 |
|
| 39 |
def removes_empty_leading_and_trailing_lines(): |
| 40 |
lines = [ |
| 41 |
"", |
| 42 |
"", |
| 43 |
" Hello,", |
| 44 |
" World!", |
| 45 |
"", |
| 46 |
" Yours,", |
| 47 |
" GraphQL.", |
| 48 |
"", |
| 49 |
"", |
| 50 |
] |
| 51 |
_assert_dedent(lines, ["Hello,", " World!", "", "Yours,", " GraphQL."]) |
| 52 |
|
| 53 |
def removes_blank_leading_and_trailing_lines(): |
| 54 |
lines = [ |
| 55 |
" ", |
| 56 |
" ", |
| 57 |
" Hello,", |
| 58 |
" World!", |
| 59 |
"", |
| 60 |
" Yours,", |
| 61 |
" GraphQL.", |
| 62 |
" ", |
| 63 |
" ", |
| 64 |
] |
| 65 |
_assert_dedent(lines, ["Hello,", " World!", "", "Yours,", " GraphQL."]) |
| 66 |
|
| 67 |
def retains_indentation_from_first_line(): |
| 68 |
lines = [" Hello,", " World!", "", " Yours,", " GraphQL."] |
| 69 |
_assert_dedent(lines, [" Hello,", " World!", "", "Yours,", " GraphQL."]) |
| 70 |
|
| 71 |
def does_not_alter_trailing_spaces(): |
| 72 |
lines = [ |
| 73 |
" ", |
| 74 |
" Hello, ", |
| 75 |
" World! ", |
| 76 |
" ", |
| 77 |
" Yours, ", |
| 78 |
" GraphQL. ", |
| 79 |
" ", |
| 80 |
] |
| 81 |
_assert_dedent( |
| 82 |
lines, |
| 83 |
[ |
| 84 |
"Hello, ", |
| 85 |
" World! ", |
| 86 |
" ", |
| 87 |
"Yours, ", |
| 88 |
" GraphQL. ", |
| 89 |
], |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
def describe_is_printable_as_block_string(): |
| 94 |
def _assert_printable(s: str) -> None: |
| 95 |
assert is_printable_as_block_string(s) is True, s |
| 96 |
|
| 97 |
def _assert_non_printable(s: str) -> None: |
| 98 |
assert is_printable_as_block_string(s) is False, s |
| 99 |
|
| 100 |
def accepts_valid_strings(): |
| 101 |
_assert_printable("") |
| 102 |
_assert_printable(" a") |
| 103 |
_assert_printable('\t"\n"') |
| 104 |
_assert_non_printable('\t"\n \n\t"') |
| 105 |
|
| 106 |
def rejects_strings_with_only_whitespace(): |
| 107 |
_assert_non_printable(" ") |
| 108 |
_assert_non_printable("\t") |
| 109 |
_assert_non_printable("\t ") |
| 110 |
_assert_non_printable(" \t") |
| 111 |
|
| 112 |
def rejects_strings_with_non_printable_characters(): |
| 113 |
_assert_non_printable("\x00") |
| 114 |
_assert_non_printable("a\x00b") |
| 115 |
|
| 116 |
def rejects_strings_with_only_empty_lines(): |
| 117 |
_assert_non_printable("\n") |
| 118 |
_assert_non_printable("\n\n") |
| 119 |
_assert_non_printable("\n\n\n") |
| 120 |
_assert_non_printable(" \n \n") |
| 121 |
_assert_non_printable("\t\n\t\t\n") |
| 122 |
|
| 123 |
def rejects_strings_with_carriage_return(): |
| 124 |
_assert_non_printable("\r") |
| 125 |
_assert_non_printable("\n\r") |
| 126 |
_assert_non_printable("\r\n") |
| 127 |
_assert_non_printable("a\rb") |
| 128 |
|
| 129 |
def rejects_strings_with_leading_empty_lines(): |
| 130 |
_assert_non_printable("\na") |
| 131 |
_assert_non_printable(" \na") |
| 132 |
_assert_non_printable("\t\na") |
| 133 |
_assert_non_printable("\n\na") |
| 134 |
|
| 135 |
def rejects_strings_with_trailing_empty_lines(): |
| 136 |
_assert_non_printable("a\n") |
| 137 |
_assert_non_printable("a\n ") |
| 138 |
_assert_non_printable("a\n\t") |
| 139 |
_assert_non_printable("a\n\n") |
| 140 |
|
| 141 |
def can_check_lazy_stings(): |
| 142 |
class LazyString: |
| 143 |
def __init__(self, string: str) -> None: |
| 144 |
self.string = string |
| 145 |
|
| 146 |
def __str__(self) -> str: |
| 147 |
return self.string |
| 148 |
|
| 149 |
_assert_printable(cast(str, LazyString(""))) |
| 150 |
_assert_non_printable(cast(str, LazyString(" "))) |
| 151 |
|
| 152 |
|
| 153 |
def describe_print_block_string(): |
| 154 |
def _assert_block_string( |
| 155 |
s: str, readable: str, minimize: Optional[str] = None |
| 156 |
) -> None: |
| 157 |
assert print_block_string(s) == readable |
| 158 |
assert print_block_string(s, minimize=True) == minimize or readable |
| 159 |
|
| 160 |
def does_not_escape_characters(): |
| 161 |
s = '" \\ / \b \f \n \r \t' |
| 162 |
_assert_block_string(s, f'"""\n{s}\n"""', f'"""\n{s}"""') |
| 163 |
|
| 164 |
def by_default_print_block_strings_as_single_line(): |
| 165 |
s = "one liner" |
| 166 |
_assert_block_string(s, '"""one liner"""') |
| 167 |
|
| 168 |
def by_default_print_block_strings_ending_with_triple_quotation_as_multi_line(): |
| 169 |
s = 'triple quotation """' |
| 170 |
_assert_block_string( |
| 171 |
s, '"""\ntriple quotation \\"""\n"""', '"""triple quotation \\""""""' |
| 172 |
) |
| 173 |
|
| 174 |
def correctly_prints_single_line_with_leading_space(): |
| 175 |
s = " space-led string" |
| 176 |
_assert_block_string(s, '""" space-led string"""') |
| 177 |
|
| 178 |
def correctly_prints_single_line_with_leading_space_and_trailing_quotation(): |
| 179 |
s = ' space-led value "quoted string"' |
| 180 |
_assert_block_string(s, '""" space-led value "quoted string"\n"""') |
| 181 |
|
| 182 |
def correctly_prints_single_line_with_trailing_backslash(): |
| 183 |
s = "backslash \\" |
| 184 |
_assert_block_string(s, '"""\nbackslash \\\n"""', '"""\nbackslash \\\n"""') |
| 185 |
|
| 186 |
def correctly_prints_multi_line_with_internal_indent(): |
| 187 |
s = "no indent\n with indent" |
| 188 |
_assert_block_string( |
| 189 |
s, '"""\nno indent\n with indent\n"""', '"""\nno indent\n with indent"""' |
| 190 |
) |
| 191 |
|
| 192 |
def correctly_prints_string_with_a_first_line_indentation(): |
| 193 |
s = join_lines(" first ", " line ", "indentation", " string") |
| 194 |
|
| 195 |
_assert_block_string( |
| 196 |
s, |
| 197 |
join_lines( |
| 198 |
'"""', " first ", " line ", "indentation", " string", '"""' |
| 199 |
), |
| 200 |
join_lines( |
| 201 |
'""" first ', |
| 202 |
" line ", |
| 203 |
"indentation", |
| 204 |
' string"""', |
| 205 |
), |
| 206 |
) |
| 207 |
|
| 208 |
def correctly_prints_lazy_stings(): |
| 209 |
class LazyString: |
| 210 |
def __str__(self) -> str: |
| 211 |
return "lazy" |
| 212 |
|
| 213 |
_assert_block_string(cast(str, LazyString()), '"""lazy"""') |