|
7 | 7 |
|
8 | 8 | from build import ( |
9 | 9 | build, |
| 10 | + detect_source_type, |
| 11 | + extract_entries, |
10 | 12 | extract_github_repo, |
| 13 | + format_stars_short, |
11 | 14 | load_stars, |
12 | 15 | sort_entries, |
13 | 16 | ) |
14 | | -from readme_parser import slugify |
| 17 | +from readme_parser import parse_readme, slugify |
15 | 18 |
|
16 | 19 | # --------------------------------------------------------------------------- |
17 | 20 | # slugify |
@@ -324,3 +327,133 @@ def test_no_stars_sorted_alphabetically(self): |
324 | 327 | ] |
325 | 328 | result = sort_entries(entries) |
326 | 329 | assert [e["name"] for e in result] == ["apple", "zebra"] |
| 330 | + |
| 331 | + def test_builtin_between_starred_and_unstarred(self): |
| 332 | + entries = [ |
| 333 | + {"name": "builtin", "stars": None, "source_type": "Built-in"}, |
| 334 | + {"name": "starred", "stars": 100, "source_type": None}, |
| 335 | + {"name": "unstarred", "stars": None, "source_type": None}, |
| 336 | + ] |
| 337 | + result = sort_entries(entries) |
| 338 | + assert [e["name"] for e in result] == ["starred", "builtin", "unstarred"] |
| 339 | + |
| 340 | + |
| 341 | +# --------------------------------------------------------------------------- |
| 342 | +# detect_source_type |
| 343 | +# --------------------------------------------------------------------------- |
| 344 | + |
| 345 | + |
| 346 | +class TestDetectSourceType: |
| 347 | + def test_github_repo_returns_none(self): |
| 348 | + assert detect_source_type("https://github.com/psf/requests") is None |
| 349 | + |
| 350 | + def test_stdlib_url(self): |
| 351 | + assert detect_source_type("https://docs.python.org/3/library/asyncio.html") == "Built-in" |
| 352 | + |
| 353 | + def test_gitlab_url(self): |
| 354 | + assert detect_source_type("https://gitlab.com/org/repo") == "GitLab" |
| 355 | + |
| 356 | + def test_bitbucket_url(self): |
| 357 | + assert detect_source_type("https://bitbucket.org/org/repo") == "Bitbucket" |
| 358 | + |
| 359 | + def test_non_github_external(self): |
| 360 | + assert detect_source_type("https://example.com/tool") == "External" |
| 361 | + |
| 362 | + def test_github_non_repo_returns_none(self): |
| 363 | + assert detect_source_type("https://github.com/org/repo/wiki") is None |
| 364 | + |
| 365 | + |
| 366 | +# --------------------------------------------------------------------------- |
| 367 | +# format_stars_short |
| 368 | +# --------------------------------------------------------------------------- |
| 369 | + |
| 370 | + |
| 371 | +class TestFormatStarsShort: |
| 372 | + def test_under_1000(self): |
| 373 | + assert format_stars_short(500) == "500" |
| 374 | + |
| 375 | + def test_exactly_1000(self): |
| 376 | + assert format_stars_short(1000) == "1k" |
| 377 | + |
| 378 | + def test_large_number(self): |
| 379 | + assert format_stars_short(52000) == "52k" |
| 380 | + |
| 381 | + def test_zero(self): |
| 382 | + assert format_stars_short(0) == "0" |
| 383 | + |
| 384 | + |
| 385 | +# --------------------------------------------------------------------------- |
| 386 | +# extract_entries |
| 387 | +# --------------------------------------------------------------------------- |
| 388 | + |
| 389 | + |
| 390 | +class TestExtractEntries: |
| 391 | + def test_basic_extraction(self): |
| 392 | + readme = textwrap.dedent("""\ |
| 393 | + # T |
| 394 | +
|
| 395 | + --- |
| 396 | +
|
| 397 | + **Tools** |
| 398 | +
|
| 399 | + ## Widgets |
| 400 | +
|
| 401 | + - [widget](https://example.com) - A widget. |
| 402 | +
|
| 403 | + # Contributing |
| 404 | +
|
| 405 | + Done. |
| 406 | + """) |
| 407 | + groups = parse_readme(readme) |
| 408 | + categories = [c for g in groups for c in g["categories"]] |
| 409 | + entries = extract_entries(categories, groups) |
| 410 | + assert len(entries) == 1 |
| 411 | + assert entries[0]["name"] == "widget" |
| 412 | + assert entries[0]["categories"] == ["Widgets"] |
| 413 | + assert entries[0]["groups"] == ["Tools"] |
| 414 | + |
| 415 | + def test_duplicate_entry_merged(self): |
| 416 | + readme = textwrap.dedent("""\ |
| 417 | + # T |
| 418 | +
|
| 419 | + --- |
| 420 | +
|
| 421 | + **Tools** |
| 422 | +
|
| 423 | + ## Alpha |
| 424 | +
|
| 425 | + - [shared](https://example.com/shared) - Shared lib. |
| 426 | +
|
| 427 | + ## Beta |
| 428 | +
|
| 429 | + - [shared](https://example.com/shared) - Shared lib. |
| 430 | +
|
| 431 | + # Contributing |
| 432 | +
|
| 433 | + Done. |
| 434 | + """) |
| 435 | + groups = parse_readme(readme) |
| 436 | + categories = [c for g in groups for c in g["categories"]] |
| 437 | + entries = extract_entries(categories, groups) |
| 438 | + shared = [e for e in entries if e["name"] == "shared"] |
| 439 | + assert len(shared) == 1 |
| 440 | + assert sorted(shared[0]["categories"]) == ["Alpha", "Beta"] |
| 441 | + |
| 442 | + def test_source_type_detected(self): |
| 443 | + readme = textwrap.dedent("""\ |
| 444 | + # T |
| 445 | +
|
| 446 | + --- |
| 447 | +
|
| 448 | + ## Stdlib |
| 449 | +
|
| 450 | + - [asyncio](https://docs.python.org/3/library/asyncio.html) - Async I/O. |
| 451 | +
|
| 452 | + # Contributing |
| 453 | +
|
| 454 | + Done. |
| 455 | + """) |
| 456 | + groups = parse_readme(readme) |
| 457 | + categories = [c for g in groups for c in g["categories"]] |
| 458 | + entries = extract_entries(categories, groups) |
| 459 | + assert entries[0]["source_type"] == "Built-in" |
0 commit comments