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