|
12 | 12 | from aiohttp.test_utils import AioHTTPTestCase |
13 | 13 |
|
14 | 14 | import blackd |
| 15 | + import blackd.client |
15 | 16 | except ImportError as e: |
16 | 17 | raise RuntimeError("Please install Black with the 'd' extra") from e |
17 | 18 |
|
| 19 | +import black |
| 20 | + |
18 | 21 |
|
19 | 22 | @pytest.mark.blackd |
20 | 23 | class BlackDTestCase(AioHTTPTestCase): |
@@ -218,3 +221,114 @@ async def test_single_character(self) -> None: |
218 | 221 | response = await self.client.post("/", data="1") |
219 | 222 | self.assertEqual(await response.text(), "1\n") |
220 | 223 | self.assertEqual(response.status, 200) |
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.blackd |
| 227 | +class BlackDClientTestCase(AioHTTPTestCase): |
| 228 | + def tearDown(self) -> None: |
| 229 | + # Work around https://github.com/python/cpython/issues/124706 |
| 230 | + gc.collect() |
| 231 | + super().tearDown() |
| 232 | + |
| 233 | + async def get_application(self) -> web.Application: |
| 234 | + return blackd.make_app() |
| 235 | + |
| 236 | + async def test_unformatted_code(self) -> None: |
| 237 | + client = blackd.client.BlackDClient(self.client.make_url("/")) |
| 238 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 239 | + expected = 'def hello():\n print("Hello, World!")\n' |
| 240 | + formatted_code = await client.format_code(unformatted_code) |
| 241 | + |
| 242 | + self.assertEqual(formatted_code, expected) |
| 243 | + |
| 244 | + async def test_formatted_code(self) -> None: |
| 245 | + client = blackd.client.BlackDClient(self.client.make_url("/")) |
| 246 | + initial_code = 'def hello():\n print("Hello, World!")\n' |
| 247 | + expected = 'def hello():\n print("Hello, World!")\n' |
| 248 | + formatted_code = await client.format_code(initial_code) |
| 249 | + |
| 250 | + self.assertEqual(formatted_code, expected) |
| 251 | + |
| 252 | + async def test_line_length(self) -> None: |
| 253 | + client = blackd.client.BlackDClient(self.client.make_url("/"), line_length=10) |
| 254 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 255 | + expected = 'def hello():\n print(\n "Hello, World!"\n )\n' |
| 256 | + formatted_code = await client.format_code(unformatted_code) |
| 257 | + |
| 258 | + self.assertEqual(formatted_code, expected) |
| 259 | + |
| 260 | + async def test_skip_source_first_line(self) -> None: |
| 261 | + client = blackd.client.BlackDClient( |
| 262 | + self.client.make_url("/"), skip_source_first_line=True |
| 263 | + ) |
| 264 | + invalid_first_line = "Header will be skipped\r\ni = [1,2,3]\nj = [1,2,3]\n" |
| 265 | + expected_result = "Header will be skipped\r\ni = [1, 2, 3]\nj = [1, 2, 3]\n" |
| 266 | + formatted_code = await client.format_code(invalid_first_line) |
| 267 | + |
| 268 | + self.assertEqual(formatted_code, expected_result) |
| 269 | + |
| 270 | + async def test_skip_string_normalization(self) -> None: |
| 271 | + client = blackd.client.BlackDClient( |
| 272 | + self.client.make_url("/"), skip_string_normalization=True |
| 273 | + ) |
| 274 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 275 | + expected = "def hello():\n print('Hello, World!')\n" |
| 276 | + formatted_code = await client.format_code(unformatted_code) |
| 277 | + |
| 278 | + self.assertEqual(formatted_code, expected) |
| 279 | + |
| 280 | + async def test_skip_magic_trailing_comma(self) -> None: |
| 281 | + client = blackd.client.BlackDClient( |
| 282 | + self.client.make_url("/"), skip_magic_trailing_comma=True |
| 283 | + ) |
| 284 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 285 | + expected = 'def hello():\n print("Hello, World!")\n' |
| 286 | + formatted_code = await client.format_code(unformatted_code) |
| 287 | + |
| 288 | + self.assertEqual(formatted_code, expected) |
| 289 | + |
| 290 | + async def test_preview(self) -> None: |
| 291 | + client = blackd.client.BlackDClient(self.client.make_url("/"), preview=True) |
| 292 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 293 | + expected = 'def hello():\n print("Hello, World!")\n' |
| 294 | + formatted_code = await client.format_code(unformatted_code) |
| 295 | + |
| 296 | + self.assertEqual(formatted_code, expected) |
| 297 | + |
| 298 | + async def test_fast(self) -> None: |
| 299 | + client = blackd.client.BlackDClient(self.client.make_url("/"), fast=True) |
| 300 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 301 | + expected = 'def hello():\n print("Hello, World!")\n' |
| 302 | + formatted_code = await client.format_code(unformatted_code) |
| 303 | + |
| 304 | + self.assertEqual(formatted_code, expected) |
| 305 | + |
| 306 | + async def test_python_variant(self) -> None: |
| 307 | + client = blackd.client.BlackDClient( |
| 308 | + self.client.make_url("/"), python_variant="3.6" |
| 309 | + ) |
| 310 | + unformatted_code = "def hello(): print('Hello, World!')" |
| 311 | + expected = 'def hello():\n print("Hello, World!")\n' |
| 312 | + formatted_code = await client.format_code(unformatted_code) |
| 313 | + |
| 314 | + self.assertEqual(formatted_code, expected) |
| 315 | + |
| 316 | + async def test_diff(self) -> None: |
| 317 | + diff_header = re.compile( |
| 318 | + r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d" |
| 319 | + ) |
| 320 | + |
| 321 | + client = blackd.client.BlackDClient(self.client.make_url("/"), diff=True) |
| 322 | + source, _ = read_data("miscellaneous", "blackd_diff") |
| 323 | + expected, _ = read_data("miscellaneous", "blackd_diff.diff") |
| 324 | + |
| 325 | + diff = await client.format_code(source) |
| 326 | + diff = diff_header.sub(DETERMINISTIC_HEADER, diff) |
| 327 | + |
| 328 | + self.assertEqual(diff, expected) |
| 329 | + |
| 330 | + async def test_syntax_error(self) -> None: |
| 331 | + client = blackd.client.BlackDClient(self.client.make_url("/")) |
| 332 | + with_syntax_error = "def hello(): a 'Hello, World!'" |
| 333 | + with self.assertRaises(black.InvalidInput): |
| 334 | + _ = await client.format_code(with_syntax_error) |
0 commit comments