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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
|
from ford.fortran_project import Project, find_all_files
from ford.settings import ProjectSettings, ExtraFileType
from ford.utils import normalise_path
from ford.sourceform import (
FortranVariable,
FortranProgram,
FortranSubroutine,
FortranModule,
NameSelector,
)
from ford._markdown import MetaMarkdown
import ford.sourceform
from itertools import chain
import pytest
from bs4 import BeautifulSoup
@pytest.fixture
def copy_fortran_file(tmp_path):
# Reset unique name tracker
setattr(ford.sourceform, "namelist", NameSelector())
def copy_file(data) -> ProjectSettings:
src_dir = tmp_path / "src"
src_dir.mkdir()
filename = src_dir / "test.f90"
with open(filename, "w") as f:
f.write(data)
return ProjectSettings(src_dir=src_dir)
return copy_file
def create_project(settings: ProjectSettings):
project = Project(settings)
project.correlate()
return project
def test_use_without_rename(copy_fortran_file):
data = """\
module module1
use module2, only: routine_1
implicit none
private
interface routine
module procedure :: routine_1
end interface routine
public :: routine
end module module1
module module2
implicit none
private
public :: routine_1
contains
subroutine routine_1(i)
integer,intent(in) :: i
write(*,*) i
end subroutine routine_1
end module module2
"""
settings = copy_fortran_file(data)
project = create_project(settings)
assert set(project.modules[0].all_procs.keys()) == {"routine_1", "routine"}
def test_use_and_rename(copy_fortran_file):
data = """\
module module1
use module2, only: routine_1 => routine
implicit none
private
interface routine
module procedure :: routine_1
end interface routine
public :: routine
end module module1
module module2
implicit none
private
public :: routine
contains
subroutine routine(i)
integer,intent(in) :: i
write(*,*) i
end subroutine routine
end module module2
"""
settings = copy_fortran_file(data)
project = create_project(settings)
assert set(project.modules[0].all_procs.keys()) == {"routine_1", "routine"}
def test_module_use_only_everything(copy_fortran_file):
data = """\
module default_access
! No access keyword
integer :: int_public, int_private
private :: int_private
real :: real_public
real, private :: real_private
type :: type_public
complex :: component_public
complex, private :: component_private
end type type_public
type :: type_private
character(len=1) :: string_public
character(len=1), private :: string_private
end type type_private
type type_public_no_attr
end type type_public_no_attr
type, public :: type_public_attr
end type type_public_attr
type, private :: type_private_attr
end type type_private_attr
private :: sub_private, func_private, type_private
contains
subroutine sub_public
end subroutine sub_public
subroutine sub_private
end subroutine sub_private
integer function func_public()
end function func_public
integer function func_private()
end function func_private
end module default_access
module use_only_everything
use default_access, only : int_public, real_public, type_public, sub_public, func_public
end module use_only_everything
"""
settings = copy_fortran_file(data)
project = create_project(settings)
# Double-check we're looking at the right module
assert project.modules[1].name == "use_only_everything"
assert set(project.modules[1].all_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[1].pub_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[1].all_types.keys()) == {
"type_public",
}
assert set(project.modules[1].pub_types.keys()) == {
"type_public",
}
assert set(project.modules[1].all_vars.keys()) == {
"int_public",
"real_public",
}
assert set(project.modules[1].pub_vars.keys()) == {
"int_public",
"real_public",
}
def test_module_use_only_everything_change_access(copy_fortran_file):
data = """\
module default_access
! No access keyword
integer :: int_public, int_private
private :: int_private
real :: real_public
real, private :: real_private
type :: type_public
complex :: component_public
complex, private :: component_private
end type type_public
type :: type_private
character(len=1) :: string_public
character(len=1), private :: string_private
end type type_private
type type_public_no_attr
end type type_public_no_attr
type, public :: type_public_attr
end type type_public_attr
type, private :: type_private_attr
end type type_private_attr
private :: sub_private, func_private, type_private
contains
subroutine sub_public
end subroutine sub_public
subroutine sub_private
end subroutine sub_private
integer function func_public()
end function func_public
integer function func_private()
end function func_private
end module default_access
module use_only_change_access
use default_access, only : int_public, real_public, type_public, sub_public, func_public
private
public :: int_public, sub_public
end module use_only_change_access
"""
settings = copy_fortran_file(data)
project = create_project(settings)
# Double-check we're looking at the right module
assert project.modules[1].name == "use_only_change_access"
assert set(project.modules[1].all_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[1].pub_procs.keys()) == {
"sub_public",
}
assert set(project.modules[1].all_types.keys()) == {
"type_public",
}
assert set(project.modules[1].pub_types.keys()) == set()
assert set(project.modules[1].all_vars.keys()) == {
"int_public",
"real_public",
}
assert set(project.modules[1].pub_vars.keys()) == {
"int_public",
}
def test_module_use_everything(copy_fortran_file):
data = """\
module default_access
! No access keyword
integer :: int_public, int_private
private :: int_private
real :: real_public
real, private :: real_private
type :: type_public
complex :: component_public
complex, private :: component_private
end type type_public
type :: type_private
character(len=1) :: string_public
character(len=1), private :: string_private
end type type_private
type type_no_attr
end type type_no_attr
type, public :: type_public_attr
end type type_public_attr
type, private :: type_private_attr
end type type_private_attr
private :: sub_private, func_private, type_private
contains
subroutine sub_public
end subroutine sub_public
subroutine sub_private
end subroutine sub_private
integer function func_public()
end function func_public
integer function func_private()
end function func_private
end module default_access
module use_everything
use default_access
end module use_everything
"""
settings = copy_fortran_file(data)
project = create_project(settings)
# Double-check we're looking at the right module
assert project.modules[1].name == "use_everything"
assert set(project.modules[1].all_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[1].pub_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[1].all_types.keys()) == {
"type_public",
"type_no_attr",
"type_public_attr",
}
assert set(project.modules[1].pub_types.keys()) == {
"type_public",
"type_no_attr",
"type_public_attr",
}
assert set(project.modules[1].all_vars.keys()) == {
"int_public",
"real_public",
}
assert set(project.modules[1].pub_vars.keys()) == {
"int_public",
"real_public",
}
def test_module_use_everything_reexport(copy_fortran_file):
data = """\
module default_access
! No access keyword
integer :: int_public, int_private
private :: int_private
real :: real_public
real, private :: real_private
type :: type_public
complex :: component_public
complex, private :: component_private
end type type_public
type :: type_private
character(len=1) :: string_public
character(len=1), private :: string_private
end type type_private
type type_no_attr
end type type_no_attr
type, public :: type_public_attr
end type type_public_attr
type, private :: type_private_attr
end type type_private_attr
private :: sub_private, func_private, type_private
contains
subroutine sub_public
end subroutine sub_public
subroutine sub_private
end subroutine sub_private
integer function func_public()
end function func_public
integer function func_private()
end function func_private
end module default_access
module use_everything
use default_access
end module use_everything
module reexport
use use_everything
end module reexport
"""
settings = copy_fortran_file(data)
project = create_project(settings)
# Double-check we're looking at the right module
assert project.modules[2].name == "reexport"
assert set(project.modules[2].all_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[2].pub_procs.keys()) == {
"sub_public",
"func_public",
}
assert set(project.modules[2].all_types.keys()) == {
"type_public",
"type_no_attr",
"type_public_attr",
}
assert set(project.modules[2].pub_types.keys()) == {
"type_public",
"type_no_attr",
"type_public_attr",
}
assert set(project.modules[2].all_vars.keys()) == {
"int_public",
"real_public",
}
assert set(project.modules[2].pub_vars.keys()) == {
"int_public",
"real_public",
}
def test_use_within_interface(copy_fortran_file):
data = """\
module module1
implicit none
private
interface
subroutine routine_1()
use module2, only: routine_2
end subroutine routine_1
end interface
public :: routine
end module module1
module module2
implicit none
private
public :: routine_2
contains
subroutine routine_2(i)
integer,intent(in) :: i
write(*,*) i
end subroutine routine_2
end module module2
"""
settings = copy_fortran_file(data)
project = create_project(settings)
assert "routine_2" in project.modules[0].all_procs["routine_1"].procedure.all_procs
def test_member_in_other_module(copy_fortran_file):
data = """\
module module1
use module2, only: testInterface
type, public, abstract :: external_type
contains
procedure(testInterface), nopass, deferred :: testProc
end type external_type
end module module1
module module2
type, public, abstract :: internal_type
contains
procedure(testInterface), nopass, deferred :: testProc
end type internal_type
abstract interface
pure function testInterface(input) result(output)
real, intent(in) :: input
real :: output
end function testInterface
end interface
end module module2
"""
settings = copy_fortran_file(data)
project = create_project(settings)
module1 = project.modules[0]
module2 = project.modules[1]
assert (
module1.all_types["external_type"].boundprocs[0].proto
is module2.all_types["internal_type"].boundprocs[0].proto
)
assert (
module2.all_types["internal_type"].boundprocs[0].proto.name == "testInterface"
)
def test_display_derived_types(copy_fortran_file):
"""Check that we can set visibility attributes on types, #388"""
data = """\
module foo
type no_attrs
end type no_attrs
type, public :: public_attr
end type public_attr
type, private :: private_attr
end type private_attr
end module foo
"""
settings = copy_fortran_file(data)
settings.display = ["public"]
project = Project(settings)
project.correlate()
type_names = set([t.name for t in project.types])
assert type_names == {"no_attrs", "public_attr"}
def test_display_derived_types_default_private(copy_fortran_file):
"""Check that we can set visibility attributes on types, #388"""
data = """\
module foo
private
type no_attrs
end type no_attrs
type, public :: public_attr
end type public_attr
type, private :: private_attr
end type private_attr
end module foo
"""
settings = copy_fortran_file(data)
settings.display = ["public"]
project = create_project(settings)
type_names = set([t.name for t in project.types])
assert type_names == {"public_attr"}
def test_display_private_derived_types(copy_fortran_file):
"""Check that we can set visibility attributes on types, #388"""
data = """\
module foo
private
type no_attrs
end type no_attrs
type, public :: public_attr
end type public_attr
type, private :: private_attr
end type private_attr
end module foo
"""
settings = copy_fortran_file(data)
settings.display = ["public", "private"]
project = create_project(settings)
type_names = set([t.name for t in project.types])
assert type_names == {"no_attrs", "public_attr", "private_attr"}
def test_display_private_module_procedure(copy_fortran_file):
data = """\
module foo
interface
module subroutine bar()
end subroutine bar
end interface
end module foo
submodule (foo) baz
contains
module procedure bar
end procedure
end submodule baz
"""
settings = copy_fortran_file(data)
settings.display = ["public", "protected", "private"]
project = create_project(settings)
assert project.submodprocedures[0].name == "bar"
def test_interface_type_name(copy_fortran_file):
"""Check for shared prototype list"""
data = """\
module foo
type name_t
end type name_t
type(name_t) :: var, var2
end module foo
"""
settings = copy_fortran_file(data)
project = create_project(settings)
proto_names = [var.proto[0].name for var in project.modules[0].variables]
assert proto_names == ["name_t", "name_t"]
def test_imported_interface_type_name(copy_fortran_file):
"""Check for shared prototype list"""
data = """\
module foo
type name_t
end type name_t
end module foo
module bar
use foo
type(name_t), allocatable :: var(:), var2(:)
end module bar
"""
settings = copy_fortran_file(data)
project = create_project(settings)
proto_names = [var.proto[0].name for var in project.modules[1].variables]
assert proto_names == ["name_t", "name_t"]
def test_abstract_interface_type_name(copy_fortran_file):
"""Check for shared prototype list"""
data = """\
module foo
abstract interface
subroutine hello
end subroutine hello
end interface
procedure(hello) :: proc1, proc2
end module foo
"""
settings = copy_fortran_file(data)
project = create_project(settings)
proto_names = [var.proto[0].name for var in project.modules[0].variables]
assert proto_names == ["hello", "hello"]
def test_imported_abstract_interface_type_name(copy_fortran_file):
"""Check for shared prototype list"""
data = """\
module foo
abstract interface
subroutine hello
end subroutine hello
end interface
contains
integer function goodbye()
goodbye = 1
end function goodbye
end module foo
module bar
use foo
procedure(hello) :: proc1, proc2
procedure(goodbye) :: proc3, proc4
end module bar
"""
settings = copy_fortran_file(data)
project = create_project(settings)
proto_names = [var.proto[0].name for var in project.modules[1].variables]
assert proto_names == ["hello", "hello", "goodbye", "goodbye"]
def test_generic_interface_inherited_attrs(copy_fortran_file):
data = """\
module module1
implicit none
interface routine
module subroutine routine_1(var)
integer, dimension(:) :: var
end subroutine routine_1
end interface
public :: routine
end module module1
submodule(module1) module2
implicit none
contains
module procedure routine_1
end procedure
end submodule module2
"""
settings = copy_fortran_file(data)
project = create_project(settings)
module_implementation = project.modules[0].all_procs["routine_1"].module
# Check that the module implementation has been linked
assert not isinstance(module_implementation, bool)
# Check that mod proc has the inherited arg
assert isinstance(module_implementation.args[0], FortranVariable)
def test_display_internal_procedures(copy_fortran_file):
"""_very_ basic test of 'proc_internals' setting"""
data = """\
subroutine no_display_internals
!! some docs
integer :: local_variable
!! This shouldn't be displayed
end subroutine no_display_internals
subroutine display_internals
!! proc_internals: true
!!
!! some docs
integer :: local_variable
!! This shouldn't be displayed
end subroutine display_internals
"""
settings = copy_fortran_file(data)
project = create_project(settings)
subroutine1 = project.procedures[0]
subroutine2 = project.procedures[1]
assert subroutine1.variables == []
assert len(subroutine2.variables) == 1
assert subroutine2.variables[0].name == "local_variable"
def test_exclude_dir(tmp_path):
exclude_dir = tmp_path / "sub1" / "sub2"
exclude_dir.mkdir(parents=True)
src = tmp_path / "src"
src.mkdir()
with open(src / "include.f90", "w") as f:
f.write("program foo\nend program")
with open(exclude_dir / "exclude.f90", "w") as f:
f.write("program bar\nend program")
settings = ProjectSettings(
src_dir=tmp_path, exclude_dir=normalise_path(tmp_path, "sub1")
)
project = Project(settings)
program_names = {program.name for program in project.programs}
assert program_names == {"foo"}
def test_exclude(tmp_path):
exclude_dir = tmp_path / "sub1" / "sub2"
exclude_dir.mkdir(parents=True)
src = tmp_path / "src"
src.mkdir()
with open(src / "include.f90", "w") as f:
f.write("program foo\nend program")
with open(exclude_dir / "exclude.f90", "w") as f:
f.write("program bar\nend program")
settings = ProjectSettings(src_dir=tmp_path, exclude="exclude.f90")
project = Project((settings))
program_names = {program.name for program in project.programs}
assert program_names == {"foo"}
def test_find_all_files(tmp_path):
src = tmp_path / "src"
src.mkdir(parents=True)
sub1 = src / "sub1"
sub1.mkdir()
sub2 = src / "sub2"
sub2.mkdir()
(src / "file1.f90").touch()
(src / "file2.F90").touch()
(sub1 / "file3.f").touch()
(sub1 / "not_this.f90").touch()
(sub2 / "file4.c").touch()
(sub2 / "not_this.f90").touch()
exclude_dir = src / "bad_dir"
exclude_dir.mkdir()
exclude_sub = exclude_dir / "sub1"
exclude_sub.mkdir()
(exclude_dir / "file5.f90").touch()
(exclude_sub / "file6.f90").touch()
src2 = tmp_path / "src2"
src2.mkdir()
(src2 / "file7.f90").touch()
settings = ProjectSettings(
exclude=["src/sub1/not_this.f90"],
exclude_dir=["src/bad_dir"],
src_dir=[src, src2],
extra_filetypes={"c": ExtraFileType("c", "//")},
)
settings.normalise_paths(tmp_path)
files = sorted(find_all_files(settings))
expected_files = sorted(
[
tmp_path / f
for f in (
"src/file1.f90",
"src/file2.F90",
"src/sub1/file3.f",
"src/sub2/file4.c",
"src/sub2/not_this.f90",
"src2/file7.f90",
)
]
)
assert files == expected_files
@pytest.mark.parametrize(
"sort_kind, expected_order",
[
(
"src",
{
"variables": ["b", "a", "d", "f", "c", "e", "g", "h", "j", "i"],
"types": ["bet", "alef", "gimel"],
"subroutines": ["bravo", "alpha", "charlie"],
"functions": ["beaver", "aardvark", "cat"],
"absinterfaces": ["apple", "carrot", "banana"],
},
),
(
"alpha",
{
"variables": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
"types": ["alef", "bet", "gimel"],
"subroutines": ["alpha", "bravo", "charlie"],
"functions": ["aardvark", "beaver", "cat"],
"absinterfaces": ["apple", "banana", "carrot"],
},
),
(
"permission",
{
"variables": ["d", "f", "c", "e", "b", "a", "g", "h", "j", "i"],
"types": ["gimel", "alef", "bet"],
"subroutines": ["charlie", "alpha", "bravo"],
"functions": ["cat", "aardvark", "beaver"],
"absinterfaces": ["apple", "carrot", "banana"],
},
),
(
"permission-alpha",
{
"variables": ["d", "f", "c", "e", "a", "b", "g", "h", "i", "j"],
"types": ["gimel", "alef", "bet"],
"subroutines": ["charlie", "alpha", "bravo"],
"functions": ["cat", "aardvark", "beaver"],
"absinterfaces": ["apple", "banana", "carrot"],
},
),
(
"type",
{
"variables": ["h", "g", "j", "i", "b", "a", "c", "e", "d", "f"],
"types": ["bet", "alef", "gimel"],
"subroutines": ["bravo", "alpha", "charlie"],
"functions": ["cat", "beaver", "aardvark"],
"absinterfaces": ["apple", "carrot", "banana"],
},
),
(
"type-alpha",
{
"variables": ["h", "g", "i", "j", "a", "b", "c", "e", "d", "f"],
"types": ["alef", "bet", "gimel"],
"subroutines": ["alpha", "bravo", "charlie"],
"functions": ["cat", "aardvark", "beaver"],
"absinterfaces": ["apple", "banana", "carrot"],
},
),
],
)
def test_sort(copy_fortran_file, sort_kind, expected_order):
data = """\
module mod_a
type :: bet
end type bet
type :: alef
end type alef
type :: gimel
end type gimel
integer, private :: b, a
type(gimel), public :: d, f
type(alef), protected :: c, e
character(len=2, kind=4) :: g
character(len=1, kind=4) :: h
character(len=1, kind=8) :: j, i
private :: g, h, i, j
abstract interface
subroutine banana
end subroutine banana
function apple()
import gimel
class(gimel), allocatable :: apple
end function apple
type(alef) function carrot()
import alef
end function carrot
end interface
public :: gimel, charlie, cat
protected :: alef, alpha, aardvark
private :: bet, bravo, beaver
contains
subroutine bravo
end subroutine bravo
subroutine alpha
end subroutine alpha
subroutine charlie
end subroutine charlie
type(gimel) function beaver()
end function beaver
function aardvark()
class(gimel), allocatable :: aardvark
end function aardvark
type(alef) function cat()
end function cat
end module mod_a
"""
settings = copy_fortran_file(data)
settings.sort = sort_kind
settings.display = ["public", "private", "protected"]
project = create_project(settings)
module = project.modules[0]
for entity_type in [
"variables",
"types",
"subroutines",
"functions",
"absinterfaces",
]:
entity_names = [entity.name for entity in getattr(module, entity_type)]
assert entity_names == expected_order[entity_type], (sort_kind, entity_type)
def test_uses(copy_fortran_file):
"""Check that module `USE`s are matched up correctly"""
data = """\
module mod_a
end module mod_a
module mod_b
use mod_a
end module mod_b
module mod_c
use mod_a
use mod_b
end module mod_c
module mod_d
use external_module
end module mod_d
"""
settings = copy_fortran_file(data)
settings.extra_mods = {"external_module": "http://example.com"}
project = create_project(settings)
mod_a = project.modules[0]
mod_b = project.modules[1]
mod_c = project.modules[2]
mod_d = project.modules[3]
assert mod_a.uses == set()
assert mod_b.uses == {mod_a}
assert mod_c.uses == {mod_a, mod_b}
for link in mod_d.uses:
assert link.get_url() == "http://example.com"
def test_submodule_uses(copy_fortran_file):
"""Check that module `USE`s are matched up correctly"""
data = """\
module mod_a
end module mod_a
submodule (mod_a) mod_b
end submodule mod_b
submodule (mod_a) mod_c
end submodule mod_c
submodule (mod_a:mod_c) mod_d
end submodule mod_d
"""
settings = copy_fortran_file(data)
project = create_project(settings)
mod_a = project.modules[0]
mod_b = project.submodules[0]
mod_c = project.submodules[1]
mod_d = project.submodules[2]
assert mod_a.uses == set()
assert mod_b.parent_submodule is None
assert mod_b.ancestor_module == mod_a
assert mod_c.parent_submodule is None
assert mod_c.ancestor_module == mod_a
assert mod_d.parent_submodule == mod_c
assert mod_d.ancestor_module == mod_a
def test_make_links(copy_fortran_file):
links = "[[a]] [[b(type)]] [[b:c]] [[a:d]] [[b:e]] [[F(proc)]] [[A:G]] [[H]] [[I]]"
data = f"""\
module a !! {links}
type b !! {links}
integer :: c !! {links}
contains
final :: d !! {links}
procedure :: e !! {links}
end type b
interface b !! {links}
module procedure f
end interface b
type(b) :: g !! {links}
contains
subroutine d(self) !! {links}
type(b) :: self !! {links}
end subroutine d
subroutine e(self) !! {links}
class(b) :: self !! {links}
end subroutine e
function f() !! {links}
type(b) :: f !! {links}
end function f
end module a
program h !! {links}
end program h
submodule (a) i !! {links}
end submodule i
"""
settings = copy_fortran_file(data)
project = create_project(settings)
md = MetaMarkdown(project=project)
project.markdown(md)
expected_links = {
"a": "../module/a.html",
"b": "../type/b.html",
"c": "../type/b.html#variable-c",
"d": "../proc/d.html",
"e": "../type/b.html#boundprocedure-e",
"f": "../proc/f.html",
"g": "../module/a.html#variable-g",
"h": "../program/h.html",
"i": "../module/i.html",
}
for item in chain(
project.files[0].children,
project.types[0].children,
project.procedures[0].children,
project.procedures[2].children,
):
docstring = BeautifulSoup(item.doc, features="html.parser")
link_locations = {a.string: a.get("href", None) for a in docstring("a")}
assert link_locations == expected_links, (item, item.name)
def test_make_links_with_entity_spec(copy_fortran_file):
links = (
"[[a(module)]] [[b(type)]] [[b(type):c(variable)]] "
"[[A(MODULE):D(SUBROUTINE)]] [[B(TYPE):E]] [[F(PROC)]] "
"[[A(module):G(variable)]] [[H(program)]] [[I(SUBMODULE)]]"
)
data = f"""\
module a !! {links}
type b !! {links}
integer :: c !! {links}
contains
final :: d !! {links}
procedure :: e !! {links}
end type b
interface b !! {links}
module procedure f
end interface b
type(b) :: g !! {links}
contains
subroutine d(self) !! {links}
type(b) :: self !! {links}
end subroutine d
subroutine e(self) !! {links}
class(b) :: self !! {links}
end subroutine e
function f() !! {links}
type(b) :: f !! {links}
end function f
end module a
program h !! {links}
end program h
submodule (a) i !! {links}
end submodule i
"""
settings = copy_fortran_file(data)
project = create_project(settings)
md = MetaMarkdown(project=project)
project.markdown(md)
expected_links = {
"a": "../module/a.html",
"b": "../type/b.html",
"c": "../type/b.html#variable-c",
"d": "../proc/d.html",
"e": "../type/b.html#boundprocedure-e",
"f": "../proc/f.html",
"g": "../module/a.html#variable-g",
"h": "../program/h.html",
"i": "../module/i.html",
}
for item in chain(
project.files[0].children,
project.types[0].children,
project.procedures[0].children,
project.procedures[2].children,
):
docstring = BeautifulSoup(item.doc, features="html.parser")
link_locations = {a.string: a.get("href", None) for a in docstring("a")}
assert link_locations == expected_links, (item, item.name)
def test_make_links_with_context(copy_fortran_file):
data = """\
module a !! [[g]]
type b !! [[c]] [[e]] [[g]]
integer :: c
contains
procedure :: e
end type b
type(b) :: g
contains
subroutine d(i) !! [[I]] [[F]] [[F:X]]
type(b) :: i
contains
integer function f(x)
integer :: x
end function f
end subroutine d
end module a
program h
end program h
"""
settings = copy_fortran_file(data)
settings.proc_internals = True
project = create_project(settings)
md = MetaMarkdown(project=project)
project.markdown(md)
expected_links = {
"a": "../module/a.html",
"b": "../type/b.html",
"c": "../type/b.html#variable-c",
"d": "../proc/d.html",
"e": "../type/b.html#boundprocedure-e",
"f": "../proc/d.html#proc-f",
"g": "../module/a.html#variable-g",
"h": "../program/h.html",
"i": "../proc/d.html#variable-i",
"x": "../proc/d.html#variable-x",
}
for item in chain(
project.files[0].children,
project.modules[0].children,
project.types[0].children,
project.procedures[0].children,
):
docstring = BeautifulSoup(item.doc, features="html.parser")
for link in docstring("a"):
assert link.string in expected_links, (item, item.name, link)
expected_link = expected_links[link.string]
assert link.get("href", None) == expected_link, (
item,
item.name,
link.string,
)
def test_submodule_procedure_issue_446(copy_fortran_file):
data = """\
module foo_mod
interface
module subroutine quaxx
end subroutine quaxx
end interface
end module foo_mod
submodule (foo_mod) bar_submod
contains
module procedure quaxx
print*, "implementation"
end procedure
end submodule bar_submod
"""
settings = copy_fortran_file(data)
settings.display = ["public", "private", "protected"]
project = create_project(settings)
module = project.modules[0]
interface = module.interfaces[0]
assert interface.name == "quaxx"
submodproc = module.descendants[0].modprocedures[0]
assert interface.procedure.module == submodproc
def test_find_namelists(copy_fortran_file):
data = """\
module mod1
contains
subroutine sub1()
namelist /namelist_b/ var_a
end subroutine sub1
end module mod1
program prog
integer :: var_b
contains
subroutine sub(var_b)
integer :: var_d, var_a, var_b
character(len=*), intent(in) :: string
namelist /namelist_a/ var_a, var_b, var_c, var_d
read(string, nml=namelist_a)
write(*, *) "Read in:"
write(*, nml=namelist_a)
end subroutine sub
end program prog
"""
settings = copy_fortran_file(data)
project = create_project(settings)
assert len(project.namelists) == 2
namelist_names = sorted((namelist.name for namelist in project.namelists))
expected_names = sorted(("namelist_a", "namelist_b"))
assert namelist_names == expected_names
def test_find(copy_fortran_file):
data = """\
module foo
type :: test_type
integer :: sub
end type test_type
contains
subroutine sub
end subroutine sub
end module
program foo
end program
"""
settings = copy_fortran_file(data)
settings.display = ["public", "private"]
project = create_project(settings)
test_type = project.find("test_type")
assert test_type.name == "test_type"
subroutine_sub = project.find("sub")
assert subroutine_sub.name == "sub"
assert isinstance(subroutine_sub, FortranSubroutine)
component_sub = project.find("test_type", child_name="sub")
assert component_sub.name == "sub"
assert isinstance(component_sub, FortranVariable)
foo = project.find("foo")
assert foo
prog_foo = project.find("foo", entity="program")
assert isinstance(prog_foo, FortranProgram)
mod_foo = project.find("foo", entity="module")
assert isinstance(mod_foo, FortranModule)
def test_links_in_deferred_bound_methods(copy_fortran_file):
"""Test for issue #591"""
data = """\
module test
type, abstract :: foo_t
contains
!> See [[foo_t]] for more information about [[foo]]
procedure(foo_iface), deferred :: foo
end type
abstract interface
subroutine foo_iface(self)
import :: foo_t
class(foo_t), intent(in) :: self
end subroutine
end interface
end module
"""
settings = copy_fortran_file(data)
project = create_project(settings)
md = MetaMarkdown(settings.md_base_dir, project=project)
project.markdown(md)
docstring = BeautifulSoup(
project.modules[0].types[0].boundprocs[0].doc, features="html.parser"
)
links = {link.text: link["href"] for link in docstring.find_all("a")}
assert links["foo_t"].endswith("foo_t.html")
assert links["foo"].endswith("foo_t.html#boundprocedure-foo")
def test_hide_undoc(copy_fortran_file):
data = """\
module foo
contains
!> A subroutine with a docstring
subroutine with_doc
end subroutine with_doc
subroutine no_doc
end subroutine no_doc
end module
"""
settings = copy_fortran_file(data)
settings.hide_undoc = True
project = create_project(settings)
assert len(project.modules[0].subroutines) == 1
|