Skip to content

Commit 2eea9cf

Browse files
committed
add fortran tests
1 parent 4fb523f commit 2eea9cf

4 files changed

Lines changed: 175 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from somesy.package_json.writer import PackageJSON
1111
from somesy.pyproject import Pyproject
1212
from somesy.julia import Julia
13+
from somesy.fortran import Fortran
1314

1415

1516
class FileTypes(Enum):
@@ -19,6 +20,7 @@ class FileTypes(Enum):
1920
SOMESY = "somesy"
2021
PACKAGE_JSON = "package_json"
2122
JULIA = "julia"
23+
FORTRAN = "fortran"
2224

2325

2426
@pytest.fixture(scope="session", autouse=True)
@@ -72,6 +74,8 @@ def _create_files(files: Set[Tuple[FileTypes, str]]):
7274
read_file_name = read_file_path / Path("package.json")
7375
elif file_type == FileTypes.JULIA:
7476
read_file_name = read_file_path / Path("Project.toml")
77+
elif file_type == FileTypes.FORTRAN:
78+
read_file_name = read_file_path / Path("fpm.toml")
7579

7680
with open(read_file_name, "r") as f:
7781
content = f.read()
@@ -120,6 +124,9 @@ def _load_files(files: Set[FileTypes]):
120124
elif file_type == FileTypes.JULIA:
121125
read_file_name = read_file_name / Path("Project.toml")
122126
file_instances[file_type] = Julia(read_file_name)
127+
elif file_type == FileTypes.FORTRAN:
128+
read_file_name = read_file_name / Path("fpm.toml")
129+
file_instances[file_type] = Fortran(read_file_name)
123130

124131
return file_instances
125132

tests/data/fpm.toml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name = "test-package"
2+
version = "0.1.0"
3+
description = "This is a test package for demonstration purposes."
4+
license = "MIT"
5+
author = "John Doe <john.doe@example.com>"
6+
copyright = "2020 John Doe"
7+
categories = ["Command Line"]
8+
keywords = ["test", "demo", "example"]
9+
homepage = "https://example.com/test-package"
10+
11+
# unit test program
12+
test = [
13+
{ name="test_func", source-dir="test", main="test_func.f90" }
14+
]
15+
16+
[install]
17+
library=true
18+
19+
[library]
20+
source-dir="src"
21+
22+
# project metadata
23+
[tool.somesy.project]
24+
name = "testproject"
25+
version = "1.0.0"
26+
description = "This is a test project for demonstration purposes."
27+
keywords = ["test", "demo", "example"]
28+
license = "MIT"
29+
repository = "https://github.com/example/testproject"
30+
homepage = "https://example.com/testproject"
31+
32+
[[tool.somesy.project.people]]
33+
family-names = "Doe"
34+
given-names = "John"
35+
email = "john.doe@example.com"
36+
orcid = "https://orcid.org/0000-0000-0000-0000"
37+
38+
contribution = "The main developer, maintainer, and tester."
39+
contribution_begin = "2023-01-15"
40+
contribution_types = ["maintenance", "code", "test", "review", "doc"]
41+
42+
author = true
43+
maintainer = true
44+
publication_author = true
45+
46+
[[tool.somesy.project.people]]
47+
family-names = "Doe"
48+
given-names = "Jane"
49+
email = "jane.doe@example.com"
50+
orcid = "https://orcid.org/0000-0000-0000-0001"
51+
52+
contribution = "Initial concepts, tool development and enhancement, documentation."
53+
contribution_begin = "2023-02-20"
54+
contribution_types = ["ideas", "code", "test", "review", "doc"]
55+
56+
author = true
57+
58+
[[tool.somesy.project.people]]
59+
family-names = "Doe"
60+
given-names = "Michael"
61+
email = "michael.doe@example.com"
62+
orcid = "https://orcid.org/0000-0000-0000-0002"
63+
64+
contribution = "Valuable input concerning metadata standards and usability."
65+
contribution_begin = "2023-03-10"
66+
contribution_types = ["ideas"]
67+
publication_author = true
68+
69+
[tool.somesy.config]
70+
no_sync_cff = false
71+
cff_file = "CITATION.cff"
72+
no_sync_pyproject = true
73+
no_sync_codemeta = false
74+
codemeta_file = "codemeta.json"
75+
no_sync_package_json = true
76+
no_sync_julia = true
77+
no_sync_fortran = false
78+
fortran_file = "fpm.toml"
79+
show_info = false
80+
verbose = false
81+
debug = false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from tomlkit import dump
3+
4+
from somesy.fortran import Fortran
5+
6+
7+
def test_fortran_validate_accept(load_files, file_types):
8+
"""Validate by loading the data fpm.toml file using the fixture."""
9+
load_files([file_types.FORTRAN])
10+
11+
12+
def test_fortran_validate(tmp_path):
13+
"""Test validating a fpm.toml file."""
14+
15+
# create a fpm.toml file but with a invalid values
16+
reject_fortran_object = {
17+
"name": "12somesy",
18+
"version": "abc",
19+
"authors": ["John Doe <"],
20+
}
21+
22+
invalid_fortran_path = tmp_path / "fpm.toml"
23+
with open(invalid_fortran_path, "w+") as f:
24+
dump(reject_fortran_object, f)
25+
26+
with pytest.raises(ValueError):
27+
Fortran(invalid_fortran_path)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from somesy.core.models import LicenseEnum, Person, ProjectMetadata
6+
from somesy.fortran.writer import Fortran
7+
8+
9+
@pytest.fixture
10+
def fortran(load_files, file_types):
11+
files = load_files([file_types.FORTRAN])
12+
return files[file_types.FORTRAN]
13+
14+
15+
@pytest.fixture
16+
def fortran_file(create_files, file_types):
17+
folder = create_files([(file_types.FORTRAN, "fpm.toml")])
18+
return folder / Path("fpm.toml")
19+
20+
21+
def test_content_match(fortran):
22+
assert fortran.name == "test-package"
23+
assert len(fortran.authors) == 1
24+
25+
26+
def test_sync(fortran, somesy_input):
27+
# with 'full name <email>' format
28+
fortran.sync(somesy_input.project)
29+
assert fortran.name == "testproject"
30+
assert fortran.version == "1.0.0"
31+
assert fortran.authors[0] == "John Doe <john.doe@example.com>"
32+
33+
34+
def test_sync_free_text(fortran_file, somesy_input):
35+
# update author to have a free text format
36+
content = fortran_file.read_text()
37+
content = content.replace(
38+
'author = "John Doe <john.doe@example.com>"', 'author = "John Doe"'
39+
)
40+
41+
# write the new content
42+
fortran_file.write_text(content)
43+
44+
# read the file again
45+
fortran = Fortran(fortran_file)
46+
fortran.sync(somesy_input.project)
47+
assert fortran.name == "testproject"
48+
assert fortran.version == "1.0.0"
49+
assert fortran.authors[0] == "John Doe <john.doe@example.com>"
50+
51+
52+
def test_save(tmp_path, fortran):
53+
custom_path = tmp_path / Path("fpm.toml")
54+
fortran.save(custom_path)
55+
assert custom_path.is_file()
56+
custom_path.unlink()
57+
58+
59+
def test_from_person(person):
60+
assert Fortran._from_person(person) == f"{person.full_name} <{person.email}>"

0 commit comments

Comments
 (0)